《windows程序设计》学习_2.2:初识消息,双键的使用

  1 /* 双键的使用 */

  2 

  3 #include <windows.h>

  4 

  5 

  6 LRESULT CALLBACK WndProc(HWND,UINT,WPARAM,LPARAM);    

  7 

  8 int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,

  9                    PSTR szCmdLine, int iCmdShow)

 10 {

 11     static TCHAR szAppName[] = TEXT ("HelloWin");

 12     HWND         hwnd;

 13     MSG          msg;

 14     WNDCLASS     wndclass;

 15     

 16     wndclass.style              = CS_HREDRAW | CS_VREDRAW  ;

 17     wndclass.lpfnWndProc        = WndProc ;

 18     wndclass.cbClsExtra         = 0 ;

 19     wndclass.cbWndExtra         = 0 ;

 20     wndclass.hInstance          = hInstance ;

 21     wndclass.hIcon              = LoadIcon(NULL,IDI_APPLICATION) ;

 22     wndclass.hCursor            = LoadCursor(NULL,IDC_ARROW) ;

 23     wndclass.hbrBackground      = (HBRUSH) GetStockObject(WHITE_BRUSH) ;

 24     wndclass.lpszMenuName       = NULL ;

 25     wndclass.lpszClassName      = szAppName ;

 26     

 27     if(!RegisterClass(&wndclass))

 28     {

 29         MessageBox(NULL, TEXT("This program requires Windows NT!"),

 30             szAppName, MB_ICONERROR);

 31         

 32         return 0;

 33     }

 34     

 35     hwnd = CreateWindow(szAppName,

 36         TEXT("The Hello Program"),

 37         WS_OVERLAPPEDWINDOW,

 38         CW_USEDEFAULT,

 39         CW_USEDEFAULT,

 40         CW_USEDEFAULT,

 41         CW_USEDEFAULT,

 42         NULL,

 43         NULL,

 44         hInstance,

 45         NULL);

 46     

 47     ShowWindow(hwnd , iCmdShow);

 48     UpdateWindow(hwnd);

 49     

 50     while(GetMessage (&msg, NULL, 0, 0))

 51     {

 52         TranslateMessage(&msg);

 53         DispatchMessage(&msg);

 54     }

 55     

 56     return msg.wParam;

 57 }

 58 

 59 LRESULT CALLBACK WndProc(HWND hwnd , UINT message , WPARAM wParam , LPARAM lParam)

 60 {

 61     HDC         hdc;

 62     PAINTSTRUCT ps;

 63     RECT        rect;

 64     

 65     switch(message)

 66     {

 67     case WM_CREATE:

 68 //        PlaySound (TEXT ("123.wav"), NULL , SND_FILENAME |SND_ASYNC);

 69         return 0;

 70         

 71     case WM_PAINT:

 72         hdc = BeginPaint(hwnd , &ps);

 73         

 74         GetClientRect(hwnd, &rect);

 75         

 76         DrawText(hdc , TEXT("Hello Windows 98!") , -1 ,&rect , 

 77             DT_SINGLELINE | DT_CENTER | DT_VCENTER);

 78         

 79         EndPaint(hwnd,&ps);

 80         

 81 //        PlaySound (TEXT ("123.wav"), NULL , SND_FILENAME |SND_ASYNC);

 82         return 0;

 83 

 84     case WM_LBUTTONDOWN:

 85         if(wParam & MK_RBUTTON)

 86             PlaySound (TEXT ("123.wav"), NULL , SND_FILENAME |SND_ASYNC);

 87         return 0;

 88 

 89     case WM_RBUTTONDOWN:

 90         if(wParam & MK_LBUTTON)

 91             PlaySound (TEXT ("123.wav"), NULL , SND_FILENAME |SND_ASYNC);

 92         return 0;

 93         

 94     case WM_DESTROY:

 95 //        PlaySound (TEXT ("123.wav"), NULL , SND_FILENAME |SND_ASYNC);

 96         PostQuitMessage(0);

 97         return 0;

 98     }

 99     return DefWindowProc(hwnd,message,wParam,lParam);

100 }

 

你可能感兴趣的:(windows)