[win32] - the form can use 2 timers?

the form can use, with WM_TIMER message, 2 timers?
why the question: because i create a 2nd timer(the 1st timer works fine) in WM_CREATE message, but seems be ignored and i don't know avaliate the SetTimer() did a job or error :(
see the code:
in WM_CREATE message:
SetTimer(inst->hWnd(),JoystickTimer,120,NULL);
the inst is the class form instance pointer.
in WM_TIMER:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
case WM_TIMER:
                {
                    if (wParam == timerid)
                    {
                        PreviousLocation = Location;
                        GetCursorPos(&Location);
                        if ((Location.x = PreviousLocation.x) && (Location.y = PreviousLocation.y))
                        {
                            KillTimer(inst->hWnd(), timerid);
                            inst->MouseStoped();
                        }
                    }
                    else if(wParam==JoystickTimer)
                    {
                        inst->setText("joystick");
                        joySetCapture(inst->hWnd(),JOYSTICKID1,120,true);
                        JOYINFOEX  b;
                        b.dwSize=sizeof(JOYINFOEX );
                        b.dwFlags=JOY_RETURNALL;
                        joyGetPosEx(JOYSTICKID1,&b);
                        inst->setText(to_string(b.dwButtonNumber));
                        joyReleaseCapture(JOYSTICKID1);
                    }
                }
                break;

i don't understand why the the timer isn't executed :(
Last edited on
maybe timer is not created. Check return value from SetTimer, and if error occurred, use GetLastError to find why
i even change from WM_NCCREATE to WM_CREATE:
1
2
3
4
5
6
7
8
9
10
case WM_CREATE:
                {
                    if(WindowMain == NULL)
                        WindowMain = HandleWindow;
                    SetTimer(inst->hWnd(),JoystickTimer,120,NULL);
                    int a=GetLastError();
                    if(a!=0)
                        MessageBox(NULL,to_string(a).c_str(),"erro",MB_OK);
                }
                break;

i get the number 0(zero). so why isn't right?
what is inst->hWnd() ?
Because WM_CREATE is called from withing the CreateWindowEx, and if this inst is some kind of structure initialized before creating window it will be invalid. Try using HandleWindow instead. Or simply create Timer after creating window.

1
2
3
4
5
6
UINT_PTR pRetTim = SetTimer(HandleWindow ,JoystickTimer,120,NULL);

if (0==pRetTim && 0==HandleWindow )
{
DWORD dwErr = GetLastError();
}
i'm sorry, but these seems crazy :(
see the WM_SHOWWINDOW:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
case WM_SHOWWINDOW:
                {
                    if(inst->blnVisible==true)
                    {
                        inst->Visible();
                    }
                    else
                    {
                        inst->Hide();
                    }

                    UINT_PTR pRetTim = SetTimer(HandleWindow,JoystickTimer,120,NULL);
                        if (0==pRetTim && 0==HandleWindow )
                        {
                            DWORD dwErr = GetLastError();
                            if(dwErr!=0)
                                MessageBox(NULL,to_string(dwErr).c_str(),"erro",MB_OK);
                        }
                }
                break;

and the timer message:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
case WM_TIMER:
                {
                    if (wParam == timerid)
                    {
                        PreviousLocation = Location;
                        GetCursorPos(&Location);
                        if ((Location.x = PreviousLocation.x) && (Location.y = PreviousLocation.y))
                        {
                            KillTimer(inst->hWnd(), timerid);
                            inst->MouseStoped();
                        }
                    }
                    else if(wParam==JoystickTimer)
                    {
                        inst->setText("joystick");
                        joySetCapture(inst->hWnd(),JOYSTICKID1,120,true);
                        JOYINFOEX  b;
                        b.dwSize=sizeof(JOYINFOEX );
                        b.dwFlags=JOY_RETURNALL;
                        joyGetPosEx(JOYSTICKID1,&b);
                        inst->setText(to_string(b.dwButtonNumber));
                        joyReleaseCapture(JOYSTICKID1);
                    }
                }
                break;

inst is the form instance class... the hWnd() is for get the form hwnd.
why these way instead the joystick messages? because the joystick messages are limited for 2 joysticks and only 4 buttons :(
Last edited on
Topic archived. No new replies allowed.