-
Notifications
You must be signed in to change notification settings - Fork 3.3k
Expand file tree
/
Copy pathSampleRichEditControl.cpp
More file actions
716 lines (642 loc) · 21.4 KB
/
SampleRichEditControl.cpp
File metadata and controls
716 lines (642 loc) · 21.4 KB
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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
//// THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF
//// ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO
//// THE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
//// PARTICULAR PURPOSE.
////
//// Copyright (c) Microsoft Corporation. All rights reserved
#include "SampleRichEditControl.h"
CSampleRichEditWindow::CSampleRichEditWindow() :
// Initialize scale factor to 100%
m_numerator(100),
m_denominator(100),
// Initialize for loading rich edit module
m_hmodRichEdit(nullptr),
m_hWnd(nullptr)
{
}
CSampleRichEditWindow::~CSampleRichEditWindow()
{
if (m_hWnd)
{
DestroyWindow(m_hWnd);
m_hWnd = nullptr;
}
if (m_hmodRichEdit)
{
FreeLibrary(m_hmodRichEdit);
m_hmodRichEdit = nullptr;
}
}
// <summary>
// This method creates and initializes a child window for the Rich Edit control.
// Notes:
// The rich edit window will be a child window to the hWnd passed in.
// The window will be positioned using default child window values,
// and rely on the caller to [re-]position the window as needed.
// </summary>
HRESULT
CSampleRichEditWindow::Initialize(
_In_ HWND parentHWD
)
{
HRESULT hr = S_OK;
BOOL fResult = FALSE;
// Load MSFTEDIT.dll. This library is required for Rich Edit v4.1 or higher.
m_hmodRichEdit = LoadLibrary(L"msftedit.dll");
if (!m_hmodRichEdit)
{
hr = HRESULT_FROM_WIN32(GetLastError());
}
// Create the rich edit window.
if (SUCCEEDED(hr))
{
m_hWnd = CreateWindowEx(WS_EX_LAYERED, // Make this a layered window.
MSFTEDIT_CLASS, // Associate with the rich edit class.
// Default text to use in window.
L"Enter text here.",
// Define as a visible child window.
// Also set flags to provide a multi-line rich edit experience.
WS_VISIBLE | //WS_CHILD |
WS_CLIPCHILDREN | WS_CLIPSIBLINGS |
WS_VSCROLL | ES_WANTRETURN | ES_MULTILINE,
// Use default x, y, width and height as these will be set later.
CW_USEDEFAULT, CW_USEDEFAULT,
CW_USEDEFAULT, CW_USEDEFAULT,
parentHWD, // Set parent as requested.
NULL, // Do not attach to a menu.
GetModuleHandleW(NULL), // Associate to the calling application.
NULL // No additional structure needed to create this window.
);
}
if (!m_hWnd)
{
hr = HRESULT_FROM_WIN32(GetLastError());
}
// Set the rich edit control to be semi-transparent.
if (SUCCEEDED(hr))
{
fResult = SetLayeredWindowAttributes(m_hWnd, 0, 255, LWA_ALPHA);
if (!fResult)
{
hr = HRESULT_FROM_WIN32(GetLastError());
}
}
// Set this window to be on top of its siblings.
if (SUCCEEDED(hr))
{
fResult = SetWindowPos(m_hWnd,
HWND_TOP,
CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
SWP_NOMOVE | SWP_NOSIZE | SWP_NOACTIVATE
);
if (!fResult)
{
hr = HRESULT_FROM_WIN32(GetLastError());
}
}
// Populate the rich edit control.
if (SUCCEEDED(hr))
{
hr = ResetDefaultContent();
}
// Cleanup code.
if (FAILED(hr))
{
if (m_hWnd)
{
fResult = DestroyWindow(m_hWnd);
m_hWnd = nullptr;
}
if (m_hmodRichEdit)
{
fResult = FreeLibrary(m_hmodRichEdit);
m_hmodRichEdit = nullptr;
}
}
return hr;
}
// <summary>
// Position the rich edit control based on coordinates provided.
// </summary>
HRESULT
CSampleRichEditWindow::Position(
_In_ UINT x,
_In_ UINT y,
_In_ UINT Width,
_In_ UINT Height
)
{
HRESULT hr = S_OK;
BOOL fResult;
// Move the Rich Edit window to its location.
fResult = MoveWindow(m_hWnd, // Move myself
x, // New position of the left side of the window
y, // New position of the top side of the window
Width, // New width
Height, // New height
TRUE); // Repaint the window
if (!fResult)
{
hr = HRESULT_FROM_WIN32(GetLastError());
}
return hr;
}
// <summary>
// Position the rich edit control relative to the dpi and RECT provided.
// </summary>
HRESULT
CSampleRichEditWindow::PositionRelativeToRect(
_In_ RECT relativeRect
)
{
// Store target dimenions.
UINT childX; // Position of the left side of the window.
UINT childY; // Position of the top side of the window.
UINT childcX; // Width of the window.
UINT childcY; // Height of the window.
// Define offsets relative to the RECT provided.
UINT parentHeight = 0;
UINT offsetTop = 5;
UINT offsetBottom = 5;
UINT offsetRight = 10;
UINT offsetLeft = 5;
// Set the bounds for the child window based on offsets from RECT.
parentHeight = relativeRect.bottom - relativeRect.top;
childX = relativeRect.left + offsetLeft;
childY = relativeRect.top + offsetTop;
childcX = relativeRect.right - offsetRight;
childcY = parentHeight - offsetTop - offsetBottom;
return Position(childX, childY, childcX, childcY);
}
// <summary>
// Respond to an explicit DPI change, including updates to the zoom factor as needed.
// As the rich edit control uses a numerator/denominator pair that must not exceed 64,
// this class will provide numerator/denominator that will generate an implied fraction
// between 0 and 1.
// </summary>
HRESULT
CSampleRichEditWindow::OnDPIChanged(
_In_ float dpi
)
{
HRESULT hr = S_OK;
// Only take action if DPI is different from what control has stored.
if (m_currentDpi != dpi)
{
m_numerator = (UINT) ((dpi / default_dpi) * 100.0F);
hr = ApplyZoomFactor();
}
if (SUCCEEDED(hr))
{
// Update current DPI.
m_currentDpi = dpi;
}
return hr;
}
void
CSampleRichEditWindow::SetDefaultDPI(float dpi)
{
m_currentDpi = dpi;
default_dpi = dpi;
}
HRESULT
CSampleRichEditWindow::IncrementFontSize()
{
return UpdateFontSize(CD_INCREMENT);
}
HRESULT
CSampleRichEditWindow::DecrementFontSize()
{
return UpdateFontSize(CD_DECREMENT);
}
// <summary>
// Enables caller to toggle the bold setting of the text in the control.
// In order to format the text in the control, we need to construct
// the parameters required to deliver the EM_SETCHARFORMAT message.
// </summary>
HRESULT
CSampleRichEditWindow::SetFormatBold()
{
HRESULT hr = S_OK;
LRESULT lr;
CHARFORMAT2 charFormat;
ZeroMemory(&charFormat, sizeof(charFormat));
charFormat.cbSize = sizeof(charFormat);
lr = SendMessage(m_hWnd, EM_GETCHARFORMAT, SCF_SELECTION, (LPARAM) &charFormat);
if (((charFormat.dwMask & CFM_BOLD) == CFM_BOLD) && ((charFormat.dwEffects & CFE_BOLD) == CFE_BOLD))
{
ZeroMemory(&charFormat, sizeof(charFormat));
charFormat.cbSize = sizeof(charFormat);
charFormat.dwMask = CFM_BOLD;
lr = SendMessage(m_hWnd, EM_SETCHARFORMAT, SCF_SELECTION, (LPARAM) &charFormat);
}
else
{
ZeroMemory(&charFormat, sizeof(charFormat));
charFormat.cbSize = sizeof(charFormat);
charFormat.dwMask = CFM_BOLD;
charFormat.dwEffects |= CFE_BOLD;
lr = SendMessage(m_hWnd, EM_SETCHARFORMAT, SCF_SELECTION, (LPARAM) &charFormat);
}
hr = HRESULT_FROM_WIN32(GetLastError());
return hr;
}
// <summary>
// Enables caller to toggle the italic setting of the text in the control.
// In order to format the text in the control, we need to construct
// the parameters required to deliver the EM_SETCHARFORMAT message.
// </summary>
HRESULT
CSampleRichEditWindow::SetFormatItalic()
{
HRESULT hr = S_OK;
LRESULT lr;
CHARFORMAT2 charFormat;
ZeroMemory(&charFormat, sizeof(charFormat));
charFormat.cbSize = sizeof(charFormat);
lr = SendMessage(m_hWnd, EM_GETCHARFORMAT, SCF_SELECTION, (LPARAM) &charFormat);
if (((charFormat.dwMask & CFM_ITALIC) == CFM_ITALIC) && ((charFormat.dwEffects & CFE_ITALIC) == CFE_ITALIC))
{
ZeroMemory(&charFormat, sizeof(charFormat));
charFormat.cbSize = sizeof(charFormat);
charFormat.dwMask = CFM_ITALIC;
lr = SendMessage(m_hWnd, EM_SETCHARFORMAT, SCF_SELECTION, (LPARAM) &charFormat);
}
else
{
ZeroMemory(&charFormat, sizeof(charFormat));
charFormat.cbSize = sizeof(charFormat);
charFormat.dwMask = CFM_ITALIC;
charFormat.dwEffects |= CFE_ITALIC;
lr = SendMessage(m_hWnd, EM_SETCHARFORMAT, SCF_SELECTION, (LPARAM) &charFormat);
}
hr = HRESULT_FROM_WIN32(GetLastError());
return hr;
}
// <summary>
// Enables caller to toggle the underline setting of the text in the control.
// In order to format the text in the control, we need to construct
// the parameters required to deliver the EM_SETCHARFORMAT message.
// </summary>
HRESULT
CSampleRichEditWindow::SetFormatUnderline()
{
HRESULT hr = S_OK;
LRESULT lr;
CHARFORMAT2 charFormat;
ZeroMemory(&charFormat, sizeof(charFormat));
charFormat.cbSize = sizeof(charFormat);
lr = SendMessage(m_hWnd, EM_GETCHARFORMAT, SCF_SELECTION, (LPARAM) &charFormat);
if (((charFormat.dwMask & CFM_UNDERLINE) == CFM_UNDERLINE) && ((charFormat.dwEffects & CFE_UNDERLINE) == CFE_UNDERLINE))
{
ZeroMemory(&charFormat, sizeof(charFormat));
charFormat.cbSize = sizeof(charFormat);
charFormat.dwMask = CFM_UNDERLINE;
lr = SendMessage(m_hWnd, EM_SETCHARFORMAT, SCF_SELECTION, (LPARAM) &charFormat);
}
else
{
ZeroMemory(&charFormat, sizeof(charFormat));
charFormat.cbSize = sizeof(charFormat);
charFormat.dwMask = CFM_UNDERLINE;
charFormat.dwEffects |= CFE_UNDERLINE;
lr = SendMessage(m_hWnd, EM_SETCHARFORMAT, SCF_SELECTION, (LPARAM) &charFormat);
}
hr = HRESULT_FROM_WIN32(GetLastError());
return hr;
}
// <summary>
// Enables caller to set the color of the selected text in the control.
// In order to format the text in the control, we need to construct
// the parameters required to deliver the EM_SETCHARFORMAT message.
// </summary>
HRESULT
CSampleRichEditWindow::SetFormatColor()
{
HRESULT hr = S_OK;
LRESULT lr;
CHARFORMAT2 charFormat;
ZeroMemory(&charFormat, sizeof(charFormat));
charFormat.cbSize = sizeof(charFormat);
lr = SendMessage(m_hWnd, EM_GETCHARFORMAT, SCF_SELECTION, (LPARAM) &charFormat);
if (((charFormat.dwMask & CFM_COLOR) == CFM_COLOR) && (((charFormat.dwEffects & CFE_AUTOCOLOR) != CFE_AUTOCOLOR) && (charFormat.crTextColor==RGB(255,0,0))))
{
ZeroMemory(&charFormat, sizeof(charFormat));
charFormat.cbSize = sizeof(charFormat);
charFormat.dwMask = CFM_COLOR;
lr = SendMessage(m_hWnd, EM_SETCHARFORMAT, SCF_SELECTION, (LPARAM) &charFormat);
}
else
{
ZeroMemory(&charFormat, sizeof(charFormat));
charFormat.cbSize = sizeof(charFormat);
charFormat.dwMask = CFM_COLOR;
charFormat.crTextColor = RGB(255, 0, 0);
lr = SendMessage(m_hWnd, EM_SETCHARFORMAT, SCF_SELECTION, (LPARAM) &charFormat);
}
hr = HRESULT_FROM_WIN32(GetLastError());
return hr;
}
// <summary>
// Enables caller to set the background color of the selected text in the control.
// In order to format the text in the control, we need to construct
// the parameters required to deliver the EM_SETCHARFORMAT message.
// </summary>
HRESULT
CSampleRichEditWindow::SetFormatBackgroundColor()
{
HRESULT hr = S_OK;
LRESULT lr;
CHARFORMAT2 charFormat;
ZeroMemory(&charFormat, sizeof(charFormat));
charFormat.cbSize = sizeof(charFormat);
lr = SendMessage(m_hWnd, EM_GETCHARFORMAT, SCF_SELECTION, (LPARAM) &charFormat);
if (((charFormat.dwMask & CFM_BACKCOLOR) == CFM_BACKCOLOR) && (((charFormat.dwEffects & CFE_AUTOCOLOR) != CFE_AUTOCOLOR) && (charFormat.crBackColor == RGB(255, 255, 0))))
{
ZeroMemory(&charFormat, sizeof(charFormat));
charFormat.cbSize = sizeof(charFormat);
charFormat.dwMask = CFM_BACKCOLOR;
charFormat.crBackColor = RGB(255, 255, 255);
lr = SendMessage(m_hWnd, EM_SETCHARFORMAT, SCF_SELECTION, (LPARAM) &charFormat);
}
else
{
ZeroMemory(&charFormat, sizeof(charFormat));
charFormat.cbSize = sizeof(charFormat);
charFormat.dwMask = CFM_BACKCOLOR;
charFormat.crBackColor = RGB(255, 255, 0);
lr = SendMessage(m_hWnd, EM_SETCHARFORMAT, SCF_SELECTION, (LPARAM) &charFormat);
}
hr = HRESULT_FROM_WIN32(GetLastError());
return hr;
}
// <summary>
// Enables caller to set the alignment of the selected paragraph.
// In order to format the text in the control, we need to construct
// the parameters required to deliver the EM_SETCHARFORMAT message.
// </summary>
HRESULT
CSampleRichEditWindow::SetAlignment(
_In_ WORD wAlignment
)
{
HRESULT hr = S_OK;
LRESULT lr;
PARAFORMAT2 paraFormat;
ZeroMemory(¶Format, sizeof(paraFormat));
paraFormat.cbSize = sizeof(paraFormat);
paraFormat.dwMask = PFM_ALIGNMENT;
paraFormat.wAlignment = wAlignment;
lr = SendMessage(m_hWnd, EM_SETPARAFORMAT, 0, (LPARAM) ¶Format);
hr = HRESULT_FROM_WIN32(GetLastError());
return hr;
}
// <summary>
// Enables caller to set the selected paragraph(s) as bulleted.
// In order to format the text in the control, we need to construct
// the parameters required to deliver the EM_SETPARAFORMAT message.
// </summary>
HRESULT
CSampleRichEditWindow::SetBulleted()
{
HRESULT hr = S_OK;
LRESULT lr;
PARAFORMAT2 paraFormat;
ZeroMemory(¶Format, sizeof(paraFormat));
paraFormat.cbSize = sizeof(paraFormat);
lr = SendMessage(m_hWnd, EM_GETPARAFORMAT, 0, (LPARAM) ¶Format);
if (((paraFormat.dwMask & PFM_NUMBERING) == PFM_NUMBERING) && (paraFormat.wNumbering == PFN_BULLET))
{
paraFormat.wNumbering = 0;
}
else
{
paraFormat.wNumbering = PFN_BULLET;
}
paraFormat.dwMask = PFM_NUMBERING;
lr = SendMessage(m_hWnd, EM_SETPARAFORMAT, 0, (LPARAM) ¶Format);
hr = HRESULT_FROM_WIN32(GetLastError());
return hr;
}
// <summary>
// Enables caller to set the selected paragraph(s) as numbered.
// In order to format the text in the control, we need to construct
// the parameters required to deliver the EM_SETPARAFORMAT message.
// </summary>
HRESULT
CSampleRichEditWindow::SetNumbered()
{
HRESULT hr = S_OK;
LRESULT lr;
PARAFORMAT2 paraFormat;
ZeroMemory(¶Format, sizeof(paraFormat));
paraFormat.cbSize = sizeof(paraFormat);
lr = SendMessage(m_hWnd, EM_GETPARAFORMAT, 0, (LPARAM) ¶Format);
if (((paraFormat.dwMask & PFM_NUMBERING) == PFM_NUMBERING) && (paraFormat.wNumbering == 7))
{
paraFormat.wNumbering = 0;
}
else
{
paraFormat.wNumbering = 7;
}
paraFormat.dwMask = PFM_NUMBERING;
lr = SendMessage(m_hWnd, EM_SETPARAFORMAT, 0, (LPARAM) ¶Format);
hr = HRESULT_FROM_WIN32(GetLastError());
return hr;
}
// <summary>
// Enables caller to set the alignment of the selected paragraph.
// In order to format the text in the control, we need to construct
// the parameters required to deliver the EM_SETCHARFORMAT message.
// </summary>
HRESULT
CSampleRichEditWindow::IncrementIndent(
_In_ INT lIncrement
)
{
HRESULT hr = S_OK;
LRESULT lr;
PARAFORMAT2 paraFormat;
ZeroMemory(¶Format, sizeof(paraFormat));
paraFormat.cbSize = sizeof(paraFormat);
lr = SendMessage(m_hWnd, EM_GETPARAFORMAT, 0, (LPARAM) ¶Format);
paraFormat.dwMask = PFM_STARTINDENT;
paraFormat.dxStartIndent = (paraFormat.dxStartIndent + lIncrement < 0) ? 0 : paraFormat.dxStartIndent + lIncrement;
lr = SendMessage(m_hWnd, EM_SETPARAFORMAT, 0, (LPARAM) ¶Format);
hr = HRESULT_FROM_WIN32(GetLastError());
return hr;
}
// <summary>
// This method restores the contents of the rich edit control to defaults.
// Default configuration includes text content, as well as formatting.
// </summary>
HRESULT
CSampleRichEditWindow::ResetDefaultContent()
{
HRESULT hr = S_OK;
PCWSTR richEditText = NULL;
// Set text to display in rich edit control from resource file.
if (SUCCEEDED(hr))
{
hr = GetResourceString(IDS_LOREM_IPSUM, &richEditText);
}
if (SUCCEEDED(hr))
{
hr = SetText(richEditText);
}
// Initialize the default format for the rich edit control.
if (SUCCEEDED(hr))
{
hr = SetDefaultFormat();
}
// Make sure control is formatted correctly for current DPI.
if (SUCCEEDED(hr))
{
hr = ApplyZoomFactor();
}
return hr;
}
// <summary>
// This method retrieves a string based on the requested resource ID.
// Once the string is converted from resource ID to string, this method calls
// the overload to apply the text to the rich edit control.
// </summary>
HRESULT
CSampleRichEditWindow::GetResourceString(
_In_ UINT stringID,
_Outptr_ PCWSTR * stringText
)
{
HRESULT hr = S_OK;
BOOL fResult = FALSE;
int stringSize = 0;
HMODULE hMod = nullptr;
// Get a non-ref counted handle to the current module.
// Module handle is required to use LoadString.
fResult = GetModuleHandleEx(GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT,
NULL,
&hMod);
if (!fResult)
{
hr = HRESULT_FROM_WIN32(GetLastError());
}
// Per MSDN documentation, we can retrieve a read-only pointer by
// passing in a PWSTR and a zero-size buffer.
if (fResult)
{
stringSize = LoadStringW(hMod, stringID, (PWSTR)stringText, 0);
}
if (stringSize <= 0)
{
hr = HRESULT_FROM_WIN32(GetLastError());
}
return hr;
}
// <summary>
// This method explictly applies the text requested to the
// rich edit control.
// </summary>
HRESULT
CSampleRichEditWindow::SetText(
_In_ PCWSTR stringText
)
{
HRESULT hr = S_OK;
LRESULT lr;
ASSERT(m_hWnd);
ASSERT(stringText);
lr = SendMessage(m_hWnd, WM_SETTEXT, (WPARAM) NULL, (LPARAM) stringText);
if (!lr)
{
hr = HRESULT_FROM_WIN32(GetLastError());
}
return hr;
}
// <summary>
// This method resets the rich edit control to default values
// for text formatting. This method will use the default values for rich edit,
// except for bold and font color, which will be set explicitly.
// </summary>
HRESULT
CSampleRichEditWindow::SetDefaultFormat()
{
HRESULT hr = S_OK;
LRESULT lr;
CHARFORMAT2 charFormat;
ZeroMemory(&charFormat, sizeof(charFormat));
// Gather the default settings that exist on the rich edit control.
charFormat.cbSize = sizeof(CHARFORMAT2) ;
lr = SendMessage(m_hWnd, EM_GETCHARFORMAT, SCF_DEFAULT, (LPARAM) &charFormat);
if (lr)
{
// Set text to be bold.
charFormat.dwMask = CFM_BOLD;
// Set text color to be black.
charFormat.dwMask |= CFM_COLOR;
charFormat.crTextColor = RGB(0, 0, 0);
// Set font size using inline conversion to twips.
charFormat.dwMask |= CFM_SIZE;
charFormat.yHeight = (LONG) (DEFAULT_FONT_PT * TWIPS_PER_PT);
// Notify the rich edit control of the requested format.
lr = SendMessage(m_hWnd, EM_SETCHARFORMAT, SCF_ALL, (LPARAM) &charFormat);
}
else
{
hr = HRESULT_FROM_WIN32(GetLastError());
}
return hr;
}
// <summary>
// This method provides an internal implementation to explicitly
// increment or decrement font size.
// </summary>
HRESULT
CSampleRichEditWindow::UpdateFontSize(
_In_ CHANGE_DIRECTION direction
)
{
HRESULT hr = S_OK;
LRESULT lr;
CHARFORMAT2 charFormat;
ZeroMemory(&charFormat, sizeof(charFormat));
// Gather the settings that exist on the rich edit control.
charFormat.cbSize = sizeof(charFormat);
lr = SendMessageW(m_hWnd, EM_GETCHARFORMAT, SCF_DEFAULT, (LPARAM) &charFormat);
// Note that only the font size is changing.
if (lr)
{
charFormat.dwMask = CFM_SIZE;
if (direction == CD_INCREMENT)
{
charFormat.yHeight = (LONG) (charFormat.yHeight * FONT_STEP);
}
else
{
charFormat.yHeight = (LONG) (charFormat.yHeight / FONT_STEP);
}
// Notify rich edit control of new format.
lr = SendMessage(m_hWnd, EM_SETCHARFORMAT, SCF_ALL, (LPARAM) &charFormat);
}
else
{
hr = HRESULT_FROM_WIN32(GetLastError());
}
return hr;
}
// <summary>
// Set the zoom factor based on values stored in rich edit member variables.
// Once the zoom factor is applied, update the window to redraw.
// </summary>
HRESULT
CSampleRichEditWindow::ApplyZoomFactor()
{
HRESULT hr = S_OK;
LRESULT lr;
lr = SendMessage(m_hWnd, EM_SETZOOM, (WPARAM) m_numerator, (LPARAM) m_denominator);
if (!lr)
{
hr = HRESULT_FROM_WIN32(GetLastError());
}
return hr;
}