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