-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathBalloonHelp.cpp
More file actions
1390 lines (1237 loc) · 44.7 KB
/
BalloonHelp.cpp
File metadata and controls
1390 lines (1237 loc) · 44.7 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
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// ******************************************************************************
// BalloonHelp.cpp : implementation file
// Copyright 2001-2002, Joshua Heyer
// You are free to use this code for whatever you want, provided you
// give credit where credit is due. (I seem to get a lot of questions
// about that statement... All i mean is, don't copy huge bits of code
// and then claim you wrote it. You don't have to put my name in an about
// box or anything. Though i'm not going to stop you if that's really what
// you want :~) )
// I'm providing this code in the hope that it is useful to someone, as i have
// gotten much use out of other peoples code over the years.
// If you see value in it, make some improvements, etc., i would appreciate it
// if you sent me some feedback.
//
// ******************************************************************************
#include "stdafx.h"
#include "BalloonHelp.h"
// allow multimonitor-aware code on Win95 systems
// comment out the first line if you have already define it in another file
// comment out both lines if you don't care about Win95
#define COMPILE_MULTIMON_STUBS
#include "multimon.h"
// debug helpers
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
//
// constants that may not be defined if you don't have the latest SDK
// (but i like to use them anyway)
//
#ifndef DFCS_HOT
#define DFCS_HOT 0x1000
#endif
#ifndef AW_HIDE
#define AW_HIDE 0x00010000
#define AW_BLEND 0x00080000
#endif
#ifndef CS_DROPSHADOW
#define CS_DROPSHADOW 0x00020000
#endif
#ifndef SPI_GETDROPSHADOW
#define SPI_GETDROPSHADOW 0x1024
#endif
#ifndef SPI_GETTOOLTIPANIMATION
#define SPI_GETTOOLTIPANIMATION 0x1016
#endif
#ifndef SPI_GETTOOLTIPFADE
#define SPI_GETTOOLTIPFADE 0x1018
#endif
/////////////////////////////////////////////////////////////////////////////
// CBalloonHelp
// option constants (bits)
const unsigned int CBalloonHelp::unCLOSE_ON_LBUTTON_UP = 0x0001;
const unsigned int CBalloonHelp::unCLOSE_ON_MBUTTON_UP = 0x0002;
const unsigned int CBalloonHelp::unCLOSE_ON_RBUTTON_UP = 0x0004;
const unsigned int CBalloonHelp::unCLOSE_ON_LBUTTON_DOWN = 0x0008;
const unsigned int CBalloonHelp::unCLOSE_ON_MBUTTON_DOWN = 0x0010;
const unsigned int CBalloonHelp::unCLOSE_ON_RBUTTON_DOWN = 0x0020;
const unsigned int CBalloonHelp::unCLOSE_ON_MOUSE_MOVE = 0x0040;
const unsigned int CBalloonHelp::unCLOSE_ON_KEYPRESS = 0x0080;
const unsigned int CBalloonHelp::unCLOSE_ON_ANYTHING = CBalloonHelp::unCLOSE_ON_MOUSE_MOVE|CBalloonHelp::unCLOSE_ON_RBUTTON_DOWN|CBalloonHelp::unCLOSE_ON_RBUTTON_DOWN|CBalloonHelp::unCLOSE_ON_MBUTTON_DOWN|CBalloonHelp::unCLOSE_ON_LBUTTON_DOWN|CBalloonHelp::unCLOSE_ON_RBUTTON_UP|CBalloonHelp::unCLOSE_ON_MBUTTON_UP|CBalloonHelp::unCLOSE_ON_LBUTTON_UP;
const unsigned int CBalloonHelp::unDELAY_CLOSE = 0x0100;
const unsigned int CBalloonHelp::unDELETE_THIS_ON_CLOSE = 0x0200;
const unsigned int CBalloonHelp::unSHOW_CLOSE_BUTTON = 0x0400;
const unsigned int CBalloonHelp::unSHOW_INNER_SHADOW = 0x0800;
const unsigned int CBalloonHelp::unSHOW_TOPMOST = 0x1000;
const unsigned int CBalloonHelp::unDISABLE_XP_SHADOW = 0x2000;
const unsigned int CBalloonHelp::unDISABLE_FADEIN = 0x4000;
const unsigned int CBalloonHelp::unDISABLE_FADEOUT = 0x8000;
const unsigned int CBalloonHelp::unDISABLE_FADE = CBalloonHelp::unDISABLE_FADEIN|CBalloonHelp::unDISABLE_FADEOUT;
// layout constants (should prolly be configurable, but who's really gonna care?)
const int CBalloonHelp::nTIP_TAIL = 20;
const int CBalloonHelp::nTIP_MARGIN = 8;
// class atom (why don't i do this the MFC way? Drop shadows!)
ATOM CBalloonHelp::s_ClassAtom = NULL;
ATOM CBalloonHelp::s_ClassAtomShadowed = NULL;
// Kill timer
#define ID_TIMER_CLOSE 1
//
// The launchers
//
//
// Show a help balloon on screen
// Parameters:
// strTitle | Title of balloon
// unTitle | Title of balloon (id of string resource)
// strContent | Content of balloon
// unContent | Content of balloon (id of string resource)
// ptAnchor | point tail of balloon will be "anchor"ed to
// szIcon | One of:
// IDI_APPLICATION
// IDI_INFORMATION IDI_ASTERISK (same)
// IDI_ERROR IDI_HAND (same)
// IDI_EXCLAMATION IDI_WARNING (same)
// IDI_QUESTION
// IDI_WINLOGO
// NULL (no icon)
// unIconID | ID of icon to display (loaded from resources)
// unOptions | One or more of:
// : unCLOSE_ON_LBUTTON_UP | closes window on WM_LBUTTON_UP
// : unCLOSE_ON_MBUTTON_UP | closes window on WM_MBUTTON_UP
// : unCLOSE_ON_RBUTTON_UP | closes window on WM_RBUTTON_UP
// : unCLOSE_ON_LBUTTON_DOWN | closes window on WM_LBUTTON_DOWN
// : unCLOSE_ON_MBUTTON_DOWN | closes window on WM_MBUTTON_DOWN
// : unCLOSE_ON_RBUTTON_DOWN | closes window on WM_RBUTTON_DOWN
// : unCLOSE_ON_MOUSE_MOVE | closes window when user moves mouse past threshhold
// : unCLOSE_ON_KEYPRESS | closes window on the next keypress message sent to this thread.
// : unCLOSE_ON_ANYTHING | all of the above.
// : unDELAY_CLOSE | when a user action triggers the close, begins timer. closes when timer expires.
// : unSHOW_CLOSE_BUTTON | shows close button in upper right
// : unSHOW_INNER_SHADOW | draw inner shadow in balloon
// : unSHOW_TOPMOST | place balloon above all other windows
// : unDISABLE_XP_SHADOW | disable Windows XP's drop-shadow effect (overrides system and user settings)
// : unDISABLE_FADE | disable the fade-in/fade-out effects (overrides system and user settings)
// : unDISABLE_FADEIN | disable the fade-in effect
// : unDISABLE_FADEOUT | disable the fade-out effect
// pParentWnd | Parent window. If NULL will be set to AfxGetMainWnd(), and anchor to screen
// strURL | If not empty, when the balloon is clicked ShellExecute() will
// | be called, with strURL passed in.
// unTimeout | If not 0, balloon will automatically close after unTimeout milliseconds.
//
void CBalloonHelp::LaunchBalloon(const CString& strTitle, const CString& strContent,
const CPoint& ptAnchor,
LPCTSTR szIcon /*= IDI_EXCLAMATION*/,
unsigned int unOptions /*= unSHOW_CLOSE_BUTTON*/,
CWnd* pParentWnd /*= NULL*/,
const CString strURL /*= ""*/,
unsigned int unTimeout /*= 10000*/)
{
CBalloonHelp* pbh = new CBalloonHelp;
if ( NULL != szIcon )
{
// Note: Since i'm scaling the icon anyway, i'll allow it to become larger
// than the standard small icon if the close button is.
CSize sizeIcon(max(::GetSystemMetrics(SM_CXSIZE), ::GetSystemMetrics(SM_CXSMICON)), max(::GetSystemMetrics(SM_CYSIZE), ::GetSystemMetrics(SM_CYSMICON)));
HICON hIcon = (HICON)::LoadImage(NULL, szIcon, IMAGE_ICON, sizeIcon.cx, sizeIcon.cy, LR_SHARED);
if (NULL != hIcon)
pbh->SetIconScaled(hIcon, sizeIcon.cx, sizeIcon.cy);
}
pbh->Create(strTitle, strContent, ptAnchor, unOptions|unDELETE_THIS_ON_CLOSE,
pParentWnd, strURL, unTimeout, NULL);
}
//
// The class
//
CBalloonHelp::CBalloonHelp()
: m_fnAnimateWindow(NULL),
m_unOptions(0),
m_unTimeout(0),
m_unTimerClose(0),
m_strURL(""),
m_ptAnchor(0,0),
m_hwndAnchor(NULL),
m_screenRect(0,0,0,0),
m_strContent(""),
m_nMouseMoveTolerance(3), // later retrieved from system
m_ptMouseOrig(0,0),
m_uCloseState(0),
m_pTitleFont(NULL),
m_pContentFont(NULL),
m_crForeground(::GetSysColor(COLOR_INFOTEXT)),
m_crBackground(::GetSysColor(COLOR_INFOBK)),
m_hKeyboardHook(NULL),
m_hMouseHook(NULL),
m_hCallWndRetHook(NULL)
{
// retrieve window animation API if available
HMODULE hUser32 = GetModuleHandle(_T("USER32.DLL"));
// can't imagine why that would fail, but might as well *look* safe... ;~)
if ( NULL != hUser32 )
m_fnAnimateWindow = (FN_ANIMATE_WINDOW)GetProcAddress(hUser32, _T("AnimateWindow"));
else
m_fnAnimateWindow = NULL;
// get system tolerance values
int nTol = 0;
if ( ::SystemParametersInfo(SPI_GETMOUSEHOVERWIDTH, 0, &nTol, 0) && nTol > 0 )
m_nMouseMoveTolerance = nTol;
// setup hook procedures
BHKeybHookThunk<CBalloonHelp>::InitThunk((TMFP)KeyboardHookProc, this);
BHMouseHookThunk<CBalloonHelp>::InitThunk((TMFP)MouseHookProc, this);
BHCallWndRetHookThunk<CBalloonHelp>::InitThunk((TMFP)CallWndRetProc, this);
}
CBalloonHelp::~CBalloonHelp()
{
if ( NULL != m_pTitleFont )
delete m_pTitleFont;
m_pTitleFont = NULL;
if ( NULL != m_pContentFont )
delete m_pContentFont;
m_pContentFont = NULL;
}
// Sets the font used for drawing the balloon title. Deleted by balloon, do not use CFont* after passing to this function.
void CBalloonHelp::SetTitleFont(CFont* pFont)
{
if ( NULL != m_pTitleFont )
delete m_pTitleFont;
m_pTitleFont = pFont;
// if already visible, resize & move
if ( NULL != m_hWnd )
PositionWindow();
}
// Sets the font used for drawing the balloon content. Deleted by balloon, do not use CFont* after passing to this function.
void CBalloonHelp::SetContentFont(CFont* pFont)
{
if ( NULL != m_pContentFont )
delete m_pContentFont;
m_pContentFont = pFont;
// if already visible, resize & move
if ( NULL != m_hWnd )
PositionWindow();
}
// Sets the icon displayed in the top left of the balloon (pass NULL to hide icon)
void CBalloonHelp::SetIcon(HICON hIcon)
{
if ( NULL != m_ilIcon.m_hImageList )
m_ilIcon.DeleteImageList();
ICONINFO iconinfo;
if ( NULL != hIcon && ::GetIconInfo(hIcon, &iconinfo) )
{
SetIcon(iconinfo.hbmColor, iconinfo.hbmMask);
::DeleteObject(iconinfo.hbmColor);
::DeleteObject(iconinfo.hbmMask);
}
// if already visible, resize & move (icon size may have changed)
if ( NULL != m_hWnd )
PositionWindow();
}
// Sets the icon displayed in the top left of the balloon (pass NULL to hide icon)
void CBalloonHelp::SetIconScaled(HICON hIcon, int cx, int cy)
{
// i now have two device contexts and two bitmaps.
// i will select a bitmap in each device context,
// draw the icon into the first one,
// scale it into the second one,
// and set the second one as the balloon icon.
// This is a rather long process to get a scaled icon,
// but ensures maximum compatibility between different
// versions of Windows, while producing the best possible
// results on each version (quite good in WinNT and better, sorta ok in Win9x).
ICONINFO iconinfo;
if ( NULL != hIcon && ::GetIconInfo(hIcon, &iconinfo) )
{
BITMAP bm;
if (::GetObject(iconinfo.hbmColor, sizeof(bm),(LPVOID)&bm))
{
CDC dc;
CDC dcTmp1;
CDC dcTmp2;
CBitmap bmpIcon;
CBitmap bmpIconScaled;
dc.Attach(::GetDC(NULL));
dcTmp1.CreateCompatibleDC(&dc);
dcTmp2.CreateCompatibleDC(&dc);
bmpIcon.CreateCompatibleBitmap(&dc, bm.bmWidth, bm.bmHeight);
bmpIconScaled.CreateCompatibleBitmap(&dc, cx, cy);
::ReleaseDC(NULL, dc.Detach());
CBitmap* pbmpOld1 = dcTmp1.SelectObject(&bmpIcon);
CBitmap* pbmpOld2 = dcTmp2.SelectObject(&bmpIconScaled);
dcTmp1.FillSolidRect(0,0,bm.bmWidth,bm.bmHeight, m_crBackground);
::DrawIconEx(dcTmp1, 0,0,hIcon,bm.bmWidth,bm.bmHeight,0,NULL,DI_NORMAL);
dcTmp2.SetStretchBltMode(HALFTONE);
dcTmp2.StretchBlt(0,0,cx,cy,&dcTmp1, 0,0,bm.bmWidth,bm.bmHeight,SRCCOPY);
dcTmp1.SelectObject(pbmpOld1);
dcTmp2.SelectObject(pbmpOld2);
SetIcon(bmpIconScaled, m_crBackground);
}
::DeleteObject(iconinfo.hbmColor);
::DeleteObject(iconinfo.hbmMask);
}
}
// Sets the icon displayed in the top left of the balloon (pass NULL hBitmap to hide icon)
void CBalloonHelp::SetIcon(HBITMAP hBitmap, COLORREF crMask)
{
if ( NULL != m_ilIcon.m_hImageList )
m_ilIcon.DeleteImageList();
if ( NULL != hBitmap )
{
BITMAP bm;
if (::GetObject(hBitmap, sizeof(bm),(LPVOID)&bm))
{
m_ilIcon.Create(bm.bmWidth, bm.bmHeight, ILC_COLOR24|ILC_MASK,1,0);
m_ilIcon.Add(CBitmap::FromHandle(hBitmap), crMask);
}
}
// if already visible, resize & move (icon size may have changed)
if ( NULL != m_hWnd )
PositionWindow();
}
// Sets the icon displayed in the top left of the balloon
void CBalloonHelp::SetIcon(HBITMAP hBitmap, HBITMAP hMask)
{
if ( NULL != m_ilIcon.m_hImageList )
m_ilIcon.DeleteImageList();
ASSERT(NULL != hBitmap);
ASSERT(NULL != hMask);
BITMAP bm;
if (::GetObject(hBitmap, sizeof(bm),(LPVOID)&bm))
{
m_ilIcon.Create(bm.bmWidth, bm.bmHeight, ILC_COLOR24|ILC_MASK,1,0);
m_ilIcon.Add(CBitmap::FromHandle(hBitmap), CBitmap::FromHandle(hMask));
}
// if already visible, resize & move (icon size may have changed)
if ( NULL != m_hWnd )
PositionWindow();
}
// Set icon displayed in the top left of the balloon to image # nIconIndex from pImageList
void CBalloonHelp::SetIcon(CImageList* pImageList, int nIconIndex)
{
// sanity checks
ASSERT_VALID(pImageList);
ASSERT(nIconIndex >= 0 && nIconIndex < pImageList->GetImageCount() );
HICON hIcon = NULL;
if ( NULL != pImageList && nIconIndex >= 0 && nIconIndex < pImageList->GetImageCount() )
hIcon = pImageList->ExtractIcon(nIconIndex);
SetIcon(hIcon);
if ( NULL != hIcon )
::DestroyIcon(hIcon);
// if already visible, resize & move (icon size may have changed)
if ( NULL != m_hWnd )
PositionWindow();
}
// Sets the URL to be opened when balloon is clicked. Pass "" to disable.
void CBalloonHelp::SetURL(const CString& strURL)
{
m_strURL = strURL;
}
// Sets the number of milliseconds the balloon can remain open. Set to 0 to disable timeout.
void CBalloonHelp::SetTimeout(unsigned int unTimeout)
{
m_unTimeout = unTimeout;
// if timer is already set, reset.
if ( NULL != m_hWnd )
{
if ( m_unTimeout > 0 )
{
m_unTimerClose = SetTimer(ID_TIMER_CLOSE, m_unTimeout, NULL);
}
else
{
KillTimer(m_unTimerClose);
}
}
}
// Sets the point to which the balloon is "anchored"
void CBalloonHelp::SetAnchorPoint(CPoint ptAnchor, CWnd* pWndAnchor /*= NULL*/)
{
m_ptAnchor = ptAnchor;
m_hwndAnchor = pWndAnchor->GetSafeHwnd();
// if we're anchored to a window, set hook
if ( NULL != m_hwndAnchor )
SetCallWndRetHook();
else
RemoveCallWndRetHook();
// if already visible, move
if ( NULL != m_hWnd )
{
// reposition
PositionWindow();
}
}
// Sets the title of the balloon
void CBalloonHelp::SetTitle(const CString& strTitle)
{
SetWindowText(strTitle);
// if already visible, resize & move
if ( NULL != m_hWnd )
PositionWindow();
}
// Sets the content of the balloon (plain text only)
void CBalloonHelp::SetContent(const CString& strContent)
{
m_strContent = strContent;
// if already visible, resize & move
if ( NULL != m_hWnd )
PositionWindow();
}
// Sets the forground (text and border) color of the balloon
void CBalloonHelp::SetForegroundColor(COLORREF crForeground)
{
m_crForeground = crForeground;
// repaint if visible
if ( NULL != m_hWnd )
Invalidate(FALSE);
}
// Sets the background color of the balloon
void CBalloonHelp::SetBackgroundColor(COLORREF crBackground)
{
m_crBackground = crBackground;
// repaint if visible
if ( NULL != m_hWnd )
Invalidate(FALSE);
}
// Sets the distance the mouse must move before the balloon closes when the unCLOSE_ON_MOUSE_MOVE option is set.
void CBalloonHelp::SetMouseMoveTolerance(int nTolerance)
{
m_nMouseMoveTolerance = nTolerance;
}
//
// creates a new balloon window
// Parameters:
// strTitle | Title of balloon
// strContent | Content of balloon
// ptAnchor | point tail of balloon will be "anchor"ed to
// unOptions | One or more of:
// : unCLOSE_ON_LBUTTON_UP | closes window on WM_LBUTTON_UP
// : unCLOSE_ON_MBUTTON_UP | closes window on WM_MBUTTON_UP
// : unCLOSE_ON_RBUTTON_UP | closes window on WM_RBUTTON_UP
// : unCLOSE_ON_LBUTTON_DOWN | closes window on WM_LBUTTON_DOWN
// : unCLOSE_ON_MBUTTON_DOWN | closes window on WM_MBUTTON_DOWN
// : unCLOSE_ON_RBUTTON_DOWN | closes window on WM_RBUTTON_DOWN
// : unCLOSE_ON_MOUSE_MOVE | closes window when user moves mouse past threshhold
// : unCLOSE_ON_KEYPRESS | closes window on the next keypress message sent to this thread.
// : unCLOSE_ON_ANYTHING | all of the above.
// : unDELAY_CLOSE | when a user action triggers the close, begins timer. closes when timer expires.
// : unDELETE_THIS_ON_CLOSE | deletes object when window is closed. Used by LaunchBalloon(), use with care
// : unSHOW_CLOSE_BUTTON | shows close button in upper right
// : unSHOW_INNER_SHADOW | draw inner shadow in balloon
// : unSHOW_TOPMOST | place balloon above all other windows
// : unDISABLE_XP_SHADOW | disable Windows XP's drop-shadow effect (overrides system and user settings)
// : unDISABLE_FADE | disable the fade-in/fade-out effects (overrides system and user settings)
// : unDISABLE_FADEIN | disable the fade-in effect
// : unDISABLE_FADEOUT | disable the fade-out effect
// pParentWnd | Parent window. If NULL will be set to AfxGetMainWnd() and anchor to screen
// strURL | If not empty, when the balloon is clicked ShellExecute() will
// | be called, with strURL passed in.
// unTimeout | If not 0, balloon will automatically close after unTimeout milliseconds.
// hIcon | If not NULL, the icon indicated by hIcon will be displayed at top-left of the balloon.
//
// Returns:
// TRUE if successful, else FALSE
//
BOOL CBalloonHelp::Create(const CString& strTitle, const CString& strContent,
const CPoint& ptAnchor, unsigned int unOptions,
CWnd* pParentWnd /*=NULL*/,
const CString strURL /*= ""*/,
unsigned int unTimeout /*= 0*/,
HICON hIcon /*= NULL*/)
{
m_strContent = strContent;
SetAnchorPoint(ptAnchor, pParentWnd);
m_unOptions = unOptions;
m_strURL = strURL;
m_unTimeout = unTimeout;
if ( NULL != hIcon )
SetIcon(hIcon);
pParentWnd = GetSafeOwner(pParentWnd);
if ( NULL == pParentWnd )
{
// something is wrong; MFC should be able to find a main window.
ASSERT(FALSE);
return FALSE;
}
// if no fonts set, use defaults
if ( NULL == m_pContentFont )
{
m_pContentFont = new CFont;
if ( !m_pContentFont->CreateStockObject(DEFAULT_GUI_FONT) )
return FALSE;
}
// title font defaults to bold version of content font
if ( NULL == m_pTitleFont )
{
m_pTitleFont = new CFont;
LOGFONT LogFont;
m_pContentFont->GetLogFont(&LogFont);
LogFont.lfWeight = FW_BOLD;
if ( !m_pTitleFont->CreateFontIndirect(&LogFont) )
return FALSE;
}
ATOM wndClass = GetClassAtom(!(m_unOptions&unDISABLE_XP_SHADOW));
if ( NULL == wndClass ) // couldn't register class
return FALSE;
// check system settings: if fade effects are disabled or unavailable, disable here too
BOOL bFade = FALSE;
::SystemParametersInfo(SPI_GETTOOLTIPANIMATION, 0, &bFade, 0);
if (bFade)
::SystemParametersInfo(SPI_GETTOOLTIPFADE, 0, &bFade, 0);
if (!bFade || NULL == m_fnAnimateWindow)
m_unOptions |= unDISABLE_FADE;
// create invisible at arbitrary position; then position, set region, and finally show
// the idea with WS_EX_TOOLWINDOW is, you can't switch to this using alt+tab
DWORD dwExStyle = WS_EX_TOOLWINDOW;
if ( m_unOptions&unSHOW_TOPMOST ) // make topmost, if requested
dwExStyle |= WS_EX_TOPMOST;
if ( !CreateEx(dwExStyle, (LPCTSTR)wndClass, strTitle, WS_POPUP, CRect(0,0,10,10), pParentWnd, 0, NULL) )
return FALSE;
PositionWindow();
if ( (m_unOptions&unCLOSE_ON_MOUSE_MOVE)
||(m_unOptions&unCLOSE_ON_LBUTTON_UP)
||(m_unOptions&unCLOSE_ON_LBUTTON_DOWN)
||(m_unOptions&unCLOSE_ON_MBUTTON_UP)
||(m_unOptions&unCLOSE_ON_MBUTTON_DOWN)
||(m_unOptions&unCLOSE_ON_RBUTTON_UP)
||(m_unOptions&unCLOSE_ON_RBUTTON_DOWN) )
{
::GetCursorPos(&m_ptMouseOrig);
SetMouseHook();
}
// these need to take effect even if the window receiving them
// is not owned by this process. So, if this process does not
// already have the mouse captured, capture it!
if ( (m_unOptions&unCLOSE_ON_LBUTTON_UP)
||(m_unOptions&unCLOSE_ON_MBUTTON_UP)
||(m_unOptions&unCLOSE_ON_RBUTTON_UP) )
{
// no, i don't particularly need or want to deal with a situation
// where a balloon is being created and another program has captured
// the mouse. If you need it, it shouldn't be too hard, just do it here.
if ( NULL == GetCapture() )
SetCapture();
}
if ( m_unOptions&unCLOSE_ON_KEYPRESS )
SetKeyboardHook();
ShowBalloon();
return TRUE;
}
// calculate anchor position (adjust for client coordinates if used)
CPoint CBalloonHelp::GetAnchorPoint()
{
CPoint ptAnchor = m_ptAnchor;
// assume if window was given, point is in client coords
if ( NULL != m_hwndAnchor )
::ClientToScreen(m_hwndAnchor, &ptAnchor);
return ptAnchor;
}
// determine bounds of screen anchor is on (Multi-Monitor compatibility)
void CBalloonHelp::GetAnchorScreenBounds(CRect& rect)
{
if ( m_screenRect.IsRectEmpty() )
{
// get the nearest monitor to the anchor
HMONITOR hMonitor = MonitorFromPoint(GetAnchorPoint(), MONITOR_DEFAULTTONEAREST);
// get the monitor bounds
MONITORINFO mi;
mi.cbSize = sizeof(mi);
GetMonitorInfo(hMonitor, &mi);
// work area (area not obscured by task bar, etc.)
m_screenRect = mi.rcWork;
}
rect = m_screenRect;
}
// calculates the area of the screen the balloon falls into
// this determins which direction the tail points
CBalloonHelp::BALLOON_QUADRANT CBalloonHelp::GetBalloonQuadrant()
{
CRect rectDesktop;
GetAnchorScreenBounds(rectDesktop);
CPoint ptAnchor = GetAnchorPoint();
if ( ptAnchor.y < rectDesktop.top + rectDesktop.Height()/2 )
{
if ( ptAnchor.x < rectDesktop.left + rectDesktop.Width()/2 )
{
return BQ_TOPLEFT;
}
else
{
return BQ_TOPRIGHT;
}
}
else
{
if ( ptAnchor.x < rectDesktop.left + rectDesktop.Width()/2 )
{
return BQ_BOTTOMLEFT;
}
else
{
return BQ_BOTTOMRIGHT;
}
}
// unreachable
}
// Draw the non-client area
void CBalloonHelp::DrawNonClientArea(CDC* pDC)
{
CRect rect;
GetWindowRect(&rect);
ScreenToClient(&rect);
CRect rectClient;
GetClientRect(&rectClient);
rectClient.OffsetRect(-rect.left, -rect.top);
rect.OffsetRect(-rect.left, -rect.top);
pDC->ExcludeClipRect(&rectClient);
pDC->FillSolidRect(&rect, m_crBackground);
pDC->SelectClipRgn(NULL);
ASSERT(NULL != m_rgnComplete.m_hObject);
CBrush brushFg;
brushFg.CreateSolidBrush(m_crForeground);
if ( m_unOptions & unSHOW_INNER_SHADOW )
{
CBrush brushHL;
// slightly lighter color
int red = 170 + GetRValue(m_crBackground)/3;
int green = 170 + GetGValue(m_crBackground)/3;
int blue = 170 + GetBValue(m_crBackground)/3;
brushHL.CreateSolidBrush(RGB(red,green,blue));
m_rgnComplete.OffsetRgn(1,1);
pDC->FrameRgn(&m_rgnComplete, &brushHL, 2, 2);
// slightly darker color
red = GetRValue(m_crForeground)/3 + GetRValue(m_crBackground)/3*2;
green = GetGValue(m_crForeground)/3 + GetGValue(m_crBackground)/3*2;
blue = GetBValue(m_crForeground)/3 + GetBValue(m_crBackground)/3*2;
brushHL.DeleteObject();
m_rgnComplete.OffsetRgn(-2,-2);
brushHL.CreateSolidBrush(RGB(red,green,blue));
pDC->FrameRgn(&m_rgnComplete, &brushHL, 2, 2);
m_rgnComplete.OffsetRgn(1,1);
}
// outline
pDC->FrameRgn(&m_rgnComplete, &brushFg, 1, 1);
}
// Draw the client area
void CBalloonHelp::DrawClientArea(CDC* pDC)
{
CSize sizeHeader = DrawHeader(pDC);
DrawContent(pDC, sizeHeader.cy+nTIP_MARGIN);
}
// Calculate the dimensions and draw the balloon header
CSize CBalloonHelp::DrawHeader(CDC* pDC, bool bDraw)
{
CSize sizeHdr(0,0);
CRect rectClient;
GetClientRect(&rectClient); // use this for positioning when drawing
// else if content is wider than title, centering wouldn't work
// calc & draw icon
if ( NULL != m_ilIcon.m_hImageList )
{
int x = 0;
int y = 0;
ImageList_GetIconSize(m_ilIcon, &x, &y);
sizeHdr.cx += x;
sizeHdr.cy = max(sizeHdr.cy, y);
m_ilIcon.SetBkColor(m_crBackground);
if (bDraw)
m_ilIcon.Draw(pDC, 0, CPoint(0,0), ILD_NORMAL);//ILD_TRANSPARENT);
rectClient.left += x;
}
// calc & draw close button
if ( m_unOptions & unSHOW_CLOSE_BUTTON )
{
int nBtnWidth = ::GetSystemMetrics(SM_CXSIZE);
// if something is already in the header (icon) leave space
if ( sizeHdr.cx > 0 )
sizeHdr.cx += nTIP_MARGIN;
sizeHdr.cx += nBtnWidth;
sizeHdr.cy = max(sizeHdr.cy, ::GetSystemMetrics(SM_CYSIZE));
if (bDraw)
pDC->DrawFrameControl(CRect(rectClient.right-nBtnWidth,0,rectClient.right,::GetSystemMetrics(SM_CYSIZE)), DFC_CAPTION, DFCS_CAPTIONCLOSE|DFCS_FLAT);
rectClient.right -= nBtnWidth;
}
// calc title size
CString strTitle;
GetWindowText(strTitle);
if ( !strTitle.IsEmpty() )
{
CFont* pOldFont = pDC->SelectObject(m_pTitleFont);
// if something is already in the header (icon or close button) leave space
if ( sizeHdr.cx > 0 )
sizeHdr.cx += nTIP_MARGIN;
CRect rectTitle(0,0,0,0);
pDC->DrawText(strTitle, &rectTitle, DT_CALCRECT | DT_NOPREFIX | DT_EXPANDTABS | DT_SINGLELINE);
sizeHdr.cx += rectTitle.Width();
sizeHdr.cy = max(sizeHdr.cy, rectTitle.Height());
// draw title
if ( bDraw )
{
pDC->SetBkMode(TRANSPARENT);
pDC->SetTextColor(m_crForeground);
pDC->DrawText(strTitle, &rectClient, DT_CENTER | DT_NOPREFIX | DT_EXPANDTABS | DT_SINGLELINE);
}
// cleanup
pDC->SelectObject(pOldFont);
}
return sizeHdr;
}
// Calculate the dimensions and draw the balloon contents
CSize CBalloonHelp::DrawContent(CDC* pDC, int nTop, bool bDraw)
{
CRect rectContent;
GetAnchorScreenBounds(rectContent);
rectContent.OffsetRect(-rectContent.left, -rectContent.top);
rectContent.top = nTop;
// limit to half screen width
rectContent.right -= rectContent.Width()/2;
// calc size
CFont* pOldFont = pDC->SelectObject(m_pContentFont);
if ( !m_strContent.IsEmpty() )
pDC->DrawText(m_strContent, &rectContent, DT_CALCRECT | DT_LEFT | DT_NOPREFIX | DT_EXPANDTABS | DT_WORDBREAK);
else
rectContent.SetRectEmpty(); // don't want to leave half the screen for empty strings ;)
// draw
if (bDraw)
{
pDC->SetBkMode(TRANSPARENT);
pDC->SetTextColor(m_crForeground);
pDC->DrawText(m_strContent, &rectContent, DT_LEFT | DT_NOPREFIX | DT_WORDBREAK | DT_EXPANDTABS);
}
// cleanup
pDC->SelectObject(pOldFont);
return rectContent.Size();
}
// calculates the client size necessary based on title and content
CSize CBalloonHelp::CalcClientSize()
{
ASSERT(NULL != m_hWnd);
CWindowDC dc(this);
CSize sizeHeader = CalcHeaderSize(&dc);
CSize sizeContent = CalcContentSize(&dc);
return CSize(max(sizeHeader.cx,sizeContent.cx), sizeHeader.cy + nTIP_MARGIN + sizeContent.cy);
}
// calculates the size for the entire window based on content size
CSize CBalloonHelp::CalcWindowSize()
{
CSize size = CalcClientSize();
size.cx += nTIP_MARGIN*2;
size.cy += nTIP_TAIL+nTIP_MARGIN*2;
//size.cx = max(size.cx, nTIP_MARGIN*2+nTIP_TAIL*4);
return size;
}
// this routine calculates the size and position of the window relative
// to it's anchor point, and moves the window accordingly. The region is also
// created and set here.
void CBalloonHelp::PositionWindow()
{
CSize sizeWnd = CalcWindowSize();
CPoint ptTail[3];
CPoint ptTopLeft(0,0);
CPoint ptBottomRight(sizeWnd.cx, sizeWnd.cy);
// force recalculation of desktop
m_screenRect.SetRectEmpty();
switch (GetBalloonQuadrant())
{
case BQ_TOPLEFT:
ptTopLeft.y = nTIP_TAIL;
ptTail[0].x = (sizeWnd.cx-nTIP_TAIL)/4 + nTIP_TAIL;
ptTail[0].y = nTIP_TAIL+1;
ptTail[2].x = (sizeWnd.cx-nTIP_TAIL)/4;
ptTail[2].y = ptTail[0].y;
ptTail[1].x = ptTail[2].x;
ptTail[1].y = 1;
break;
case BQ_TOPRIGHT:
ptTopLeft.y = nTIP_TAIL;
ptTail[0].x = (sizeWnd.cx-nTIP_TAIL)/4*3;
ptTail[0].y = nTIP_TAIL+1;
ptTail[2].x = (sizeWnd.cx-nTIP_TAIL)/4*3 + nTIP_TAIL;
ptTail[2].y = ptTail[0].y;
ptTail[1].x = ptTail[2].x;
ptTail[1].y = 1;
break;
case BQ_BOTTOMLEFT:
ptBottomRight.y = sizeWnd.cy-nTIP_TAIL;
ptTail[0].x = (sizeWnd.cx-nTIP_TAIL)/4 + nTIP_TAIL;
ptTail[0].y = sizeWnd.cy-nTIP_TAIL-2;
ptTail[2].x = (sizeWnd.cx-nTIP_TAIL)/4;
ptTail[2].y = ptTail[0].y;
ptTail[1].x = ptTail[2].x;
ptTail[1].y = sizeWnd.cy-2;
break;
case BQ_BOTTOMRIGHT:
ptBottomRight.y = sizeWnd.cy-nTIP_TAIL;
ptTail[0].x = (sizeWnd.cx-nTIP_TAIL)/4*3;
ptTail[0].y = sizeWnd.cy-nTIP_TAIL-2;
ptTail[2].x = (sizeWnd.cx-nTIP_TAIL)/4*3 + nTIP_TAIL;
ptTail[2].y = ptTail[0].y;
ptTail[1].x = ptTail[2].x;
ptTail[1].y = sizeWnd.cy-2;
break;
}
// adjust for very narrow balloons
if ( ptTail[0].x < nTIP_MARGIN )
ptTail[0].x = nTIP_MARGIN;
if ( ptTail[0].x > sizeWnd.cx - nTIP_MARGIN )
ptTail[0].x = sizeWnd.cx - nTIP_MARGIN;
if ( ptTail[1].x < nTIP_MARGIN )
ptTail[1].x = nTIP_MARGIN;
if ( ptTail[1].x > sizeWnd.cx - nTIP_MARGIN )
ptTail[1].x = sizeWnd.cx - nTIP_MARGIN;
if ( ptTail[2].x < nTIP_MARGIN )
ptTail[2].x = nTIP_MARGIN;
if ( ptTail[2].x > sizeWnd.cx - nTIP_MARGIN )
ptTail[2].x = sizeWnd.cx - nTIP_MARGIN;
// get window position
CPoint ptAnchor = GetAnchorPoint();
CPoint ptOffs(ptAnchor.x - ptTail[1].x, ptAnchor.y - ptTail[1].y);
// adjust position so all is visible
CRect rectScreen;
GetAnchorScreenBounds(rectScreen);
int nAdjustX = 0;
int nAdjustY = 0;
if ( ptOffs.x < rectScreen.left )
nAdjustX = rectScreen.left-ptOffs.x;
else if ( ptOffs.x + sizeWnd.cx >= rectScreen.right )
nAdjustX = rectScreen.right - (ptOffs.x + sizeWnd.cx);
if ( ptOffs.y + nTIP_TAIL < rectScreen.top )
nAdjustY = rectScreen.top - (ptOffs.y + nTIP_TAIL);
else if ( ptOffs.y + sizeWnd.cy - nTIP_TAIL >= rectScreen.bottom )
nAdjustY = rectScreen.bottom - (ptOffs.y + sizeWnd.cy - nTIP_TAIL);
// reposition tail
// uncomment two commented lines below to move entire tail
// instead of just anchor point
//ptTail[0].x -= nAdjustX;
ptTail[1].x -= nAdjustX;
//ptTail[2].x -= nAdjustX;
ptOffs.x += nAdjustX;
ptOffs.y += nAdjustY;
// place window
MoveWindow(ptOffs.x, ptOffs.y, sizeWnd.cx, sizeWnd.cy, TRUE);
// apply region
CRgn region;
CRgn regionRound;
CRgn regionComplete;
region.CreatePolygonRgn(&ptTail[0], 3, ALTERNATE);
regionRound.CreateRoundRectRgn(ptTopLeft.x,ptTopLeft.y,ptBottomRight.x,ptBottomRight.y,nTIP_MARGIN*3,nTIP_MARGIN*3);
regionComplete.CreateRectRgn(0,0,1,1);
regionComplete.CombineRgn(®ion, ®ionRound, RGN_OR);
if ( NULL == m_rgnComplete.m_hObject )
m_rgnComplete.CreateRectRgn(0,0,1,1);
if ( !m_rgnComplete.EqualRgn(®ionComplete) )
{
m_rgnComplete.CopyRgn(®ionComplete);
SetWindowRgn((HRGN)regionComplete.Detach(), TRUE);
// There is a bug with layered windows and NC changes in Win2k
// As a workaround, redraw the entire window if the NC area changed.
// Changing the anchor point is the ONLY thing that will change the
// position of the client area relative to the window during normal
// operation.
RedrawWindow(NULL, NULL, RDW_UPDATENOW| RDW_ERASE | RDW_INVALIDATE | RDW_FRAME | RDW_ALLCHILDREN);
}
}
// Returns the class ATOM for a BalloonHelp control. Registers the class first, if necessary.
ATOM CBalloonHelp::GetClassAtom(BOOL bShadowed)
{
if ( NULL == s_ClassAtom )
{
WNDCLASSEX wcx;
// Fill in the window class structure with parameters
// that describe the main window.
wcx.cbSize = sizeof(wcx); // size of structure
wcx.style = CS_DBLCLKS|CS_SAVEBITS
|CS_DROPSHADOW; // notify of double clicks, save screen under, show dropshadow
wcx.lpfnWndProc = AfxWndProc; // points to window procedure
wcx.cbClsExtra = 0; // no extra class memory
wcx.cbWndExtra = 0; // no extra window memory
wcx.hInstance = AfxGetInstanceHandle(); // handle to instance
wcx.hIcon = NULL; // no app. icon
wcx.hCursor = LoadCursor(NULL,IDC_ARROW); // predefined arrow
wcx.hbrBackground = ::GetSysColorBrush(COLOR_WINDOW); // no background brush
wcx.lpszMenuName = NULL; // no menu resource
wcx.lpszClassName = "BalloonHelpClassDS"; // name of window class
wcx.hIconSm = NULL; // no small class icon
// Register the window class (this may not work if dropshadows are not supported)
s_ClassAtomShadowed = RegisterClassEx(&wcx);
// Register shadow-less class
wcx.style &= ~CS_DROPSHADOW;
wcx.lpszClassName = "BalloonHelpClass";
s_ClassAtom = RegisterClassEx(&wcx);
}
if ( bShadowed && NULL != s_ClassAtomShadowed )
return s_ClassAtomShadowed;
return s_ClassAtom;
}
// Displays the balloon on the screen, performing fade-in if enabled.
void CBalloonHelp::ShowBalloon(void)
{
ShowWindow(SW_SHOWNOACTIVATE);
if ( !(m_unOptions&unDELAY_CLOSE) )
SetTimeout(m_unTimeout); // start close timer
}
// Removes the balloon from the screen, performing the fade-out if enabled
void CBalloonHelp::HideBalloon(void)
{
if ( m_unOptions&unDELAY_CLOSE )
{
m_unOptions &= ~(unDELAY_CLOSE|unCLOSE_ON_ANYTHING); // close only via timer or button
SetTimeout(m_unTimeout); // start close timer
return;
}
ShowWindow( SW_HIDE );
if ( GetCapture() == this )
ReleaseCapture();
DestroyWindow();