/* Author : Willie Tang * Compiler : MS Visual C++ 6.0 * Created : September 05, 2002 * Modified : September 06, 2002 * Website : http://nomad.openglforums.com * * Notes : The code below works as a Win32 Application. * To run the code, create a new project (File->New). * In the Projects Tab, select "Win32 Application", * give the project a name them click OK. Then select * the "An Empty Project" radio button, click Finish. * * To insert the code to the project. Click on the * Project menu->Add to Project->New. On the Files * Tab, select "C++ Source File", give any name to * the file, then click OK. After that, copy and paste * the code below to the editor. ************************************************************/ #include #include #include //--[ I know, I know, global variables are bad...but hey, //--[ my goal is to let you know how to use these stuffs. //--[ Besides, it's actually OK to use global variables //--[ for small programs...;). HWND hWindow; // handle to window HDC hDC; // window's device context LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) { switch(message) { case WM_CREATE: // window is created MessageBox(NULL, "Hit Esc To Close This Window.", "So You Know", MB_OK); break; case WM_DESTROY: // window is destroyed PostQuitMessage(0); // add "quit" to msg queue break; // We grab system keystrokes here...this will // disable the Alt-F4 key!!! =). case WM_SYSKEYDOWN: case WM_SYSKEYUP: case WM_SYSCHAR: //...do nothing... break; // Note that I shall only be using the WM_KEYDOWN // message. If you want your program to react when // the key is released, use WM_KEYUP instead... // Also, I shall only be detecting function keys F1-F4 case WM_KEYDOWN: switch(wParam) { case VK_F1: // user has pressed F1 key MessageBox(NULL, // accepts hWnd, can be NULL here "You Pressed F1!!!", // text shown in message box "Notice", // text shown in title bar MB_OK); // put an OK button break; case VK_F2: MessageBox(NULL, // accepts hWnd, can be NULL here "You Pressed F2!!!", // text shown in message box "Notice", // text shown in title bar MB_OK); // put an OK button break; case VK_F3: // user has pressed F3 key MessageBox(NULL, // accepts hWnd, can be NULL here "You Pressed F3!!!", // text shown in message box "Notice", // text shown in title bar MB_OK); // put an OK button break; case VK_F4: MessageBox(NULL, // accepts hWnd, can be NULL here "You Pressed F4!!!", // text shown in message box "Notice", // text shown in title bar MB_OK); // put an OK button break; } break; // This is the WM_CHAR message, you grab your // character inputs here. Note that I shall only // be detecting letters 'a' to 'c' (small and big)... case WM_CHAR: // received character message switch(wParam) { case 'a': MessageBox(NULL, // accepts hWnd, can be NULL here "You Pressed a!!!", // text shown in message box "Notice", // text shown in title bar MB_OK); // put an OK button break; case 'A': MessageBox(NULL, // accepts hWnd, can be NULL here "You Pressed A!!!", // text shown in message box "Notice", // text shown in title bar MB_OK); // put an OK button break; case 'b': MessageBox(NULL, // accepts hWnd, can be NULL here "You Pressed b!!!", // text shown in message box "Notice", // text shown in title bar MB_OK); // put an OK button break; case 'B': MessageBox(NULL, // accepts hWnd, can be NULL here "You Pressed B!!!", // text shown in message box "Notice", // text shown in title bar MB_OK); // put an OK button break; case 'c': MessageBox(NULL, // accepts hWnd, can be NULL here "You Pressed c!!!", // text shown in message box "Notice", // text shown in title bar MB_OK); // put an OK button break; case 'C': MessageBox(NULL, // accepts hWnd, can be NULL here "You Pressed C!!!", // text shown in message box "Notice", // text shown in title bar MB_OK); // put an OK button break; } break; default: return(DefWindowProc(hWnd, message, wParam, lParam)); } return(0L); // return a zero } int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd) { WNDCLASS wndclass; MSG msg; BOOL done = FALSE; // Initialize values of wndclass' fields wndclass.style = CS_HREDRAW | CS_VREDRAW; // window's style wndclass.lpfnWndProc = (WNDPROC)WndProc; // pointer to WndProc wndclass.cbClsExtra = 0; // disregard... wndclass.cbWndExtra = 0; // disregard... wndclass.hInstance = hInstance; // copy from WinMain() wndclass.hIcon = LoadIcon(NULL, IDI_APPLICATION); // window icon // IDI_ASTERISK // IDI_EXCLAMATION // IDI_HAND // IDI_QUESTION // IDI_WINLOGO wndclass.hCursor = LoadCursor(NULL, IDC_ARROW); // window's mouse cursor // IDC_CROSS // a...'cross'...:) // IDC_NO // slashed circle // IDC_WAIT // hourglass // IDC_HELP // arrow with question mark // IDC_UPARROW // arrow pointing up wndclass.hbrBackground = (HBRUSH)CreateSolidBrush(RGB(255,255,255)); wndclass.lpszMenuName = NULL; // window's menu name wndclass.lpszClassName = "MyWindowName"; // window's name // Register wndclass if(RegisterClass(&wndclass) == 0) return FALSE; // Create our window and get its handle hWindow = CreateWindow ("MyWindowName", // from wndclass.lpszClassName "My Title Bar", // this will show on title bar // WS_OVERLAPPEDWINDOW, // border, title, min-max-close button WS_OVERLAPPED, // border, title // WS_POPUP // no title, no button // WS_VISIBLE // window is visible from start // WS_VSCROLL // window has vertical scroll bar // WS_HSCROLL // window has horizontal scroll bar // WS_MAXIMIZE // create window in maximized state // WS_MINIMIZE // create window in minimized state 0, // starting x-coord (upper left) 0, // starting y-coord (upper left) 200, // window's x-size 100, // window's y-size NULL, // parent window's handle NULL, // handle to a menu hInstance, // copy from WinMain()'s parameter NULL); // disregard... // Check if window was created if(hWindow == NULL) return FALSE; // Show our window ShowWindow(hWindow, SW_SHOW); // show created window // SW_HIDE // hide created window // SW_RESTORE // show window in restored state // SW_MAXIMIZE // show window in maximized state // SW_MINIMIZE // show window in minimized state // Keep on grabbing messages while(done == FALSE) { if(PeekMessage(&msg,NULL,0,0,PM_REMOVE)) // Did A Message Arrive? { if(msg.message == WM_QUIT) // if message says WM_QUIT done = TRUE; // quit from while-loop else // else... { TranslateMessage(&msg); // convert to "known" msg DispatchMessage(&msg); // send to msg queue } } else { if(GetKeyState(VK_ESCAPE) & 0x80) // if user pressed Esc key { MessageBox(NULL, // accepts hWnd, can be NULL! "Closing Window", // text to show in message box "Note", // text to show in title bar MB_OK); // OK button in message box PostQuitMessage(0); // put a WM_QUIT to msg queue done = TRUE; // quit from loop } if((GetKeyState(VK_MENU) & 0x80) && // If user pressed Alt key and (GetKeyState(VK_F1) & 0x80)) // the F1 key at the same time... { MessageBox(NULL, // accepts hWnd, can be NULL! "You Pressed Alt-F1!!!", // text to show in message box "Note", // text to show in title bar MB_OK); // OK button in message box } } } return 0; }