-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathwindowFunctions.cpp
More file actions
1829 lines (1516 loc) · 53.2 KB
/
windowFunctions.cpp
File metadata and controls
1829 lines (1516 loc) · 53.2 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
/*!
* \file windowFunctions.cpp
* \brief Methods related to manipulation of windows or information retrieval.
*
* This file contains the informative (debug/error/status) information for DCX dll routines.
*
* \author William Nguyen ( twig at genscripts dot net )
* \version 1.0
*
* \b Revisions
*
* © ScriptsDB.org - 2007-2008
*/
#include "defines.h"
#include "Dcx.h"
/// <summary>
/// Finds an owner of a dialog, used with styles.
/// </summary>
/// <param name="data"></param>
/// <param name="defaultWnd"></param>
/// <returns>Returns the owner HWND</returns>
HWND FindOwner(const TString& data, const gsl::not_null<HWND>& defaultWnd)
{
if (data.empty())
return defaultWnd;
const auto i = data.findtok(TEXT("owner"), 1);
// 'owner' token not found in data
if (i == 0)
return defaultWnd;
// if there is a token after 'owner'
if (i < data.numtok())
{
const auto tsHwnd(data.gettok(gsl::narrow_cast<int>(i) + 1));
// if it is a number (HWND) passed
if (const auto wnd = to_hwnd(tsHwnd.to_<size_t>()); wnd)
return wnd;
// try to retrieve dialog hwnd from name
if (const auto wnd = GetHwndFromString(tsHwnd); wnd)
return wnd;
}
return defaultWnd;
}
std::optional<HWND> FindOwner(const TString& data)
{
if (data.empty())
return {};
const auto i = data.findtok(TEXT("owner"), 1);
// 'owner' token not found in data
if (i == 0)
return {};
// if there is a token after 'owner'
if (i < data.numtok())
{
const auto tsHwnd(data.gettok(gsl::narrow_cast<int>(i) + 1));
// if it is a number (HWND) passed
if (const auto wnd = to_hwnd(tsHwnd.to_<size_t>()); wnd)
return wnd;
// try to retrieve dialog hwnd from name
if (const auto wnd = GetHwndFromString(tsHwnd); wnd)
return wnd;
}
return {};
//if (auto wnd = FindOwner(data, nullptr); wnd)
// return wnd;
//return {};
}
/*!
* \brief Retrieves a HWND from the string.
*/
HWND GetHwndFromString(const TString& str)
{
if (str.empty())
return nullptr;
//return GetHwndFromString(str.to_chr());
// test code to allow docking by hwnd (wtf its only 3 lines)
if (const auto hwnd = to_hwnd(str.to_<size_t>()); IsWindow(hwnd))
return hwnd;
TString tsRes;
//mIRCLinker::tsEvalex(tsRes, TEXT("$dialog(%s).hwnd"), str.to_chr());
mIRCLinker::eval(tsRes, TEXT("$dialog(%).hwnd"), str);
return to_hwnd(tsRes.to_<size_t>());
}
/*!
* \brief Retireves a HWND from the string.
*/
//HWND GetHwndFromString(gsl::not_null<const TCHAR *> str) {
//
// // test code to allow docking by hwnd (wtf its only 3 lines)
// auto hwnd = (HWND)dcx_atoi64(str);
// if (IsWindow(hwnd))
// return hwnd;
//
// TCHAR res[20];
// res[0] = 0;
// mIRCLinker::evalex( res, Dcx::countof(res), TEXT("$dialog(%s).hwnd"), str);
//
// return (HWND) dcx_atoi(res);
//}
// Removes window style to a window
void RemStyles(const HWND hwnd, const int parm, const long RemStyles) noexcept
{
auto Styles = gsl::narrow_cast<DWORD>(GetWindowLong(hwnd, parm));
Styles &= ~RemStyles;
SetWindowLong(hwnd, parm, gsl::narrow_cast<LONG>(Styles));
}
// Adds window styles to a window
void AddStyles(const HWND hwnd, const int parm, const long AddStyles) noexcept
{
auto Styles = gsl::narrow_cast<DWORD>(GetWindowLong(hwnd, parm));
Styles |= AddStyles;
SetWindowLong(hwnd, parm, gsl::narrow_cast<LONG>(Styles));
}
/***************************************************/
/* David Gallardo Llopis */
/* */
/* Based on the code in the book */
/* PROGRAMACION AVANZADA EN WINDOWS 2000 */
/* at McGraw-Hill (c) 2000 */
/* by */
/* J. Pascual, F. Charte, M.J. Segarra, */
/* J.A. Clavijo, A. de Antonio. */
/* */
/* The code in this book is based on an original */
/* code by Jean-Edouard Lachand-Robert */
/***************************************************/
//HRGN BitmapRegion(HBITMAP hBitmap,COLORREF cTransparentColor,BOOL bIsTransparent)
//{
// // We create an empty region
// HRGN hRegion = nullptr;
//
// // If the passed bitmap is nullptr, go away!
// if (hBitmap == nullptr)
// return nullptr;
//
// // Computation of the bitmap size
// BITMAP bmBitmap;
//
// if (GetObject(hBitmap, sizeof(bmBitmap), &bmBitmap) == 0)
// return nullptr;
//
// // We create a memory context for working with the bitmap
// // The memory context is compatible with the display context (screen)
// HDC hMemDC = CreateCompatibleDC(nullptr);
//
// // If no context is created, go away, too!
// if (hMemDC == nullptr)
// return nullptr;
//
// Auto(DeleteDC(hMemDC));
//
// // In order to make the space for the region, we
// // create a bitmap with 32bit depth color and with the
// // size of the loaded bitmap!
// //BITMAPINFOHEADER RGB32BITSBITMAPINFO=
// //{
// // sizeof(BITMAPINFOHEADER),
// // bmBitmap.bmWidth,
// // bmBitmap.bmHeight,
// // 1,32,BI_RGB,0,0,0,0,0
// //};
// BITMAPINFO RGB32BITSBITMAPINFO = {
// sizeof(BITMAPINFOHEADER),
// bmBitmap.bmWidth,
// bmBitmap.bmHeight,
// 1,32,BI_RGB,0,0,0,0,0,0
// };
//
// // Here is the pointer to the bitmap data
// VOID *pBits;
//
// // With the previous information, we create the new bitmap!
// HBITMAP hNewBitmap;
// hNewBitmap = CreateDIBSection(hMemDC, (BITMAPINFO *)&RGB32BITSBITMAPINFO, DIB_RGB_COLORS, &pBits, nullptr, 0);
//
// // If the creation process succeded...
// if (hNewBitmap == nullptr)
// throw Dcx::dcxException("BitmapRegion() - CreateDIBSection() Failed: Invalid Parameter");
//
// Auto(DeleteBitmap(hNewBitmap));
//
// GdiFlush();
// // We select the bitmap onto the created memory context
// // and then we store the previosly selected bitmap on this context!
// HBITMAP hPrevBmp = (HBITMAP)SelectObject(hMemDC, hNewBitmap);
//
// Auto(SelectObject(hMemDC, hPrevBmp));
// // We create another device context compatible with the first!
// HDC hDC = CreateCompatibleDC(hMemDC);
//
// // If success...
// if (hDC != nullptr)
// {
// // We compute the number of bytes per row that the bitmap contains, rounding to 32 bit-multiples
// BITMAP bmNewBitmap;
//
// GetObject(hNewBitmap, sizeof(bmNewBitmap), &bmNewBitmap);
//
// while (bmNewBitmap.bmWidthBytes % 4)
// bmNewBitmap.bmWidthBytes++;
//
// // Copy of the original bitmap on the memory context!
// HBITMAP hPrevBmpOrg = (HBITMAP)SelectObject(hDC, hBitmap);
// BitBlt(hMemDC, 0, 0, bmBitmap.bmWidth, bmBitmap.bmHeight, hDC, 0, 0, SRCCOPY);
//
// // In order to optimize the code, we don't call the GDI each time we
// // find a transparent pixel. We use a RGN_DATA structure were we store
// // consecutive rectangles, until we have a large amount of them and then we crete
// // the composed region with ExtCreateRgn(), combining it with the main region.
// // Then we begin again initializing the RGN_DATA structure and doing another
// // iteration, until the entire bitmap is analyzed.
//
// // Also, in order to not saturate the Windows API with calls for reserving
// // memory, we wait until NUMRECT rectangles are stores in order to claim
// // for another NUMRECT memory space!
//#define NUMRECT 100
// DWORD maxRect = NUMRECT;
//
// // We create the memory data
// HANDLE hData = GlobalAlloc(GMEM_MOVEABLE, sizeof(RGNDATAHEADER) + (sizeof(RECT)*maxRect));
//
// if (hData != nullptr) {
// RGNDATA *pData = (RGNDATA*)GlobalLock(hData);
// pData->rdh.dwSize = sizeof(RGNDATAHEADER);
// pData->rdh.iType = RDH_RECTANGLES;
// pData->rdh.nCount = pData->rdh.nRgnSize = 0;
// SetRect(&pData->rdh.rcBound, MAXLONG, MAXLONG, 0, 0);
//
// // We study each pixel on the bitmap...
// BYTE *Pixeles = (BYTE*)bmNewBitmap.bmBits + (bmNewBitmap.bmHeight - 1)*bmNewBitmap.bmWidthBytes;
//
// // Main loop
// for (int Row = 0; Row < bmBitmap.bmHeight; Row++)
// {
// // Horizontal loop
// for (int Column = 0; Column < bmBitmap.bmWidth; Column++)
// {
// // We optimized searching for adjacent transparent pixels!
// int Xo = Column;
// RGBQUAD *Pixel = (RGBQUAD*)Pixeles + Column;
//
// while (Column < bmBitmap.bmWidth)
// {
// BOOL bInRange = FALSE;
//
// // If the color is that indicated as transparent...
// if (Pixel->rgbRed == GetRValue(cTransparentColor) &&
// Pixel->rgbGreen == GetGValue((cTransparentColor & 0xFFFF)) &&
// Pixel->rgbBlue == GetBValue(cTransparentColor))
// bInRange = TRUE;
//
// if ((bIsTransparent) && (bInRange))
// break;
//
// if ((!bIsTransparent) && (!bInRange))
// break;
//
// Pixel++;
// Column++;
// } // while (Column < bm.bmWidth)
//
// if (Column > Xo)
// {
// // We add the rectangle (Xo,Row),(Column,Row+1) to the region
//
// // If the number of rectangles is greater then NUMRECT, we claim
// // another pack of NUMRECT memory places!
// if (pData->rdh.nCount >= maxRect)
// {
// GlobalUnlock(hData);
// maxRect += NUMRECT;
// hData = GlobalReAlloc(hData, sizeof(RGNDATAHEADER) + (sizeof(RECT)*maxRect), GMEM_MOVEABLE);
// pData = (RGNDATA *)GlobalLock(hData);
// } // if (pData->rdh.nCount>=maxRect)
//
// RECT *pRect = (RECT*)&pData->Buffer;
// SetRect(&pRect[pData->rdh.nCount], Xo, Row, Column, Row + 1);
//
// if (Xo<pData->rdh.rcBound.left)
// pData->rdh.rcBound.left = Xo;
//
// if (Row<pData->rdh.rcBound.top)
// pData->rdh.rcBound.top = Row;
//
// if (Column>pData->rdh.rcBound.right)
// pData->rdh.rcBound.right = Column;
//
// if (Row + 1>pData->rdh.rcBound.bottom)
// pData->rdh.rcBound.bottom = Row + 1;
//
// pData->rdh.nCount++;
//
// // In Win95/08 there is a limitation on the maximum number of
// // rectangles a RGN_DATA can store (aprox. 4500), so we call
// // the API for a creation and combination with the main region
// // each 2000 rectangles. This is a good optimization, because
// // instead of calling the routines for combining for each new
// // rectangle found, we call them every 2000 rectangles!!!
// if (pData->rdh.nCount == 2000)
// {
// HRGN hNewRegion = ExtCreateRegion(nullptr, sizeof(RGNDATAHEADER) + (sizeof(RECT) * maxRect), pData);
// if (hNewRegion != nullptr) {
// // Si ya existe la región principal,sumamos la nueva,
// // si no,entonces de momento la principal coincide con
// // la nueva región.
// if (hRegion) {
// CombineRgn(hRegion, hRegion, hNewRegion, RGN_OR);
// DeleteObject(hNewRegion);
// }
// else
// hRegion = hNewRegion;
//
//
// } // if (hNewRegion != nullptr)
// // Volvemos a comenzar la suma de rectángulos
// pData->rdh.nCount = 0;
// SetRect(&pData->rdh.rcBound, MAXLONG, MAXLONG, 0, 0);
// } // if(pData->rdh.nCount==2000)
//
// } // if (Column > Xo)
// } // for (int Column ...)
//
// // Nueva Row. Lo del negativo se debe a que el bitmap está invertido
// // verticalmente.
// Pixeles -= bmNewBitmap.bmWidthBytes;
//
// } // for (int Row...)
//
// if (pData->rdh.nCount > 0) {
// // Una vez finalizado el proceso,procedemos a la fusión de la
// // región remanente desde la última fusión hasta el final
// HRGN hNewRegion = ExtCreateRegion(nullptr, sizeof(RGNDATAHEADER) + (sizeof(RECT)*maxRect), pData);
//
// if (hNewRegion != nullptr)
// {
// // If the main region does already exist, we add the new one,
// if (hRegion)
// {
// CombineRgn(hRegion, hRegion, hNewRegion, RGN_OR);
// DeleteObject(hNewRegion);
// }
// else
// // if not, we consider the new one to be the main region at first!
// hRegion = hNewRegion;
// } // if(hNewRegion != nullptr)
// } // if (pData->rdh.nCount > 0)
// // We free the allocated memory and the rest of used ressources
// GlobalUnlock(hData);
// GlobalFree(hData);
// SelectObject(hDC, hPrevBmpOrg); // don't del prev bitmap, as its our supplied one.
// DeleteDC(hDC);
// } // if (hData != nullptr)
//
// }// if (hDC)
//
// DeleteBitmap(SelectObject(hMemDC, hPrevBmp)); // del prev bitmap as it's the DIB Section
// return hRegion;
//}
HRGN BitmapRegion(HBITMAP hBitmap, const COLORREF cTransparentColor, const bool bIsTransparent)
{
#if DCX_USE_WRAPPERS
// If the passed bitmap is nullptr, go away!
if (!hBitmap)
return nullptr;
// We create an empty region
HRGN hRegion = nullptr;
// Computation of the bitmap size
BITMAP bmBitmap{};
if (GetObject(hBitmap, sizeof(bmBitmap), &bmBitmap) == 0)
throw Dcx::dcxException("BitmapRegion() - Unable to get bitmap info");
// We create a memory context for working with the bitmap
// The memory context is compatible with the display context (screen)
const Dcx::dcxHDCResource hMemDC((HDC)nullptr);
// In order to make the space for the region, we
// create a bitmap with 32bit depth color and with the
// size of the loaded bitmap!
const BITMAPINFO RGB32BITSBITMAPINFO{
sizeof(BITMAPINFOHEADER),
bmBitmap.bmWidth,
bmBitmap.bmHeight,
1, 32, BI_RGB, 0, 0, 0, 0, 0, 0
};
// Here is the pointer to the bitmap data
VOID* pBits = nullptr;
// With the previous information, we create the new bitmap!
const Dcx::dcxBitmapResource hNewBitmap(hMemDC, &RGB32BITSBITMAPINFO, DIB_RGB_COLORS, &pBits, nullptr, 0);
GdiFlush();
// We select the bitmap onto the created memory context
// and then we store the previosly selected bitmap on this context!
const auto hPrevBmp = SelectObject(hMemDC, hNewBitmap);
Auto(SelectObject(hMemDC, hPrevBmp));
// We compute the number of bytes per row that the bitmap contains, rounding to 32 bit-multiples
BITMAP bmNewBitmap{};
if (GetObject(hNewBitmap, sizeof(bmNewBitmap), &bmNewBitmap) == 0)
throw Dcx::dcxException("BitmapRegion() - Unable to get bitmap info");
while (bmNewBitmap.bmWidthBytes % 4)
bmNewBitmap.bmWidthBytes++;
// We create another device context compatible with the first!
// Copy of the original bitmap on the memory context!
const Dcx::dcxHDCBitmapResource hDC((HDC)hMemDC, hBitmap);
BitBlt(hMemDC, 0, 0, bmBitmap.bmWidth, bmBitmap.bmHeight, hDC, 0, 0, SRCCOPY);
#else
// We create an empty region
HRGN hRegion = nullptr;
// If the passed bitmap is nullptr, go away!
if (!hBitmap)
return nullptr;
// Computation of the bitmap size
BITMAP bmBitmap;
if (GetObject(hBitmap, sizeof(bmBitmap), &bmBitmap) == 0)
throw Dcx::dcxException("BitmapRegion() - Unable to get bitmap info");
// We create a memory context for working with the bitmap
// The memory context is compatible with the display context (screen)
const HDC hMemDC = CreateCompatibleDC(nullptr);
// If no context is created, go away, too!
if (!hMemDC)
throw Dcx::dcxException("BitmapRegion() - Unable to create DC");
Auto(DeleteDC(hMemDC));
// In order to make the space for the region, we
// create a bitmap with 32bit depth color and with the
// size of the loaded bitmap!
//BITMAPINFOHEADER RGB32BITSBITMAPINFO=
//{
// sizeof(BITMAPINFOHEADER),
// bmBitmap.bmWidth,
// bmBitmap.bmHeight,
// 1,32,BI_RGB,0,0,0,0,0
//};
const BITMAPINFO RGB32BITSBITMAPINFO = {
sizeof(BITMAPINFOHEADER),
bmBitmap.bmWidth,
bmBitmap.bmHeight,
1, 32, BI_RGB, 0, 0, 0, 0, 0, 0
};
// Here is the pointer to the bitmap data
VOID* pBits;
// With the previous information, we create the new bitmap!
const auto hNewBitmap = CreateDIBSection(hMemDC, (BITMAPINFO*)&RGB32BITSBITMAPINFO, DIB_RGB_COLORS, &pBits, nullptr, 0);
// If the creation process succeded...
if (!hNewBitmap)
throw Dcx::dcxException("BitmapRegion() - CreateDIBSection() Failed: Invalid Parameter");
Auto(DeleteObject(hNewBitmap));
GdiFlush();
// We select the bitmap onto the created memory context
// and then we store the previosly selected bitmap on this context!
const auto hPrevBmp = SelectObject(hMemDC, hNewBitmap);
Auto(SelectObject(hMemDC, hPrevBmp));
// We create another device context compatible with the first!
HDC hDC = CreateCompatibleDC(hMemDC);
// If success...
if (hDC == nullptr)
throw Dcx::dcxException("BitmapRegion() - Unable to create DC");
Auto(DeleteDC(hDC));
// We compute the number of bytes per row that the bitmap contains, rounding to 32 bit-multiples
BITMAP bmNewBitmap;
if (GetObject(hNewBitmap, sizeof(bmNewBitmap), &bmNewBitmap) == 0)
throw Dcx::dcxException("BitmapRegion() - Unable to get bitmap info");
while (bmNewBitmap.bmWidthBytes % 4)
bmNewBitmap.bmWidthBytes++;
// Copy of the original bitmap on the memory context!
const auto hPrevBmpOrg = SelectObject(hDC, hBitmap);
Auto(SelectObject(hDC, hPrevBmpOrg));
BitBlt(hMemDC, 0, 0, bmBitmap.bmWidth, bmBitmap.bmHeight, hDC, 0, 0, SRCCOPY);
#endif
// In order to optimize the code, we don't call the GDI each time we
// find a transparent pixel. We use a RGN_DATA structure were we store
// consecutive rectangles, until we have a large amount of them and then we create
// the composed region with ExtCreateRgn(), combining it with the main region.
// Then we begin again initializing the RGN_DATA structure and doing another
// iteration, until the entire bitmap is analyzed.
// Also, in order to not saturate the Windows API with calls for reserving
// memory, we wait until NUMRECT rectangles are stored in order to claim
// for another NUMRECT memory space!
#define NUMRECT 100U
DWORD maxRect = NUMRECT;
// We create the memory data
auto hData = GlobalAlloc(GMEM_MOVEABLE, sizeof(RGNDATAHEADER) + (sizeof(RECT) * maxRect));
if (!hData)
throw Dcx::dcxException("BitmapRegion() - GlobalAlloc() failed");
Auto(GlobalFree(hData));
auto pData = static_cast<RGNDATA*>(GlobalLock(hData));
if (!pData)
throw Dcx::dcxException("BitmapRegion() - GlobalLock() failed");
Auto(GlobalUnlock(hData));
pData->rdh.dwSize = sizeof(RGNDATAHEADER);
pData->rdh.iType = RDH_RECTANGLES;
pData->rdh.nCount = pData->rdh.nRgnSize = 0;
SetRect(&pData->rdh.rcBound, MAXLONG, MAXLONG, 0, 0);
// We study each pixel on the bitmap...
//auto Pixeles = reinterpret_cast<BYTE*>(bmNewBitmap.bmBits) + (bmNewBitmap.bmHeight - 1) * bmNewBitmap.bmWidthBytes;
auto Pixeles = static_cast<BYTE*>(bmNewBitmap.bmBits) + (bmNewBitmap.bmHeight - 1) * bmNewBitmap.bmWidthBytes;
// Main loop
for (auto Row = 0; Row < bmBitmap.bmHeight; ++Row)
{
// Horizontal loop
for (auto Column = 0; Column < bmBitmap.bmWidth; ++Column)
{
// We optimized searching for adjacent transparent pixels!
const int Xo = Column;
const auto* Pixel = (RGBQUAD*)Pixeles + Column;
while (Column < bmBitmap.bmWidth)
{
bool bInRange = false;
// If the color is that indicated as transparent...
if (Pixel->rgbRed == GetRValue(cTransparentColor) &&
Pixel->rgbGreen == GetGValue((cTransparentColor & 0xFFFF)) &&
Pixel->rgbBlue == GetBValue(cTransparentColor))
bInRange = true;
if ((bIsTransparent) && (bInRange))
break;
if ((!bIsTransparent) && (!bInRange))
break;
++Pixel;
++Column;
} // while (Column < bm.bmWidth)
if (Column > Xo)
{
// We add the rectangle (Xo,Row),(Column,Row+1) to the region
// If the number of rectangles is greater then NUMRECT, we claim
// another pack of NUMRECT memory places!
if (pData->rdh.nCount >= maxRect)
{
GlobalUnlock(hData);
maxRect += NUMRECT;
hData = GlobalReAlloc(hData, sizeof(RGNDATAHEADER) + (sizeof(RECT) * maxRect), GMEM_MOVEABLE);
pData = static_cast<RGNDATA*>(GlobalLock(hData));
} // if (pData->rdh.nCount>=maxRect)
const auto pRect = (RECT*)&pData->Buffer[0];
SetRect(&pRect[pData->rdh.nCount], Xo, Row, Column, Row + 1);
if (Xo < pData->rdh.rcBound.left)
pData->rdh.rcBound.left = Xo;
if (Row < pData->rdh.rcBound.top)
pData->rdh.rcBound.top = Row;
if (Column > pData->rdh.rcBound.right)
pData->rdh.rcBound.right = Column;
if (Row + 1 > pData->rdh.rcBound.bottom)
pData->rdh.rcBound.bottom = Row + 1;
++pData->rdh.nCount;
// In Win95/08 there is a limitation on the maximum number of
// rectangles a RGN_DATA can store (aprox. 4500), so we call
// the API for a creation and combination with the main region
// each 2000 rectangles. This is a good optimization, because
// instead of calling the routines for combining for each new
// rectangle found, we call them every 2000 rectangles!!!
if (pData->rdh.nCount == 2000)
{
if (auto hNewRegion = ExtCreateRegion(nullptr, sizeof(RGNDATAHEADER) + (sizeof(RECT) * maxRect), pData); hNewRegion)
{
// Si ya existe la región principal,sumamos la nueva,
// si no,entonces de momento la principal coincide con
// la nueva región.
if (hRegion)
{
CombineRgn(hRegion, hRegion, hNewRegion, RGN_OR);
DeleteObject(hNewRegion);
}
else
hRegion = hNewRegion;
} // if (hNewRegion != nullptr)
// Volvemos a comenzar la suma de rectángulos
pData->rdh.nCount = 0;
SetRect(&pData->rdh.rcBound, MAXLONG, MAXLONG, 0, 0);
} // if(pData->rdh.nCount==2000)
} // if (Column > Xo)
} // for (int Column ...)
// Nueva Row. Lo del negativo se debe a que el bitmap está invertido
// verticalmente.
Pixeles -= bmNewBitmap.bmWidthBytes;
} // for (int Row...)
if (pData->rdh.nCount > 0)
{
// Una vez finalizado el proceso,procedemos a la fusión de la
// región remanente desde la última fusión hasta el final
if (auto hNewRegion = ExtCreateRegion(nullptr, sizeof(RGNDATAHEADER) + (sizeof(RECT) * maxRect), pData); hNewRegion)
{
// If the main region does already exist, we add the new one,
if (hRegion)
{
CombineRgn(hRegion, hRegion, hNewRegion, RGN_OR);
DeleteObject(hNewRegion);
}
else
// if not, we consider the new one to be the main region at first!
hRegion = hNewRegion;
} // if(hNewRegion != nullptr)
} // if (pData->rdh.nCount > 0)
return hRegion;
}
/*!
* \brief Changes the icon for a window.
*
* throws dcxException on error.
*/
void ChangeHwndIcon(const HWND hwnd, const TString& flags, const int index, TString& filename)
{
if (!hwnd)
throw Dcx::dcxException("ChangeHwndIcon() - Invalid Window");
const XSwitchFlags xflags(flags);
filename.trim();
if (!xflags[TEXT('+')])
throw Dcx::dcxException("ChangeHwndIcon() - Invalid Flags");
HICON iconSmall{ nullptr };
HICON iconLarge{ nullptr };
if (filename == TEXT("none"))
{
dcxSetWindowExStyle(hwnd, WindowExStyle::DialogModalFrame);
//SetClassLongW(hwnd, GCL_HICON, 0);
//SetClassLongW(hwnd, GCL_HICONSM, 0);
iconSmall = Dcx::dcxWindow_SetIcon(hwnd, ICON_SMALL, iconSmall);
iconLarge = Dcx::dcxWindow_SetIcon(hwnd, ICON_BIG, iconLarge);
}
else if (filename == TEXT("default"))
{
if ((!xflags[TEXT('s')]) && (!xflags[TEXT('b')]))
{
iconSmall = Dcx::dcxWindow_SetIcon(hwnd, ICON_SMALL, iconSmall);
iconLarge = Dcx::dcxWindow_SetIcon(hwnd, ICON_BIG, iconLarge);
}
else {
if (xflags[TEXT('s')])
iconSmall = Dcx::dcxWindow_SetIcon(hwnd, ICON_SMALL, iconSmall);
if (xflags[TEXT('b')])
iconLarge = Dcx::dcxWindow_SetIcon(hwnd, ICON_BIG, iconLarge);
}
}
else {
if (!xflags[TEXT('B')])
{
// not base64 data as filename
if (!IsFile(filename))
throw Dcx::dcxException(TEXT("ChangeHwndIcon() - Unable to Access File: %"), filename);
}
// check for +s small icon flag
if (xflags[TEXT('s')])
iconSmall = dcxLoadIcon(index, filename, false, flags);
// check for +b big icon flag
if (xflags[TEXT('b')])
iconLarge = dcxLoadIcon(index, filename, true, flags);
if ((!iconLarge) && (!iconSmall))
{
// No big or small flags, so do both icons.
iconSmall = dcxLoadIcon(index, filename, false, flags);
iconLarge = dcxLoadIcon(index, filename, true, flags);
}
// set the new icons, get back the current icon
if (iconSmall)
iconSmall = Dcx::dcxWindow_SetIcon(hwnd, ICON_SMALL, iconSmall);
if (iconLarge)
iconLarge = Dcx::dcxWindow_SetIcon(hwnd, ICON_BIG, iconLarge);
}
// delete the old icons
if (iconSmall)
DestroyIcon(iconSmall);
if ((iconLarge) && (iconSmall != iconLarge)) // dont delete twice
DestroyIcon(iconLarge);
}
bool GetWindowRectParent(const HWND hwnd, RECT* rcWin) noexcept
{
if ((!hwnd) || (!rcWin))
return false;
#if DCX_USE_WRAPPERS
const Dcx::dcxWindowRect rc(hwnd, GetParent(hwnd));
return (CopyRect(rcWin, &rc) != FALSE);
#else
if (GetWindowRect(hwnd, rcWin))
{
SetLastError(0U);
MapWindowRect(nullptr, GetParent(hwnd), rcWin.get());
return (GetLastError() == 0U);
}
return false;
#endif
}
/// <summary>
/// draws a line using a pre set pen/colour.
/// </summary>
/// <param name="hdc">- The HDC to draw on.</param>
/// <param name="x1">- The x start of the line.</param>
/// <param name="y1">- The y start of the line.</param>
/// <param name="x2">- The x end of the line.</param>
/// <param name="y2">- The y end of the line.</param>
/// <returns></returns>
void dcxDrawLine(HDC hdc, LONG x1, LONG y1, LONG x2, LONG y2) noexcept
{
if (!hdc)
return;
MoveToEx(hdc, x1, y1, nullptr);
LineTo(hdc, x2, y2);
}
/// <summary>
/// draws a edge using a specified colour.
/// falls back on DrawEdge() if it fails to allocate a pen or an invalid colour is supplied.
/// </summary>
/// <param name="hdc">- The HDC to draw on.</param>
/// <param name="rc">- The bounding rect for the edge. (atm only drawing on the right side of the rect is supported)</param>
/// <param name="clr">- The colour to use for the edge.</param>
/// <returns></returns>
void dcxDrawEdge(HDC hdc, const LPRECT rc, COLORREF clr) noexcept
{
if (!hdc)
return;
if (clr != CLR_INVALID)
{
if (auto hPen = CreatePen(PS_SOLID, 5, clr); hPen)
{
Auto(DeleteObject(hPen));
const auto oldPen = Dcx::dcxSelectObject<HPEN>(hdc, hPen);
Auto(Dcx::dcxSelectObject<HPEN>(hdc, oldPen));
dcxDrawLine(hdc, rc->right, rc->top, rc->right, rc->bottom);
return;
}
}
DrawEdge(hdc, rc, EDGE_BUMP, BF_RIGHT);
}
/// <summary>
/// draws a border using a specified colour.
/// </summary>
/// <param name="hdc">- The HDC to draw on.</param>
/// <param name="lprc">- The bounding rect for the border.</param>
/// <param name="dwBorder">- The type of border to draw, (BF_LEFT BF_RIGHT BF_TOP BF_BOTTOM)</param>
/// <param name="clr">- The colour to use.</param>
/// <returns></returns>
void dcxDrawBorder(HDC hdc, LPCRECT lprc, DWORD dwBorder, COLORREF clr) noexcept
{
if (!hdc || !lprc || clr == CLR_INVALID)
return;
//LOGPEN oLogPen{};
//
//auto hOld = SelectObject(hdc, GetStockObject(BLACK_PEN));
//GetObject(hOld, sizeof(oLogPen), &oLogPen);
//oLogPen.lopnColor = clr;
//
////Don't attempt to delete stock object
//SelectObject(hdc, CreatePenIndirect(&oLogPen));
//
//if (dwBorder & BF_LEFT)
// dcxDrawLine(hdc, lprc->left, lprc->top, lprc->left, lprc->bottom);
//if (dwBorder & BF_TOP)
// dcxDrawLine(hdc, lprc->left, lprc->top, lprc->right, lprc->top);
//if (dwBorder & BF_RIGHT)
// dcxDrawLine(hdc, lprc->right, lprc->top, lprc->right, lprc->bottom);
//if (dwBorder & BF_BOTTOM)
// dcxDrawLine(hdc, lprc->left, lprc->bottom, lprc->right, lprc->bottom);
//
//DeleteObject(SelectObject(hdc, hOld));
if (auto hPen = CreatePen(PS_SOLID, 1, clr); hPen)
{
Auto(DeleteObject(hPen));
const auto oldPen = Dcx::dcxSelectObject<HPEN>(hdc, hPen);
Auto(Dcx::dcxSelectObject<HPEN>(hdc, oldPen));
if (dwBorder & BF_LEFT)
dcxDrawLine(hdc, lprc->left, lprc->top, lprc->left, lprc->bottom);
if (dwBorder & BF_TOP)
dcxDrawLine(hdc, lprc->left, lprc->top, lprc->right, lprc->top);
if (dwBorder & BF_RIGHT)
dcxDrawLine(hdc, lprc->right, lprc->top, lprc->right, lprc->bottom);
if (dwBorder & BF_BOTTOM)
dcxDrawLine(hdc, lprc->left, lprc->bottom, lprc->right, lprc->bottom);
}
}
void dcxDrawArrow(_In_ HDC hdc, _In_ LPCRECT lprc, _In_ COLORREF clr, _In_ dcxArrowFlags eFlags) noexcept
{
if (!hdc || !lprc)
return;
const auto hPen = CreatePen(PS_SOLID, 1, clr);
if (!hPen)
return;
Auto(DeleteObject(hPen));
const auto hOldPen = SelectObject(hdc, hPen);
Auto(SelectObject(hdc, hOldPen));
const auto x = lprc->right - XPMI_SUBARROW_WIDTH;
const auto y = ((lprc->bottom + lprc->top) / 2) - (XPMI_SUBARROW_HEIGHT / 2);
//MoveToEx(hdc, x, y, nullptr);
//LineTo(hdc, x + 1, y);
//MoveToEx(hdc, x, y + 1, nullptr);
//LineTo(hdc, x + 2, y + 1);
//MoveToEx(hdc, x, y + 2, nullptr);
//LineTo(hdc, x + 3, y + 2);
//MoveToEx(hdc, x, y + 3, nullptr);
//LineTo(hdc, x + 4, y + 3);
//MoveToEx(hdc, x, y + 4, nullptr);
//LineTo(hdc, x + 5, y + 4);
//MoveToEx(hdc, x, y + 5, nullptr);
//LineTo(hdc, x + 4, y + 5);
//MoveToEx(hdc, x, y + 6, nullptr);
//LineTo(hdc, x + 3, y + 6);
//MoveToEx(hdc, x, y + 7, nullptr);
//LineTo(hdc, x + 2, y + 7);
//MoveToEx(hdc, x, y + 8, nullptr);
//LineTo(hdc, x + 1, y + 8);
// Draw the arrow using PolyDraw
const POINT pts[]{
{x, y},
{x + 1, y},
{x, y + 1},
{x + 2, y + 1},
{x, y + 2},
{x + 3, y + 2},
{x, y + 3},
{x + 4, y + 3},
{x, y + 4},
{x + 5, y + 4},
{x, y + 5},
{x + 4, y + 5},
{x, y + 6},
{x + 3, y + 6},
{x, y + 7},
{x + 2, y + 7},
{x, y + 8},
{x + 1, y + 8}
};
const static BYTE flags[]{
PT_MOVETO,
PT_LINETO,
PT_MOVETO,
PT_LINETO,
PT_MOVETO,
PT_LINETO,
PT_MOVETO,
PT_LINETO,
PT_MOVETO,
PT_LINETO,
PT_MOVETO,
PT_LINETO,
PT_MOVETO,
PT_LINETO,
PT_MOVETO,
PT_LINETO,
PT_MOVETO,
PT_LINETO
};
PolyDraw(hdc, &pts[0], &flags[0], gsl::narrow_cast<int>(std::size(pts)));
}
namespace
{
COLORREF getCheckBoxBkgColour(const clrCheckBox* lpcol, DWORD dState) noexcept
{
if (!lpcol)
return RGB(0, 0, 0);
if (dcx_testflag(dState, CDIS_DISABLED))
return lpcol->m_clrDisabledBackground;
if (dcx_testflag(dState, CDIS_HOT))
return lpcol->m_clrHotBackground;
return lpcol->m_clrBackground;
}
COLORREF getCheckBoxFrameColour(const clrCheckBox* lpcol, DWORD dState) noexcept
{
if (!lpcol)
return RGB(0, 0, 0);
if (dcx_testflag(dState, CDIS_DISABLED))
return lpcol->m_clrDisabledFrame;
if (dcx_testflag(dState, CDIS_HOT))
return lpcol->m_clrHotFrame;