Adblock breaks this site

Win32 Timers

Discussion in 'Programming General' started by Govind, Jan 1, 2011.

  1. Govind

    Govind The One Musketeer
    Mudkips Highly Respected Retired Administrator

    Joined:
    Apr 22, 2005
    Posts:
    7,825
    Referrals:
    13
    Sythe Gold:
    23
    Prove it! Trole Tier 1 Prizebox Tortoise Penis Le Monkey UWotM8? Wait, do you not have an Archer rank? Potamus
    Win32 Timers

    Sleep() is a nice function, however, it is not ideal for graphical applications because it causes the entire UI (because that's the thread where your code is running) to hang.

    Instead of Sleep, Timers should be used to handle events you want to have occur at an arbitrary interval.

    Here is a simple application of timers to get the pixel value under the current cursor position every 50 milliseconds.
    Code:
    #define IDT_TIMER1 1001 // define the timer as something meaningul
    #include <Windows.h>
    
    VOID CALLBACK TimerProc(HWND hWnd, UINT Msg, UINT_PTR nIDEvent, DWORD dwInterval);
    LRESULT CALLBACK WndProc(HWND hWnd, UINT Msg, WPARAM wParam, LPARAM lParam);
    BOOL CALLBACK EnumChildProc(HWND hWnd, LPARAM lParam);
    
    int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd)
    {
    	//decls
    	WNDCLASSEX wc;
    	MSG Msg;
    	HWND hWnd, hStatic;
    
    	// initialize window class struct
    	wc.cbWndExtra = 0;
    	wc.cbClsExtra = 0;
    	wc.cbSize = sizeof(WNDCLASSEX);
    	wc.hbrBackground = (HBRUSH) COLOR_WINDOW;
    	wc.hCursor = LoadCursor(NULL, IDC_ARROW);
    	wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
    	wc.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
    	wc.hInstance = hInstance;
    	wc.lpfnWndProc = WndProc;
    	wc.lpszClassName = TEXT("TimerExampleWnd");
    	wc.lpszMenuName = NULL;
    	wc.style = 0;
    
    	RegisterClassEx(&wc);
    
    	hWnd = CreateWindowEx(WS_EX_OVERLAPPEDWINDOW, TEXT("TimerExampleWnd"), TEXT("Timer Example"), WS_VISIBLE | WS_SYSMENU, 100, 100, 200, 60, NULL, NULL, hInstance, NULL);
    	hStatic = CreateWindow(TEXT("STATIC"), TEXT(""), WS_VISIBLE | WS_CHILD, 0, 0, 200, 200, hWnd, NULL, hInstance, NULL);
    	
    
    	ShowWindow(hWnd, SW_SHOW);
    	EnumChildWindows(hWnd, EnumChildProc, MAKELPARAM(FALSE, 0));
    	UpdateWindow(hWnd);
    
    	// Initializes a timer which executes the callback function every 50 milliseconds.  
    	SetTimer(hWnd, IDT_TIMER1, 50, TimerProc);
    
    	while(GetMessage(&Msg, NULL, 0, 0) > 0)
    	{
    		TranslateMessage(&Msg);
    		DispatchMessage(&Msg);
    	}
    
    	return Msg.wParam;
    
    }
    BOOL CALLBACK EnumChildProc(HWND hWnd, LPARAM lParam)
    {
    	HFONT hfDefault = (HFONT) GetStockObject(DEFAULT_GUI_FONT);
    	SendMessage(hWnd, WM_SETFONT, (WPARAM) hfDefault, MAKELPARAM(FALSE, 0));
    	return TRUE;
    }
    
    LRESULT CALLBACK WndProc(HWND hWnd, UINT Msg, WPARAM wParam, LPARAM lParam)
    {
    	switch(Msg)
    	{
    	case WM_CLOSE:
    		DestroyWindow(hWnd);
    		break;
    	case WM_DESTROY:
    		PostQuitMessage(0);
    		break;
    	default:
    		return DefWindowProc(hWnd, Msg, wParam, lParam);
    	}
    	return 0;
    }
    
    VOID CALLBACK TimerProc(HWND hWnd, UINT Msg, UINT_PTR nIDEvent, DWORD dwInterval)
    {
    	//declarations
    	POINT p;
    	TCHAR buf[200];
    	// get cursor position into p
    	GetCursorPos(&p);
    	// format string
    	wsprintf(buf, TEXT("The pixel value under the cursor is: 0x%.8X"), GetPixel(GetDC(NULL), p.x, p.y));
    
    	// find the only static control in hwnd and set the text to buf
    	SetWindowText(FindWindowEx(hWnd, 0, TEXT("STATIC"), NULL), buf);
    }
    Most of the code above is the basic skeleton I use for all my Windows apps, and the timer part should be pretty straightforward; basically, you call SetTimer with the window, timer id, interval, and callback function name, typically in WinMain or WM_COMMAND / (HWND)lParam==(whatever button is supposed to initialize the timer) in WndProc, then the callback function is run every interval.

    Note: do not use modal dialogs (LIKE MESSAGEBOX) before calling KillTimer to prevent the timer from firing several times while the window is not active.
     
< need help i want to load rs inside a vb.net window. | i need help >


 
 
Adblock breaks this site