1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236
|
// TextWriter_Demo.cpp
#define WIN32_LEAN_AND_MEAN
#define _CRT_SECURE_NO_WARNINGS
#include <windows.h>
#include <commdlg.h>
#include <tchar.h>
#include "resource.h"
TCHAR szTitle [] = _T("TextWriter Demo"); // The title bar text
TCHAR szWindowClass[] = _T("TextWriter_Demo_Class"); // the main window class name
// Forward declarations of functions included in this code module:
ATOM MyRegisterClass(HINSTANCE hInstance);
BOOL InitInstance(HINSTANCE, int);
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
void OnTextColor(HWND hWnd);
const COLORREF colorRed = RGB(255, 0, 0);
class TextWriter
{
private:
HFONT hfont;
COLORREF textColor;
public:
TextWriter() : hfont(NULL), textColor(colorRed)
{
}
~TextWriter()
{
Finalize();
}
void Initialize()
{
Finalize();
// initialize font here (I prefer CreateFontIndirect to CreateFont,
// as it's easier to deal with unused params using memset.)
LOGFONT logFont;
memset(&logFont, 0, sizeof(logFont));
logFont.lfHeight = -48; // see PS
logFont.lfWeight = FW_BOLD;
_tcscpy(logFont.lfFaceName, _T("Broadway"));
hfont = CreateFontIndirect(&logFont);
}
void Finalize()
{
if (hfont != NULL)
{
DeleteObject(hfont);
hfont = NULL;
}
}
COLORREF GetTextColor() const
{
return textColor;
}
void SetTextColor(COLORREF cref)
{
if(cref != textColor)
{
textColor = cref;
Initialize(); // Initialize will Finalize first
}
}
void WriteText(HDC _hdc, int _Xpos, int _Ypos, const TCHAR* _szMessage)
{
// set text color and font
COLORREF oldTextColor = ::SetTextColor(_hdc, textColor);
HFONT oldHFont = (HFONT)::SelectObject(_hdc, hfont);
::TextOut(_hdc, _Xpos, _Ypos, _szMessage, _tcslen(_szMessage));
// restore text color and font
::SetTextColor(_hdc, oldTextColor);
::SelectObject(_hdc, oldHFont);
}
};
TextWriter g_tw;
int APIENTRY
_tWinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPTSTR lpCmdLine,
int nCmdShow)
{
UNREFERENCED_PARAMETER(hPrevInstance);
UNREFERENCED_PARAMETER(lpCmdLine);
MyRegisterClass(hInstance);
if(!InitInstance(hInstance, nCmdShow))
{
return FALSE;
}
MSG msg = {0};
while(GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return (int)msg.wParam;
}
ATOM MyRegisterClass(HINSTANCE hInstance)
{
WNDCLASSEX wcex;
wcex.cbSize = sizeof(WNDCLASSEX);
wcex.style = CS_HREDRAW | CS_VREDRAW;
wcex.lpfnWndProc = WndProc;
wcex.cbClsExtra = 0;
wcex.cbWndExtra = 0;
wcex.hInstance = hInstance;
wcex.hIcon = NULL; // no icon provided
wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
wcex.lpszMenuName = MAKEINTRESOURCE(IDR_MAIN);
wcex.lpszClassName = szWindowClass;
wcex.hIconSm = NULL; // no small icon provided
return RegisterClassEx(&wcex);
}
BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
HWND hWnd = CreateWindow(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInstance, NULL);
if(!hWnd)
{
return FALSE;
}
ShowWindow(hWnd, nCmdShow);
UpdateWindow(hWnd);
return TRUE;
}
LRESULT CALLBACK WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
switch(uMsg)
{
case WM_CREATE:
{
g_tw.Initialize();
return 0; // return 0 to indicate success
}
// no need for break
case WM_DESTROY:
{
g_tw.Finalize();
PostQuitMessage(0);
}
break;
case WM_COMMAND:
{
int wmId = LOWORD(wParam);
int wmEvent = HIWORD(wParam);
switch (wmId)
{
case IDM_EXIT:
DestroyWindow(hWnd);
break;
case IDM_TEXT_COLOR:
OnTextColor(hWnd);
break;
default:
return DefWindowProc(hWnd, uMsg, wParam, lParam);
}
}
break;
case WM_PAINT:
{
PAINTSTRUCT ps = {0};
HDC hdc = BeginPaint(hWnd, &ps);
g_tw.WriteText(hdc, 50, 100, _T("Hello, cplusplus.com"));
EndPaint(hWnd, &ps);
}
break;
default:
return DefWindowProc(hWnd, uMsg, wParam, lParam);
}
return 0;
}
// use standard color dialog for demo
void OnTextColor(HWND hWnd)
{
COLORREF cref = g_tw.GetTextColor();
static COLORREF acrCustClr[16]; // array of custom colors
CHOOSECOLOR cc = {0}; // initilize all members to zero
cc.lStructSize = sizeof(cc);
cc.hwndOwner = hWnd;
cc.lpCustColors = acrCustClr; // cannot be NULL
cc.Flags = CC_RGBINIT;
cc.rgbResult = cref; // pre-select current color
BOOL ret = ChooseColor(&cc);
// note that it's up to the app to persist the custom colors
// for the chooser to redisplay next time. Not done here!
if(ret)
{
if(cc.rgbResult != cref)
{
g_tw.SetTextColor(cc.rgbResult);
InvalidateRect(hWnd, NULL, TRUE);
}
}
}
|