// The WinProc LRESULT CALLBACK WinProc(HWND hwnd, UINT message, WPARAM wparam, LPARAM lparam) { // Depending on the message -- we'll do different stuff switch(message) { // This is the message where all the magic happens. A WM_CHAR message // gets sent every time WM_KEYDOWN message is translated by the // TranslateMessage() function. So what does that mean? Okay see in our WinMain() // how we have the function call TranslateMessage(&msg). You HAVE to have // this call in WinMain() (with a similar type set up we are using here) // for you to receive the WM_CHAR message in your WinProc(). case WM_CHAR: { // Okay the first thing is what in the heck is a TCHAR? // For all intensive purposes, a TCHAR == char (the normal C/C++ char) // On a UNICODE machine it actually equals something else, but either // way you can think of it as just a good ole' char // Now for the WM_CHAR message, the WPARAM equals the "character code" // that was pressed. You can think of the WPARAM equaling the ASCII // representation of the key that was pressed. TCHAR tchar = (TCHAR)wparam; // Get the key that was pressed. // Now lets check for the 'R', 'G' or 'B' key // Notice how we check for BOTH upper and lowercase so not // to miss one case or the other if(tchar == 'r' || tchar == 'R') PrintColoredText(hwnd,"RED",255,0,0); else if(tchar == 'g' || tchar == 'G') PrintColoredText(hwnd,"GREEN",0,255,0); else if(tchar == 'b' || tchar == 'B') PrintColoredText(hwnd,"BLUE",0,0,255); return 0; } // Destroy the window when it's time case WM_DESTROY: PostQuitMessage(0); return 0; } // end of switch(message) return DefWindowProc(hwnd, message, wparam, lparam); }
/* * Sys_ConsoleOutput * * Print text to the dedicated console */ void Sys_ConsoleOutput( char *string ) { DWORD dummy; char text[MAX_CONSOLETEXT+2]; /* need 2 chars for the \r's */ if( !dedicated || !dedicated->integer ) return; if( !houtput ) houtput = GetStdHandle( STD_OUTPUT_HANDLE ); if( console_textlen ) { text[0] = '\r'; memset( &text[1], ' ', console_textlen ); text[console_textlen+1] = '\r'; text[console_textlen+2] = 0; WriteFile( houtput, text, console_textlen+2, &dummy, NULL ); } string = utf8_to_OEM( string ); #if 0 WriteFile( houtput, string, (unsigned)strlen( string ), &dummy, NULL ); #else PrintColoredText( string ); #endif if( console_textlen ) WriteFile( houtput, console_text, console_textlen, &dummy, NULL ); }
int WINAPI WinMain(HINSTANCE hinstance, HINSTANCE hprev, PSTR cmdline, int ishow) { HWND hwnd; // This will hold the handle to our window MSG msg; // This will hold any messages (such as a key press) that // our window might receive WNDCLASSEX wndclassex = {0}; // This is our "window class" -- The "EX" stands // for "extended style" which gives us more options // when creating our window (although we're going to // completely ignore 'em) // Fill the fields we care about wndclassex.cbSize = sizeof(WNDCLASSEX); // Always must be set wndclassex.style = CS_HREDRAW | CS_VREDRAW; // Window classes style wndclassex.lpfnWndProc = WinProc; // Pointer to where the WinProc() is defined wndclassex.hInstance = hinstance; // The handle to the instance of our window wndclassex.lpszClassName = kClassName; // The name of our window class wndclassex.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH); // Background color of white wndclassex.hCursor = (HCURSOR)LoadImage(NULL, MAKEINTRESOURCE(IDC_ARROW), IMAGE_CURSOR, 0, 0, LR_SHARED); // Use default arrow cursor RegisterClassEx(&wndclassex); // Register the window class so calls to CreateWindow() // and CreateWindowEx() will know what window class to // use when creating the window hwnd = CreateWindowEx(NULL, // Extra window attributes, NULL means none kClassName, // Name we gave our WNDCLASSEX "www.GameTutorials.com -- Keyboard Input Part2", // Title bar text WS_OVERLAPPEDWINDOW, // Style of window (see MSDN for full description) CW_USEDEFAULT, // Upper left xPos of window (Windows picks default) CW_USEDEFAULT, // Upper left yPos of window (Windows picks default) kWinWid, // Width of window in pixels kWinHgt, // Height of window in pixels NULL, // Handle to "parent window" (we have none, this is the parent) NULL, // Handle to a menu (we have none) hinstance, // Handle to instance of this window (passed in by WinMain()) NULL); // "Extra info" to pass to the WinProc (we have none) // Error check if(!hwnd) return EXIT_FAILURE; // Something really bad happened! ShowWindow(hwnd, ishow); // Show the window (make it visible) UpdateWindow(hwnd); // Updates the window (initially draw the window) while(1) { // Get message(s) (for instance a key press) if there is any if(PeekMessage(&msg,NULL,0,0,PM_REMOVE)) { if(msg.message == WM_QUIT) break; TranslateMessage(&msg); DispatchMessage(&msg); } else { // If any of the letters (case sensitive) in "GameTutorials" are pressed // Print to the screen "GameTutorials" in a random color if (KeyPressed('G') || KeyPressed('a') || KeyPressed('m') || KeyPressed('e') || KeyPressed('T') || KeyPressed('u') || KeyPressed('t') || KeyPressed('o') || KeyPressed('r') || KeyPressed('i') || KeyPressed('l') || KeyPressed('s')) { // Get a random R, G, and B uchar red = rand()%256; uchar green = rand()%256; uchar blue = rand()%256; PrintColoredText(hwnd,"GameTutorials",red,green,blue); } Sleep(100); // Chill out for awhile man! // What Sleep() does is not do anything for the number of // milliseconds passed to the function } } UnregisterClass(kClassName,hinstance); // Free up the memory allocated by our WNDCLASSEX return msg.wParam; // And we're out }