Skip to main content
GameDev.net gamedev.net
🔒 Locked

just a test

Started by cpp boy Jun 12, 2002 at 8:08 PM 2 replies 850+ views
Original Post
cpp boy
cpp boy
#include <windows.h> #define WIN_WIDTH 640 #define WIN_HEIGHT 480 // Where begin drawing the text to #define TEXT_X 300 #define TEXT_Y 220 #define class_name "GT_KeyboardInput" // Save us a little typing %) typedef unsigned char uchar; // This function will print to the screen the text passed in, using // the RGB color passed in void PrintColoredText(HWND hwnd, char *text, uchar R, uchar G, uchar B); // Standard callback function LRESULT CALLBACK WinProc(HWND hwnd, UINT message, WPARAM wparam, LPARAM lparam); 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 = class_name; // The name of our window class wndclassex.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH); // This sets // the background color // of the window to white 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 class_name, // Name we gave our WNDCLASSEX "www.GameTutorials.com -- Keyboard Input", // Text for the title bar 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) WIN_WIDTH, // Width of window in pixels WIN_HEIGHT, // 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,hwnd,0,0,PM_REMOVE)) { if(msg.message == WM_QUIT) break; TranslateMessage(&msg); DispatchMessage(&msg); } else { // Do all the hardcore computational stuff here } } return msg.wParam; // And we're out } // The WinProc LRESULT CALLBACK WinProc(HWND hwnd, UINT message, WPARAM wparam, LPARAM lparam) { PAINTSTRUCT ps; // 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); else if(tchar == 'q' || tchar == 'Q') PostQuitMessage(0); return 0; } case WM_PAINT: BeginPaint(hwnd,&ps); EndPaint(hwnd,&ps); return 0; case WM_DESTROY: case WM_CLOSE: PostQuitMessage(0); return 0; } // end of switch(message) return DefWindowProc(hwnd, message, wparam, lparam); } // Fill the window color to the RGB color passed in void PrintColoredText(HWND hwnd, char *text, uchar red, uchar green, uchar blue) { // Error Check if(!text) return; // If "text" isn't valid, we'll just return HDC hdc = GetDC(hwnd); // Get the windows device context // Error Check if(!hdc) return; // Can't fill the window without an HDC, we'll just return RECT rect; GetClientRect(hwnd,▭); // Get the window's client area RECT // Clear the client area of the window to all white FillRect(hdc,▭,(HBRUSH)GetStockObject(WHITE_BRUSH)); // This Win32 API does just what you expect, it sets the text color for the // specified device context to the RGB color value passed SetTextColor(hdc,RGB(red,green,blue)); // This function prints text to the window -- Lets break it down by parameter: // hdc == The device context to draw to (here hdc == "our window") // TEXT_X == The Window X coordinate to begin drawing the text to // TEXT_Y == The Window Y coordinate to begin drawing the text to // text == a char* (Windows defines this variable as a LPCSTR, but really that's just // the same as a char*) that contains the string to be displayed // strlen(text) == The number of characters in the string TextOut(hdc,TEXT_X,TEXT_Y,text,strlen(text)); ReleaseDC(hwnd,hdc); // Be sure to always free up your HDC /***** NOTE: In SetTextColor() we use the macro RGB() RGB() is a Win32 macro that makes a COLORREF. A COLORREF is a 32-bit value that specifies an RGB color. A COLORREF looks like this in hex: 0x00bbggrr With bb == the blue component, gg == the green component, and rr == red component which are each an unsigned value between 0 - 255 *****/ } [edited by - cpp boy on June 12, 2002 9:10:16 PM]
Kevin
Gaiiden
Gaiiden
mind telling me what this is before I close it??
Drew Sikora
Executive Producer
GameDev.net
garconbifteck
garconbifteck
quote:
Original post by Gaiiden
mind telling me what this is before I close it??


Just close it.
Gaiiden
Gaiiden
Yeah well... I was curious.
Drew Sikora
Executive Producer
GameDev.net

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.