i have 1 class Timer. the class works fine, but when the windows loses the focus, the timer stops... why these happens?

What version of Windows is this? It sounds like when the window is out of focus, it is put in a hold state. There may be some Win32 settings you can use to let it continue to run in such a situation, but I am not expert enough with Win32 API's to tell you how to do that.

heres how i create the window:

oid setParent(HWND parent=GetDesktopWindow())
        {
            if (hwnd==NULL)
            {
                WNDCLASSEX FormClass;
                char classname[20]="Form";
                sprintf(classname,"%s",strCaption.c_str());
                HINSTANCE mod = (HINSTANCE)GetModuleHandle(NULL);

                FormClass.cbSize        = sizeof(WNDCLASSEX);
                FormClass.style         = CS_VREDRAW | CS_HREDRAW;
                FormClass.lpfnWndProc   = WndProcForm;
                FormClass.cbClsExtra    = 0;
                FormClass.cbWndExtra    = 0;
                FormClass.hInstance     = mod;
                FormClass.hIcon         = LoadIcon(NULL, IDI_APPLICATION);
                FormClass.hCursor       = LoadCursor(NULL, IDC_ARROW);
                FormClass.hbrBackground = (HBRUSH)((COLOR_WINDOW)+1);
                FormClass.lpszMenuName  = NULL;
                FormClass.lpszClassName = classname;
                FormClass.hIconSm       = LoadIcon(NULL, IDI_APPLICATION);

                // register the new window class
                RegisterClassEx(&FormClass);

                hwnd = CreateWindowEx(0, classname, strCaption.c_str(),WS_OVERLAPPEDWINDOW | WS_CLIPCHILDREN | WS_CLIPSIBLINGS,
                                  CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, parent, NULL, mod, this);

                SetLayeredWindowAttributes(hwnd, 0, 255, LWA_ALPHA);
                if (hwnd == NULL)
                    MessageBox(NULL, "Can't create the control", "error", MB_OK);
            }
            else
            {
                SetParent(hwnd,parent);
            }

            ShowWindow(hwnd, SW_NORMAL);
            InvalidateRect(hwnd,NULL,true);
            UpdateWindow(hwnd);
            clrBackColor =GetDCBrushColor(GetDC(hwnd));
            clrTextColor = GetTextColor(GetDC(hwnd));
        }

i found the problem: is my message loop :(

WPARAM MessageLoop()
{
    MSG msgEvents;

    while(GetMessage(&msgEvents, NULL, 0, 0) > 0)
    {
        //if(IsChild(WindowMain,msgEvents.hwnd)==TRUE || WindowMain==msgEvents.hwnd)
        {
            if (!IsDialogMessage(GetForegroundWindow(), &msgEvents))
            {
               TranslateMessage(&msgEvents);
               DispatchMessage(&msgEvents);
            }
        }
    }
    return msgEvents.wParam;
}

if i uncomment that line, when the window, loses the focus, the messages aren't executed.
but the GetForegroundWindow() can give me 1 window that isn't from my application. so what you can advice me?

i did a global variable that i change what form have the focus. so i change the GetFeregroundWindow() to that form, and now works fine ;)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.