forked from NTDLS/NSWFL
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNSWFL_Windows.Cpp
1048 lines (829 loc) · 31.4 KB
/
NSWFL_Windows.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
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
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Copyright © NetworkDLS 2023, All rights reserved
//
// 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.
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#ifndef _NSWFL_WINDOWS_CPP_
#define _NSWFL_WINDOWS_CPP_
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#include "NSWFL.H"
#include <shlobj.h>
#ifdef _USE_GLOBAL_MEMPOOL
extern NSWFL::Memory::MemoryPool *pMem; //pMem must be defined and initalized elsewhere.
#endif
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
namespace NSWFL {
namespace Windows {
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void CopyToClipboard(char *str)
{
OpenClipboard(NULL);
EmptyClipboard();
size_t iLength = strlen(str);
HGLOBAL hg = GlobalAlloc(GMEM_MOVEABLE, iLength + 1);
if (!hg)
{
CloseClipboard();
return;
}
memcpy(GlobalLock(hg), str, iLength + 1);
GlobalUnlock(hg);
SetClipboardData(CF_TEXT, hg);
CloseClipboard();
GlobalFree(hg);
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
bool EnableDialogSystemMenu(HWND hWnd, bool bState)
{
if (bState)
{
return(SetWindowLong(hWnd, GWL_STYLE, GetWindowLong(hWnd, GWL_STYLE) & WS_SYSMENU) != 0);
}
else {
return(SetWindowLong(hWnd, GWL_STYLE, GetWindowLong(hWnd, GWL_STYLE) & ~WS_SYSMENU) != 0);
}
return false;
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
bool EnableDialogCloseButton(HWND hWnd, bool bState)
{
HMENU hMenu;
UINT dwExtra = bState ? MF_ENABLED : (MF_DISABLED | MF_GRAYED);
if ((hMenu = GetSystemMenu(hWnd, FALSE)) == NULL)
{
return FALSE;
}
return EnableMenuItem(hMenu, SC_CLOSE, MF_BYCOMMAND | dwExtra) != -1;
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void CopyTextBoxToClipboard(HWND hWnd)
{
SendMessage(hWnd, EM_SETSEL, (WPARAM)0, (LPARAM)-1);
SendMessage(hWnd, WM_COPY, (WPARAM)0, (LPARAM)0);
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void TextBoxInsert(HWND hWnd, const char *sInBuf)
{
LRESULT iLen = SendMessage(hWnd, WM_GETTEXTLENGTH, 0, 0);
SendMessage(hWnd, EM_REPLACESEL, 0, (LPARAM)(LPSTR)sInBuf);
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void TextBoxWrite(HWND hWnd, const char *sInBuf)
{
LRESULT iLen = SendMessage(hWnd, WM_GETTEXTLENGTH, 0, 0);
SendMessage(hWnd, EM_SETSEL, (WPARAM)iLen, (LPARAM)iLen);
SendMessage(hWnd, EM_REPLACESEL, 0, (LPARAM)(LPSTR)sInBuf);
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void MsgBox(const char *sValue)
{
MessageBox(GetActiveWindow(), sValue, "Message", MB_TASKMODAL);
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
int MsgBoxF(HWND hWnd, const char *sCaption, DWORD dwFlags, const char *sFormat, ...)
{
va_list ArgList;
va_start(ArgList, sFormat);
int iMemoryRequired = _vscprintf(sFormat, ArgList);
#ifdef _USE_GLOBAL_MEMPOOL
char *sBuf = (char *)pMem->Allocate(sizeof(char), iMemoryRequired + 1);
#else
char *sBuf = (char *)calloc(sizeof(char), iMemoryRequired + 1);
#endif
int iSz = _vsprintf_s_l(sBuf, iMemoryRequired + 1, sFormat, NULL, ArgList);
va_end(ArgList);
int iResult = MessageBox(hWnd, sBuf, sCaption, dwFlags);
#ifdef _USE_GLOBAL_MEMPOOL
pMem->Free(sBuf);
#else
free(sBuf);
#endif
return iResult;
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
int MsgBoxF(HWND hWnd, const char *sCaption, const char *sFormat, ...)
{
va_list ArgList;
va_start(ArgList, sFormat);
int iMemoryRequired = _vscprintf(sFormat, ArgList);
#ifdef _USE_GLOBAL_MEMPOOL
char *sBuf = (char *)pMem->Allocate(sizeof(char), iMemoryRequired + 1);
#else
char *sBuf = (char *)calloc(sizeof(char), iMemoryRequired + 1);
#endif
int iSz = _vsprintf_s_l(sBuf, iMemoryRequired + 1, sFormat, NULL, ArgList);
va_end(ArgList);
int iResult = MessageBox(hWnd, sBuf, sCaption, MB_TASKMODAL);
#ifdef _USE_GLOBAL_MEMPOOL
pMem->Free(sBuf);
#else
free(sBuf);
#endif
return iResult;
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
int MsgBoxF(HWND hWnd, const char *sFormat, ...)
{
va_list ArgList;
va_start(ArgList, sFormat);
int iMemoryRequired = _vscprintf(sFormat, ArgList);
#ifdef _USE_GLOBAL_MEMPOOL
char *sBuf = (char *)pMem->Allocate(sizeof(char), iMemoryRequired + 1);
#else
char *sBuf = (char *)calloc(sizeof(char), iMemoryRequired + 1);
#endif
int iSz = _vsprintf_s_l(sBuf, iMemoryRequired + 1, sFormat, NULL, ArgList);
va_end(ArgList);
int iResult = MessageBox(hWnd, sBuf, "Message", MB_TASKMODAL);
#ifdef _USE_GLOBAL_MEMPOOL
pMem->Free(sBuf);
#else
free(sBuf);
#endif
return iResult;
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
int MsgBoxF(const char *sFormat, ...)
{
va_list ArgList;
va_start(ArgList, sFormat);
int iMemoryRequired = _vscprintf(sFormat, ArgList);
#ifdef _USE_GLOBAL_MEMPOOL
char *sBuf = (char *)pMem->Allocate(sizeof(char), iMemoryRequired + 1);
#else
char *sBuf = (char *)calloc(sizeof(char), iMemoryRequired + 1);
#endif
int iSz = _vsprintf_s_l(sBuf, iMemoryRequired + 1, sFormat, NULL, ArgList);
va_end(ArgList);
int iResult = MessageBox(GetActiveWindow(), sBuf, "Message", MB_TASKMODAL);
#ifdef _USE_GLOBAL_MEMPOOL
pMem->Free(sBuf);
#else
free(sBuf);
#endif
return iResult;
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void MsgBox(const __int64 i64Value)
{
char sValue[64];
sprintf_s(sValue, sizeof(sValue), "%I64i", i64Value);
MessageBox(GetActiveWindow(), sValue, "Message", MB_TASKMODAL);
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void MsgBox(const int iValue)
{
char sValue[64];
sprintf_s(sValue, sizeof(sValue), "%d", iValue);
MessageBox(GetActiveWindow(), sValue, "Message", MB_TASKMODAL);
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void MsgBox(const float fValue)
{
char sValue[1024];
sprintf_s(sValue, sizeof(sValue), "%f", fValue);
MessageBox(GetActiveWindow(), sValue, "Message", MB_TASKMODAL);
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void MsgBox(const double dValue)
{
char sValue[1024];
sprintf_s(sValue, sizeof(sValue), "%f", dValue);
MessageBox(GetActiveWindow(), sValue, "Message", MB_TASKMODAL);
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void ErrorMessage(HWND hWnd, const char *sInBuf)
{
HWND hOwner = NULL;
if (hWnd == NULL)
{
hOwner = GetActiveWindow();
}
else hOwner = hWnd;
MessageBox(hOwner, sInBuf, "Error", MB_ICONHAND + MB_SYSTEMMODAL + MB_TASKMODAL);
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void AlertMessage(HWND hWnd, const char *sInBuf, const char *sTitle)
{
HWND hOwner = NULL;
if (hWnd == NULL)
{
hOwner = GetActiveWindow();
}
else hOwner = hWnd;
MessageBox(hOwner, sInBuf, sTitle, MB_SYSTEMMODAL + MB_TASKMODAL);
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
int Get_TextLength(HWND hWnd)
{
return GetWindowTextLength(hWnd);
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
double Get_Double(HWND hWnd)
{
char sBuf[16];
NSWFL::Windows::Get_Text(hWnd, sBuf, sizeof(sBuf));
return NSWFL::Conversion::dFormattedValue(sBuf);
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
float Get_Float(HWND hWnd)
{
return (float)Get_Double(hWnd);
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
__int64 Get_Int64(HWND hWnd)
{
char sBuf[16];
NSWFL::Windows::Get_Text(hWnd, sBuf, sizeof(sBuf));
return NSWFL::Conversion::i64FormattedValue(sBuf);
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
long Get_Long(HWND hWnd)
{
char sBuf[16];
NSWFL::Windows::Get_Text(hWnd, sBuf, sizeof(sBuf));
return (long)NSWFL::Conversion::i64FormattedValue(sBuf);
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
int Get_Int(HWND hWnd)
{
return (int)NSWFL::Windows::Get_Long(hWnd);
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void Set_LongFormatted(HWND hWnd, long lInput)
{
char sBuf[64];
NSWFL::Conversion::FormatInteger(sBuf, sizeof(sBuf), lInput);
NSWFL::Windows::Set_Text(hWnd, sBuf);
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void Set_Long(HWND hWnd, long lInput)
{
char sBuf[64];
sprintf_s(sBuf, sizeof(sBuf), "%d", lInput);
NSWFL::Windows::Set_Text(hWnd, sBuf);
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void Set_Double(HWND hWnd, double dInput, int iDecimals)
{
char sFmt[64];
sprintf_s(sFmt, sizeof(sFmt), "%%.%df", iDecimals);
char sBuf[256];
sprintf_s(sBuf, sizeof(sBuf), sFmt, dInput);
NSWFL::Windows::Set_Text(hWnd, sBuf);
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void Set_Float(HWND hWnd, float fInput, int iDecimals)
{
char sFmt[64];
sprintf_s(sFmt, sizeof(sFmt), "%%.%df", iDecimals);
char sBuf[256];
sprintf_s(sBuf, sizeof(sBuf), sFmt, fInput);
NSWFL::Windows::Set_Text(hWnd, sBuf);
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void Set_DoubleFormatted(HWND hWnd, double dInput, int iDecimals)
{
char sNumber[256];
NSWFL::Conversion::FormatDouble(sNumber, sizeof(sNumber), dInput, iDecimals);
NSWFL::Windows::Set_Text(hWnd, sNumber);
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void Set_FloatFormatted(HWND hWnd, float fInput, int iDecimals)
{
char sNumber[256];
NSWFL::Conversion::FormatFloat(sNumber, sizeof(sNumber), fInput, iDecimals);
NSWFL::Windows::Set_Text(hWnd, sNumber);
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void Set_Int(HWND hWnd, int iInput)
{
NSWFL::Windows::Set_Long(hWnd, (DWORD)iInput);
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void Set_Text(HWND hWnd, const char *sInBuf)
{
SendMessage(hWnd, (UINT)WM_SETTEXT, (WPARAM)0, (LPARAM)sInBuf);
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
int Set_TextF(HWND hWnd, const char *sFormat, ...)
{
va_list ArgList;
va_start(ArgList, sFormat);
int iMemoryRequired = _vscprintf(sFormat, ArgList);
#ifdef _USE_GLOBAL_MEMPOOL
char *sBuf = (char *)pMem->Allocate(sizeof(char), iMemoryRequired + 1);
#else
char *sBuf = (char *)calloc(sizeof(char), iMemoryRequired + 1);
#endif
int iSz = _vsprintf_s_l(sBuf, iMemoryRequired + 1, sFormat, NULL, ArgList);
va_end(ArgList);
Set_Text(hWnd, sBuf);
#ifdef _USE_GLOBAL_MEMPOOL
pMem->Free(sBuf);
#else
free(sBuf);
#endif
return iSz;
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//The pointer returned through [sOutBuf] must be freed externally.
int Get_Text(HWND hWnd, char *&sOutBuf)
{
int iLength = NSWFL::Windows::Get_TextLength(hWnd);
if (iLength > 0)
{
#ifdef _USE_GLOBAL_MEMPOOL
sOutBuf = (char *)pMem->Allocate(sizeof(char), iLength + 1);
#else
sOutBuf = (char *)calloc(sizeof(char), iLength + 1);
#endif
return GetWindowText(hWnd, sOutBuf, iLength + 1);
}
return 0;
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
int Get_Text(HWND hWnd, char *sOutBuf, int iMaxSize)
{
int iLen = GetWindowTextLength(hWnd);
if (iLen >= iMaxSize)
{
return iLen;
}
iLen = GetWindowText(hWnd, sOutBuf, iMaxSize);
sOutBuf[iLen] = '\0';
return iLen;
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void CenterOverOwner(HWND hWnd)
{
NSWFL::Windows::CenterOverWindow(hWnd, GetParent(hWnd));
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void CenterOverWindow(HWND hWnd, HWND hOtherWindow)
{
if (hOtherWindow == NULL)
{
NSWFL::Windows::CenterWindow(hWnd);
return;
}
int iTaksbarOffsetX = 0;
int iTaksbarOffsetY = 0;
int iScreenX = GetSystemMetrics(SM_CXSCREEN) - 10;
int iScreenY = (GetSystemMetrics(SM_CYSCREEN) - GetSystemMetrics(SM_CYCAPTION)) - 10;
MONITORINFO MonitorInfo;
memset(&MonitorInfo, 0, sizeof(MonitorInfo));
HMONITOR hMonitor = MonitorFromWindow(hWnd, MONITOR_DEFAULTTONEAREST);
if (hMonitor)
{
MonitorInfo.cbSize = sizeof(MonitorInfo);
if (GetMonitorInfo(hMonitor, &MonitorInfo))
{
iScreenX = MonitorInfo.rcMonitor.right - MonitorInfo.rcMonitor.left;
iScreenY = MonitorInfo.rcMonitor.bottom - MonitorInfo.rcMonitor.top;
//Figure the height/width of the taskbar.
int iTaskScreenX = GetSystemMetrics(SM_CXSCREEN);
int iTaskScreenY = (GetSystemMetrics(SM_CYSCREEN) - GetSystemMetrics(SM_CYCAPTION));
int iTaskFullScreenX = GetSystemMetrics(SM_CXFULLSCREEN);
int iTaskFullScreenY = (GetSystemMetrics(SM_CYFULLSCREEN) - GetSystemMetrics(SM_CYCAPTION));
iTaksbarOffsetX = abs(iTaskScreenX - iTaskFullScreenX);
iTaksbarOffsetY = abs(iTaskScreenY - iTaskFullScreenY);
}
else {
memset(&MonitorInfo, 0, sizeof(MonitorInfo));
}
}
RECT recOther;
memset(&recOther, 0, sizeof(recOther));
GetWindowRect(hOtherWindow, &recOther);
RECT recDlg;
memset(&recDlg, 0, sizeof(recDlg));
GetWindowRect(hWnd, &recDlg);
int iX = ((recOther.right - ((recOther.right - recOther.left) / 2)) - ((recDlg.right - recDlg.left) / 2));
int iY = ((recOther.bottom - ((recOther.bottom - recOther.top) / 2)) - ((recDlg.bottom - recDlg.top) / 2));
if (iX - MonitorInfo.rcMonitor.left > (iScreenX - (recDlg.right - recDlg.left)) - iTaksbarOffsetX)
{
iX = ((MonitorInfo.rcMonitor.left + (iScreenX - (recDlg.right - recDlg.left))) - iTaksbarOffsetX) - 10;
}
else if (iX - MonitorInfo.rcMonitor.left < 10)
{
iX = (MonitorInfo.rcMonitor.left + 10);
}
if (iY - MonitorInfo.rcMonitor.top > (iScreenY - (recDlg.bottom - recDlg.top)) - iTaksbarOffsetY)
{
iY = (MonitorInfo.rcMonitor.top + (iScreenY - (recDlg.bottom - recDlg.top)) - iTaksbarOffsetY) - 10;
}
else if (iY - MonitorInfo.rcMonitor.top < 10)
{
iY = MonitorInfo.rcMonitor.top + 10;
}
SetWindowPos(hWnd, NULL, iX, iY, 0, 0, SWP_NOSIZE | SWP_NOZORDER);
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void CenterWindow(HWND hWnd)
{
POINT MousePos;
memset(&MousePos, 0, sizeof(MousePos));
int iScreenX = GetSystemMetrics(SM_CXSCREEN);
int iScreenY = GetSystemMetrics(SM_CYSCREEN);
int MonitorOffsetX = 0;
int MonitorOffsetY = 0;
if (GetCursorPos(&MousePos))
{
MONITORINFO MonitorInfo;
HMONITOR hMonitor = MonitorFromPoint(MousePos, MONITOR_DEFAULTTONEAREST);
if (hMonitor)
{
memset(&MonitorInfo, 0, sizeof(MonitorInfo));
MonitorInfo.cbSize = sizeof(MonitorInfo);
if (GetMonitorInfo(hMonitor, &MonitorInfo))
{
MonitorOffsetX = MonitorInfo.rcMonitor.left;
MonitorOffsetY = MonitorInfo.rcMonitor.top;
iScreenX = MonitorInfo.rcMonitor.right - MonitorInfo.rcMonitor.left;
iScreenY = MonitorInfo.rcMonitor.bottom - MonitorInfo.rcMonitor.top;
}
}
}
RECT wRect;
memset(&wRect, 0, sizeof(wRect));
GetWindowRect(hWnd, &wRect);
DWORD x = ((iScreenX - (wRect.right - wRect.left)) / 2) + MonitorOffsetX;
DWORD y = ((iScreenY - (wRect.bottom - wRect.top + GetSystemMetrics(SM_CYCAPTION))) / 2) + MonitorOffsetY;
SetWindowPos(hWnd, NULL, x, y, 0, 0, SWP_NOSIZE | SWP_NOZORDER);
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
LRESULT CALLBACK Set_Color(int iFGColor, int iBKColor, WPARAM wParam)
{
static HBRUSH ReUsableBrush;
DeleteObject(ReUsableBrush);
ReUsableBrush = CreateSolidBrush(iBKColor);
SetTextColor((HDC)wParam, iFGColor);
SetBkColor((HDC)wParam, iBKColor);
return (LRESULT)ReUsableBrush;
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/*
This function returns HWND of the child window under the mouse cursor.
The window must be visible and uncovered.
*/
HWND GetWindowUnderPoint(void)
{
POINT Pointer;
memset(&Pointer, 0, sizeof(Pointer));
if (GetCursorPos(&Pointer))
{
return WindowFromPoint(Pointer);
}
return NULL;
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/*
This function returns TRUE if the mouse cursor is over the window specified by HWND.
The window must be visible and uncovered.
*/
bool IsMouseOverWindow(HWND hWnd)
{
POINT Pointer;
memset(&Pointer, 0, sizeof(Pointer));
GetCursorPos(&Pointer);
return(WindowFromPoint(Pointer) == hWnd);
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/*
This function returns TRUE if the mouse cursor is over the window specified by HWND.
This function does not care if the window is covered, enabled or visible.
*/
bool IsMouseOverHwnd(HWND hWnd)
{
RECT Rectangle;
POINT Pointer;
memset(&Rectangle, 0, sizeof(Rectangle));
memset(&Pointer, 0, sizeof(Pointer));
GetWindowRect(hWnd, &Rectangle);
GetCursorPos(&Pointer);
return(PtInRect(&Rectangle, Pointer) > 0);
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
int OSFileDialog(HWND hOwner, char *sInOutFileName, int iMaxOutBufSz, char *sInDfltExt,
char *sInTitle, char *sInFilters, char *sInDfltDir,
NSWFL::Windows::OpenOrSaveDialogType iDlgType, DWORD dwFlags)
{
char sFileName[MAX_PATH];
int iRetVal = 0;
char *sCurrDir = NULL;
OPENFILENAME OFN;
if ((iDlgType != OFD) && (iDlgType != SFD))
{
return 0;
}
strcpy_s(sFileName, sizeof(sFileName), sInOutFileName);
OFN.lpstrCustomFilter = NULL;
OFN.nMaxCustFilter = 0;
OFN.lpstrFileTitle = NULL;
OFN.nMaxFileTitle = 0;
if (!sInFilters)
{
OFN.lpstrFilter = "All-Files\0*.*\0Text-Files\0*.txt\0\0";
}
else OFN.lpstrFilter = sInFilters;
if (!sInDfltExt)
{
OFN.lpstrDefExt = "txt";
}
else OFN.lpstrDefExt = sInDfltExt;
if (!sInTitle)
{
if (iDlgType == OFD)
{
OFN.lpstrTitle = "Select file to open";
}
else if (iDlgType == SFD)
{
OFN.lpstrTitle = "Save file as";
}
}
else OFN.lpstrTitle = sInTitle;
if (!sInDfltDir)
{
#ifdef _USE_GLOBAL_MEMPOOL
if ((sCurrDir = (char *)pMem->Allocate(sizeof(char), MAX_PATH + 1)))
#else
if ((sCurrDir = (char *)calloc(sizeof(char), MAX_PATH + 1)))
#endif
{
if (NSWFL::File::Get_CurrentDirectory(sCurrDir, MAX_PATH))
{
OFN.lpstrInitialDir = sCurrDir;
}
}
}
else OFN.lpstrInitialDir = sInDfltDir;
OFN.lStructSize = sizeof(OFN);
OFN.hwndOwner = hOwner;
OFN.nFilterIndex = 1;
OFN.lpstrFile = sFileName;
OFN.nMaxFile = sizeof(sFileName);
if (dwFlags == 0)
{
OFN.Flags = OFN_EXPLORER + OFN_LONGNAMES + OFN_PATHMUSTEXIST;
}
else OFN.Flags = dwFlags;
if (iDlgType == OFD)
{
iRetVal = GetOpenFileName(&OFN);
}
else if (iDlgType == SFD)
{
iRetVal = GetSaveFileName(&OFN);
}
strcpy_s(sInOutFileName, iMaxOutBufSz, sFileName);
if (!sInDfltDir && sCurrDir)
{
#ifdef _USE_GLOBAL_MEMPOOL
pMem->Free(sCurrDir);
#else
free(sCurrDir);
#endif
}
return iRetVal;
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
int CALLBACK BrowseDirectoryCallbackProc(HWND hwnd, UINT uMsg, LPARAM lParam, LPARAM lpData)
{
if (uMsg == BFFM_INITIALIZED)
{
SendMessage(hwnd, BFFM_SETSELECTION, (WPARAM)TRUE, lpData);
}
return 0;
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
LPITEMIDLIST ConvertPathToLpItemIdList(const char *pszPath)
{
LPITEMIDLIST pidl = NULL;
LPSHELLFOLDER pDesktopFolder = NULL;
OLECHAR olePath[MAX_PATH];
ULONG chEaten;
HRESULT hr;
if (SUCCEEDED(SHGetDesktopFolder(&pDesktopFolder)))
{
MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED, pszPath, -1,
olePath, MAX_PATH);
hr = pDesktopFolder->ParseDisplayName(NULL, NULL,
olePath, &chEaten, &pidl, NULL);
pDesktopFolder->Release();
return pidl;
}
return NULL;
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//Assumes that sOutBuf is at least MAX_PATH, because SHGetPathFromIDList does not check for buffer size.
bool BrowseDirectory(HWND hOwner, char *sInTitle,
unsigned long ulFlags, char *sOutBuf, int iMaxOutBufSz)
{
return NSWFL::Windows::BrowseDirectory(hOwner, sInTitle, ulFlags, sOutBuf, iMaxOutBufSz, NULL, NULL);
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//Assumes that sOutBuf is at least MAX_PATH, because SHGetPathFromIDList does not check for buffer size.
bool BrowseDirectory(HWND hOwner, char *sInTitle, unsigned long ulFlags, char *sOutBuf, int iMaxOutBufSz, char *sInitialDirectory)
{
return BrowseDirectory(hOwner, sInTitle, ulFlags, sOutBuf, iMaxOutBufSz, sInitialDirectory, NULL);
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//Assumes that sOutBuf is at least MAX_PATH, because SHGetPathFromIDList does not check for buffer size.
/*
BrowseDirectory(hWnd, "Select a folder.", BIF_RETURNONLYFSDIRS|BIF_USENEWUI, sDeleteFolder, sizeof(sDeleteFolder));
*/
bool BrowseDirectory(HWND hOwner, char *sInTitle, unsigned long ulFlags, char *sOutBuf, int iMaxOutBufSz, char *sInitialDirectory, const char *sRoot)
{
LPMALLOC pMalloc;
LPITEMIDLIST lpItemIDList;
BROWSEINFO browseInfo;
bool bResult = false;
if (iMaxOutBufSz < MAX_PATH)
{
return false;
}
memset(&pMalloc, 0, sizeof(pMalloc));
memset(&browseInfo, 0, sizeof(browseInfo));
memset(&lpItemIDList, 0, sizeof(lpItemIDList));
if (S_OK != SHGetMalloc(&pMalloc))
{
return false;
}
memset(&browseInfo, 0, sizeof(BROWSEINFO));
browseInfo.hwndOwner = hOwner;
browseInfo.pszDisplayName = sOutBuf;
browseInfo.lpszTitle = sInTitle;
browseInfo.ulFlags = ulFlags;
if (!sRoot)
{
browseInfo.pidlRoot = ConvertPathToLpItemIdList(sRoot);
}
if (sInitialDirectory)
{
browseInfo.lParam = (LPARAM)sInitialDirectory;
browseInfo.lpfn = BrowseDirectoryCallbackProc;
}
lpItemIDList = SHBrowseForFolder(&browseInfo);
if (lpItemIDList != NULL)
{
strcpy_s(sOutBuf, iMaxOutBufSz, "");
if (SHGetPathFromIDList(lpItemIDList, sOutBuf))
{
if (strlen(sOutBuf) > 0)
{
bResult = true;
}
}
pMalloc->Free(lpItemIDList);
}
pMalloc->Release();
return bResult;
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
HFONT MakeFont(char *sFontName, int iPointSize)
{
HDC hDC;
int CyPixels = 0;
hDC = GetDC(HWND_DESKTOP);
CyPixels = GetDeviceCaps(hDC, LOGPIXELSY);
ReleaseDC(HWND_DESKTOP, hDC);
int iiPointSize = (iPointSize * CyPixels) / 72;
return CreateFont(0 - iiPointSize, 0, 0, 0, FW_NORMAL, 0, 0, 0,
ANSI_CHARSET, OUT_TT_PRECIS, CLIP_DEFAULT_PRECIS,
DEFAULT_QUALITY, FF_DONTCARE, sFontName);
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void SetFont(HWND hWnd, HFONT hFont)
{
SendMessage(hWnd, WM_SETFONT, (WPARAM)hFont, (LPARAM)TRUE);
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//This function will wait for the window specified by hWnd to become visible,
// it will then set the window to the foreground and return.
bool WaitOnWindow(HWND &hWnd)
{
while (!hWnd) //Wait on valid handle.
{
Sleep(1);
}
while (!IsWindowVisible(hWnd)) //Wait on window to be visible.
{
Sleep(1);
}
if (IsWindow(hWnd))
{
SetForegroundWindow(hWnd);
return true;
}
return false;
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//This function will wait for a specified amount of time for the
// window specified by hWnd to become visible, it will then set
// the window to the foreground and return.
bool WaitOnWindow(HWND &hWnd, int iMilliSeconds)
{
while (!hWnd) //Wait on valid handle.
{
Sleep(1);
}
int iWaited = 0;
while (!IsWindowVisible(hWnd))
{
if (iMilliSeconds > 0)
{
if (iWaited++ >= iMilliSeconds)
{
return false;
}
}
Sleep(1);
}
if (IsWindow(hWnd))
{
SetForegroundWindow(hWnd);
return true;
}
return false;
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void ShowWindowInTaskbar(HWND hWnd, bool bShow)
{
DWORD dwCurrentStyle = GetWindowLong(hWnd, GWL_EXSTYLE);
if (bShow)
{
if (!(dwCurrentStyle &WS_EX_APPWINDOW))
{
SetWindowLong(hWnd, GWL_EXSTYLE, dwCurrentStyle | WS_EX_APPWINDOW);
}
}
else {
if ((dwCurrentStyle &WS_EX_APPWINDOW))
{
SetWindowLong(hWnd, GWL_EXSTYLE, (GetWindowLong(hWnd, GWL_EXSTYLE) & ~WS_EX_APPWINDOW));
}
}
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
bool SelectComboItem(HWND hCombo, const char *sText, int iMatchLength)
{
LRESULT iIndex = -1;
if (iMatchLength <= 0)
{
iIndex = SendMessage(hCombo, (UINT)CB_FINDSTRING, (WPARAM)-1, (LPARAM)sText);
}
else {
#ifdef _USE_GLOBAL_MEMPOOL
char *sShortText = (char *)pMem->Allocate(iMatchLength + 1, sizeof(char));
#else
char *sShortText = (char *)calloc(iMatchLength + 1, sizeof(char));
#endif
strncpy_s(sShortText, iMatchLength + 1, sText, iMatchLength);
iIndex = SendMessage(hCombo, (UINT)CB_FINDSTRING, (WPARAM)-1, (LPARAM)sShortText);
#ifdef _USE_GLOBAL_MEMPOOL
pMem->Free(sShortText);
#else
free(sShortText);