《windows程序设计》学习_3.2:左键的使用

 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 (NULL, IDI_APPLICATION);
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 ("Stick 1"),
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;
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;
74 
75         return 0;
76 
77     case WM_LBUTTONDOWN:
78         hdc = GetDC(hwnd);
79 
80         hdcMem = CreateCompatibleDC(hdc);
81         SelectObject(hdcMem, hBitmap);
82 
83         x = LOWORD(lParam);
84         y = HIWORD(lParam);
85 
86         BitBlt(hdc, x, y, cxSource, cySource, hdcMem, 0, 0, SRCCOPY);
87 
88         DeleteDC(hdcMem);
89         ReleaseDC(hwnd, hdc);
90         return 0;
91     case WM_DESTROY:
92         DeleteObject(hBitmap);
93         PostQuitMessage(0);
94         return 0;
95     }
96     return DefWindowProc(hwnd, message, wParam, lParam);
97 }

《windows程序设计》学习_3.2:左键的使用_第1张图片

你可能感兴趣的:(windows)