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;
}