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