winmain.cpp ( File view )

  • By zzhan08 2014-11-01
  • View(s):104
  • Download(s):0
  • Point(s): 2
			// Programming 2D Games
// Copyright (c) 2011 by:
// Charles Kelly
// Chapter 2 Keyboard State with Windows API v1.0
// winmain.cpp

#define WIN32_LEAN_AND_MEAN

#include <windows.h>

// Function prototypes
int WINAPI WinMain(HINSTANCE, HINSTANCE, LPSTR, int); 
bool CreateMainWindow(HINSTANCE, int);
LRESULT WINAPI WinProc(HWND, UINT, WPARAM, LPARAM); 

// Global variables
HINSTANCE hinst;
HDC    hdc;                 // handle to device context
TCHAR ch = ' ';             // character entered
RECT rect;                  // rectangle
PAINTSTRUCT ps;             // used in WM_PAINT
bool vkKeys[256];           // state of virtual keys, false or true

// Constants
const char CLASS_NAME[]  = "Keyboard";
const char APP_TITLE[]   = "Keys Down";
const int  WINDOW_WIDTH  = 400;  // width of window
const int  WINDOW_HEIGHT = 400;  // height of window

//=============================================================================
// Starting point for a Windows application
//=============================================================================
int WINAPI WinMain(HINSTANCE hInstance,
                   HINSTANCE hPrevInstance,
                   LPSTR     lpCmdLine,
                   int       nCmdShow)
{

    MSG     msg;

    // Create the main window
    if (!CreateMainWindow(hInstance, nCmdShow))
        return false;

    for (int i=0; i<256; i++)   // initialize virtual key array
        vkKeys[i] = false;

    // main message loop
    int done = 0;
    while (!done)
    {

        if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)) 
        {

            //look for quit message
            if (msg.message == WM_QUIT)
                done = 1;

            //decode and pass messages on to WinProc
            TranslateMessage(&msg);
            DispatchMessage(&msg);
        
}
    
}

    return msg.wParam;

}

//=============================================================================
// window event callback function
//=============================================================================
LRESULT WINAPI WinProc( HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam )
{

    short nVirtKey;                 // virtual-key code 
    const short SHIFTED = (short)0x8000; 
    TEXTMETRIC tm;                  // structure for text metrics 
    DWORD chWidth = 20;             // width of characters
    DWORD chHeight = 20;            // height of characters

    switch( msg )
    {

        case WM_CREATE:
            // get the text metrics
            hdc = GetDC(hwnd);
            GetTextMetrics(hdc, &tm);
            ReleaseDC(hwnd, hdc);
            chWidth = tm.tmAveCharWidth;        // average character width
            chHeight = tm.tmHeight;             // character height
            return 0;

        case WM_DESTROY:
            //tell Windows to kill this program
            PostQuitMessage(0);
            return 0;

        case WM_KEYDOWN:                                // key down
            vkKeys[wParam] = true;
            switch(wParam)
            {

                case VK_SHIFT:                          // shift key
                    nVirtKey = GetKeyState(VK_LSHIFT);  // get state of left shift
                    if (nVirtKey & SHIFTED)             // if left shift
                        vkKeys[VK_LSHIFT] = true;
                    nVirtKey = GetKeyState(VK_RSHIFT);  // get state of right shift
                    if (nVirtKey & SHIFTED)             // if right shift
                        vkKeys[VK_RSHIFT] = true;
                    break;
                case VK_CONTROL:                        // control key
                    nVirtKey = GetKeyState(VK_LCONTROL);
                    if (nVirtKey & SHIFTED)             // if left control
                        vkKeys[VK_LCONTROL] = true;
                    nVirtKey = GetKeyState(VK_RCONTROL);
                    if (nVirtKey & SHIFTED)             // if right control
                        vkKeys[VK_RCONTROL] = true;
                    break;
            
}
            InvalidateRect(hwnd, NULL, TRUE);           // force WM_PAINT
            return 0;
            break;

        case WM_KEYUP:                                  // key up
            vkKeys[wParam] = false;
            switch(wParam)
            {

                case VK_SHIFT:                          // shift key
                    nVirtKey = GetKeyState(VK_LSHIFT); 
                    if ((nVirtKey & SHIFTED) == 0)      // if left shift
                        vkKeys[VK_LSHIFT] = false;
                    nVirtKey = GetKeyState(VK_RSHIFT); 
                    if ((nVirtKey & SHIFTED) == 0)      // if right shift
                        vkKeys[VK_RSHIFT] = false;
                    break;
                case VK_CONTROL:                        // control key
                    nVirtKey = GetKeyState(VK_LCONTROL);
                    if ((nVirtKey & SHIFTED) == 0)      // if left control
                        vkKeys[VK_LCONTROL] = false;
                    nVirtKey = GetKeyState(VK_RCONTROL);
                    if ((nVirtKey & SHIFTED) == 0)      // if right control
                        vkKeys[VK_RCONTROL] = false;
                    break;
            
}
            InvalidateRect(hwnd, NULL, TRUE);    // force WM_PAINT
            return 0;
            break;

        case WM_CHAR:               // a character was entered by the keyboard
            switch (wParam)         // the character is in wParam
            {

                case 0x08:              // backspace
                case 0x09:              // tab
                case 0x0A:              // linefeed
                case 0x0D:              // carriage return
                case 0x1B:              // escape
                    return 0;           // non displayable character
                default:                // displayable character
                    ch = (TCHAR) wParam;    // get the character
                    InvalidateRect(hwnd, NULL, TRUE);   // force WM_PAINT
                    return 0;
            
}

        case WM_PAINT:
            hdc = BeginPaint(hwnd, &ps);    // get handle to device context
            TextOut(hdc, 0, 0, &ch, 1);     // display the character
    
...
...
(Please download the complete source code to view)
			
...
Expand> <Close

Want complete source code? Download it here

Point(s): 2

Download
0 lines left, continue to read
Sponsored links

File list

Tips: You can preview the content of files by clicking file names^_^
Name Size Date
01.97 kB
HelloWorld.exe32.50 kB2012-01-13|04:36
01.97 kB
01.97 kB
._HelloWorld.exe222.00 B2012-01-13|04:36
HelloWorld.sln886.00 B2011-06-21|02:15
._HelloWorld.sln222.00 B2011-06-21|02:15
HelloWorld.vcxproj3.20 kB2011-05-19|02:44
._HelloWorld.vcxproj222.00 B2011-05-19|02:44
HelloWorld.vcxproj.filters956.00 B2010-12-31|10:21
._HelloWorld.vcxproj.filters222.00 B2010-12-31|10:21
license.htm24.85 kB2012-01-26|11:20
._license.htm222.00 B2012-01-26|11:20
01.97 kB
cc-logo.jpg33.26 kB2012-01-26|11:20
01.97 kB
._cc-logo.jpg222.00 B2012-01-26|11:20
connection-min.js11.33 kB2012-01-26|11:20
._connection-min.js222.00 B2012-01-26|11:20
container-min.js71.26 kB2012-01-26|11:20
._container-min.js222.00 B2012-01-26|11:20
container.css4.58 kB2012-01-26|11:20
._container.css222.00 B2012-01-26|11:20
deed67.00 B2012-01-26|11:20
._deed222.00 B2012-01-26|11:20
deed.js1.57 kB2012-01-26|11:20
._deed.js222.00 B2012-01-26|11:20
deed.txt731.00 B2012-01-26|11:20
._deed.txt222.00 B2012-01-26|11:20
deed3-print.css2.49 kB2012-01-26|11:20
._deed3-print.css222.00 B2012-01-26|11:20
deed3.css13.55 kB2012-01-26|11:20
._deed3.css222.00 B2012-01-26|11:20
ga.js32.45 kB2012-01-26|11:20
._ga.js222.00 B2012-01-26|11:20
ga_002.js32.45 kB2012-01-26|11:20
._ga_002.js222.00 B2012-01-26|11:20
help.js4.68 kB2012-01-26|11:20
._help.js222.00 B2012-01-26|11:20
information.png778.00 B2012-01-26|11:20
._information.png222.00 B2012-01-26|11:20
json-min.js3.40 kB2012-01-26|11:20
._json-min.js222.00 B2012-01-26|11:20
jurisdictions.css841.00 B2012-01-26|11:20
._jurisdictions.css222.00 B2012-01-26|11:20
seal.png7.16 kB2012-01-26|11:20
._seal.png222.00 B2012-01-26|11:20
yahoo-dom-event.js30.90 kB2012-01-26|11:20
._yahoo-dom-event.js222.00 B2012-01-26|11:20
._license_files222.00 B2014-01-30|04:26
winmain.cpp4.66 kB2012-01-13|06:31
._winmain.cpp222.00 B2012-01-13|06:31
HelloWorld.vcxproj.user143.00 B2014-01-30|04:30
01.97 kB
HelloWorld.log1.99 kB2014-01-30|04:31
HelloWorld.vcxprojResolveAssemblyReference.cache713.00 B2014-01-30|04:31
HelloWorld.lastbuildstate106.00 B2014-01-30|04:31
HelloWorld.write.1.tlog0.00 B2014-01-30|04:31
vc100.idb179.00 kB2014-01-30|04:31
vc100.pdb60.00 kB2014-01-30|04:31
winmain.obj13.98 kB2014-01-30|04:31
CL.read.1.tlog7.23 kB2014-01-30|04:31
CL.write.1.tlog380.00 B2014-01-30|04:31
cl.command.1.tlog724.00 B2014-01-30|04:31
HelloWorld.ilk329.23 kB2014-01-30|04:31
mt.read.1.tlog924.00 B2014-01-30|04:31
HelloWorld.pdb371.00 kB2014-01-30|04:31
HelloWorld.exe.intermediate.manifest381.00 B2014-01-30|04:31
link.read.1.tlog2.85 kB2014-01-30|04:31
link.write.1.tlog1.03 kB2014-01-30|04:31
link.command.1.tlog1.53 kB2014-01-30|04:31
HelloWorld.exe28.50 kB2014-01-30|04:31
mt.write.1.tlog250.00 B2014-01-30|04:31
mt.command.1.tlog634.00 B2014-01-30|04:31
HelloWorld.suo8.50 kB2014-01-30|04:31
._HelloWorld222.00 B2014-01-30|04:31
Character0.00 B2014-01-30|04:36
Character33.00 kB2012-01-13|04:35
Character0.00 B2014-02-11|09:52
Character222.00 B2012-01-13|04:35
Character894.00 B2011-06-28|03:09
Character222.00 B2011-06-28|03:09
Character4.54 kB2010-01-29|04:49
Character222.00 B2010-01-29|04:49
Character943.00 B2010-01-08|14:18
Character222.00 B2010-01-08|14:18
Character24.85 kB2012-01-26|11:20
Character222.00 B2012-01-26|11:20
Character0.00 B2014-01-30|04:27
Character33.26 kB2012-01-26|11:20
Character0.00 B2014-02-11|09:52
Character222.00 B2012-01-26|11:20
Character11.33 kB2012-01-26|11:20
Character222.00 B2012-01-26|11:20
Character71.26 kB2012-01-26|11:20
Character222.00 B2012-01-26|11:20
Character4.58 kB2012-01-26|11:20
Character222.00 B2012-01-26|11:20
Character67.00 B2012-01-26|11:20
Character222.00 B2012-01-26|11:20
Character1.57 kB2012-01-26|11:20
Character222.00 B2012-01-26|11:20
Character731.00 B2012-01-26|11:20
Character222.00 B2012-01-26|11:20
Character2.49 kB2012-01-26|11:20
Character222.00 B2012-01-26|11:20
Character13.55 kB2012-01-26|11:20
Character222.00 B2012-01-26|11:20
Character32.45 kB2012-01-26|11:20
Character222.00 B2012-01-26|11:20
Character32.45 kB2012-01-26|11:20
Character222.00 B2012-01-26|11:20
Character4.68 kB2012-01-26|11:20
Character222.00 B2012-01-26|11:20
Character778.00 B2012-01-26|11:20
Character222.00 B2012-01-26|11:20
Character3.40 kB2012-01-26|11:20
Character222.00 B2012-01-26|11:20
Character841.00 B2012-01-26|11:20
Character222.00 B2012-01-26|11:20
Character7.16 kB2012-01-26|11:20
Character222.00 B2012-01-26|11:20
Character30.90 kB2012-01-26|11:20
Character222.00 B2012-01-26|11:20
Character222.00 B2014-01-30|04:27
Character5.60 kB2012-01-13|06:38
Character222.00 B2012-01-13|06:38
Character143.00 B2014-01-30|04:33
Character0.00 B2014-01-30|04:34
Character2.08 kB2014-01-30|04:34
Character111.00 B2014-01-30|04:34
Character0.00 B2014-01-30|04:33
Character179.00 kB2014-01-30|04:34
Character60.00 kB2014-01-30|04:34
Character15.78 kB2014-01-30|04:34
Character7.09 kB2014-01-30|04:34
Character400.00 B2014-01-30|04:34
Character744.00 B2014-01-30|04:34
Character946.39 kB2014-01-30|04:34
Character960.00 B2014-01-30|04:34
Character1.57 MB2014-01-30|04:34
Character381.00 B2014-01-30|04:34
Character2.88 kB2014-01-30|04:34
Character650.00 B2014-01-30|04:34
Character1.60 kB2014-01-30|04:34
Character388.50 kB2014-01-30|04:34
Character268.00 B2014-01-30|04:34
Character678.00 B2014-01-30|04:34
Character8.50 kB2014-01-30|04:36
._Character222.00 B2014-01-30|04:36
Keys0.00 B2014-01-30|04:27
Keys34.00 kB2012-01-13|04:37
Keys0.00 B2014-02-11|09:52
Keys222.00 B2012-01-13|04:37
Keys882.00 B2011-06-21|02:24
Keys222.00 B2011-06-21|02:24
Keys3.64 kB2010-01-08|14:18
Keys222.00 B2010-01-08|14:18
Keys943.00 B2010-01-08|14:18
Keys222.00 B2010-01-08|14:18
Keys24.85 kB2012-01-26|11:20
Keys222.00 B2012-01-26|11:20
Keys0.00 B2014-01-30|04:27
Keys33.26 kB2012-01-26|11:20
Keys0.00 B2014-02-11|09:52
Keys222.00 B2012-01-26|11:20
Keys11.33 kB2012-01-26|11:20
Keys222.00 B2012-01-26|11:20
Keys71.26 kB2012-01-26|11:20
Keys222.00 B2012-01-26|11:20
Keys4.58 kB2012-01-26|11:20
Keys222.00 B2012-01-26|11:20
Keys67.00 B2012-01-26|11:20
Keys222.00 B2012-01-26|11:20
Keys1.57 kB2012-01-26|11:20
Keys222.00 B2012-01-26|11:20
Keys731.00 B2012-01-26|11:20
Keys222.00 B2012-01-26|11:20
Keys2.49 kB2012-01-26|11:20
Keys222.00 B2012-01-26|11:20
Keys13.55 kB2012-01-26|11:20
Keys222.00 B2012-01-26|11:20
Keys32.45 kB2012-01-26|11:20
Keys222.00 B2012-01-26|11:20
Keys32.45 kB2012-01-26|11:20
Keys222.00 B2012-01-26|11:20
Keys4.68 kB2012-01-26|11:20
Keys222.00 B2012-01-26|11:20
Keys778.00 B2012-01-26|11:20
Keys222.00 B2012-01-26|11:20
Keys3.40 kB2012-01-26|11:20
Keys222.00 B2012-01-26|11:20
Keys841.00 B2012-01-26|11:20
Keys222.00 B2012-01-26|11:20
Keys7.16 kB2012-01-26|11:20
Keys222.00 B2012-01-26|11:20
Keys30.90 kB2012-01-26|11:20
Keys222.00 B2012-01-26|11:20
Keys222.00 B2014-01-30|04:27
Keys9.36 kB2012-01-13|06:38
Keys222.00 B2012-01-13|06:38
._Keys222.00 B2014-01-30|04:27
Prevent0.00 B2014-01-30|05:36
Prevent24.85 kB2012-01-26|11:20
Prevent0.00 B2014-02-11|09:52
Prevent222.00 B2012-01-26|11:20
Prevent0.00 B2014-01-30|04:28
Prevent33.26 kB2012-01-26|11:20
Prevent0.00 B2014-02-11|09:52
Prevent222.00 B2012-01-26|11:20
Prevent11.33 kB2012-01-26|11:20
Prevent222.00 B2012-01-26|11:20
Prevent71.26 kB2012-01-26|11:20
Prevent222.00 B2012-01-26|11:20
Prevent4.58 kB2012-01-26|11:20
Prevent222.00 B2012-01-26|11:20
Prevent67.00 B2012-01-26|11:20
Prevent222.00 B2012-01-26|11:20
Prevent1.57 kB2012-01-26|11:20
Prevent222.00 B2012-01-26|11:20
Prevent731.00 B2012-01-26|11:20
Prevent222.00 B2012-01-26|11:20
Prevent2.49 kB2012-01-26|11:20
Prevent222.00 B2012-01-26|11:20
Prevent13.55 kB2012-01-26|11:20
Prevent222.00 B2012-01-26|11:20
Prevent32.45 kB2012-01-26|11:20
Prevent222.00 B2012-01-26|11:20
Prevent32.45 kB2012-01-26|11:20
Prevent222.00 B2012-01-26|11:20
Prevent4.68 kB2012-01-26|11:20
Prevent222.00 B2012-01-26|11:20
Prevent778.00 B2012-01-26|11:20
Prevent222.00 B2012-01-26|11:20
Prevent3.40 kB2012-01-26|11:20
Prevent222.00 B2012-01-26|11:20
Prevent841.00 B2012-01-26|11:20
Prevent222.00 B2012-01-26|11:20
Prevent7.16 kB2012-01-26|11:20
Prevent222.00 B2012-01-26|11:20
Prevent30.90 kB2012-01-26|11:20
Prevent222.00 B2012-01-26|11:20
Prevent222.00 B2014-01-30|04:28
Prevent5.14 kB2012-01-13|06:40
Prevent222.00 B2012-01-13|06:40
Prevent896.00 B2011-06-21|02:25
Prevent222.00 B2011-06-21|02:25
Prevent4.58 kB2011-05-19|02:41
Prevent222.00 B2011-05-19|02:41
Prevent951.00 B2011-05-19|02:41
Prevent222.00 B2011-05-19|02:41
Prevent10.00 kB2014-01-30|05:38
Prevent143.00 B2014-01-30|05:35
Prevent0.00 B2014-01-30|05:36
Prevent2.09 kB2014-01-30|05:36
Prevent713.00 B2014-01-30|05:36
Prevent112.00 B2014-01-30|05:36
Prevent0.00 B2014-01-30|05:36
Prevent179.00 kB2014-01-30|05:36
Prevent60.00 kB2014-01-30|05:36
Prevent15.32 kB2014-01-30|05:36
Prevent7.26 kB2014-01-30|05:36
Prevent420.00 B2014-01-30|05:36
Prevent780.00 B2014-01-30|05:36
Prevent381.00 B2014-01-30|05:36
Prevent2.91 kB2014-01-30|05:36
Prevent1.11 kB2014-01-30|05:36
Prevent1.61 kB2014-01-30|05:36
Prevent956.00 B2014-01-30|05:36
Prevent272.00 B2014-01-30|05:36
Prevent676.00 B2014-01-30|05:36
Prevent933.80 kB2014-01-30|05:36
Prevent1.57 MB2014-01-30|05:36
Prevent388.00 kB2014-01-30|05:36
._Prevent222.00 B2014-01-30|05:36
...
Sponsored links

winmain.cpp (2.08 MB)

Need 2 point
Your Point(s)

Your Point isn't enough.

Get point immediately by PayPal

More(Debit card / Credit card / PayPal Credit / Online Banking)

Submit your source codes. Get more point

LOGIN

Don't have an account? Register now
Need any help?
Mail to: support@codeforge.com

切换到中文版?

CodeForge Chinese Version
CodeForge English Version

Where are you going?

^_^"Oops ...

Sorry!This guy is mysterious, its blog hasn't been opened, try another, please!
OK

Warm tip!

CodeForge to FavoriteFavorite by Ctrl+D