《windows程序设计》学习_3.3:利用xp扫雷资源

  1 #include<windows.h>
  2 #include "resource.h"
  3 
  4 LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM);
  5 
  6 int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstace, PSTR szCmdLine, int iCmdShow)
  7 {
  8     static TCHAR szAppName[] = TEXT ("stick1");
  9     HWND        hwnd;
 10     MSG         msg;
 11     WNDCLASS    wndclass;
 12 
 13     wndclass.style                  = CS_HREDRAW | CS_VREDRAW ;
 14     wndclass.lpfnWndProc            = WndProc;
 15     wndclass.cbClsExtra             = 0;
 16     wndclass.cbWndExtra             = 0;
 17     wndclass.hInstance              = hInstance;
 18     wndclass.hIcon                  = LoadIcon (hInstance, MAKEINTRESOURCE(IDI_ICON1));
 19     wndclass.hCursor                = LoadCursor(NULL, IDC_ARROW);
 20     wndclass.hbrBackground          = (HBRUSH)GetStockObject(WHITE_BRUSH);
 21     wndclass.lpszMenuName           = NULL;
 22     wndclass.lpszClassName          = szAppName;
 23 
 24     if(!RegisterClass(&wndclass))
 25     {
 26         MessageBox(NULL, TEXT ("The Program requires Windows NT!"), szAppName, MB_ICONERROR);
 27         return 0;
 28     }
 29 
 30     hwnd = CreateWindow (szAppName,
 31                         TEXT ("ɨÀ×"),
 32                         WS_OVERLAPPEDWINDOW,
 33                         CW_USEDEFAULT,
 34                         CW_USEDEFAULT,
 35                         CW_USEDEFAULT,
 36                         CW_USEDEFAULT,
 37                         NULL,
 38                         NULL,
 39                         hInstance,
 40                         NULL);
 41 
 42     ShowWindow(hwnd , iCmdShow);
 43     UpdateWindow(hwnd);
 44 
 45     while(GetMessage(&msg, NULL, 0, 0))
 46     {
 47         TranslateMessage(&msg);
 48         DispatchMessage(&msg);
 49     }
 50     return msg.wParam;
 51 }
 52 
 53 LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
 54 {
 55     static HBITMAP hBitmap;
 56     static int     cxClient, cyClient, cxSource, cySource;
 57     BITMAP         bitmap;
 58     HDC            hdc,hdcMem;
 59     HINSTANCE      hInstance;
 60     int            x,y,mx,my;
 61     PAINTSTRUCT    ps;
 62 
 63 
 64     switch(message)
 65     {
 66     case WM_CREATE:
 67         hInstance = ((LPCREATESTRUCT) lParam) ->hInstance;
 68 
 69         hBitmap = LoadBitmap(hInstance,MAKEINTRESOURCE(IDM_BITMAP)/*TEXT("bitmap1")*/);
 70         GetObject(hBitmap,sizeof(BITMAP),&bitmap);
 71 
 72         cxSource = bitmap.bmWidth;
 73         cySource = bitmap.bmHeight/16;
 74 
 75         return 0;
 76     case WM_SIZE:
 77         cxClient = LOWORD (lParam);
 78         cyClient = HIWORD (lParam);
 79         return 0;
 80     case WM_PAINT:
 81         hdc = BeginPaint(hwnd , &ps);
 82 
 83         hdcMem = CreateCompatibleDC(hdc);
 84         SelectObject(hdcMem,hBitmap);
 85 
 86         for(y = 100; y < cyClient - 100 ; y += cySource)
 87         {
 88             for(x = 100; x < cxClient - 100; x += cxSource)
 89             {
 90                 BitBlt(hdc,x,y,cxSource,cySource,hdcMem,0,0,SRCCOPY);
 91             }
 92         }
 93         DeleteDC(hdcMem);
 94         EndPaint(hwnd, &ps);
 95         return 0;
 96     case WM_LBUTTONDOWN:
 97         hdc = GetDC(hwnd);
 98 
 99         mx = LOWORD(lParam);
100         my = HIWORD(lParam);
101 
102         hdcMem = CreateCompatibleDC(hdc);
103         SelectObject(hdcMem, hBitmap);
104 
105         BitBlt(hdc, mx, my, cxSource, cySource, hdcMem, 0, cySource, SRCCOPY);
106 
107         DeleteDC(hdcMem);
108         ReleaseDC(hwnd, hdc);
109         return 0;
110     case WM_DESTROY:
111         DeleteObject(hBitmap);
112         PostQuitMessage(0);
113         return 0;
114     }
115     return DefWindowProc(hwnd, message, wParam, lParam);
116 }

《windows程序设计》学习_3.3:利用xp扫雷资源_第1张图片

你可能感兴趣的:(windows)