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