DEMO1_2.C
上传用户:cncajx
上传日期:2007-01-03
资源大小:190k
文件大小:6k
源码类别:

GDI/图象编程

开发平台:

Visual C++

  1.   1 /****************************************************************/
  2.   2 /*         Demo1_2   ---  The use of WM_PAINT                   */
  3.   3 /****************************************************************/
  4.   4 
  5.   5 #include <windows.h>
  6.   6 #include "demo1_2.h"
  7.   7 
  8.   8 int  PASCAL  WinMain(HANDLE, HANDLE, LPSTR, int);
  9.   9 long FAR PASCAL MainWndProc(HWND, unsigned, WORD, LONG);
  10.  10 
  11.  11 
  12.  12 /****************************************************************/
  13.  13 /*                      WinMain()                               */
  14.  14 /****************************************************************/
  15.  15 
  16.  16 int PASCAL WinMain(HANDLE hInstance, HANDLE hPrevInstance,
  17.  17                    LPSTR lpszCmdLine, int nCmdShow)
  18.  18 {
  19.  19    WNDCLASS wclass;
  20.  20    MSG      msg;
  21.  21    HWND     hWnd;
  22.  22    char     szName[] = "Demo1_2";
  23.  23 
  24.  24    if (!hPrevInstance)
  25.  25     {
  26.  26         wclass.style         = CS_HREDRAW | CS_VREDRAW;
  27.  27         wclass.lpfnWndProc   = MainWndProc;
  28.  28         wclass.cbClsExtra    = 0;
  29.  29         wclass.cbWndExtra    = 0;
  30.  30         wclass.hInstance     = hInstance;
  31.  31         wclass.hIcon         = LoadIcon(NULL, IDI_APPLICATION);
  32.  32         wclass.hCursor       = LoadCursor(NULL, IDC_ARROW);
  33.  33         wclass.hbrBackground = GetStockObject(WHITE_BRUSH);
  34.  34         wclass.lpszMenuName  = szName;
  35.  35         wclass.lpszClassName = szName;
  36.  36 
  37.  37         if (!RegisterClass (&wclass))
  38.  38            return (FALSE);
  39.  39     }
  40.  40 
  41.  41     hWnd = CreateWindow(
  42.  42                 szName,
  43.  43                 "The Use of WM_PAINT",
  44.  44                 WS_OVERLAPPEDWINDOW,
  45.  45                 CW_USEDEFAULT,
  46.  46                 CW_USEDEFAULT,
  47.  47                 CW_USEDEFAULT,
  48.  48                 CW_USEDEFAULT,
  49.  49                 NULL,
  50.  50                 NULL,
  51.  51                 hInstance,
  52.  52                 NULL );
  53.  53 
  54.  54     if (!hWnd)
  55.  55         return (FALSE);
  56.  56 
  57.  57     ShowWindow(hWnd, nCmdShow);
  58.  58     UpdateWindow(hWnd);
  59.  59 
  60.  60     while (GetMessage(&msg, NULL, NULL,NULL))
  61.  61        {
  62.  62            TranslateMessage(&msg);
  63.  63            DispatchMessage(&msg);
  64.  64        }
  65.  65     return (msg.wParam);
  66.  66 }
  67.  67 
  68.  68 
  69.  69 /****************************************************************/
  70.  70 /*                      MainWndProc()                           */
  71.  71 /****************************************************************/
  72.  72 
  73.  73 long FAR PASCAL MainWndProc(HWND hWnd, unsigned message,
  74.  74                             WORD wParam, LONG lParam)
  75.  75 {
  76.  76    HDC           hDC;
  77.  77    PAINTSTRUCT   ps;
  78.  78    HANDLE        hFont;
  79.  79    char          szString[30];
  80.  80    static int    i = 1;
  81.  81    static char   szMovePos[48];
  82.  82    static char   szLBDownPos[48];
  83.  83    static char   szLBUpPos[48];
  84.  84    static char   szRBDownPos[48];
  85.  85    static char   szRBUpPos[48];
  86.  86 
  87.  87    switch (message)
  88.  88     {
  89.  89         case WM_COMMAND:
  90.  90                 switch(wParam)
  91.  91                   {
  92.  92                     case IDM_MESG :
  93.  93                          hDC = GetDC(hWnd);
  94.  94                          wsprintf(szString,
  95.  95                                  "This is message %d", i++);
  96.  96                          TextOut(hDC, 10, 10, szString,
  97.  97                                  strlen(szString));
  98.  98                          ReleaseDC(hWnd, hDC);
  99.  99                          break;
  100. 100 
  101. 101                     case IDM_EXIT :
  102. 102                          DestroyWindow (hWnd);
  103. 103                          break;
  104. 104                   }
  105. 105                 break;
  106. 106 
  107. 107         case WM_MOUSEMOVE :
  108. 108                 wsprintf(szMovePos, "WM_MOUSEMOVE: %3d %3d",
  109. 109                          LOWORD(lParam), HIWORD(lParam));
  110. 110                 InvalidateRect(hWnd, NULL, FALSE);
  111. 111                 break;
  112. 112 
  113. 113 
  114. 114         case WM_LBUTTONDOWN :
  115. 115                 wsprintf(szLBDownPos, "WM_LBUTTONDOWN: %3d %3d",
  116. 116                          LOWORD(lParam), HIWORD(lParam));
  117. 117                 InvalidateRect(hWnd, NULL, FALSE);
  118. 118                 break;
  119. 119 
  120. 120         case WM_LBUTTONUP :
  121. 121                 wsprintf(szLBUpPos, "WM_LBUTTONUP: %3d %3d",
  122. 122                          LOWORD(lParam), HIWORD(lParam));
  123. 123                 InvalidateRect(hWnd, NULL, FALSE);
  124. 124                 break;
  125. 125 
  126. 126         case WM_RBUTTONDOWN :
  127. 127                 wsprintf(szRBDownPos, "WM_RBUTTONDOWN: %3d %3d",
  128. 128                          LOWORD(lParam), HIWORD(lParam));
  129. 129                 InvalidateRect(hWnd, NULL, FALSE);
  130. 130                 break;
  131. 131 
  132. 132         case WM_RBUTTONUP :
  133. 133                 wsprintf(szRBUpPos, "WM_RBUTTONUP: %3d %3d",
  134. 134                          LOWORD(lParam), HIWORD(lParam));
  135. 135                 InvalidateRect(hWnd, NULL, FALSE);
  136. 136                 break;
  137. 137 
  138. 138         case WM_PAINT :
  139. 139                 hDC = BeginPaint(hWnd, &ps);
  140. 140 
  141. 141                 hFont = GetStockObject(SYSTEM_FIXED_FONT);
  142. 142                 SelectObject(hDC, hFont);
  143. 143 
  144. 144                 TextOut(hDC, 10, 30, szMovePos,
  145. 145                         strlen(szMovePos));
  146. 146                 TextOut(hDC, 10, 50, szLBDownPos,
  147. 147                         strlen(szLBDownPos));
  148. 148                 TextOut(hDC, 10, 70, szLBUpPos,
  149. 149                         strlen(szLBUpPos));
  150. 150                 TextOut(hDC, 10, 90, szRBDownPos,
  151. 151                         strlen(szRBDownPos));
  152. 152                 TextOut(hDC, 10, 110, szRBUpPos,
  153. 153                         strlen(szRBUpPos));
  154. 154 
  155. 155                 EndPaint(hWnd, &ps);
  156. 156                 break;
  157. 157 
  158. 158       case WM_DESTROY :
  159. 159                 PostQuitMessage(0);
  160. 160                 break ;
  161. 161 
  162. 162       default :
  163. 163                 return (DefWindowProc(hWnd, message, wParam, lParam));
  164. 164     }
  165. 165    return (NULL);
  166. 166 }