/////////////////////////////////////////////////////////////////
// AddingATimer.cpp //
// Shows how to add a timer that causes Windows to pass us
// a WM_TIMER message every few seconds.
/////////////////////////////////////////////////////////////////
// USING THE WINDOWS TIMER:
// DEFINE some constants for the timers.
#define TIMER_SECOND 1 // first timer identified by integer 1
#define TIMER_MINUTE 2 // second timer identified by integer 2
// we will use these constants when we first create the timers,
// then Windows will use these numbers to notify us when each
// timer event has occurred.
#include <windows.h>
// Prototype for WndProc function
LRESULT CALLBACK WndProc ( HWND, UINT, WPARAM, LPARAM );
/////////////////////////////////////////////////////////////////
// WinMain: Application entry point for Windows applications. //
// //
// Just as said every C++ program starts at main, //
// every Windows program will start at WinMain. //
/////////////////////////////////////////////////////////////////
int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance,
LPSTR lpCmdLine, int nShowCmd )
{
// String used to identify our application
TCHAR applicationClassName[] = TEXT("FirstWindowApp");
#pragma region -_1. CREATE THE WNDCLASSEX STRUCTURE/OBJECT_-
WNDCLASSEX window; // Look at members of this struct.
// Below, we initialize all of them.
window.cbClsExtra = 0; // extra bytes
window.cbSize = sizeof(WNDCLASSEX); // keeps track of its own size
window.cbWndExtra = 0; // extra bytes
window.hbrBackground = (HBRUSH) GetStockObject(WHITE_BRUSH);
window.hCursor = LoadCursor(NULL, IDC_HAND); // load hand cursor
//Tip: type in IDC_ then press CTRL+SPACE to get a list of avail. cursors
window.hIcon = LoadIcon(NULL, IDI_APPLICATION); // large icon
window.hIconSm = LoadIcon(NULL, IDI_APPLICATION); // small icon that shows up in top left of window
window.hInstance = hInstance; // program's instance handle that was first passed to WinMain by windows when program was first run
window.lpfnWndProc = WndProc; // function pointer to WndProc function
window.lpszClassName = applicationClassName; // window class name.. defined above
window.lpszMenuName = NULL; // main menu
window.style = CS_OWNDC | CS_HREDRAW | CS_VREDRAW; // redraw on horizontal or vertical resize
#pragma endregion
#pragma region -_2. REGISTER THAT WNDCLASSEX STRUCTURE WITH WINDOWS O/S + CREATE WINDOW_-
if(!RegisterClassEx( &window ))
{
MessageBox(NULL, TEXT("Something's wrong.. quitting"), TEXT("error"), MB_OK);
return 1; // return from WinMain.. i.e. quit
}
// CREATE THE WINDOW AND KEEP THE HANDLE TO IT.
HWND hwnd; // declare a handle to a window
hwnd = CreateWindowExW( WS_EX_TOPMOST, // extended window style.. this sets the window to being always on top
applicationClassName, // window class name.. defined above
TEXT("WINDOW TITLE"), // title bar of window
WS_OVERLAPPEDWINDOW, // window style
CW_USEDEFAULT, 40, // initial x, y start position of window
// CW_USEDEFAULT means "let Windows choose it"
400, 160, // initial width, height of window
NULL, NULL, // parent window, window menu
hInstance, NULL); // program instance handle, creation params
// now we show and paint our window, so it appears
ShowWindow(hwnd, nShowCmd ); // you have to ask that your Window be shown to see it
UpdateWindow(hwnd); // paint the window
#pragma endregion
#pragma region -_3. MESSAGE LOOP_-
MSG msg;
// this is the struct that holds the message
// msg.hwnd - handle to the window to whom the message is for
// msg.message - type of message for the window (mouse click, key press, key up, etc)
// msg.lParam - a parameter that contains data. the data it contains DEPENDS ON THE MESSAGE!!
// msg.wParam - a parameter that contains data. the data it contains DEPENDS ON THE MESSAGE!!
// msg.time - the time the message was created
// msg.pt - cursor position in screen coordinates when message was posted
// enter the message loop
while(GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg); // send off to WndProc for processing
}
#pragma endregion
return 0; // end program once we exit the message loop
}
////////////////////////////////////////////////////////////////////////
// WndProc - "Window procedure" function -- this function gets called
// by Windows whenever there is a "message" available for
// your window.
// A "message" is available for your window whenever something "happens"
// to it -- i.e. the user clicks on it, or the user types a key when your
// window has "input focus"
LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
HDC hdc;
PAINTSTRUCT ps;
RECT rect;
// this switch looks at the message that was passed to the WndProc function.
// There are tons of possible messages..
// WM_CREATE: // message passed to WndProc when the window is first created
// WM_PAINT: // message passed when there is a request from O/S to paint window
// WM_KEYDOWN: // message passed when a key is being pressed down.. see MSDN for more
// Generally, all messages to the window will start with WM_
switch(message)
{
case WM_CREATE: // WndProc gets sent the WM_CREATE message when
{ // the window is first created
// (if you don't have the 8254 chip (lots of modern pc's don't!))
MessageBeep( 1 ) ;
// upon creation, beep 3 times @ 500Hz
//Beep( 500, 10 );
//Beep( 500, 10 );
//Beep( 500, 10 );
// Upon window creation, we want to initialize the timers:
SetTimer( hwnd, TIMER_SECOND, 1000, NULL );
SetTimer( hwnd, TIMER_MINUTE, 60000, NULL );
return 0; // return from WndProc
}
break;
case WM_PAINT:
{
// if the message was "paint the window", just draw the text "This is my window..."
hdc = BeginPaint(hwnd, &ps); // hdc is a "handle to a device context"
GetClientRect(hwnd, &rect); // the "client rectangle" is the window area but
// EXCLUDING the title bar and borders
DrawText(hdc, TEXT("This is my window..."), -1, &rect, DT_SINGLELINE | DT_BOTTOM | DT_VCENTER );
EndPaint(hwnd, &ps);
return 0;
}
break;
case WM_DESTROY:
{
PostQuitMessage(0);
return 0;
}
break;
// +++ new.
case WM_TIMER:
{
switch(wParam)
{
case TIMER_SECOND:
//Beep(200, 20); // just tick every time the WM_TIMER message is passed
MessageBeep( 1 ) ;
break;
case TIMER_MINUTE:
//Beep(700, 100); // beep higher every minute
MessageBeep( 1 ) ;
}
return 0;
}
break;
}
// If message was NOT handled by us, we pass it off to
// the windows operating system to handle it.
return DefWindowProc(hwnd, message, wParam, lParam);
}
Advertisement