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

GDI/图象编程

开发平台:

Visual C++

  1.   1 /****************************************************************/
  2.   2 /*         Demo5_2   ---  Bitmap Demo                           */
  3.   3 /****************************************************************/
  4.   4 
  5.   5 #include <windows.h>
  6.   6 #include "Demo5_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 void DrawBitmap(HDC, POINT);
  12.  12 
  13.  13 
  14.  14 HANDLE  hInst;
  15.  15 int     TypeID = -1;
  16.  16 
  17.  17 /****************************************************************/
  18.  18 /*                      WinMain()                               */
  19.  19 /****************************************************************/
  20.  20 
  21.  21 int PASCAL WinMain(HANDLE hInstance, HANDLE hPrevInstance,
  22.  22                    LPSTR lpszCmdLine, int nCmdShow)
  23.  23 {
  24.  24    WNDCLASS wclass;
  25.  25    MSG      msg;
  26.  26    HWND     hWnd;
  27.  27    char     szName[] = "Demo5_2";
  28.  28 
  29.  29    if (!hPrevInstance)
  30.  30     {
  31.  31         wclass.style         = CS_HREDRAW | CS_VREDRAW;
  32.  32         wclass.lpfnWndProc   = MainWndProc;
  33.  33         wclass.cbClsExtra    = 0;
  34.  34         wclass.cbWndExtra    = 0;
  35.  35         wclass.hInstance     = hInstance;
  36.  36         wclass.hIcon         = LoadIcon(NULL, IDI_APPLICATION);
  37.  37         wclass.hCursor       = LoadCursor(NULL, IDC_ARROW);
  38.  38         wclass.hbrBackground = GetStockObject(WHITE_BRUSH);
  39.  39         wclass.lpszMenuName  = szName;
  40.  40         wclass.lpszClassName = szName;
  41.  41 
  42.  42         if (!RegisterClass (&wclass))
  43.  43            return (FALSE);
  44.  44     }
  45.  45 
  46.  46     hWnd = CreateWindow(
  47.  47                 szName,
  48.  48                 "Bitmap Demo" ,
  49.  49                 WS_OVERLAPPEDWINDOW,
  50.  50                 CW_USEDEFAULT,
  51.  51                 CW_USEDEFAULT,
  52.  52                 CW_USEDEFAULT,
  53.  53                 CW_USEDEFAULT,
  54.  54                 NULL,
  55.  55                 NULL,
  56.  56                 hInstance,
  57.  57                 NULL );
  58.  58 
  59.  59     if (!hWnd)
  60.  60         return (FALSE);
  61.  61 
  62.  62     hInst = hInstance;
  63.  63 
  64.  64     ShowWindow(hWnd, nCmdShow);
  65.  65     UpdateWindow(hWnd);
  66.  66 
  67.  67     while (GetMessage(&msg, NULL, NULL,NULL))
  68.  68        {
  69.  69            TranslateMessage(&msg);
  70.  70            DispatchMessage(&msg);
  71.  71        }
  72.  72     return (msg.wParam);
  73.  73 }
  74.  74 
  75.  75 
  76.  76 
  77.  77 /****************************************************************/
  78.  78 /*                      MainWndProc()                           */
  79.  79 /****************************************************************/
  80.  80 
  81.  81 long FAR PASCAL MainWndProc(HWND hWnd, unsigned message,
  82.  82                             WORD wParam, LONG lParam)
  83.  83 {
  84.  84    HMENU        hMenu;
  85.  85    HDC          hDC;
  86.  86    PAINTSTRUCT  ps;
  87.  87    POINT        ptSize;
  88.  88 
  89.  89    switch (message)
  90.  90     {
  91.  91       case WM_COMMAND :
  92.  92                 switch (wParam)
  93.  93                   {
  94.  94                     case IDM_FILE :
  95.  95                     case IDM_ARR :
  96.  96 
  97.  97                          hMenu = GetMenu(hWnd);
  98.  98                          if (TypeID == wParam)
  99.  99                             break;
  100. 100 
  101. 101                          CheckMenuItem(hMenu, TypeID,
  102. 102                                         MF_UNCHECKED);
  103. 103                          TypeID = wParam;
  104. 104                          CheckMenuItem(hMenu, TypeID,
  105. 105                                         MF_CHECKED);
  106. 106 
  107. 107                          InvalidateRect(hWnd, NULL, TRUE);
  108. 108                          break;
  109. 109                   }
  110. 110                 return (0);
  111. 111 
  112. 112       case WM_SIZE :
  113. 113                 ptSize = MAKEPOINT(lParam);
  114. 114                 return (0);
  115. 115 
  116. 116       case WM_PAINT :
  117. 117                 hDC = BeginPaint(hWnd, &ps);
  118. 118 
  119. 119                 DrawBitmap(hDC, ptSize);
  120. 120 
  121. 121                 EndPaint(hWnd, &ps);
  122. 122                 return (0);
  123. 123 
  124. 124       case WM_DESTROY :
  125. 125                 PostQuitMessage(0);
  126. 126                 return (0);
  127. 127     }
  128. 128 
  129. 129   return(DefWindowProc(hWnd, message, wParam, lParam));
  130. 130 }
  131. 131 
  132. 132 
  133. 133 void DrawBitmap(HDC hDC, POINT ptSize)
  134. 134 {
  135. 135   HBITMAP  hBitmap;
  136. 136   HBRUSH   hBrush;
  137. 137   BITMAP   Bmp;
  138. 138   HDC      hMemDC;
  139. 139   WORD     Pat[] = {
  140. 140                      0xF8, 0x74, 0x22, 0x47,
  141. 141                      0x8F, 0x17, 0x22, 0x71
  142. 142                    };
  143. 143 
  144. 144   switch (TypeID)
  145. 145    {
  146. 146      case IDM_FILE :
  147. 147                 hBitmap = LoadBitmap(hInst, "Paper");
  148. 148                 hMemDC = CreateCompatibleDC(hDC);
  149. 149 
  150. 150                 SelectObject(hMemDC, hBitmap);
  151. 151                 GetObject(hBitmap, sizeof(BITMAP),
  152. 152                           (LPSTR) &Bmp);
  153. 153                 BitBlt(hDC, 0, 0,
  154. 154                        Bmp.bmWidth, Bmp.bmHeight,
  155. 155                        hMemDC, 0, 0,
  156. 156                        SRCCOPY);
  157. 157 
  158. 158                 DeleteDC(hMemDC);
  159. 159                 DeleteObject(hBitmap);
  160. 160                 break;
  161. 161 
  162. 162      case IDM_ARR :
  163. 163                 hBitmap = CreateBitmap(8, 8, 1, 1,
  164. 164                                        (LPSTR) Pat);
  165. 165                 hBrush = CreatePatternBrush(hBitmap);
  166. 166 
  167. 167                 hBrush = SelectObject(hDC, hBrush);
  168. 168                 PatBlt(hDC, 0, 0, ptSize.x, ptSize.y,
  169. 169                        PATCOPY);
  170. 170                 hBrush = SelectObject(hDC, hBrush);
  171. 171 
  172. 172                 DeleteObject(hBrush);
  173. 173                 DeleteObject(hBitmap);
  174. 174                 break;
  175. 175    }
  176. 176 }