Using Windows SetTimer () Function

I got confused in the SetTimer () function.

SetTimer () takes three parameters:

SetTimer(1,2000,Timerflow); 

However, I saw another version of SetTimer that takes four parameters:

 SetTimer(NULL,1,2000,Timerflow); 

What is the difference between these two functions?

I know SetTimer () Three parameters. But when I try to use the four SetTimer () parameters, I get an error:

 error C2660: 'SetTimer' : function does not take 4 parameters 

So what is the main difference and what causes this error?

+4
source share
4 answers

The 4-parameter version is a simple version of the Win32 API, and the first parameter is a window handle.

The 3-parameter version is a member of the MFC CWnd class and works with the window handle of the CWnd instance for which you name it.

If you need to call the 4-parameter Win32 API from a method of the CWnd object, do the following:

 ::SetTimer(NULL, 1, 2000, Timerflow); 
+5
source

The only Windows API called SetTimer accepts four parameters. Presumably, the other is part of MFC or some other structure, and the first parameter is implied by the object on which you call it. For instance:

 CWnd * w = .... // get window somehow w->SetTimer(1,2000,Timerflow); 
+2
source

If you use SetTimer to create a timer in GUI classes such as MFC CWnd, you can use a three-parameter form:

 UINT SetTimer( UINT nIDEvent, // timer identifier UINT uElapse, // time-out value TIMERPROC lpTimerFunc // address of timer procedure ); 

But if you use it in classes other than the GUI, you must use a 4-parameter form. The first parameter is to indicate which GUI component will respond to the timer event. This version of the function is called from the Win32 API.

 eUINT SetTimer( HWND hWnd, // handle of window for timer messages UINT nIDEvent, // timer identifier UINT uElapse, // time-out value TIMERPROC lpTimerFunc // address of timer procedure ); 

It is very simple, right?

+1
source

According to MSDN: https://msdn.microsoft.com/en-us/library/windows/desktop/ms644906(v=vs.85).aspx , the first and last parameters are optional. This way you can have a SetTimer call with two parameters (not recommended). The error is most likely due to improper casting (uint_ptr is required, you give hwnd, for example)

0
source

All Articles