YourSurrogateGod
May 29th, 2004, 03:54 PM
I got this example out of Petzold's book and decided to mess around with it...
#include <windows.h>
#define ID_TIMER 1
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR szCmdLine, int iCmdShow)
{
static TCHAR szAppName[] = TEXT("Beeper1");
HWND hwnd;
MSG msg;
WNDCLASS wndclass;
wndclass.style = CS_HREDRAW | CS_VREDRAW;
wndclass.lpfnWndProc = WndProc;
wndclass.cbClsExtra = 0;
wndclass.cbWndExtra = 0;
wndclass.hInstance = hInstance;
wndclass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wndclass.hCursor = LoadCursor(NULL, IDC_ARROW);
wndclass.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
wndclass.lpszMenuName = NULL;
wndclass.lpszClassName = szAppName;
if(!RegisterClass(&wndclass))
{
MessageBox(NULL, TEXT("This program requires a Windows OS that supports Unicode."),
szAppName, MB_ICONERROR);
return 0;
}
hwnd = CreateWindow(szAppName, TEXT("Beeper1 Timer Demo"), WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, NULL, NULL, hInstance, NULL);
ShowWindow(hwnd, iCmdShow);
UpdateWindow(hwnd);
while(GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return msg.wParam;
}
LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
static BOOL fFlipFlop;
HBRUSH hBrush;
HDC hdc;
PAINTSTRUCT ps;
static RECT rcTop;
static RECT rcBottom;
static int cxClient;
static int cyClient;
switch(message)
{
case WM_CREATE:
fFlipFlop = FALSE;
SetTimer(hwnd, ID_TIMER, 1000, NULL);
return 0;
case WM_TIMER:
fFlipFlop = !fFlipFlop;
InvalidateRect(hwnd, NULL, FALSE);
return 0;
case WM_SIZE:
cxClient = LOWORD(lParam);
cyClient = HIWORD(lParam);
SetRect(&rcTop, 0, 0, cxClient, cyClient / 2);
SetRect(&rcBottom, 0, cyClient / 2, cxClient, cyClient);
return 0;
case WM_PAINT:
hdc = BeginPaint(hwnd, &ps);
hBrush = CreateSolidBrush(fFlipFlop ? RGB(0, 0, 255) : RGB(255, 243, 21));
FillRect(hdc, &rcTop, hBrush);
hBrush = CreateSolidBrush(!fFlipFlop ? RGB(0, 0, 255) : RGB(255, 243, 21));
FillRect(hdc, &rcBottom, hBrush);
EndPaint(hwnd, &ps);
DeleteObject(hBrush);
return 0;
case WM_DESTROY:
KillTimer(hwnd, ID_TIMER);
PostQuitMessage(0);
return 0;
}
return DefWindowProc(hwnd, message, wParam, lParam);
}
... consequently it does what I want it to do. However, whenever I try to send the WM_TIME messages to a specific function, it doesn't work as well, as shown here....
#include <windows.h>
#define ID_TIMER 1
static int cxClient;
static int cyClient;
static RECT rcTop;
static RECT rcBottom;
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
VOID CALLBACK TimerProc(HWND, UINT, UINT, DWORD);
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR szCmdLine, int iCmdShow)
{
static TCHAR szAppName[] = "Beeper2";
HWND hwnd;
MSG msg;
WNDCLASS wndclass;
wndclass.style = CS_HREDRAW | CS_VREDRAW;
wndclass.lpfnWndProc = WndProc;
wndclass.cbClsExtra = 0;
wndclass.cbWndExtra = 0;
wndclass.hInstance = hInstance;
wndclass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wndclass.hCursor = LoadCursor(NULL, IDC_ARROW);
wndclass.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
wndclass.lpszMenuName = NULL;
wndclass.lpszClassName = szAppName;
if(!RegisterClass(&wndclass))
{
MessageBox(NULL, TEXT("This program requires a Windows OS that supports Unicode."),
szAppName, MB_ICONERROR);
return 0;
}
hwnd = CreateWindow(szAppName, "Beeper2 Timer Demo", WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, NULL, NULL, hInstance, NULL);
ShowWindow(hwnd, iCmdShow);
UpdateWindow(hwnd);
while(GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return msg.wParam;
}
LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
switch(message)
{
case WM_CREATE:
SetTimer(hwnd, ID_TIMER, 1000, TimerProc);
return 0;
case WM_SIZE:
cxClient = LOWORD(lParam);
cxClient = HIWORD(lParam);
SetRect(&rcTop, 0, 0, cxClient, cyClient / 2);
SetRect(&rcBottom, 0, cyClient / 2, cxClient, cyClient);
return 0;
case WM_DESTROY:
KillTimer(hwnd, ID_TIMER);
PostQuitMessage(0);
return 0;
}
return DefWindowProc(hwnd, message, wParam, lParam);
}
VOID CALLBACK TimerProc(HWND hwnd, UINT message, UINT iTimerID, DWORD dwTime)
{
static BOOL fFlipFlop = FALSE;
HBRUSH hBrush;
HDC hdc;
MessageBeep(0);
fFlipFlop = !fFlipFlop;
//GetClientRect(hwnd, &rcTop);
hdc = GetDC(hwnd);
hBrush = CreateSolidBrush(fFlipFlop ? RGB(0, 0, 255) : RGB(255, 243, 21));
FillRect(hdc, &rcTop, hBrush);
hBrush = CreateSolidBrush(!fFlipFlop ? RGB(0, 0, 255) : RGB(255, 243, 21));
FillRect(hdc, &rcBottom, hBrush);
ReleaseDC(hwnd, hdc);
DeleteObject(hBrush);
}
... the only thing that I hear is just the beeping noise every 1 second. I think I know what the problem is, when the window is first created, it gets the WM_CREATE message and then a WM_SIZE message and then the WM_PAINT message(I think that's right). However my program has no way to deal with the WM_PAINT message, the TimerProc takes only time related messages, as a result it's impossible for it to make re-draw itself(I think that's right). Anyways, I just need it to draw 2 separate rectangles, one at the bottom and one at the top. I eve tried this in the timer proc, but it didn't work.....
if(message & WM_PAINT)
{
MessageBeep(0);
fFlipFlop = !fFlipFlop;
//GetClientRect(hwnd, &rcTop);
hdc = GetDC(hwnd);
hBrush = CreateSolidBrush(fFlipFlop ? RGB(0, 0, 255) : RGB(255, 243, 21));
FillRect(hdc, &rcTop, hBrush);
hBrush = CreateSolidBrush(!fFlipFlop ? RGB(0, 0, 255) : RGB(255, 243, 21));
FillRect(hdc, &rcBottom, hBrush);
ReleaseDC(hwnd, hdc);
DeleteObject(hBrush);
}
Thanks in advance for your help.
#include <windows.h>
#define ID_TIMER 1
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR szCmdLine, int iCmdShow)
{
static TCHAR szAppName[] = TEXT("Beeper1");
HWND hwnd;
MSG msg;
WNDCLASS wndclass;
wndclass.style = CS_HREDRAW | CS_VREDRAW;
wndclass.lpfnWndProc = WndProc;
wndclass.cbClsExtra = 0;
wndclass.cbWndExtra = 0;
wndclass.hInstance = hInstance;
wndclass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wndclass.hCursor = LoadCursor(NULL, IDC_ARROW);
wndclass.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
wndclass.lpszMenuName = NULL;
wndclass.lpszClassName = szAppName;
if(!RegisterClass(&wndclass))
{
MessageBox(NULL, TEXT("This program requires a Windows OS that supports Unicode."),
szAppName, MB_ICONERROR);
return 0;
}
hwnd = CreateWindow(szAppName, TEXT("Beeper1 Timer Demo"), WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, NULL, NULL, hInstance, NULL);
ShowWindow(hwnd, iCmdShow);
UpdateWindow(hwnd);
while(GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return msg.wParam;
}
LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
static BOOL fFlipFlop;
HBRUSH hBrush;
HDC hdc;
PAINTSTRUCT ps;
static RECT rcTop;
static RECT rcBottom;
static int cxClient;
static int cyClient;
switch(message)
{
case WM_CREATE:
fFlipFlop = FALSE;
SetTimer(hwnd, ID_TIMER, 1000, NULL);
return 0;
case WM_TIMER:
fFlipFlop = !fFlipFlop;
InvalidateRect(hwnd, NULL, FALSE);
return 0;
case WM_SIZE:
cxClient = LOWORD(lParam);
cyClient = HIWORD(lParam);
SetRect(&rcTop, 0, 0, cxClient, cyClient / 2);
SetRect(&rcBottom, 0, cyClient / 2, cxClient, cyClient);
return 0;
case WM_PAINT:
hdc = BeginPaint(hwnd, &ps);
hBrush = CreateSolidBrush(fFlipFlop ? RGB(0, 0, 255) : RGB(255, 243, 21));
FillRect(hdc, &rcTop, hBrush);
hBrush = CreateSolidBrush(!fFlipFlop ? RGB(0, 0, 255) : RGB(255, 243, 21));
FillRect(hdc, &rcBottom, hBrush);
EndPaint(hwnd, &ps);
DeleteObject(hBrush);
return 0;
case WM_DESTROY:
KillTimer(hwnd, ID_TIMER);
PostQuitMessage(0);
return 0;
}
return DefWindowProc(hwnd, message, wParam, lParam);
}
... consequently it does what I want it to do. However, whenever I try to send the WM_TIME messages to a specific function, it doesn't work as well, as shown here....
#include <windows.h>
#define ID_TIMER 1
static int cxClient;
static int cyClient;
static RECT rcTop;
static RECT rcBottom;
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
VOID CALLBACK TimerProc(HWND, UINT, UINT, DWORD);
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR szCmdLine, int iCmdShow)
{
static TCHAR szAppName[] = "Beeper2";
HWND hwnd;
MSG msg;
WNDCLASS wndclass;
wndclass.style = CS_HREDRAW | CS_VREDRAW;
wndclass.lpfnWndProc = WndProc;
wndclass.cbClsExtra = 0;
wndclass.cbWndExtra = 0;
wndclass.hInstance = hInstance;
wndclass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wndclass.hCursor = LoadCursor(NULL, IDC_ARROW);
wndclass.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
wndclass.lpszMenuName = NULL;
wndclass.lpszClassName = szAppName;
if(!RegisterClass(&wndclass))
{
MessageBox(NULL, TEXT("This program requires a Windows OS that supports Unicode."),
szAppName, MB_ICONERROR);
return 0;
}
hwnd = CreateWindow(szAppName, "Beeper2 Timer Demo", WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, NULL, NULL, hInstance, NULL);
ShowWindow(hwnd, iCmdShow);
UpdateWindow(hwnd);
while(GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return msg.wParam;
}
LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
switch(message)
{
case WM_CREATE:
SetTimer(hwnd, ID_TIMER, 1000, TimerProc);
return 0;
case WM_SIZE:
cxClient = LOWORD(lParam);
cxClient = HIWORD(lParam);
SetRect(&rcTop, 0, 0, cxClient, cyClient / 2);
SetRect(&rcBottom, 0, cyClient / 2, cxClient, cyClient);
return 0;
case WM_DESTROY:
KillTimer(hwnd, ID_TIMER);
PostQuitMessage(0);
return 0;
}
return DefWindowProc(hwnd, message, wParam, lParam);
}
VOID CALLBACK TimerProc(HWND hwnd, UINT message, UINT iTimerID, DWORD dwTime)
{
static BOOL fFlipFlop = FALSE;
HBRUSH hBrush;
HDC hdc;
MessageBeep(0);
fFlipFlop = !fFlipFlop;
//GetClientRect(hwnd, &rcTop);
hdc = GetDC(hwnd);
hBrush = CreateSolidBrush(fFlipFlop ? RGB(0, 0, 255) : RGB(255, 243, 21));
FillRect(hdc, &rcTop, hBrush);
hBrush = CreateSolidBrush(!fFlipFlop ? RGB(0, 0, 255) : RGB(255, 243, 21));
FillRect(hdc, &rcBottom, hBrush);
ReleaseDC(hwnd, hdc);
DeleteObject(hBrush);
}
... the only thing that I hear is just the beeping noise every 1 second. I think I know what the problem is, when the window is first created, it gets the WM_CREATE message and then a WM_SIZE message and then the WM_PAINT message(I think that's right). However my program has no way to deal with the WM_PAINT message, the TimerProc takes only time related messages, as a result it's impossible for it to make re-draw itself(I think that's right). Anyways, I just need it to draw 2 separate rectangles, one at the bottom and one at the top. I eve tried this in the timer proc, but it didn't work.....
if(message & WM_PAINT)
{
MessageBeep(0);
fFlipFlop = !fFlipFlop;
//GetClientRect(hwnd, &rcTop);
hdc = GetDC(hwnd);
hBrush = CreateSolidBrush(fFlipFlop ? RGB(0, 0, 255) : RGB(255, 243, 21));
FillRect(hdc, &rcTop, hBrush);
hBrush = CreateSolidBrush(!fFlipFlop ? RGB(0, 0, 255) : RGB(255, 243, 21));
FillRect(hdc, &rcBottom, hBrush);
ReleaseDC(hwnd, hdc);
DeleteObject(hBrush);
}
Thanks in advance for your help.