-
Notifications
You must be signed in to change notification settings - Fork 2
/
frmMain.twin
1354 lines (1229 loc) · 57.8 KB
/
frmMain.twin
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
[Description("Memory List Manager Main Form")]
[FormDesignerId("DBB73BBF-63FC-45F2-8BD5-C5047D3F5ECA")]
[PredeclaredId]
Class frmMain
' ***********************************************************************
' * *
' * Memory List Manager Version 2.1.4 *
' * GUI Form *
' * *
' * See modMemListMgr.twin for full details. *
' * *
' ***********************************************************************
Private bExitBug As Boolean 'temp workaround for not exiting when minimized
'signals to use PostQuitMessage from Form_Unload
Private ldbg As Boolean, sdbg As String
Private Const sSizeFmt_byte = "0 bytes"
Private Const sSizeFmt_kb = "#,##0 KB"
Private Const sSizeFmt_mb = "#,##0.00 MB"
Private Const sSizeFmt_gb = "#,##0.00 GB"
Private Const sSizeFmt_tb = "#,##0.00 TB"
Private Const sSizeFmt_pb = "#,##0.00 PB"
Private Const PAGE_SIZE As LongLong = &H1000
Private Const szWelcome = "Welcome to Memory List Manager v2.1"
Private Const szHelpTitle = "Memory List Manager"
Private Const szHelpHeader = "Memory List Manager v2.1"
Private Const szHelpMessage = "This is a small utility to free up memory held in standby and in caches " & _
"by Jon Johnson based code from SystemInformer. Developed in twinBASIC. " & vbcrlf & vbcrlf & _
"For the readme, filing bug reports, original source, and more utilities, visit the project's GitHub repository:" & vbcrlf & _
"<a href=""https://github.com/fafalone/MemListMgr"">https://github.com/fafalone/MemListMgr</a>"
Private Const szHelpFooter = "Copyright ©2024 Jon Johnson. Licensed under the MIT license. See code or repository for more information."
Private Const szHelpIconRes = "101"
Private Const szPrivError = "Insufficient privileges to execute memory operation."
Private Const szBadVersion = "This command is not supported on your version of Windows."
Private Const szUnknown = "Unknown"
Private Const szBadOS = "This program requires Windows XP or later."
Private Const szWow64Warn = "It's strongly recommended you run the 64bit version of this app on 64bit Windows. Continue anyway?"
Private Const szCSBOk = "Successfully cleared standby memory."
Private Const szCSBErr = "Failed to clear standby memory, "
Private Const szCSBLPOk = "Successfully cleared low priority standby memory."
Private Const szCSBLPErr = "Failed to clear low priority standby memory, "
Private Const szFMOk = "Successfully flushed modified pages."
Private Const szFMErr = "Failed to flush modified pages, "
Private Const szWSOk = "Successfully emptied working sets."
Private Const szWSErr = "Failed to empty working sets, "
Private Const szCombined = "Combined %1 (%2 pages)"
Private Const szCmbOk = "Succesfully combined pages."
Private Const szCmbErr = "Failed to combine pages, "
Private Const szRegOk = "Successfully flushed registry cache."
Private Const szRegErr = "Failed to flush registry cache, "
Private Const szFileOk = "Successully flushed %1 in system file cache."
Private Const szFileOk2 = "Successully flushed system file cache."
Private Const szFileErr = "Failed to flush system file cache, "
Private Const szBadAR = "Auto-refresh interval must be greater than zero."
Private Const szAOOk = "Successfully set Auto-apply options."
Private Const szAOErr = "Invalid Auto-apply options. At least one memory type, percent 1-100, and interval > 1 required."
Private Const szAOExec = "Automatically optimized memory at %d."
Private Const szAAOk = "Successfully completed auto-optimize routine."
Private Const szAAErr = "Failed to complete auto-optimize routine, "
Private Const szNA = "N/A"
Private Const szOpErr = "Operation failed, "
Private cxyIcon As Long, cxyIconBtn As Long, cxyIconTray As Long
Private m_ScaleX As Single, m_ScaleY As Single
Private mDPI As Long
Private mSubclass As Boolean
Private mInTray As Boolean
Private mAOPct As Long
Private mAOInt As Long
Private mAOUseM As Boolean
Private mAOUseS As Boolean
Private Const WM_MLMNOTIFYICON = WM_USER + &H123
Private tNIGuid As UUID
Private clrCustom(15) As Long
Private mInMenu As Boolean
Private Sub SetStatus(szMessage As String)
txtStatus.Text = szMessage
txtStatus.ToolTipText = szMessage
End Sub
Private Sub OnLoad() Handles Form.Load
' SetWindowLong Me.hWnd, GWL_HWNDPARENT, 0
If NTDDI_VERSION < NTDDI_WINXP Then
MsgBox szBadOS, vbCritical + vbOKOnly, App.Title
End
End If
If NTDDI_VERSION < NTDDI_VISTA Then
'No V4 notification icon for tray; disable
Label32.Visible = False
optMin(0).Visible = False
optMin(1).Visible = False
End If
LoadSavedSettings
cmdAuto.ToolTipText = szAutoOptTT
SetStatus szWelcome
'SetStatus "Using notify GUID " & GUIDToString(tNIGuid)
'Temp fix for tB bug, add min button back:
Dim dwStyle As WindowStyles = CLng(GetWindowLong(Me.hWnd, GWL_STYLE))
dwStyle = dwStyle Or WS_MINIMIZEBOX
SetWindowLong Me.hWnd, GWL_STYLE, dwStyle
Dim bWow64 As BOOL
IsWow64Process GetCurrentProcess(), bWow64
If bWow64 Then
Dim r As VbMsgBoxResult = MsgBox(szWow64Warn, vbExclamation + vbYesNo, App.Title)
If r = vbNo Then End
End If
Dim hDC As LongPtr
hDC = GetDC(0&)
mDPI = GetDeviceCaps(hDC, LOGPIXELSX)
m_ScaleX = mDPI / 96
m_ScaleY = GetDeviceCaps(hDC, LOGPIXELSY) / 96
Select Case m_ScaleX
Case Is <= 1: cxyIcon = 24
Case Is <= 1.25: cxyIcon = 32
Case Is <= 1.5: cxyIcon = 48
Case Else: cxyIcon = 64
End Select
ReleaseDC 0&, hDC
InitButtonIcons
UpdateMemoryInfo
If mSubclass = False Then
Subclass2 Me.hWnd, AddressOf F1WndProc, Me.hWnd
mSubclass = True
End If
End Sub
Private Sub HideMemoryBar()
picMem.Visible = False
lblKeyU.Visible = False
lblKeyDispU.Visible = False
picKeyU.Visible = False
lblKeyM.Visible = False
lblKeyDispM.Visible = False
picKeyM.Visible = False
lblKeyS.Visible = False
lblKeyDIspS.Visible = False
picKeyS.Visible = False
lblKeyF.Visible = False
lblKeyDispF.Visible = False
picKeyF.Visible = False
Frame1.Top -= (78 * m_ScaleY)
Frame2.Top -= (78 * m_ScaleY)
Image1.Top -= (78 * m_ScaleY)
Label23.Top -= (78 * m_ScaleY)
Label1.Top -= (78 * m_ScaleY)
Label24.Top -= (78 * m_ScaleY)
Label32.Top -= (78 * m_ScaleY)
cmdAbout.Top -= (78 * m_ScaleY)
cmdAuto.Top -= (78 * m_ScaleY)
cmdExit.Top -= (78 * m_ScaleY)
cmdEmptyStandby.Top -= (78 * m_ScaleY)
cmdEmptyLPStandby.Top -= (78 * m_ScaleY)
cmdEmptyWS.Top -= (78 * m_ScaleY)
cmdCombine.Top -= (78 * m_ScaleY)
cmdFlushFileCache.Top -= (78 * m_ScaleY)
cmdFlushModified.Top -= (78 * m_ScaleY)
cmdFlushRegCache.Top -= (78 * m_ScaleY)
cmdAutoOpts.Top -= (78 * m_ScaleY)
cmdUpdate.Top -= (78 * m_ScaleY)
chkToggleBar.Top -= (78 * m_ScaleY)
chkAutoRefresh.Top -= (78 * m_ScaleY)
chkAAO.Top -= (78 * m_ScaleY)
optMin(0).Top -= (78 * m_ScaleY)
optMin(1).Top -= (78 * m_ScaleY)
txtAutoRefreshTime.Top -= (78 * m_ScaleY)
Me.Height -= ((78 * m_ScaleY) * Screen.TwipsPerPixelY)
Label24.Refresh
End Sub
Private Sub UnhideMemoryBar()
If Frame1.Top > (20 * m_ScaleY) Then Exit Sub 'tb Bug::This sub fires on startup, so nothing has been moved up or hidden
picMem.Visible = True
lblKeyU.Visible = True
lblKeyDispU.Visible = True
picKeyU.Visible = True
lblKeyM.Visible = True
lblKeyDispM.Visible = True
picKeyM.Visible = True
lblKeyS.Visible = True
lblKeyDIspS.Visible = True
picKeyS.Visible = True
lblKeyF.Visible = True
lblKeyDispF.Visible = True
picKeyF.Visible = True
Frame1.Top += (78 * m_ScaleY)
Frame2.Top += (78 * m_ScaleY)
Image1.Top += (78 * m_ScaleY)
Label23.Top += (78 * m_ScaleY)
Label1.Top += (78 * m_ScaleY)
Label24.Top += (78 * m_ScaleY)
Label32.Top += (78 * m_ScaleY)
cmdAbout.Top += (78 * m_ScaleY)
cmdAuto.Top += (78 * m_ScaleY)
cmdExit.Top += (78 * m_ScaleY)
cmdEmptyStandby.Top += (78 * m_ScaleY)
cmdEmptyLPStandby.Top += (78 * m_ScaleY)
cmdEmptyWS.Top += (78 * m_ScaleY)
cmdCombine.Top += (78 * m_ScaleY)
cmdFlushFileCache.Top += (78 * m_ScaleY)
cmdFlushModified.Top += (78 * m_ScaleY)
cmdFlushRegCache.Top += (78 * m_ScaleY)
cmdAutoOpts.Top += (78 * m_ScaleY)
cmdUpdate.Top += (78 * m_ScaleY)
chkToggleBar.Top += (78 * m_ScaleY)
chkAutoRefresh.Top += (78 * m_ScaleY)
chkAAO.Top += (78 * m_ScaleY)
optMin(0).Top += (78 * m_ScaleY)
optMin(1).Top += (78 * m_ScaleY)
txtAutoRefreshTime.Top += (78 * m_ScaleY)
Me.Height += ((78 * m_ScaleY) * Screen.TwipsPerPixelY)
Label24.Refresh
End Sub
Private Sub OnToggleMemoryBar() Handles chkToggleBar.Click
Debug.Print "OnToggleMemoryBar vis=" & picMem.Visible & ",top=" & Frame1.Top
If chkToggleBar.Value = vbUnchecked Then
HideMemoryBar
Else
UnhideMemoryBar
End If
End Sub
Private Sub OnSetAutoRefresh() Handles chkAutoRefresh.Click
If chkAutoRefresh.Value = vbChecked Then
Timer1.Interval = CLng(txtAutoRefreshTime.Text) * 1000
Timer1.Enabled = True
Else
Timer1.Enabled = False
End If
End Sub
Private Sub OnUpdateInterval() Handles txtAutoRefreshTime.Change
Dim lInt As Long = CLng(txtAutoRefreshTime.Text) * 1000&
If lInt > 0 Then
Timer1.Interval = lInt
Else
Beep
SetStatus szBadAR
End If
End Sub
Private Sub OnUpdateInfo() Handles Timer1.Timer, cmdUpdate.Click
UpdateMemoryInfo
End Sub
Private Sub OnCheckAutoApply() Handles Timer2.Timer
Debug.Print "OnCheckAutoApply"
Dim status As NTSTATUS
Dim cbRet As Long
Dim sbi As SYSTEM_BASIC_INFORMATION
Dim spi As SYSTEM_PERFORMANCE_INFORMATION
status = NtQuerySystemInformation(SystemBasicInformation, sbi, LenB(Of SYSTEM_BASIC_INFORMATION), cbRet)
status = NtQuerySystemInformation(SystemPerformanceInformation, spi, LenB(Of SYSTEM_PERFORMANCE_INFORMATION), cbRet)
Dim sml As SYSTEM_MEMORY_LIST_INFORMATION
status = NtQuerySystemInformation(SystemMemoryListInformation, sml, LenB(Of SYSTEM_MEMORY_LIST_INFORMATION), cbRet)
Dim i As Long
Dim llST As LongLong
For i = 0 To 7
llST += sml.PageCountByPriority(i)
Next
Dim llTot As LongLong
If mAOUseM Then
llTot = sml.ModifiedPageCount
End If
If mAOUseS Then
llTot += llST
End If
' (sbi.NumberOfPhysicalPages - spi.AvailablePages), sml.ModifiedPageCount , llST , sml.FreePageCount
Dim llGT As LongLong = (sbi.NumberOfPhysicalPages - spi.AvailablePages) + sml.ModifiedPageCount + llST + sml.FreePageCount
Dim llNotUsed As LongLong
llNotUsed = llGT - (sbi.NumberOfPhysicalPages - spi.AvailablePages)
Dim nPct As Long = CLng(Round((CSng(llTot) / CSng(llNotUsed)) * 100, 0))
' Debug.Print "AutoApply::llST=" & llST & ", llTot=" & llTot & ", llNotUsed=" & llNotUsed
' Debug.Print "AutoApply::nPct=" & nPct & ",target=" & mAOPct
If nPct > mAOPct Then
status = AutoOptimize()
SetStatus Replace$(szAOExec, "%d", Format$(Now, "yyyy-mm-dd Hh:nn:Ss"))
End If
End Sub
Private Sub OnEmptyStandby() Handles cmdEmptyStandby.Click
Dim status As NTSTATUS = ClearStandby(False)
If NT_SUCCESS(status) Then
SetStatus szCSBOk
UpdateMemoryInfo()
ElseIf status = STATUS_PRIVILEGE_NOT_HELD Then
Beep
SetStatus szPrivError
ElseIf status = STATUS_INVALID_INFO_CLASS Then
Beep
SetStatus szBadVersion
Else
Beep
SetStatus szCSBErr & GetNtErrorString(status)
End If
End Sub
Private Sub OnEmptyLPStandby() Handles cmdEmptyLPStandby.Click
Dim status As NTSTATUS = ClearStandby(True)
If NT_SUCCESS(status) Then
SetStatus szCSBLPOk
UpdateMemoryInfo()
ElseIf status = STATUS_PRIVILEGE_NOT_HELD Then
Beep
SetStatus szPrivError
ElseIf status = STATUS_INVALID_INFO_CLASS Then
Beep
SetStatus szBadVersion
Else
Beep
SetStatus szCSBLPErr & GetNtErrorString(status)
End If
End Sub
Private Sub OnFlushModified() Handles cmdFlushModified.Click
Dim status As NTSTATUS = FlushModified()
If NT_SUCCESS(status) Then
SetStatus szFMOk
UpdateMemoryInfo()
ElseIf status = STATUS_PRIVILEGE_NOT_HELD Then
Beep
SetStatus szPrivError
ElseIf status = STATUS_INVALID_INFO_CLASS Then
Beep
SetStatus szBadVersion
Else
Beep
SetStatus szFMErr & GetNtErrorString(status)
End If
End Sub
Private Sub OnEmptyWorkingSets() Handles cmdEmptyWS.Click
Dim status As NTSTATUS = EmptyWorkingSets()
If NT_SUCCESS(status) Then
SetStatus szWSOk
UpdateMemoryInfo()
ElseIf status = STATUS_PRIVILEGE_NOT_HELD Then
Beep
SetStatus szPrivError
ElseIf status = STATUS_INVALID_INFO_CLASS Then
Beep
SetStatus szBadVersion
Else
Beep
SetStatus szWSErr & GetNtErrorString(status)
End If
End Sub
Private Sub OnCombinePages() Handles cmdCombine.Click
Dim llCb As LongLong
Dim status As NTSTATUS = CombinePages(llCb)
Dim szTmp As String = szCombined
If NT_SUCCESS(status) Then
szTmp = Replace$(szTmp, "%1", FormatSizeStd(llCb * PAGE_SIZE))
SetStatus Replace$(szTmp, "%2", CStr(llCb))
UpdateMemoryInfo()
ElseIf status = STATUS_PRIVILEGE_NOT_HELD Then
Beep
SetStatus szPrivError
ElseIf status = STATUS_INVALID_INFO_CLASS Then
Beep
SetStatus szBadVersion
Else
Beep
SetStatus szCmbErr & GetNtErrorString(status)
End If
End Sub
Private Sub OnFlushRegCache() Handles cmdFlushRegCache.Click
Dim status As NTSTATUS = FlushRegistryCache()
If NT_SUCCESS(status) Then
SetStatus szRegOk
UpdateMemoryInfo()
ElseIf status = STATUS_PRIVILEGE_NOT_HELD Then
Beep
SetStatus szPrivError
ElseIf status = STATUS_INVALID_INFO_CLASS Then
Beep
SetStatus szBadVersion
Else
Beep
SetStatus szRegErr & GetNtErrorString(status)
End If
End Sub
Private Sub OnFlushFileCache() Handles cmdFlushFileCache.Click
Dim llCb As LongLong
Dim status As NTSTATUS = FlushFileCache(llCb)
If NT_SUCCESS(status) Then
SetStatus Replace$(szFileOk, "%1", FormatSizeStd(llCb))
UpdateMemoryInfo()
ElseIf status = STATUS_PRIVILEGE_NOT_HELD Then
Beep
SetStatus szPrivError
ElseIf status = STATUS_INVALID_INFO_CLASS Then
Beep
SetStatus szBadVersion
Else
Beep
SetStatus szFileErr & GetNtErrorString(status)
End If
End Sub
Private Sub OnAutoOptimize() Handles cmdAuto.Click
Dim status As NTSTATUS = AutoOptimize()
If NT_SUCCESS(status) Then
SetStatus szAAOk
UpdateMemoryInfo()
ElseIf status = STATUS_PRIVILEGE_NOT_HELD Then
Beep
SetStatus szPrivError
ElseIf status = STATUS_INVALID_INFO_CLASS Then
Beep
SetStatus szBadVersion
Else
Beep
SetStatus szAAErr & GetNtErrorString(status)
End If
End Sub
Private Sub OnAutoApplyOptsSettings() Handles cmdAutoOpts.Click
SetParent pbAutoOpts.hWnd, Me.hWnd
pbAutoOpts.Visible = True
Dim dwFrEx As Long
dwFrEx = CLng(GetWindowLong(pbAutoOpts.hWnd, GWL_EXSTYLE))
dwFrEx = dwFrEx Or WS_EX_DLGMODALFRAME
SetWindowLong pbAutoOpts.hWnd, GWL_EXSTYLE, dwFrEx
'PostLog "OptionsLeft=" & Frame2.Left
SetWindowPos pbAutoOpts.hWnd, 0&, cmdAuto.Left, cmdAuto.Top + cmdAuto.Height + 4, 0&, 0&, SWP_NOSIZE Or SWP_NOZORDER Or SWP_NOACTIVATE Or SWP_FRAMECHANGED
chkAOUseM.Value = If(mAOUseM, vbChecked, vbUnchecked)
chkAOUseS.Value = If(mAOUseS, vbChecked, vbUnchecked)
txtAOPct.Text = CStr(mAOPct)
txtAOTimer.Text = CStr(mAOInt)
End Sub
Private Sub lblOpts_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single) Handles lblOpts.MouseMove
If Button = 1 Then
Call ReleaseCapture
Call SendMessage(pbAutoOpts.hWnd, WM_NCLBUTTONDOWN, HTCAPTION, 0&)
End If
End Sub
Private Sub pbAutoOpts_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single) Handles pbAutoOpts.MouseMove
If Button = 1 Then
Call ReleaseCapture
Call SendMessage(pbAutoOpts.hWnd, WM_NCLBUTTONDOWN, HTCAPTION, 0&)
End If
End Sub
Private Sub cmdAutoOptOk_Click() Handles cmdAutoOptOk.Click
If ValidateAASettings() Then
mAOUseM = If(chkAOUseM.Value = vbChecked, True, False)
mAOUseS = If(chkAOUseS.Value = vbChecked, True, False)
mAOInt = CLng(txtAOTimer.Text)
mAOPct = CLng(txtAOPct.Text)
pbAutoOpts.Visible = False
SetStatus szAOOk
Else
SetStatus szAOErr
End If
End Sub
Private Function ValidateAASettings() As Boolean
If (chkAOUseM.Value = vbUnchecked) And (chkAOUseS.Value = vbUnchecked) Then
Return False
End If
If (CLng(txtAOPct.Text) < 1) Or (CLng(txtAOPct.Text) > 100) Then
Return False
End If
If CLng(txtAOTimer.Text) < 1 Then Return False
Return True
End Function
Private Sub cmdAutoOptCancel_Click() Handles cmdAutoOptCancel.Click
pbAutoOpts.Visible = False
End Sub
Private Sub OnEnableAutoApply() Handles chkAAO.Click
If chkAAO.Value = vbChecked Then
Timer2.Interval = mAOInt * 1000
Timer2.Enabled = True
Else
Timer2.Enabled = False
End If
End Sub
Private Sub OnAbout() Handles cmdAbout.Click
ShowAbout
End Sub
Private Sub picKeyU_DblClick() Handles picKeyU.DblClick
SetKeyColor 0
End Sub
Private Sub picKeyM_DblClick() Handles picKeyM.DblClick
SetKeyColor 1
End Sub
Private Sub picKeyS_DblClick() Handles picKeyS.DblClick
SetKeyColor 2
End Sub
Private Sub picKeyF_DblClick() Handles picKeyF.DblClick
SetKeyColor 3
End Sub
Private Sub OnExit() Handles cmdExit.Click
Unload Me
End Sub
Private Sub OnUnload(Cancel As Integer) Handles Form.Unload
'Debug.Print "Form.Unload"
SaveCurrentSettings
UnSubclass2 Me.hWnd, AddressOf F1WndProc, Me.hWnd
Set cJL = Nothing
If bExitBug Then
' ExitProcess(0)
PostQuitMessage(0)
End If
End Sub
Private Sub ShowAbout()
Dim tdc As TASKDIALOGCONFIG
tdc.cbSize = LenB(Of TASKDIALOGCONFIG)
tdc.hInstance = hMod
tdc.hWndParent = Me.hWnd
tdc.dwCommonButtons = TDCBF_CLOSE_BUTTON
tdc.dwFlags = TDF_ENABLE_HYPERLINKS
tdc.pfCallback = AddressOf TaskDialogCallbackProc
tdc.pszMainIcon = MAKEINTRESOURCE(CLng(TD_SHIELD_GRAY_ICON))
tdc.pszFooterIcon = MAKEINTRESOURCE(CLng(TD_INFORMATION_ICON))
tdc.pszWindowTitle = StrPtr(szHelpTitle)
tdc.pszMainInstruction = StrPtr(szHelpHeader)
tdc.pszContent = StrPtr(szHelpMessage)
tdc.pszFooter = StrPtr(szHelpFooter)
Dim lRes As Long = TaskDialogIndirect(tdc)
End Sub
Private Sub SaveCurrentSettings()
SaveSetting App.Title, "Config0", "MinimizeToTray", IIf(optMin(0).Value = True, "0", "1")
SaveSetting App.Title, "Config0", "AAUseModified", IIf(mAOUseM, "1", "0")
SaveSetting App.Title, "Config0", "AAUseStandby", IIf(mAOUseS, "1", "0")
SaveSetting App.Title, "Config0", "AAInterval", CStr(mAOInt)
SaveSetting App.Title, "Config0", "AATargetPercent", CStr(mAOPct)
SaveSetting App.Title, "Config0", "ColorKeyU", CStr(shpKeyU.FillColor)
SaveSetting App.Title, "Config0", "ColorKeyM", CStr(shpKeyM.FillColor)
SaveSetting App.Title, "Config0", "ColorKeyS", CStr(shpKeyS.FillColor)
SaveSetting App.Title, "Config0", "ColorKeyF", CStr(shpKeyF.FillColor)
Dim i As Long
For i = 4 To 15
SaveSetting App.Title, "Config0", "CustomColor" & i, clrCustom(i)
Next
SaveSetting App.Title, "Config0", "NotifyGuid", GUIDToString(tNIGuid)
SaveSetting App.Title, "Config0", "ImagePath", GetExePath()
SaveSetting App.Title, "Config0", "ShowMemoryBar", CStr(chkToggleBar.Value)
End Sub
Private Sub LoadSavedSettings()
If GetSetting(App.Title, "Config0", "MinimizeToTray", "0") = "0" Then
optMin(0).Value = True
optMin(1).Value = False
Else
optMin(1).Value = True
optMin(0).Value = False
End If
mAOUseM = IIf(GetSetting(App.Title, "Config0", "AAUseModified", "1") = "1", True, False)
mAOUseS = IIf(GetSetting(App.Title, "Config0", "AAUseStandby", "1") = "1", True, False)
mAOInt = CLng(GetSetting(App.Title, "Config0", "AAInterval", "600"))
mAOPct = CLng(GetSetting(App.Title, "Config0", "AATargetPercent", "75"))
If ValidateAASettings() = False Then 'Bad setting(s), revert to defaults
mAOUseM = True
mAOUseS = True
mAOInt = 600
mAOPct = 75
End If
shpKeyU.FillColor = CLng(GetSetting(App.Title, "Config0", "ColorKeyU", CStr(&H3891E6)))
shpKeyM.FillColor = CLng(GetSetting(App.Title, "Config0", "ColorKeyM", CStr(&H94530B)))
shpKeyS.FillColor = CLng(GetSetting(App.Title, "Config0", "ColorKeyS", CStr(&HDCA86F)))
shpKeyF.FillColor = CLng(GetSetting(App.Title, "Config0", "ColorKeyF", CStr(&HF3E2CF)))
shpUsed.FillColor = shpKeyU.FillColor
shpModified.FillColor = shpKeyM.FillColor
shpStandby.FillColor = shpKeyS.FillColor
shpFree.FillColor = shpKeyF.FillColor
Dim i As Long
For i = 4 To 15
clrCustom(i) = CLng(GetSetting(App.Title, "Config0", "CustomColor" & i, CStr(&HFFFFFF)))
Next
Dim sPathReg As String = GetSetting(App.Title, "Config0", "ImagePath", "")
Dim sPathExe As String = GetExePath()
Dim sGuid As String = GetSetting(App.Title, "Config0", "NotifyGuid", "")
If sPathReg = sPathExe Then
If sGuid <> "" Then
If IIDFromString(sGuid, tNIGuid) <> S_OK Then
CoCreateGuid tNIGuid
End If
End If
Else
'Our executable has moved or been renamed; we need to generate a new GUID for the notify icon
CoCreateGuid tNIGuid
End If
chkToggleBar.Value = CLng(GetSetting(App.Title, "Config0", "ShowMemoryBar", vbChecked))
If chkToggleBar.Value = vbUnchecked Then HideMemoryBar()
End Sub
Private Function GetExePath() As String
Dim sPath As String = String$(MAX_PATH, 0)
Dim lRet As Long = GetModuleFileName(0, sPath, MAX_PATH)
If lRet Then Return Left$(sPath, lRet)
End Function
Private Function GUIDToString(tg As UUID, Optional bBrack As Boolean = True) As String
'StringFromGUID2 never works, even "working" code from vbaccelerator AND MSDN
GUIDToString = Right$("00000000" & Hex$(tg.Data1), 8) & "-" & Right$("0000" & Hex$(tg.Data2), 4) & "-" & Right$("0000" & Hex$(tg.Data3), 4) & _
"-" & Right$("00" & Hex$(CLng(tg.Data4(0))), 2) & Right$("00" & Hex$(CLng(tg.Data4(1))), 2) & "-" & Right$("00" & Hex$(CLng(tg.Data4(2))), 2) & _
Right$("00" & Hex$(CLng(tg.Data4(3))), 2) & Right$("00" & Hex$(CLng(tg.Data4(4))), 2) & Right$("00" & Hex$(CLng(tg.Data4(5))), 2) & _
Right$("00" & Hex$(CLng(tg.Data4(6))), 2) & Right$("00" & Hex$(CLng(tg.Data4(7))), 2)
If bBrack Then GUIDToString = "{" & GUIDToString & "}"
End Function
Private Sub SetShowInTaskbar(hWnd As LongPtr, bShow As Boolean)
Debug.Print "ShowInTaskbar " & bShow
Dim lStyle As Long
ShowWindow hWnd, SW_HIDE
lStyle = CLng(GetWindowLong(hWnd, GWL_EXSTYLE))
If bShow = False Then
If lStyle And WS_EX_APPWINDOW Then
lStyle = lStyle - WS_EX_APPWINDOW
End If
Else
lStyle = lStyle Or WS_EX_APPWINDOW
End If
SetWindowLong hWnd, GWL_EXSTYLE, lStyle
'App.TaskVisible = bShow
ShowWindow hWnd, SW_NORMAL
End Sub
Private Sub ShowTrayContextMenu(hwndPar As LongPtr)
On Error GoTo e0
Debug.Print "ShowTrayContextMenu()"
Dim hMenu As LongPtr = CreatePopupMenu()
Dim mii As MENUITEMINFOW
Dim mScaleTray As Single = GetTaskbarDPI() / 96!
Select Case mScaleTray
Case Is <= 1: cxyIconTray = 16
Case Is <= 1.25: cxyIconTray = 20
Case Is <= 1.5: cxyIconTray = 24
Case Else: cxyIconTray = 32
End Select
Dim hIcoAuto As LongPtr, hbmAuto As LongPtr
hIcoAuto = LoadImage(hMod, ByVal StrPtr(IDI_AUTO), IMAGE_ICON, cxyIconTray, cxyIconTray, LR_DEFAULTCOLOR Or LR_SHARED)
hbmAuto = HBitmapFromHIcon(hIcoAuto, cxyIconTray, cxyIconTray)
With mii
.cbSize = LenB(Of MENUITEMINFOW)
.fMask = MIIM_ID Or MIIM_STRING Or MIIM_BITMAP
.cch = Len(szAutoOpt)
.dwTypeData = StrPtr(szAutoOpt)
.wID = IDM_AUTOOPT
.hbmpItem = hbmAuto
End With
InsertMenuItemW hMenu, 0, CTRUE, mii
With mii
.fMask = MIIM_TYPE
.fType = MFT_SEPARATOR
InsertMenuItemW hMenu, 1, CTRUE, mii
End With
Dim hIcoCS As LongPtr, hbmCS As LongPtr
hIcoCS = LoadImage(hMod, ByVal StrPtr(IDI_MAIN), IMAGE_ICON, cxyIconTray, cxyIconTray, LR_DEFAULTCOLOR Or LR_SHARED)
hbmCS = HBitmapFromHIcon(hIcoCS, cxyIconTray, cxyIconTray)
With mii
.cbSize = LenB(Of MENUITEMINFOW)
.fMask = MIIM_ID Or MIIM_STRING Or MIIM_BITMAP
.cch = Len(szClearStd)
.dwTypeData = StrPtr(szClearStd)
.wID = IDM_CLEARSTD
.hbmpItem = hbmCS
End With
InsertMenuItemW hMenu, 2, CTRUE, mii
With mii
.cch = Len(szClearStdLP)
.dwTypeData = StrPtr(szClearStdLP)
.wID = IDM_CLEARSTDLP
.hbmpItem = hbmCS
End With
InsertMenuItemW hMenu, 3, CTRUE, mii
With mii
.cch = Len(szFlushMod)
.dwTypeData = StrPtr(szFlushMod)
.wID = IDM_FLUSHMOD
.hbmpItem = hbmCS
End With
InsertMenuItemW hMenu, 4, CTRUE, mii
With mii
.fMask = MIIM_TYPE
.fType = MFT_SEPARATOR
InsertMenuItemW hMenu, 5, CTRUE, mii
End With
Dim hIcoWS As LongPtr, hbmWS As LongPtr
hIcoWS = LoadImage(hMod, ByVal StrPtr(IDI_WS), IMAGE_ICON, cxyIconTray, cxyIconTray, LR_DEFAULTCOLOR Or LR_SHARED)
hbmWS = HBitmapFromHIcon(hIcoWS, cxyIconTray, cxyIconTray)
With mii
.cbSize = LenB(Of MENUITEMINFOW)
.fMask = MIIM_ID Or MIIM_STRING Or MIIM_BITMAP
.cch = Len(szEmptyWS)
.dwTypeData = StrPtr(szEmptyWS)
.wID = IDM_EMPTYWS
.hbmpItem = hbmWS
End With
InsertMenuItemW hMenu, 6, CTRUE, mii
Dim hIcoCmb As LongPtr, hbmCmb As LongPtr
hIcoCmb = LoadImage(hMod, ByVal StrPtr(IDI_CMB), IMAGE_ICON, cxyIconTray, cxyIconTray, LR_DEFAULTCOLOR Or LR_SHARED)
hbmCmb = HBitmapFromHIcon(hIcoCmb, cxyIconTray, cxyIconTray)
With mii
.cbSize = LenB(Of MENUITEMINFOW)
.fMask = MIIM_ID Or MIIM_STRING Or MIIM_BITMAP
.cch = Len(szCmb)
.dwTypeData = StrPtr(szCmb)
.wID = IDM_COMBINE
.hbmpItem = hbmCmb
End With
InsertMenuItemW hMenu, 7, CTRUE, mii
Dim hIcoFR As LongPtr, hbmFR As LongPtr
hIcoFR = LoadImage(hMod, ByVal StrPtr(IDI_REG), IMAGE_ICON, cxyIconTray, cxyIconTray, LR_DEFAULTCOLOR Or LR_SHARED)
hbmFR = HBitmapFromHIcon(hIcoFR, cxyIconTray, cxyIconTray)
With mii
.cbSize = LenB(Of MENUITEMINFOW)
.fMask = MIIM_ID Or MIIM_STRING Or MIIM_BITMAP
.cch = Len(szFlushReg)
.dwTypeData = StrPtr(szFlushReg)
.wID = IDM_FLUSHREG
.hbmpItem = hbmFR
End With
InsertMenuItemW hMenu, 8, CTRUE, mii
Dim hIcoFF As LongPtr, hbmFF As LongPtr
hIcoFF = LoadImage(hMod, ByVal StrPtr(IDI_DISK), IMAGE_ICON, cxyIconTray, cxyIconTray, LR_DEFAULTCOLOR Or LR_SHARED)
hbmFF = HBitmapFromHIcon(hIcoFF, cxyIconTray, cxyIconTray)
With mii
.cbSize = LenB(Of MENUITEMINFOW)
.fMask = MIIM_ID Or MIIM_STRING Or MIIM_BITMAP
.cch = Len(szFlushFiles)
.dwTypeData = StrPtr(szFlushFiles)
.wID = IDM_FLUSHFILES
.hbmpItem = hbmFF
End With
InsertMenuItemW hMenu, 9, CTRUE, mii
With mii
.fMask = MIIM_TYPE
.fType = MFT_SEPARATOR
InsertMenuItemW hMenu, 10, CTRUE, mii
End With
With mii
.cbSize = LenB(Of MENUITEMINFOW)
.fMask = MIIM_ID Or MIIM_STRING Or MIIM_BITMAP Or MIIM_STATE
.cch = Len(szRestore)
.dwTypeData = StrPtr(szRestore)
.wID = IDM_RESTORE
.hbmpItem = hbmCS
.fState = MFS_DEFAULT
End With
InsertMenuItemW hMenu, 11, CTRUE, mii
With mii
.fMask = MIIM_TYPE
.fType = MFT_SEPARATOR
InsertMenuItemW hMenu, 12, CTRUE, mii
End With
With mii
.cbSize = LenB(Of MENUITEMINFOW)
.fMask = MIIM_ID Or MIIM_STRING
.cch = Len(szExit)
.dwTypeData = StrPtr(szExit)
.wID = IDM_EXIT
.hbmpItem = 0
.fState = 0
End With
InsertMenuItemW hMenu, 13, CTRUE, mii
Dim pt As POINT
GetCursorPos pt
Dim idCmd As Long = TrackPopupMenu(hMenu, TPM_LEFTBUTTON Or TPM_RIGHTBUTTON Or TPM_LEFTALIGN Or TPM_TOPALIGN Or TPM_VERTICAL Or TPM_VERPOSANIMATION Or TPM_RETURNCMD, pt.x, pt.y, 0, hwndPar, ByVal 0)
DestroyIcon hIcoAuto: DeleteObject hbmAuto
DestroyIcon hIcoCS: DeleteObject hbmCS
DestroyIcon hIcoWS: DeleteObject hbmWS
DestroyIcon hIcoCmb: DeleteObject hbmCmb
DestroyIcon hIcoFR: DeleteObject hbmFR
DestroyIcon hIcoFF: DeleteObject hbmFF
If idCmd Then
Dim status As NTSTATUS
Select Case idCmd
Case IDM_AUTOOPT: status = AutoOptimize()
Case IDM_CLEARSTD: status = ClearStandby(False)
Case IDM_CLEARSTDLP: status = ClearStandby(True)
Case IDM_FLUSHMOD: status = FlushModified()
Case IDM_EMPTYWS: status = EmptyWorkingSets()
Case IDM_COMBINE: status = CombinePages()
Case IDM_FLUSHREG: status = FlushRegistryCache()
Case IDM_FLUSHFILES: status = FlushFileCache()
Case IDM_RESTORE
ShowWindow Me.hWnd, SW_RESTORE
SetShowInTaskbar(Me.hWnd, True)
RemoveTrayIcon()
Case IDM_EXIT
RemoveTrayIcon()
PostMessage Me.hWnd, WM_CLOSE, 0, ByVal 0
End Select
End If
Exit Sub
e0:
Debug.Print "ShowTrayInContextMenu Error: " & Err.Number & ", " & Err.Description
End Sub
Private Function SetKeyColor(idx As Long) As Boolean
Dim tClr As CHOOSECOLORW
Dim rgbDef As Long
Select Case idx
Case 0: rgbDef = WinColor(shpKeyU.FillColor)
Case 1: rgbDef = WinColor(shpKeyM.FillColor)
Case 2: rgbDef = WinColor(shpKeyS.FillColor)
Case 3: rgbDef = WinColor(shpKeyF.FillColor)
End Select
clrCustom(0) = WinColor(&H3891E6)
clrCustom(1) = WinColor(&H94530B)
clrCustom(2) = WinColor(&HDCA86F)
clrCustom(3) = WinColor(&HF3E2CF)
tClr.lStructSize = LenB(Of CHOOSECOLORW)
tClr.hInstance = App.hInstance
tClr.hwndOwner = Me.hWnd
tClr.lpCustColors = VarPtr(clrCustom(0))
tClr.rgbResult = rgbDef
tClr.Flags = CC_ANYCOLOR Or CC_RGBINIT
If ChooseColorW(tClr) Then
Select Case idx
Case 0
shpKeyU.FillColor = tClr.rgbResult
shpUsed.FillColor = shpKeyU.FillColor
Case 1
shpKeyM.FillColor = tClr.rgbResult
shpModified.FillColor = shpKeyM.FillColor
Case 2
shpKeyS.FillColor = tClr.rgbResult
shpStandby.FillColor = shpKeyS.FillColor
Case 3
shpKeyF.FillColor = tClr.rgbResult
shpFree.FillColor = shpKeyF.FillColor
End Select
Else
Dim lErr As Long = CommDlgExtendedError()
Debug.Print "Color picker canceled/error, code=0x" & Hex$(lErr)
End If
End Function
Private Function WinColor(ByVal Color As Long, Optional ByVal hPal As LongPtr) As Long
If OleTranslateColor(Color, hPal, WinColor) <> 0 Then WinColor = -1
End Function
Private Function HBitmapFromHIcon(hIcon As LongPtr, cx As Long, cy As Long) As LongPtr
Dim hDC As LongPtr
Dim hBackDC As LongPtr
Dim hBitmap As LongPtr
Dim hBackSV As LongPtr
hDC = GetDC(0)
hBackDC = CreateCompatibleDC(hDC)
hBitmap = Create32BitHBITMAP(hBackDC, cx, cy)
hBackSV = SelectObject(hBackDC, hBitmap)
DrawIconEx hBackDC, 0, 0, hIcon, cx, cy, 0, 0, DI_NORMAL
Call SelectObject(hBackDC, hBackSV)
Call ReleaseDC(0, hDC)
Call DeleteDC(hBackDC)
HBitmapFromHIcon = hBitmap
End Function
Private Function Create32BitHBITMAP(hDC As LongPtr, cx As Long, cy As Long) As LongPtr
Dim bmi As BITMAPINFO
bmi.bmiHeader.biSize = LenB(bmi.bmiHeader)
bmi.bmiHeader.biPlanes = 1
bmi.bmiHeader.biCompression = 0
bmi.bmiHeader.biWidth = cx
bmi.bmiHeader.biHeight = cy
bmi.bmiHeader.biBitCount = 32
Create32BitHBITMAP = CreateDIBSection(hDC, bmi, DIB_RGB_COLORS, ByVal 0&, 0, 0)
End Function
[Description("Loads button icons from the resource file (which is was a compiled exe must exist to take them from when running in the IDE). ")]
Private Sub InitButtonIcons()
Select Case m_ScaleX
Case Is <= 1: cxyIconBtn = 24
Case Is <= 1.25: cxyIconBtn = 32
Case Is <= 1.5: cxyIconBtn = 48
Case Else: cxyIconBtn = 64
End Select
Dim hBtn As LongPtr = LoadImage(hMod, ByVal StrPtr(IDI_MAIN), IMAGE_ICON, cxyIconBtn, cxyIconBtn, LR_DEFAULTCOLOR Or LR_SHARED)
If hBtn Then
ButtonIconAssign cmdEmptyLPStandby.hWnd, hBtn, cxyIconBtn, cxyIconBtn, 4&, 2&, 2&, 0&
ButtonIconAssign cmdEmptyStandby.hWnd, hBtn, cxyIconBtn, cxyIconBtn, 4&, 2&, 2&, 0&
ButtonIconAssign cmdFlushModified.hWnd, hBtn, cxyIconBtn, cxyIconBtn, 4&, 2&, 2&, 0&
DestroyIcon hBtn: hBtn = 0
End If
hBtn = LoadImage(hMod, ByVal StrPtr(IDI_WS), IMAGE_ICON, cxyIconBtn, cxyIconBtn, LR_DEFAULTCOLOR Or LR_SHARED)
If hBtn Then ButtonIconAssign cmdEmptyWS.hWnd, hBtn, cxyIconBtn, cxyIconBtn, 4&, 2&, 2&, 0&
DestroyIcon hBtn: hBtn = 0
hBtn = LoadImage(hMod, ByVal StrPtr(IDI_CMB), IMAGE_ICON, cxyIconBtn, cxyIconBtn, LR_DEFAULTCOLOR Or LR_SHARED)
If hBtn Then ButtonIconAssign cmdCombine.hWnd, hBtn, cxyIconBtn, cxyIconBtn, 4&, 2&, 2&, 0&
DestroyIcon hBtn: hBtn = 0
hBtn = LoadImage(hMod, ByVal StrPtr(IDI_DISK), IMAGE_ICON, cxyIconBtn, cxyIconBtn, LR_DEFAULTCOLOR Or LR_SHARED)
If hBtn Then ButtonIconAssign cmdFlushFileCache.hWnd, hBtn, cxyIconBtn, cxyIconBtn, 4&, 2&, 2&, 0&
DestroyIcon hBtn: hBtn = 0
hBtn = LoadImage(hMod, ByVal StrPtr(IDI_REG), IMAGE_ICON, cxyIconBtn, cxyIconBtn, LR_DEFAULTCOLOR Or LR_SHARED)
If hBtn Then ButtonIconAssign cmdFlushRegCache.hWnd, hBtn, cxyIconBtn, cxyIconBtn, 4&, 2&, 2&, 0&
DestroyIcon hBtn: hBtn = 0
hBtn = LoadImage(hMod, ByVal StrPtr(IDI_REFRESH), IMAGE_ICON, cxyIconBtn, cxyIconBtn, LR_DEFAULTCOLOR Or LR_SHARED)
If hBtn Then ButtonIconAssign cmdUpdate.hWnd, hBtn, cxyIconBtn, cxyIconBtn, 4&, 2&, 2&, 0&
DestroyIcon hBtn: hBtn = 0
hBtn = LoadImage(hMod, ByVal StrPtr(IDI_AUTO), IMAGE_ICON, cxyIconBtn, cxyIconBtn, LR_DEFAULTCOLOR Or LR_SHARED)
If hBtn Then ButtonIconAssign cmdAuto.hWnd, hBtn, cxyIconBtn, cxyIconBtn, 4&, 2&, 2&, 0&
DestroyIcon hBtn: hBtn = 0
End Sub
[Description("Assigns an HICON image to a CommandButton control with the given size and margins.")]
Private Sub ButtonIconAssign(hWnd As LongPtr, hIcon As LongPtr, cx As Long, CY As Long, margLeft As Long, margRight As Long, margTop As Long, margBottom As Long)
On Error GoTo e0
Dim bi4 As BUTTON_IMAGELIST
bi4.himl = ImageList_Create(cx, CY, ILC_COLOR32 Or ILC_MASK, 1, 1)
If bi4.himl Then
bi4.margin.Left = margLeft
bi4.margin.Right = margRight
bi4.margin.Top = margTop
bi4.margin.Bottom = margBottom
ImageList_ReplaceIcon bi4.himl, -1, hIcon
Call SendMessage(hWnd, BCM_SETIMAGELIST, 0&, bi4)
End If
On Error GoTo 0
Exit Sub
e0:
Debug.Print "ButtonIconAssign.Error->" & Err.Description & " (" & Err.Number & ")"
End Sub
Private Sub RenderMemoryBar(ByVal llUsed As LongLong, ByVal llMod As LongLong, ByVal llSB As LongLong, ByVal llFree As LongLong)
Dim llTot As LongLong = llUsed + llMod + llSB + llFree
Dim cxTot As Long = picMem.ScaleWidth
Dim pctUsed As Double = CDbl(llUsed) / CDbl(llTot)
Dim pctMod As Double = CDbl(llMod) / CDbl(llTot)
Dim pctSB As Double = CDbl(llSB) / CDbl(llTot)
Dim pctFree As Double = CDbl(llFree) / CDbl(llTot)
Dim cxUsed As Long = CLng(Round(CDbl(cxTot) * pctUsed, 0))
Dim cxMod As Long = CLng(Round(CDbl(cxTot) * pctMod, 0))
Dim cxSB As Long = CLng(Round(CDbl(cxTot) * pctSB, 0))
Dim cxFree As Long = CLng(Round(CDbl(cxTot) * pctFree, 0))
shpUsed.Left = 0: shpUsed.Width = cxUsed
shpModified.Left = cxUsed - 1: shpModified.Width = cxMod
shpStandby.Left = cxUsed + cxMod - 2: shpStandby.Width = cxSB
shpFree.Left = cxUsed + cxMod + cxSB - 3: shpFree.Width = cxFree + 3
shpUsed.Visible = True
shpModified.Visible = True