The console program is not easy to manipulate by default only by dragging the scroll bar to see what is printed in the window.
This article adds the mouse wheel slide function to the console form by using multithreading techniques. It is worth noting that when there is content output, the window is automatically positioned to the cursor at the output;
It is best to pause the main thread first, then scroll the mouse to view the printed content, and then proceed to the main thread after you have finished viewing it.
First, you need to have the console program's screen buffer height > window Height (the scroll bar is generated on the right side of the window), or you do not need to scroll the window.
The following code implements the following functions:
(1) Scrolling mouse sliding window
(2) Press SPACEBAR to pause/resume the main thread
#include <windows.h>/** * Scroll Console window by relative coordinate*/Static intScrollbyrelativecoord (intirows) {Console_screen_buffer_info csbiinfo; Small_rect Srctwindow; //Get the current Screen buffer window position.HANDLE Hconsoleoutput =GetStdHandle (Std_output_handle); if(! Getconsolescreenbufferinfo (Hconsoleoutput, &csbiinfo)) { return 0; } //Check Whether the window is too close to the screen buffer top or bottom if(CsbiInfo.srWindow.Top <irows) {irows=CsbiInfo.srWindow.Top; } Else if(CsbiInfo.srWindow.Bottom > csbiinfo.dwsize.y+irows-1) {irows= -1* (CSBIINFO.DWSIZE.Y-1-csbiInfo.srWindow.Bottom); } srctwindow.top=-(short) irows;//Move top upSrctwindow.bottom =-(short) irows;//Move Bottom upSrctwindow.left =0;//No changeSrctwindow.right =0;//No change if(!Setconsolewindowinfo (Hconsoleoutput,//Screen buffer handleFALSE,//relative coordinates&srctwindow))//Specifies new location { return 0; } returnirows;} DWORD WINAPI Consoleinputeventproc (lpvoid lParam) {//VC6 version SDK don ' t have openthread API, need get by call GetProcAddress;HANDLE Hmainthreadhandle =NULL;#if_msc_ver <= 1200hmodule hDLL=::loadlibrary ("Kernel32.dll"); if(hdll) {typedef HANDLE (__stdcall*openthread) (DWORD, BOOL, DWORD); Openthread Fnopenthread= (Openthread):: GetProcAddress (hDLL,"Openthread"); if(fnopenthread) {Hmainthreadhandle=Fnopenthread (Thread_suspend_resume, FALSE, (DWORD) lParam); } }#elseHmainthreadhandle=Openthread (Thread_suspend_resume, FALSE, (DWORD) lParam);#endifHANDLE hconsoleinput=GetStdHandle (Std_input_handle); BOOL Bsuspend=FALSE; BOOL fcontinue=TRUE; DWORD dwevents; Input_record input; while(FContinue &&readconsoleinput (Hconsoleinput,&input,1, &dwevents) &&dwevents>0) { Switch(input. EventType) { Casekey_event:if(Input. Event.KeyEvent.wVirtualKeyCode = =vk_space) { if(Input. Event.KeyEvent.bKeyDown && Hmainthreadhandle! =NULL) {HANDLE Hconsoleoutput=GetStdHandle (Std_output_handle); if(bsuspend) {Setconsoletextattribute (hconsoleoutput, Foreground_green); printf ("Resume mainthread\n"); ResumeThread (Hmainthreadhandle); } Else{setconsoletextattribute (hconsoleoutput, foreground_red); printf ("Suspend mainthread\n"); SuspendThread (Hmainthreadhandle); } setconsoletextattribute (Hconsoleoutput, foreground_red| Foreground_green |foreground_blue); Bsuspend= !Bsuspend; } } Casemouse_event:if(Input. event.mouseevent.dweventflags==4)//MouseWheel { Const intNLine =5; if((int) input. Event.mouseevent.dwbuttonstate>0)//Scroll up{Scrollbyrelativecoord (nLine); } Else//Scroll up{Scrollbyrelativecoord (-1*nLine); } } Break; }} CloseHandle (Hmainthreadhandle); return 0;}intMainintargcChar*argv[]) {CreateThread (NULL,0, Consoleinputeventproc, (LPVOID) GetCurrentThreadID (),0, NULL); intNLine = -; while(nline--) {printf ("Hello world!\n"); Sleep ( +); } Sleep (INFINITE); return 0;}
Console program add roller slide support