ReactOS  0.4.13-dev-455-g28ed234
txtscale.cpp
Go to the documentation of this file.
1 
2 // ------------------------------------------------------------------
3 // Windows 2000 Graphics API Black Book
4 // Chapter 8 - Listing 8.1 (Scaled Text Demo)
5 //
6 // Created by Damon Chandler <dmc27@ee.cornell.edu>
7 // Updates can be downloaded at: <www.coriolis.com>
8 //
9 // Please do not hesistate to e-mail me at dmc27@ee.cornell.edu
10 // if you have any questions about this code.
11 // ------------------------------------------------------------------
12 
13 
14 //>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
15 #include <windows.h>
16 #include <commctrl.h>
17 #include <cassert>
18 
19 // for the MakeFont() function...
20 #include "mk_font.h"
21 //<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
22 
23 
25 const char* WndClassName = "GMainWnd";
27  LPARAM LParam);
28 
29 
31  int nCmdShow)
32 {
33  hInst = hInstance;
34 
35  WNDCLASS wc;
36  memset(&wc, 0, sizeof(WNDCLASS));
37 
41  wc.hInstance = hInst;
43  wc.hbrBackground = reinterpret_cast<HBRUSH>(
44  COLOR_BTNFACE + 1
45  );
46 
47  if (RegisterClass(&wc))
48  {
49  HWND hWnd =
51  WndClassName, TEXT("Scaled Text Demo"),
54  CW_USEDEFAULT, CW_USEDEFAULT, 800, 300,
55  NULL, NULL, hInst, NULL
56  );
57 
58  if (hWnd)
59  {
60  ShowWindow(hWnd, nCmdShow);
62 
63  MSG msg;
64  while (GetMessage(&msg, NULL, 0, 0))
65  {
68  }
69  }
70  }
71  return 0;
72 }
73 //-------------------------------------------------------------------------
74 
75 
78 double scale = 0.0;
79 LPCSTR pText = TEXT("The Scaled Text!");
80 
82  LPARAM lParam)
83 {
84  switch (msg)
85  {
86  case WM_CREATE:
87  {
89  icx.dwSize = sizeof(INITCOMMONCONTROLSEX);
90  icx.dwICC = ICC_BAR_CLASSES;
91 
93 
94  hTrackBar =
96  TRACKBAR_CLASS, "",
100  10, 260, 375, 40,
101  hWnd, NULL, hInst, NULL
102  );
103 
104  assert(hTrackBar != NULL);
107 
108  // create the TrueType (scalable) font
109  HDC hDC = GetDC(hWnd);
110  try
111  {
112  // see Chapter 4 for the definition of MakeFont
113  hTTFont = font::MakeFont(hDC, "Impact", 72);
114  if (!hTTFont) throw;
115  }
116  catch (...)
117  {
118  ReleaseDC(hWnd, hDC);
119  }
120  ReleaseDC(hWnd, hDC);
121  break;
122  }
123  case WM_HSCROLL:
124  {
125  if (reinterpret_cast<HWND>(lParam) == hTrackBar)
126  {
127  //
128  // adjust the scaling factor according to
129  // the position of the trackbar's slider
130  //
131  scale = static_cast<double>(
132  (SNDMSG(hTrackBar, TBM_GETPOS, 0, 0) + 1) / 50.0
133  );
134  InvalidateRect(hWnd, NULL, true);
135  }
136  break;
137  }
138  case WM_ERASEBKGND:
139  {
141 
142  HDC hDC = reinterpret_cast<HDC>(wParam);
143  HFONT hOldFont = static_cast<HFONT>(
145  );
146  try
147  {
149 
150  // open a path bracket
151  if (!BeginPath(hDC)) throw;
152 
153  // record the text to the path
154  TextOut(hDC, 10, 10, pText, lstrlen(pText));
155 
156  // close the path bracket and
157  // select the path into hDC
158  EndPath(hDC);
159 
160  // determine the number of endpoints in the path
161  const int num_points = GetPath(hDC, NULL, NULL, 0);
162  if (num_points > 0)
163  {
164  // make room for the POINTs and vertex types
165  POINT* pPEnds = new POINT[num_points];
166  unsigned char* pTypes = new unsigned char[num_points];
167  try
168  {
169  // get the path's description
170  int num_got = GetPath(hDC, pPEnds, pTypes, num_points);
171  if (num_got > 0)
172  {
173  // start a new path bracket
174  if (!BeginPath(hDC)) throw;
175 
176  // scale each point in the description
177  int iPoint;
178  for (iPoint = 0; iPoint < num_got; ++iPoint)
179  {
180  pPEnds[iPoint].x = static_cast<LONG>(
181  scale * pPEnds[iPoint].x + 0.5
182  );
183  pPEnds[iPoint].y = static_cast<LONG>(
184  scale * pPEnds[iPoint].y + 0.5
185  );
186  }
187 
188  for (iPoint = 0; iPoint < num_points; ++iPoint)
189  {
190  // handle the MoveToEx case
191  if (pTypes[iPoint] == PT_MOVETO)
192  {
193  MoveToEx(
194  hDC, pPEnds[iPoint].x, pPEnds[iPoint].y, NULL
195  );
196  }
197  // handle the LineTo case
198  else if (
199  pTypes[iPoint] == PT_LINETO ||
200  pTypes[iPoint] == (PT_LINETO | PT_CLOSEFIGURE)
201  )
202  {
203  LineTo(hDC, pPEnds[iPoint].x, pPEnds[iPoint].y);
204  }
205  // handle the PolyBezierTo case
206  else if (
207  pTypes[iPoint] == PT_BEZIERTO ||
208  pTypes[iPoint] == (PT_BEZIERTO | PT_CLOSEFIGURE)
209  )
210  {
211  PolyBezierTo(hDC, pPEnds + iPoint, 3);
212  iPoint += 2;
213  }
214  }
215 
216  // close the new path bracket
217  EndPath(hDC);
218 
219  // stroke and fill the new path
221  }
222  }
223  catch (...)
224  {
225  // clean up
226  delete [] pTypes;
227  delete [] pPEnds;
228  throw;
229  }
230  // clean up
231  delete [] pTypes;
232  delete [] pPEnds;
233  }
234  // ...
235  }
236  catch (...)
237  {
238  SelectObject(hDC, hOldFont);
239  }
240  SelectObject(hDC, hOldFont);
241  return res;
242  }
243  case WM_SIZE:
244  {
245  MoveWindow(
246  hTrackBar,
247  0, HIWORD(lParam) - 40, LOWORD(lParam), 40,
248  false
249  );
250  break;
251  }
252  case WM_DESTROY:
253  {
254  // clean up
256  PostQuitMessage(0);
257  break;
258  }
259  }
260  return DefWindowProc(hWnd, msg, wParam, lParam);
261 }
262 //-------------------------------------------------------------------------
263 
264 
265 
266 
const char * WndClassName
Definition: txtscale.cpp:25
#define TBS_FIXEDLENGTH
Definition: commctrl.h:1997
#define DispatchMessage
Definition: winuser.h:5631
BOOL WINAPI TranslateMessage(_In_ const MSG *)
#define TRUE
Definition: types.h:120
LPCSTR pText
Definition: txtscale.cpp:79
long y
Definition: polytest.cpp:48
#define TBS_ENABLESELRANGE
Definition: commctrl.h:1996
#define CreateWindow
Definition: winuser.h:5620
long x
Definition: polytest.cpp:48
HDC WINAPI GetDC(_In_opt_ HWND)
#define TRACKBAR_CLASS
Definition: commctrl.h:1985
GLenum GLenum GLenum GLenum GLenum scale
Definition: glext.h:9032
TW_UINT32 TW_UINT16 TW_UINT16 MSG
Definition: twain.h:1827
BOOL WINAPI EndPath(_In_ HDC)
#define TBS_BOTH
Definition: commctrl.h:1994
static HDC
Definition: imagelist.c:92
#define CALLBACK
Definition: compat.h:27
BOOL WINAPI UpdateWindow(_In_ HWND)
HWND hWnd
Definition: settings.c:17
HGDIOBJ WINAPI SelectObject(_In_ HDC, _In_ HGDIOBJ)
Definition: dc.c:1497
#define assert(x)
Definition: debug.h:53
BOOL WINAPI DeleteObject(_In_ HGDIOBJ)
#define CS_HREDRAW
Definition: winuser.h:648
BOOL WINAPI LineTo(_In_ HDC, _In_ int, _In_ int)
GLint GLint GLint GLint GLint x
Definition: gl.h:1548
UINT_PTR WPARAM
Definition: windef.h:207
HBRUSH hbrBackground
Definition: winuser.h:3123
#define WS_CHILD
Definition: pedump.c:617
BOOL WINAPI ShowWindow(_In_ HWND, _In_ int)
#define WS_CLIPCHILDREN
Definition: pedump.c:619
HWND hTrackBar
Definition: txtscale.cpp:76
struct tagINITCOMMONCONTROLSEX INITCOMMONCONTROLSEX
WPARAM wParam
Definition: combotst.c:138
CHAR * LPTSTR
Definition: xmlstorage.h:192
struct @1571 Msg[]
BOOL WINAPI MoveToEx(_In_ HDC, _In_ int, _In_ int, _Out_opt_ LPPOINT)
int WINAPI SetBkMode(_In_ HDC, _In_ int)
Definition: dc.c:1032
HINSTANCE hInstance
Definition: charmap.c:20
BOOL WINAPI PolyBezierTo(_In_ HDC hdc, _In_reads_(cpt) const POINT *apt, _In_ DWORD cpt)
Definition: painting.c:263
#define TBM_SETRANGEMAX
Definition: commctrl.h:2011
#define TBM_GETPOS
Definition: commctrl.h:2003
BOOL WINAPI StrokeAndFillPath(_In_ HDC)
int WINAPI ReleaseDC(_In_opt_ HWND, _In_ HDC)
HCURSOR hCursor
Definition: winuser.h:3122
#define TRANSPARENT
Definition: wingdi.h:949
#define CW_USEDEFAULT
Definition: winuser.h:225
smooth NULL
Definition: ftsmooth.c:416
HFONT MakeFont(IN HDC hDestDC, IN LPCSTR typeface_name, IN int point_size, IN const BYTE charset, IN const DWORD style)
Definition: mk_font.cpp:23
LONG_PTR LPARAM
Definition: windef.h:208
#define TBS_AUTOTICKS
Definition: commctrl.h:1987
HINSTANCE hInst
Definition: txtscale.cpp:24
const char * LPCSTR
Definition: xmlstorage.h:183
HFONT hTTFont
Definition: txtscale.cpp:77
#define DefWindowProc
Definition: ros2win.h:31
#define WM_DESTROY
Definition: winuser.h:1591
#define WS_CAPTION
Definition: pedump.c:624
#define CS_VREDRAW
Definition: winuser.h:653
#define ICC_BAR_CLASSES
Definition: commctrl.h:60
#define WM_SIZE
Definition: winuser.h:1593
BOOL WINAPI InvalidateRect(_In_opt_ HWND, _In_opt_ LPCRECT, _In_ BOOL)
int WINAPI GetPath(_In_ HDC hdc, _Out_writes_opt_(cpt) LPPOINT apt, _Out_writes_opt_(cpt) LPBYTE aj, int cpt)
int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE, LPTSTR, int nCmdShow)
Definition: txtscale.cpp:30
BOOL WINAPI InitCommonControlsEx(const INITCOMMONCONTROLSEX *lpInitCtrls)
Definition: commctrl.c:893
#define COLOR_BTNFACE
Definition: winuser.h:918
LPCSTR lpszClassName
Definition: winuser.h:3125
_In_ UINT iPoint
Definition: ntgdi.h:2197
BOOL WINAPI MoveWindow(_In_ HWND, _In_ int, _In_ int, _In_ int, _In_ int, _In_ BOOL)
#define LoadCursor
Definition: winuser.h:5678
#define TBM_SETTHUMBLENGTH
Definition: commctrl.h:2029
UINT style
Definition: winuser.h:3116
#define SNDMSG
Definition: treelist.h:31
static HDC hDC
Definition: 3dtext.c:33
LRESULT CALLBACK MainWndProc(HWND HWnd, UINT Msg, WPARAM WParam, LPARAM LParam)
Definition: txtscale.cpp:81
#define lstrlen
Definition: winbase.h:3690
#define TEXT(s)
Definition: k32.h:26
HINSTANCE hInstance
Definition: winuser.h:3120
unsigned int UINT
Definition: ndis.h:50
#define WM_HSCROLL
Definition: winuser.h:1719
#define GetMessage
Definition: winuser.h:5656
GLint GLint GLint GLint GLint GLint y
Definition: gl.h:1548
#define TBS_HORZ
Definition: commctrl.h:1989
WNDPROC lpfnWndProc
Definition: winuser.h:3117
static DWORD *static HFONT(WINAPI *pCreateFontIndirectExA)(const ENUMLOGFONTEXDVA *)
#define msg(x)
Definition: auth_time.c:54
#define IDC_ARROW
Definition: winuser.h:682
BOOL WINAPI BeginPath(_In_ HDC hdc)
#define PT_CLOSEFIGURE
Definition: wingdi.h:886
#define WM_CREATE
Definition: winuser.h:1590
GLuint res
Definition: glext.h:9613
#define PT_LINETO
Definition: wingdi.h:884
#define PT_MOVETO
Definition: wingdi.h:883
#define WS_OVERLAPPEDWINDOW
Definition: pedump.c:637
#define HIWORD(l)
Definition: typedefs.h:246
#define TextOut
Definition: wingdi.h:4461
#define RegisterClass
Definition: winuser.h:5702
#define WM_ERASEBKGND
Definition: winuser.h:1607
#define PT_BEZIERTO
Definition: wingdi.h:885
LONG_PTR LRESULT
Definition: windef.h:209
#define WS_VISIBLE
Definition: pedump.c:620
#define memset(x, y, z)
Definition: compat.h:39
LPARAM lParam
Definition: combotst.c:139
#define LOWORD(l)
Definition: pedump.c:82
__analysis_noreturn void WINAPI PostQuitMessage(_In_ int)
#define APIENTRY
Definition: api.h:79