-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathWindowTreeViewer.cpp
More file actions
196 lines (164 loc) · 5.27 KB
/
Copy pathWindowTreeViewer.cpp
File metadata and controls
196 lines (164 loc) · 5.27 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
// WindowTreeViewer.cpp
// Copyright (C) 2018 Katayama Hirofumi MZ <katayama.hirofumi.mz@gmail.com>.
// This file is public domain software.
#include "targetver.h"
#define MWINDOWTREE_ADJUST_OWNER
#include "MWindowTreeView.hpp"
#include "MResizable.hpp"
#include "resource.h"
BOOL EnableProcessPriviledge(LPCTSTR pszSE_)
{
BOOL f;
HANDLE hProcess;
HANDLE hToken;
LUID luid;
TOKEN_PRIVILEGES tp;
f = FALSE;
hProcess = GetCurrentProcess();
if (OpenProcessToken(hProcess, TOKEN_ADJUST_PRIVILEGES, &hToken))
{
if (LookupPrivilegeValue(NULL, pszSE_, &luid))
{
tp.PrivilegeCount = 1;
tp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
tp.Privileges[0].Luid = luid;
f = AdjustTokenPrivileges(hToken, FALSE, &tp, 0, NULL, NULL);
}
CloseHandle(hToken);
}
return f;
}
class WindowTreeViewer : public MDialogBase
{
public:
HINSTANCE m_hInst; // the instance handle
HICON m_hIcon; // the main icon
HICON m_hIconSm; // the small icon
HWND m_hwndSelected;
MWindowTreeView m_ctl1;
MResizable m_resizable;
WindowTreeViewer(HINSTANCE hInst)
: MDialogBase(IDD_MAIN), m_hInst(hInst),
m_hIcon(NULL), m_hIconSm(NULL), m_hwndSelected(NULL)
{
m_hIcon = LoadIconDx(IDI_MAIN);
m_hIconSm = LoadSmallIconDx(IDI_MAIN);
}
virtual ~WindowTreeViewer()
{
DestroyIcon(m_hIcon);
DestroyIcon(m_hIconSm);
}
BOOL OnInitDialog(HWND hwnd, HWND hwndFocus, LPARAM lParam)
{
EnableProcessPriviledge(SE_DEBUG_NAME);
SubclassChildDx(m_ctl1, ctl1);
//m_ctl1.set_style(MWTVS_PROCESSWINDOW);
//m_ctl1.set_style(MWTVS_PROCESSTHREAD);
m_ctl1.set_style(MWTVS_DESKTOPTREE);
if (HINSTANCE hinstUXTheme = LoadLibrary(TEXT("UXTHEME")))
{
typedef HRESULT (WINAPI *SETWINDOWTHEME)(HWND, LPCWSTR, LPCWSTR);
SETWINDOWTHEME pSetWindowTheme =
(SETWINDOWTHEME)GetProcAddress(hinstUXTheme, "SetWindowTheme");
if (pSetWindowTheme)
{
// apply Explorer's visual style
(*pSetWindowTheme)(m_ctl1, L"Explorer", NULL);
}
FreeLibrary(hinstUXTheme);
}
m_ctl1.refresh();
SendMessageDx(WM_SETICON, ICON_BIG, LPARAM(m_hIcon));
SendMessageDx(WM_SETICON, ICON_SMALL, LPARAM(m_hIconSm));
m_resizable.OnParentCreate(hwnd);
m_resizable.SetLayoutAnchor(ctl1, mzcLA_TOP_LEFT, mzcLA_BOTTOM_RIGHT);
m_resizable.SetLayoutAnchor(psh1, mzcLA_BOTTOM_LEFT);
m_resizable.SetLayoutAnchor(IDOK, mzcLA_BOTTOM_RIGHT);
m_resizable.SetLayoutAnchor(IDCANCEL, mzcLA_BOTTOM_RIGHT);
return TRUE;
}
void OnSize(HWND hwnd, UINT state, int cx, int cy)
{
m_resizable.OnSize();
}
void OnOK(HWND hwnd)
{
m_hwndSelected = NULL;
m_ctl1.get_selected_hwnd(m_hwndSelected);
EndDialog(IDOK);
}
void OnCommand(HWND hwnd, int id, HWND hwndCtl, UINT codeNotify)
{
switch (id)
{
case IDOK:
OnOK(hwnd);
break;
case IDCANCEL:
EndDialog(IDCANCEL);
break;
case psh1:
m_ctl1.refresh();
break;
}
}
virtual INT_PTR CALLBACK
DialogProcDx(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
switch (uMsg)
{
HANDLE_MSG(hwnd, WM_INITDIALOG, OnInitDialog);
HANDLE_MSG(hwnd, WM_COMMAND, OnCommand);
HANDLE_MSG(hwnd, WM_SIZE, OnSize);
default:
return DefaultProcDx();
}
}
BOOL StartDx(INT nCmdShow)
{
return TRUE;
}
INT RunDx()
{
if (IDOK == DialogBoxDx(NULL))
{
TCHAR szText[64];
StringCbPrintf(szText, sizeof(szText), TEXT("HWND: 0x%08lX"),
(LONG)(LONG_PTR)m_hwndSelected);
MsgBoxDx(szText, TEXT("Selected Window"), MB_ICONINFORMATION);
}
}
};
//////////////////////////////////////////////////////////////////////////////
// Win32 main function
extern "C"
INT APIENTRY _tWinMain(
HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPTSTR lpCmdLine,
INT nCmdShow)
{
int ret = -1;
{
WindowTreeViewer app(hInstance);
::InitCommonControls();
if (app.StartDx(nCmdShow))
{
ret = app.RunDx();
}
}
#if (WINVER >= 0x0500)
HANDLE hProcess = GetCurrentProcess();
DebugPrintDx(TEXT("Count of GDI objects: %ld\n"),
GetGuiResources(hProcess, GR_GDIOBJECTS));
DebugPrintDx(TEXT("Count of USER objects: %ld\n"),
GetGuiResources(hProcess, GR_USEROBJECTS));
#endif
#if defined(_MSC_VER) && !defined(NDEBUG)
// for detecting memory leak (MSVC only)
_CrtSetDbgFlag(_CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF);
#endif
return ret;
}
//////////////////////////////////////////////////////////////////////////////