Click to See Complete Forum and Search --> : The last timer method thingyy
YourSurrogateGod
May 29th, 2004, 04:55 PM
This is the last timer that I'm using, or trying to get to work, here is the program...
#include <windows.h>
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[] = "Beeper3";
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, "Beeper3 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 int iTimerID;
switch(message)
{
case WM_CREATE:
iTimerID = SetTimer(NULL, 0, 1000, TimerProc);
return 0;
case WM_DESTROY:
KillTimer(hwnd, iTimerID);
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;
RECT rc;
MessageBeep(-1);
fFlipFlop = !fFlipFlop;
GetClientRect(hwnd, &rc);
hdc = GetDC(hwnd);
hBrush = CreateSolidBrush(fFlipFlop ? RGB(255, 0 ,0) : RGB(0, 0, 255));
FillRect(hdc, &rc, hBrush);
ReleaseDC(hwnd, hdc);
DeleteObject(hBrush);
}
The problem here is that nothing at all works, no stupid beeping sound or anything and I can't even begin to try to figure out what's wrong.
Mick
May 29th, 2004, 05:48 PM
Do you have a sound enabled for 'Default Beep' ?
mdmd
May 30th, 2004, 01:14 AM
You also don't have a window associated with the timer( setTimer) so the
hwnd in the timer proc will probably be null.
YourSurrogateGod
May 30th, 2004, 04:30 AM
Originally posted by Mick
Do you have a sound enabled for 'Default Beep' ?
How would you do that?
Mick
May 30th, 2004, 04:38 AM
Originally posted by YourSurrogateGod
How would you do that?
Control panel -> sounds and audio devices, default beep.
YourSurrogateGod
May 30th, 2004, 05:00 AM
I'm an idiot, I was thinking about something else, the default beep works fine, but that's about it...
By the way, I'm using XP and I didn't find what the "default beep" the only "Default beep" that I found that was in the "Sounds" tab and all that did was give a preview of all of the sounds that are available, I didn't see any option to turn the sound on/off(this is something stupid again......).
Mick
May 30th, 2004, 05:06 AM
Originally posted by YourSurrogateGod
I'm an idiot, I was thinking about something else, the default beep works fine, but that's about it...
By the way, I'm using XP and I didn't find what the "default beep" the only "Default beep" that I found that was in the "Sounds" tab and all that did was give a preview of all of the sounds that are available, I didn't see any option to turn the sound on/off(this is something stupid again......).
Did you associate the window with the timer as mdmd said? Or are you still not getting a beep?
YourSurrogateGod
May 30th, 2004, 05:52 AM
The beep works like a charm, it did so from the start(which is why I called myself an idiot :rolleyes:). The only thing that doesn't work correctly is the drawing. When you said that I have to "associate" the timer proc with the window handle, I'm assuming that you mean something like this...
static HWND hwndTimer;
LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
HDC hdc;
PAINTSTRUCT ps;
switch(message)
{
case WM_CREATE:
// associate?
hwndTimer = hwnd;
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;
hdc = GetDC(hwndTimer);
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(hwndTimer, hdc);
DeleteObject(hBrush);
}
I also tried to set hwndTimer to the hwnd in the WinMain method, but that also didn't do what I wanted. Thanks in advance.
Mick
May 30th, 2004, 05:56 AM
go back to your original code, just pass the hwnd in to your SetTimer(...)
YourSurrogateGod
May 30th, 2004, 06:22 AM
Originally posted by Mick
go back to your original code, just pass the hwnd in to your SetTimer(...)
No can do, I'm trying to pull that off without passing it in, Petzold's book says it's possible, so I want to try it out.
This is the latest darn thing, the beeping thing works fine, but there is no drawing, program....
#include <windows.h>
static int cxClient;
static int cyClient;
static RECT rcTop;
static RECT rcBottom;
static HWND hwndTimer;
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[] = "Beeper3";
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, "Beeper3 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 int iTimerID;
switch(message)
{
case WM_CREATE:
// associate
hwndTimer = hwnd;
iTimerID = SetTimer(NULL, 0, 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, iTimerID);
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(-1);
fFlipFlop = !fFlipFlop;
hdc = GetDC(hwndTimer);
// draw the top rectangle
hBrush = CreateSolidBrush(fFlipFlop ? RGB(0, 0, 255) : RGB(255, 243, 21));
FillRect(hdc, &rcTop, hBrush);
// draw the bottom rectangle
hBrush = CreateSolidBrush(!fFlipFlop ? RGB(0, 0, 255) : RGB(255, 243, 21));
FillRect(hdc, &rcBottom, hBrush);
ReleaseDC(hwndTimer, hdc);
DeleteObject(hBrush);
}
That's what I have now. As said before the only thing that doesn't work is the drawing, everything else is great.
In order to associate the windows handle, I even did something like this...
hwndTimer = hwnd = CreateWindow(szAppName, "Beeper3 Timer Demo", WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, NULL, NULL, hInstance, NULL);
... but nevertheless, no good, didn't do what I want.
P.S.: Ignore my 4th post, I copied out of the wrong source file :blush:
Marc G
May 30th, 2004, 06:39 AM
Well, there is a little typo in your code:
case WM_SIZE:
cxClient = LOWORD(lParam);
cxClient = HIWORD(lParam);
should be
case WM_SIZE:
cxClient = LOWORD(lParam);
cyClient = HIWORD(lParam);
;)
YourSurrogateGod
May 30th, 2004, 06:50 AM
Son of a monkey, that did the trick <smashed head against the wall>....
Marc G
May 30th, 2004, 09:40 AM
If you would have used the debugger, you would have found this typo rather quickly ;)
YourSurrogateGod
May 30th, 2004, 02:00 PM
I tried to use the darn thing once, but it didn't work that well for me.
/*The only debugger that I know how to yield well is one that gets rid of mosquitoes and roaches.*/
Mick
May 30th, 2004, 02:10 PM
Originally posted by YourSurrogateGod
I tried to use the darn thing once, but it didn't work that well for me.
/*The only debugger that I know how to yield well is one that gets rid of mosquitoes and roaches.*/
what did not work out for you? The IDE debugger is not overly complex...
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vcug98/html/_asug_Home_Page.3a_.Debugger.asp
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vsdebug/html/_asug_how_do_i_topics3a_debugging.asp
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vsdebug/html/vc_Debugging_Your_Application_home_page.asp
Marc G
May 30th, 2004, 02:46 PM
You really should take a look at the debugger again and learn how to use it. It is a really valuable tool. You can't write a program without knowing how to use the debugger ;)
YourSurrogateGod
May 30th, 2004, 03:32 PM
I used to fudge with that thing, but I never really mastered it fully, instead I just used stuff like output to the command line(that's way before) in order figure out the behaviour of my program and stuff. That and a pencil and paper often helped work through logic quite easily.
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.