-
Notifications
You must be signed in to change notification settings - Fork 13
/
MonitorKeeper.cpp
712 lines (623 loc) · 39 KB
/
MonitorKeeper.cpp
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
// MonitorKeeper.cpp
//
// Author: Garr Godfrey
// License: MIT License
// License Summary: Free but author takes no responsibility.
//
// Monitor Keeper is a simple app that runs as an application on the task bar that will restore windows
// to their original locations when a monitor because available. When a monitor is turned off, or HDMI is unplugged,
// Windows 7 and higher detect that and will rearrange all application windows onto the remaining monitor(s).
// If the monitor is reconnected, the applications stay on the single monitor, requiring the user to
// move them back manually. This application moves them back automatically.
//
// Limitations:
// - Windows are only repositions when the number of monitors INCREASES. There is no way to specify a layout
// to use on a single monitor, for example (although this would not be a difficult change)
// - Support is limited to 5 monitors. This is arbitrary and done simply to limit the storage for each application.
// - If application is run as a standard user, it cannot move any applications that are running as a privileged user.
// If you run into this, you can run this program as administrator, perhaps using Task Scheduler to launch it at login.
// - Window position is only saved while application is running. There is no persistent storage of position (say, between reboots)
// - Windows will return to their state when the number of monitors was most recently seen. So, a window may go from minimize to
// maximized or be a different size once the second (or third) monitor is plugged back in.
//
// DEMO:
// To quickly test the functionality, go to Display Settings... in windows (right click on desktop), with two monitors, change
// the setting for "Multiple Displays" from "Extend These Displays" to "Duplicate These Displays". Compare when running Monitor
// Keeper and when not.
#include "MonitorKeeper.h"
#define MAX_MONITORS 5
#define MIN_MONITORTORESTORE 2
#define LOGBUFFERSIZE (32*1024)
#define MAX_LOADSTRING 100
// Global Variables:
HINSTANCE hInst; // current instance
WCHAR szTitle[MAX_LOADSTRING]; // The title bar text
WCHAR szWindowClass[MAX_LOADSTRING]; // the main window class name
void LogMessage(LPCTSTR);
//
// class representing the data we save for each top level window.
//
class SavedWindowData {
public:
SavedWindowData() {
m_wndClass[0] = '\0';
m_hwnd = NULL;
m_nUnusedCount = 1;
ZeroMemory(m_windowPlacement, sizeof(m_windowPlacement));
}
int m_nUnusedCount;
WINDOWPLACEMENT m_windowPlacement[MAX_MONITORS-MIN_MONITORTORESTORE+1];
HWND m_hwnd;
TCHAR m_wndClass[40]; // window class, for verification.
BOOL SetData(HWND hwnd, int NumMonitors)
{
m_hwnd = hwnd;
m_nUnusedCount = 0;
RealGetWindowClass(hwnd, m_wndClass, sizeof(m_wndClass) / sizeof(TCHAR));
if (NumMonitors < MIN_MONITORTORESTORE || NumMonitors > MAX_MONITORS) return false; // too many monitors or not enought
m_windowPlacement[NumMonitors - MIN_MONITORTORESTORE].length = sizeof(WINDOWPLACEMENT);
GetWindowPlacement(hwnd, &(m_windowPlacement[NumMonitors- MIN_MONITORTORESTORE]));
return true;
}
void RestoreWindow(int NumMonitors)
{
TCHAR szTempClass[40];
if (NumMonitors >= MIN_MONITORTORESTORE && NumMonitors <= MAX_MONITORS) {
if (IsWindow(m_hwnd) &&
m_windowPlacement[NumMonitors - MIN_MONITORTORESTORE].length == sizeof(WINDOWPLACEMENT)) {
// verify window class
RealGetWindowClass(m_hwnd, szTempClass, sizeof(szTempClass) / sizeof(TCHAR));
if (lstrcmp(szTempClass, m_wndClass) == 0) {
WINDOWPLACEMENT* place = &(m_windowPlacement[NumMonitors - MIN_MONITORTORESTORE]);
if (place->showCmd == SW_MAXIMIZE) {
// we need to treat this special, first restore it to the correct position,
// then maximize. Otherwise, it will just maximize it on the current screen
// and ingore the coordinates.
place->showCmd = SW_SHOWNOACTIVATE;
SetWindowPlacement(m_hwnd, &(m_windowPlacement[NumMonitors - MIN_MONITORTORESTORE]));
place->showCmd = SW_MAXIMIZE;
}
else if (place->showCmd == SW_MINIMIZE || place->showCmd == SW_SHOWMINIMIZED) {
place->showCmd = SW_SHOWMINNOACTIVE;
}
else if (place->showCmd == SW_NORMAL) {
place->showCmd = SW_SHOWNOACTIVATE;
}
// don't worry about "minimized position", it is a concept from Windows 3.0.
place->flags &= ~WPF_SETMINPOSITION;
place->flags |= WPF_ASYNCWINDOWPLACEMENT;
SetWindowPlacement(m_hwnd, &(m_windowPlacement[NumMonitors - MIN_MONITORTORESTORE]));
}
}
}
}
};
//
// Other global information we need for our application in this class, as
// well as methods that operate on the saved data.
//
class InstanceData {
public:
InstanceData() {
_Hook = NULL;
#ifdef _DEBUG
_LogInfo[0] = '\0';
#endif
_WindowDataLength = 32;
_NumMonitors = 1;
_WindowData = new SavedWindowData[_WindowDataLength];
_MainWnd = NULL;
InChangingState = false;
#define APPLICATION_INSTANCE_MUTEX_NAME L"{f7ef2518-1a96-11ec-9621-0242ac130002}"
// check running instance
_MutexSingleInstance = ::CreateMutex(NULL, TRUE, APPLICATION_INSTANCE_MUTEX_NAME);
AlreadyRunning = ::GetLastError() == ERROR_ALREADY_EXISTS;
}
~InstanceData()
{
Shutdown();
}
void Shutdown() {
if (_Hook != NULL) UnhookWinEvent(_Hook);
_Hook = NULL;
if (_WindowData != NULL) {
delete [] _WindowData;
}
if (_MutexSingleInstance)
{
::ReleaseMutex(_MutexSingleInstance);
::CloseHandle(_MutexSingleInstance);
}
}
static InstanceData g_Instance;
//
// there is a primiative log window in debug mode.
//
void LogMessage(LPCTSTR str)
{
#ifdef _DEBUG
int len = lstrlen(_LogInfo);
int newlen = lstrlen(str);
if (len + newlen >= LOGBUFFERSIZE)
{
len = 0;
}
lstrcpy(_LogInfo + len, str);
if (_MainWnd != NULL) {
SetScrollPos(_MainWnd, SB_VERT, 10000, true);
InvalidateRect(_MainWnd, NULL, TRUE);
}
#endif
}
//
// we are not notified on a window destroy, so we marked windows
// if we haven't seen them. If we don't see it 3 times in a row,
// we will reuse it's position in the array.
//
void TagWindowsUnused()
{
int i;
for (i = 0; i < _WindowDataLength; i++)
{
// don't count to the point we rollover
if (_WindowData[i].m_hwnd != NULL && _WindowData[i].m_nUnusedCount < 100)
{
_WindowData[i].m_nUnusedCount++;
}
}
}
//
// restore all the top level windows
//
void RestoreWindowPositions(int monitors)
{
int i;
for (i = 0; i < _WindowDataLength; i++)
{
// don't count to the point we rollover
if (_WindowData[i].m_hwnd != NULL && _WindowData[i].m_nUnusedCount <= 2)
{
_WindowData[i].RestoreWindow(monitors);
}
}
}
//
// find slow for the window we found.
SavedWindowData* FindWindowSlot(HWND hwnd)
{
//
// find existing HWND in array
int i;
for (i = 0; i < _WindowDataLength; i++)
{
if (_WindowData[i].m_hwnd == hwnd) {
return &(_WindowData[i]);
}
}
//
// find an unused slot.
for (i = 0; i < _WindowDataLength; i++)
{
if (_WindowData[i].m_hwnd == NULL ||
_WindowData[i].m_nUnusedCount > 2) {
return &(_WindowData[i]);
}
}
//
// hmmm, all used, need to reallocate.
i = _WindowDataLength;
int newlength = _WindowDataLength + 32;
SavedWindowData* newdata = new SavedWindowData[newlength];
for (i = 0; i < _WindowDataLength; i++) {
newdata[i] = _WindowData[i];
}
delete[] _WindowData;
_WindowData = newdata;
_WindowDataLength = newlength;
// i === old _WindowDataLength
return &(_WindowData[i]);
}
HWINEVENTHOOK _Hook;
SavedWindowData *_WindowData;
int _WindowDataLength;
int _NumMonitors;
HWND _MainWnd;
BOOL InChangingState;
BOOL AlreadyRunning;
HANDLE _MutexSingleInstance;
#ifdef _DEBUG
TCHAR _LogInfo[LOGBUFFERSIZE];
#endif
};
/*static*/ InstanceData InstanceData::g_Instance;
void LogMessage(LPCTSTR lpz)
{
InstanceData::g_Instance.LogMessage(lpz);
}
// Forward declarations of functions included in this code module:
ATOM MyRegisterClass(HINSTANCE hInstance);
BOOL InitInstance(HINSTANCE, int);
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
INT_PTR CALLBACK About(HWND, UINT, WPARAM, LPARAM);
int APIENTRY wWinMain(_In_ HINSTANCE hInstance,
_In_opt_ HINSTANCE hPrevInstance,
_In_ LPWSTR lpCmdLine,
_In_ int nCmdShow)
{
UNREFERENCED_PARAMETER(hPrevInstance);
UNREFERENCED_PARAMETER(lpCmdLine);
if (InstanceData::g_Instance.AlreadyRunning) {
return FALSE;
}
// Initialize global strings
LoadStringW(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
LoadStringW(hInstance, IDC_MONITORKEEPER, szWindowClass, MAX_LOADSTRING);
MyRegisterClass(hInstance);
// Perform application initialization:
if (!InitInstance (hInstance, nCmdShow))
{
return FALSE;
}
HACCEL hAccelTable = LoadAccelerators(hInstance, MAKEINTRESOURCE(IDC_MONITORKEEPER));
MSG msg;
// Main message loop:
while (GetMessage(&msg, nullptr, 0, 0))
{
if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
return (int) msg.wParam;
}
//
// FUNCTION: MyRegisterClass()
//
// PURPOSE: Registers the window class.
//
ATOM MyRegisterClass(HINSTANCE hInstance)
{
WNDCLASSEXW 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 = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_MONITORKEEPER));
wcex.hCursor = LoadCursor(nullptr, IDC_ARROW);
wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
wcex.lpszMenuName = MAKEINTRESOURCEW(IDC_MONITORKEEPER);
wcex.lpszClassName = szWindowClass;
wcex.hIconSm = LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_SMALL));
return RegisterClassExW(&wcex);
}
LPCTSTR TranslateShowCommand(int nShowCmd)
{
switch (nShowCmd)
{
case SW_RESTORE:
case SW_SHOWNORMAL:
return _T("SW_SHOWNORMAL");
case SW_MAXIMIZE:
return _T("SW_MAXIMIZE");
case SW_MINIMIZE:
case SW_SHOWMINIMIZED:
return _T("SW_MINIMIZE");
case SW_SHOWNOACTIVATE:
return _T("SW_SHOWNOACTIVATE");
case SW_SHOWMINNOACTIVE:
return _T("SW_SHOWMINNOACTIVE");
default:
return _T("Unknown");
}
}
//
// Called by EnumDesktopWindows whenever a window changes state.
// This will capture a lot of events.
//
BOOL CALLBACK SaveWindowsCallback(
_In_ HWND hwnd,
_In_ LPARAM lParam
)
{
int monitors = (int)lParam;
//
// only track windows that are visible, don't have a parent,
// have at least one style that is in the OVERLAPPEDWINDOW style and
// do not have the WS_EX_NOACTIVE style.
//
// we include WS_EX_TOOLBAR because they sometimes are useful and get
// moved as well.
//
if (IsWindowVisible(hwnd) &&
GetParent(hwnd) == NULL) {
DWORD dwStyle = (DWORD)GetWindowLong(hwnd, GWL_STYLE);
DWORD dwExStyle = (DWORD)GetWindowLong(hwnd, GWL_EXSTYLE);
if (((dwStyle & (WS_OVERLAPPEDWINDOW)) != 0 ||
(dwExStyle & WS_EX_APPWINDOW) != 0 )
&&
(dwExStyle & (WS_EX_NOACTIVATE)) == 0)
{
SavedWindowData *pData = InstanceData::g_Instance.FindWindowSlot(hwnd);
if (pData->SetData(hwnd, monitors))
{
TCHAR sz[128];
wsprintf(sz, _T("Save Position for %s, monitors %d, x=%d, y=%d, show=%s\n"),
pData->m_wndClass, monitors, pData->m_windowPlacement[monitors - MIN_MONITORTORESTORE].rcNormalPosition.left,
pData->m_windowPlacement[monitors - MIN_MONITORTORESTORE].rcNormalPosition.top,
TranslateShowCommand(pData->m_windowPlacement[monitors - MIN_MONITORTORESTORE].showCmd));
LogMessage(sz);
}
}
}
return true;
}
//
// Process when the number of monitors changes. If we have changed monitor count
// we attempt to restore /
//
void ProcessMonitors()
{
int monitors = GetSystemMetrics(SM_CMONITORS);
if (monitors > 1 && InstanceData::g_Instance._NumMonitors != monitors)
{
// restore windows.
InstanceData::g_Instance.RestoreWindowPositions(monitors);
}
InstanceData::g_Instance._NumMonitors = monitors;
InstanceData::g_Instance.InChangingState = false;
}
//
// called by hook for window changes
//
void ProcessDesktopWindows()
{
TCHAR sz[128];
int monitors = GetSystemMetrics(SM_CMONITORS);
if (monitors != InstanceData::g_Instance._NumMonitors)
{
// we haven't completed our switch to change of monitors yet.
// so don't save positions until we've repositioned things.
return;
}
wsprintf(sz, _T("Monitors: %d\n"), monitors);
InstanceData::g_Instance.TagWindowsUnused();
InstanceData::g_Instance.LogMessage(sz);
EnumDesktopWindows(NULL, SaveWindowsCallback, monitors);
}
//
// save windows positions after a slight delay
VOID CALLBACK SaveTimerCallback(
_In_ HWND hwnd,
_In_ UINT uMsg,
_In_ UINT_PTR idEvent,
_In_ DWORD dwTime
)
{
ProcessDesktopWindows();
KillTimer(hwnd, idEvent);
}
//
// Our window hook, grabbing the event when the active window changes.
VOID CALLBACK WinEventProcCallback(HWINEVENTHOOK hWinEventHook, DWORD dwEvent, HWND hwnd, LONG idObject, LONG idChild, DWORD dwEventThread, DWORD dwmsEventTime)
{
if (InstanceData::g_Instance.InChangingState) return;
if (hwnd != NULL &&
(dwEvent == EVENT_SYSTEM_MOVESIZEEND ||
dwEvent == EVENT_OBJECT_LOCATIONCHANGE))
{
// use our HWND so that this timer get replaced each time we call SetTimer.
SetTimer(InstanceData::g_Instance._MainWnd, 2, 200, SaveTimerCallback);
}
}
//
// reposition windows after a slight delay
VOID CALLBACK TimerCallback(
_In_ HWND hwnd,
_In_ UINT uMsg,
_In_ UINT_PTR idEvent,
_In_ DWORD dwTime
)
{
ProcessMonitors();
KillTimer(hwnd, idEvent);
}
HWINEVENTHOOK HookDisplayChange()
{
return SetWinEventHook(EVENT_OBJECT_LOCATIONCHANGE, EVENT_OBJECT_LOCATIONCHANGE, NULL, WinEventProcCallback, 0, 0, WINEVENT_OUTOFCONTEXT);
}
//
// FUNCTION: InitInstance(HINSTANCE, int)
//
// PURPOSE: Saves instance handle and creates main window
//
// COMMENTS:
//
// In this function, we save the instance handle in a global variable and
// create and display the main program window.
//
BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
hInst = hInstance; // Store instance handle in our global variable
HWND hWnd = CreateWindowW(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW & ~WS_VISIBLE,
CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, nullptr, nullptr, hInstance, nullptr);
ProcessDesktopWindows();
InstanceData::g_Instance._NumMonitors = GetSystemMetrics(SM_CMONITORS);
if (!hWnd)
{
return FALSE;
}
InstanceData::g_Instance._MainWnd = hWnd;
InstanceData::g_Instance._Hook = HookDisplayChange();
SetScrollRange(hWnd, SB_VERT, 0, 10000, false);
NOTIFYICONDATA icon;
//
// create notify icon
icon.cbSize = sizeof(icon);
icon.hWnd = hWnd;
icon.uID = 1;
icon.szTip[0] = '\0';
icon.uFlags = NIF_ICON|NIF_MESSAGE| NIM_SETVERSION;
icon.uCallbackMessage = WM_USER + 100;
icon.hIcon = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_MONITORKEEPER));
icon.uVersion = NOTIFYICON_VERSION_4;
Shell_NotifyIcon(NIM_ADD, &icon);
return TRUE;
}
//
// FUNCTION: WndProc(HWND, UINT, WPARAM, LPARAM)
//
// PURPOSE: Processes messages for the main window.
//
// WM_COMMAND - process the application menu
// WM_PAINT - Paint the main window
// WM_DESTROY - post a quit message and return
//
//
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
switch (message)
{
case WM_DISPLAYCHANGE:
LogMessage(_T("WM_DISPLAYCHANGE\n"));
InstanceData::g_Instance.InChangingState = true;
SetTimer(hWnd, 99, 500, TimerCallback);
break;
case WM_COMMAND:
{
int wmId = LOWORD(wParam);
// Parse the menu selections:
switch (wmId)
{
case IDM_ABOUT:
DialogBox(hInst, MAKEINTRESOURCE(IDD_ABOUTBOX), hWnd, About);
break;
case IDM_EXIT:
DestroyWindow(hWnd);
break;
case IDM_SHOWWINDOW:
ShowWindow(hWnd, SW_RESTORE);
UpdateWindow(hWnd);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
}
break;
case WM_CLOSE:
ShowWindow(hWnd, SW_HIDE);
return false;
case (WM_USER+100):
// notify icon
{
//
// pop up our context menu on the notify icon.
//
UINT nMsg = LOWORD(lParam);
if (nMsg == WM_CONTEXTMENU || nMsg == WM_RBUTTONUP) {
int x = LOWORD(wParam);
int y = HIWORD(wParam);
HMENU menu = GetMenu(hWnd);
menu = GetSubMenu(menu, 1);
RECT r;
NOTIFYICONIDENTIFIER id;
id.cbSize = sizeof(id);
id.hWnd = hWnd;
id.uID = 1;
id.guidItem = GUID_NULL;
Shell_NotifyIconGetRect(&id, &r);
x += r.left;
y += r.top;
TrackPopupMenu(menu, TPM_RIGHTALIGN | TPM_BOTTOMALIGN | TPM_RIGHTBUTTON, x, y, 0, hWnd, NULL);
}
}
break;
case WM_VSCROLL:
{
UINT cmd = LOWORD(wParam);
int pos = GetScrollPos(hWnd, SB_VERT);
if (cmd == SB_BOTTOM) {
pos = 10000;
}
else if (cmd == SB_TOP) {
pos += 0;
}
else if (cmd == SB_PAGEDOWN) {
pos += 1000;
}
else if (cmd == SB_PAGEUP) {
pos -= 1000;
}
else if (cmd == SB_THUMBPOSITION) {
pos = HIWORD(wParam);
}
else if (cmd == SB_THUMBTRACK) {
pos = HIWORD(wParam);
}
else {
break;
}
if (pos < 0) pos = 0;
if (pos > 10000) pos = 10000;
SetScrollPos(hWnd, SB_VERT, pos, true);
InvalidateRect(hWnd, NULL, true);
}
break;
case WM_PAINT:
{
#ifdef _DEBUG
PAINTSTRUCT ps;
RECT r,r2;
HDC hdc = BeginPaint(hWnd, &ps);
GetClientRect(hWnd, &r);
r2 = r;
DrawText(hdc, InstanceData::g_Instance._LogInfo,
-1, &r2, DT_LEFT | DT_NOPREFIX | DT_WORDBREAK | DT_CALCRECT);
//
// get scroll position.
//
int pos = GetScrollPos(hWnd, SB_VERT);
pos = pos * (r2.bottom - r.bottom)/ 10000;
r.top = r.top - pos;
DrawText(hdc, InstanceData::g_Instance._LogInfo,
-1, &r, DT_LEFT | DT_NOPREFIX | DT_WORDBREAK);
EndPaint(hWnd, &ps);
#endif
}
break;
case WM_DESTROY:
{
// destory our notify icon.
NOTIFYICONDATA icon;
icon.cbSize = sizeof(icon);
icon.hWnd = hWnd;
icon.uID = 1;
Shell_NotifyIcon(NIM_DELETE, &icon);
PostQuitMessage(0);
}
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
return 0;
}
// Message handler for about box.
INT_PTR CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
UNREFERENCED_PARAMETER(lParam);
switch (message)
{
case WM_INITDIALOG:
return (INT_PTR)TRUE;
case WM_COMMAND:
if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL)
{
EndDialog(hDlg, LOWORD(wParam));
return (INT_PTR)TRUE;
}
break;
}
return (INT_PTR)FALSE;
}