Thread: WM_MESSAGES arrrrr

  1. #1
    Registered User cppdude's Avatar
    Join Date
    Jan 2002
    Posts
    62

    WM_MESSAGES arrrrr

    ok i have a problem that is annoying the hell out of me. In my WM_INITIALISE message the handle to the dialog is correct allowing me to use SetDlgItem to the controls on the dialogs. But in my custom message WM_TOGGLE the handle to the window is not correct when. I know i can trigger this message but i cant get the handle to the dialog from within that message. Here is my source.






    #include <windows.h>
    #include "resource.h"
    #include <stdio.h> //for sprintf - puts integers into strings

    #define ID_TIMER 1

    BOOL CALLBACK DialogProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam);
    BOOL CALLBACK AboutProc(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam);
    BOOL CALLBACK KeyboardProc(int nCode, WPARAM wParam, LPARAM lParam);
    int WINAPI WinMain(HINSTANCE hInst, HINSTANCE hPrevInst, char * cmdParam, int cmdShow);

    int xoffset = 1;
    int yoffset = 0;
    int zoomfactor = 4;
    int scanarea = 10;
    COLORREF colourarray[2560001];
    int arrayindex;
    char buffer[11]; //used for lots of things
    int onoff = 0; //whether the Scope is on

    HINSTANCE hInst;
    HDC ScreenDC;
    HWND hwnd;
    HHOOK hhook;

    //--------------------------------------------------------------------------------------------
    BOOL CALLBACK DialogProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
    {
    switch (message)
    {
    case WM_COMMAND:
    {
    switch (LOWORD(wParam))
    {
    case IDQUIT:
    {
    PostQuitMessage(0);
    return TRUE;
    }
    case IDCANCEL:
    {
    PostQuitMessage(0);
    return TRUE;
    }
    case IDABOUT:
    {

    DialogBox(hInst, MAKEINTRESOURCE(IDD_ABOUT), hwnd, (DLGPROC)AboutProc);
    return TRUE;
    }
    } //switch loword(wParam)
    return TRUE;
    } //case WM_COMMAND

    case WM_QUIT:
    {
    KillTimer(hwnd, ID_TIMER);
    ReleaseDC(GetForegroundWindow(), ScreenDC);
    UnhookWindowsHookEx(hhook);
    EndDialog(hwnd,NULL);
    return TRUE;
    }

    case WM_TOGGLE: //the message that doesnt have the handle correctly
    {
    if(onoff)
    {
    onoff = 0;
    SetDlgItemText(hwnd, IDC_STATUS, "Inactive");
    ReleaseDC(GetForegroundWindow(), ScreenDC);
    KillTimer(hwnd, ID_TIMER);
    }
    else
    {
    sprintf(buffer,"%i", IsWindow(hwnd));
    MessageBox(hwnd, buffer, "ERROR NOTICE", MB_OK);

    GetDlgItemText(hwnd, IDC_ZOOMFACTOR, buffer, 10); // GetDlgItemText stores dlg items into buffer
    zoomfactor = strtol(buffer, NULL, 10); //strtol returns long from the buffer
    GetDlgItemText(hwnd, IDC_SCANAREA, buffer, 10);
    scanarea = strtol(buffer, NULL, 10);
    GetDlgItemText(hwnd, IDC_XOFFSET, buffer, 10);
    xoffset = strtol(buffer, NULL, 10);
    GetDlgItemText(hwnd, IDC_YOFFSET, buffer, 10);
    yoffset = strtol(buffer, NULL, 10);

    ScreenDC = GetDC(GetForegroundWindow());
    SetTimer(hwnd, ID_TIMER, 1, NULL);
    onoff = 1;
    }
    return TRUE;
    }

    case WM_INITDIALOG: //i can get the handle correctly in this message
    {
    SetDlgItemText(hwnd, IDC_STATUS, "Inactive");
    SetDlgItemInt(hwnd, IDC_ZOOMFACTOR, zoomfactor, NULL);
    SetDlgItemInt(hwnd, IDC_SCANAREA, scanarea, NULL);
    SetDlgItemText(hwnd, IDC_HOTKEY, "F12");

    sprintf(buffer, "%i", xoffset);
    SetDlgItemText(hwnd, IDC_XOFFSET, buffer);
    sprintf(buffer, "%i", yoffset);
    SetDlgItemText(hwnd, IDC_YOFFSET, buffer);
    hhook = SetWindowsHookEx(WH_KEYBOARD,(HOOKPROC)KeyboardPro c, hInst,0);
    return TRUE;
    }

    case WM_TIMER:
    {
    int x;
    int y;

    //read screen to array//////////////
    arrayindex = 1;
    for(x=1; x< (scanarea + 1); x++)
    {
    for(y=1; y< (scanarea +1); y++)
    {
    colourarray[arrayindex] = GetPixel(ScreenDC, (GetDeviceCaps(ScreenDC, HORZRES)/2) - (scanarea / 2) + x, (GetDeviceCaps(ScreenDC, VERTRES)/2) - (scanarea / 2) + y);
    arrayindex++;
    }
    }

    //write to screen magnified/////////
    arrayindex = 1;
    for(x=1; x< (scanarea +1); x++)
    {
    for(y=1; y< (scanarea +1); y++)
    {
    for(int i1 = 1; i1< (zoomfactor + 1); i1++)
    {
    for(int i2 =1; i2< (zoomfactor + 1); i2++)
    {
    SetPixel(ScreenDC,
    GetDeviceCaps(ScreenDC, HORZRES)/2 - (zoomfactor * scanarea / 2) - zoomfactor + (zoomfactor * x) + i1 + (xoffset * zoomfactor / 2),
    GetDeviceCaps(ScreenDC, VERTRES)/2 - (zoomfactor * scanarea / 2) - zoomfactor + (zoomfactor * y) + i2 + (yoffset * zoomfactor / 2),
    colourarray[arrayindex]);
    } //for
    } //for
    } //for
    arrayindex++;
    } //for

    SetTimer(hwnd, ID_TIMER, 1, NULL);
    return TRUE;
    } //case WM_TIMER

    }
    return FALSE;
    }

    //--------------------------------------------------------------------------------------------
    BOOL CALLBACK AboutProc(HWND habout, UINT message, WPARAM wParam, LPARAM lParam) //message handler for the about box
    {
    switch(message)
    {
    case WM_COMMAND:
    {
    if (LOWORD(wParam) == IDOK || IDCANCEL)
    {
    EndDialog(habout, LOWORD(wParam));
    return TRUE;
    }
    return FALSE;
    }
    }
    return FALSE;
    }
    //--------------------------------------------------------------------------------------------
    BOOL CALLBACK KeyboardProc(int nCode, WPARAM wParam, LPARAM lParam)
    {
    if(wParam == 123) //F12 apparently
    {
    DialogProc(hwnd, WM_TOGGLE, 0,0);
    }
    return CallNextHookEx(hhook, nCode, wParam, lParam);
    }
    //--------------------------------------------------------------------------------------------
    int WINAPI WinMain(HINSTANCE hInst, HINSTANCE hPrevInst, char * cmdParam, int cmdShow)
    {
    DialogBox(hInst, MAKEINTRESOURCE(IDD_DIALOG), 0, (DLGPROC)DialogProc);
    return FALSE;
    }

  2. #2
    erstwhile
    Join Date
    Jan 2002
    Posts
    2,227
    Try storing the dialog box handle (the global hwnd) in your WM_INITDIALOG handler - it looks like your 'KeyboardProc' is using an unitialised 'hwnd' variable which is being passed to your DialogProc.

    If that's the case, you may also consider initialising 'hwnd' to NULL; that way you can test its value prior to calling your DialogProc within your 'KeyboardProc' fn.

    An alternative might be to #define your own WM_TOGGLE msg as WM_APP+n where n is an integer and use SendMessage instead of the direct call to your DialogProc. Again, alternatively you could send your WM_TOGGLE as a WM_COMMAND msg as MAKEWPARAM(WM_TOGGLE,0), again using SendMessage. You would still need your global 'hwnd' variable to be valid in either case though.

  3. #3
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,868
    ReleaseDC(GetForegroundWindow(), ScreenDC);

    Could also be a problem, try testing to see is valid. If the window is losing focus, say at a breakpoint while you are debugging, will return NULL or wrong HWND. In your case this will equate to the display DC.
    "Man alone suffers so excruciatingly in the world that he was compelled to invent laughter."
    Friedrich Nietzsche

    "I spent a lot of my money on booze, birds and fast cars......the rest I squandered."
    George Best

    "If you are going through hell....keep going."
    Winston Churchill

Popular pages Recent additions subscribe to a feed