Skip to content

Commit 76e92da

Browse files
committed
Fix for runtime style swap in demo
fixes #3
1 parent 8506b7b commit 76e92da

File tree

4 files changed

+80
-62
lines changed

4 files changed

+80
-62
lines changed

imgui-borderless-win32/BorderlessWindow.cpp

Lines changed: 23 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -95,29 +95,27 @@ namespace
9595
return window_class_name;
9696
}
9797

98-
unique_handle create_window(WNDPROC wndproc, void* userdata)
98+
HWND create_window(WNDPROC wndproc, void* userdata)
9999
{
100100
HWND handle = CreateWindowExW(0, window_class(wndproc), L"Borderless Window",
101101
static_cast<DWORD>(Style::aero_borderless), CW_USEDEFAULT, CW_USEDEFAULT,
102102
1280, 720, nullptr, nullptr, nullptr, userdata
103103
);
104104

105105
if (!handle) throw last_error("failed to create window");
106-
107-
108-
return unique_handle{ handle };
106+
return handle;
109107
}
110108
}
111109

112110
BorderlessWindow::BorderlessWindow()
113-
: m_hHWND{ create_window(&this->BorderlessWindow::WndProc, this) }
111+
: m_hWND{ create_window(&this->BorderlessWindow::WndProc, this) }
114112
{
115-
s_BorderlessInstanceMap.insert({ m_hHWND.get(), this });
116-
GetClassNameW(m_hHWND.get(), m_wstrWC, 256);
117-
set_composition(true);
118-
set_borderless(true);
119-
set_borderless_shadow(true);
120-
::ShowWindow(m_hHWND.get(), SW_SHOW);
113+
s_BorderlessInstanceMap.insert({ m_hWND, this });
114+
GetClassNameW(m_hWND, m_wstrWC, 256);
115+
set_composition(m_bCompositionEnabled);
116+
set_borderless(m_bBorderless);
117+
set_borderless_shadow(m_bBorderless_shadow);
118+
::ShowWindow(m_hWND, SW_SHOW);
121119

122120
#ifdef BORDERLESS_DEBUG
123121
AllocConsole();
@@ -129,13 +127,13 @@ BorderlessWindow::BorderlessWindow()
129127
}
130128

131129
BorderlessWindow::BorderlessWindow(std::function<void()> render)
132-
: m_hHWND{ create_window(&BorderlessWindow::WndProc, this) }, m_fRender(render)
130+
: m_hWND{ create_window(&BorderlessWindow::WndProc, this) }, m_fRender(render)
133131
{
134-
GetClassNameW(m_hHWND.get(), m_wstrWC, 256);
135-
set_composition(true);
136-
set_borderless(true);
137-
set_borderless_shadow(true);
138-
::ShowWindow(m_hHWND.get(), SW_SHOW);
132+
GetClassNameW(m_hWND, m_wstrWC, 256);
133+
set_composition(m_bCompositionEnabled);
134+
set_borderless(m_bBorderless);
135+
set_borderless_shadow(m_bBorderless_shadow);
136+
::ShowWindow(m_hWND, SW_SHOW);
139137
}
140138

141139
VOID BorderlessWindow::set_composition(BOOL enabled)
@@ -145,26 +143,26 @@ VOID BorderlessWindow::set_composition(BOOL enabled)
145143
bb.dwFlags = DWM_BB_ENABLE | DWM_BB_BLURREGION;
146144
bb.hRgnBlur = hRgn;
147145
bb.fEnable = TRUE;
148-
DwmEnableBlurBehindWindow(m_hHWND.get(), &bb);
146+
DwmEnableBlurBehindWindow(m_hWND, &bb);
149147
}
150148

151149
VOID BorderlessWindow::set_borderless(BOOL enabled)
152150
{
153151
Style new_style = (enabled) ? select_borderless_style() : Style::windowed;
154-
Style old_style = static_cast<Style>(::GetWindowLongPtrW(m_hHWND.get(), GWL_STYLE));
152+
Style old_style = static_cast<Style>(::GetWindowLongPtrW(m_hWND, GWL_STYLE));
155153

156154
if (new_style != old_style)
157155
{
158156
m_bBorderless = enabled;
159157

160-
::SetWindowLongPtrW(m_hHWND.get(), GWL_STYLE, static_cast<LONG>(new_style));
158+
::SetWindowLongPtrW(m_hWND, GWL_STYLE, static_cast<LONG>(new_style));
161159

162160
// when switching between borderless and windowed, restore appropriate shadow state
163-
set_shadow(m_hHWND.get(), m_bBorderless_shadow && (new_style != Style::windowed));
161+
set_shadow(m_hWND, m_bBorderless_shadow && (new_style != Style::windowed));
164162

165163
// redraw frame
166-
::SetWindowPos(m_hHWND.get(), nullptr, 0, 0, 0, 0, SWP_FRAMECHANGED | SWP_NOMOVE | SWP_NOSIZE);
167-
::ShowWindow(m_hHWND.get(), SW_SHOW);
164+
::SetWindowPos(m_hWND, nullptr, 0, 0, 0, 0, SWP_FRAMECHANGED | SWP_NOMOVE | SWP_NOSIZE);
165+
::ShowWindow(m_hWND, SW_SHOW);
168166
}
169167
}
170168

@@ -173,7 +171,7 @@ VOID BorderlessWindow::set_borderless_shadow(BOOL enabled)
173171
if (m_bBorderless)
174172
{
175173
m_bBorderless_shadow = enabled;
176-
set_shadow(m_hHWND.get(), enabled);
174+
set_shadow(m_hWND, enabled);
177175
}
178176
}
179177

@@ -348,7 +346,7 @@ LRESULT BorderlessWindow::hit_test(POINT cursor)
348346
};
349347

350348
RECT window{};
351-
if (!::GetWindowRect(m_hHWND.get(), &window)) return HTNOWHERE;
349+
if (!::GetWindowRect(m_hWND, &window)) return HTNOWHERE;
352350

353351
CONST UINT drag = m_bBorderless_drag ? HTCAPTION : HTCLIENT;
354352

imgui-borderless-win32/BorderlessWindow.hpp

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,6 @@
1111

1212
#define BORDERLESS_USE_IMGUI // Tell BorderlessWindow to call ImGui_ImplWin32_WndProcHandler in WndProc
1313

14-
struct hwnd_deleter {
15-
using pointer = HWND;
16-
auto operator()(HWND handle) const -> void {
17-
::DestroyWindow(handle);
18-
}
19-
};
20-
21-
using unique_handle = std::unique_ptr<HWND, hwnd_deleter>;
22-
2314
class BorderlessWindow
2415
{
2516
public:
@@ -37,7 +28,7 @@ class BorderlessWindow
3728
UINT get_width() CONST;
3829
UINT get_height() CONST;
3930

40-
unique_handle m_hHWND;
31+
HWND m_hWND;
4132
LPWSTR m_wstrWC;
4233

4334
private:

imgui-borderless-win32/imgui.ini

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,8 @@ Collapsed=0
4343
DockId=0x00000002,0
4444

4545
[Window][Example: Simple overlay]
46-
Pos=355,86
47-
Size=346,46
46+
Pos=340,76
47+
Size=339,46
4848
Collapsed=0
4949

5050
[Window][Borderless Demo]
@@ -53,7 +53,7 @@ Size=224,316
5353
Collapsed=0
5454

5555
[Window][Borderless Settings]
56-
Pos=224,80
56+
Pos=953,10
5757
Size=109,58
5858
Collapsed=0
5959

@@ -73,22 +73,22 @@ Size=232,401
7373
Collapsed=0
7474

7575
[Window][DWM Accent State]
76-
Pos=18,85
76+
Pos=13,74
7777
Size=179,196
7878
Collapsed=0
7979

8080
[Window][DWM Gradient]
81-
Pos=20,291
81+
Pos=12,273
8282
Size=224,293
8383
Collapsed=0
8484

8585
[Window][DWM Accent Flags]
86-
Pos=354,9
86+
Pos=15,9
8787
Size=312,58
8888
Collapsed=0
8989

9090
[Window][DWM Animation id]
91-
Pos=13,11
91+
Pos=340,9
9292
Size=312,58
9393
Collapsed=0
9494

@@ -125,10 +125,15 @@ Size=224,58
125125
Collapsed=0
126126

127127
[Window][glClearColor]
128-
Pos=266,291
128+
Pos=252,272
129129
Size=224,293
130130
Collapsed=0
131131

132+
[Window][ImGui Window Bg]
133+
Pos=663,9
134+
Size=277,58
135+
Collapsed=0
136+
132137
[Table][0x47600645,3]
133138
RefScale=16
134139
Column 0 Width=56
@@ -213,7 +218,7 @@ Column 1 Width=84
213218
Column 2 Width=126
214219

215220
[Docking][Data]
216-
DockSpace ID=0x8B93E3BD Window=0xA787BDB4 Pos=296,111 Size=1280,720 Split=X
221+
DockSpace ID=0x8B93E3BD Window=0xA787BDB4 Pos=-1430,-162 Size=1280,720 Split=X
217222
DockNode ID=0x00000003 Parent=0x8B93E3BD SizeRef=836,720 Split=X
218223
DockNode ID=0x00000001 Parent=0x00000003 SizeRef=955,720 Split=Y
219224
DockNode ID=0x00000009 Parent=0x00000001 SizeRef=1280,591 Split=X

imgui-borderless-win32/main.cpp

Lines changed: 42 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ reinterpret_cast<PFN_SET_WINDOW_COMPOSITION_ATTRIBUTE>(GetProcAddress(GetModuleH
2222

2323
#include <string>
2424
#include <vector>
25-
25+
#include <iostream>
2626
#include "BorderlessWindow.hpp"
2727

2828
// Data stored per platform window
@@ -117,18 +117,18 @@ int APIENTRY wWinMain(_In_ HINSTANCE hInstance,
117117

118118
BorderlessWindow window; // Instantiate our borderless window
119119

120-
if (!CreateDeviceWGL(window.m_hHWND.get(), &g_MainWindow))
120+
if (!CreateDeviceWGL(window.m_hWND, &g_MainWindow))
121121
{
122-
CleanupDeviceWGL(window.m_hHWND.get(), &g_MainWindow);
123-
::DestroyWindow(window.m_hHWND.get());
122+
CleanupDeviceWGL(window.m_hWND, &g_MainWindow);
123+
::DestroyWindow(window.m_hWND);
124124
::UnregisterClassW((LPCWSTR)window.m_wstrWC, GetModuleHandle(NULL));
125125
return 1;
126126
}
127127

128128
wglMakeCurrent(g_MainWindow.hDC, g_hRC);
129129

130-
::ShowWindow(window.m_hHWND.get(), SW_SHOWDEFAULT);
131-
::UpdateWindow(window.m_hHWND.get());
130+
::ShowWindow(window.m_hWND, SW_SHOWDEFAULT);
131+
::UpdateWindow(window.m_hWND);
132132

133133
IMGUI_CHECKVERSION();
134134
ImGui::CreateContext();
@@ -150,7 +150,7 @@ int APIENTRY wWinMain(_In_ HINSTANCE hInstance,
150150
}
151151

152152
// Setup Platform/Renderer backends
153-
ImGui_ImplWin32_InitForOpenGL(window.m_hHWND.get());
153+
ImGui_ImplWin32_InitForOpenGL(window.m_hWND);
154154
ImGui_ImplOpenGL3_Init();
155155

156156
if (io.ConfigFlags & ImGuiConfigFlags_ViewportsEnable)
@@ -165,6 +165,7 @@ int APIENTRY wWinMain(_In_ HINSTANCE hInstance,
165165
platform_io.Renderer_SwapBuffers = Hook_Renderer_SwapBuffers;
166166
platform_io.Platform_RenderWindow = Hook_Platform_RenderWindow;
167167
}
168+
168169
//ImGui::GetIO().ConfigViewportsNoDecoration = false;
169170

170171
// Main loop
@@ -174,6 +175,9 @@ int APIENTRY wWinMain(_In_ HINSTANCE hInstance,
174175

175176
while (!done)
176177
{
178+
static bool swap_styles = false; // Make sure we swap styles outside the WndProc
179+
static bool borderless = true; // Borderless Window's default constructs to borderless
180+
177181
while (::PeekMessage(&msg, NULL, 0U, 0U, PM_REMOVE))
178182
{
179183
::TranslateMessage(&msg);
@@ -183,6 +187,14 @@ int APIENTRY wWinMain(_In_ HINSTANCE hInstance,
183187
}
184188
if (done) break;
185189

190+
// If we want to swap styles
191+
if (swap_styles)
192+
{
193+
borderless = !borderless;
194+
window.set_borderless(borderless);
195+
swap_styles = false;
196+
}
197+
186198
static auto render = [&]() {
187199
ImGui_ImplOpenGL3_NewFrame();
188200
ImGui_ImplWin32_NewFrame();
@@ -202,14 +214,14 @@ int APIENTRY wWinMain(_In_ HINSTANCE hInstance,
202214
{
203215
if (ImGui::Begin("Borderless Settings", 0, window_flags))
204216
{
205-
static SmartProperty<INT> window_mode{ 1 };
217+
static SmartProperty<INT> window_mode{ static_cast<int>(borderless) };
206218
ImGui::RadioButton("Windowed", &window_mode.m_Value, 0);
207219
ImGui::RadioButton("Borderless", &window_mode.m_Value, 1);
208-
if (window_mode.update()) window.set_borderless(window_mode.m_Value);
220+
if (window_mode.update()) swap_styles = true;
209221
}
210222
ImGui::End();
211223
}
212-
224+
213225
// Borderless Demo
214226
{
215227
// DWM api is undocumented, and has varying behavior between Windows Releases
@@ -235,7 +247,13 @@ int APIENTRY wWinMain(_In_ HINSTANCE hInstance,
235247
ImGui::Begin("DWM Accent Flags", 0, window_flags);
236248
ImGui::SeparatorText("DWM Accent Flags");
237249
static SmartProperty<INT> accent_flags{ 1 };
238-
ImGui::SliderInt("Accent Flags", &accent_flags.m_Value, 0, 255);
250+
ImGui::InputInt("Accent Flags", &accent_flags.m_Value, 0, 255);
251+
ImGui::End();
252+
253+
ImGui::Begin("DWM Animation id", 0, window_flags);
254+
ImGui::SeparatorText("DWM Animation id");
255+
static SmartProperty<INT> animation_id{ 0 };
256+
ImGui::SliderInt("Accent Flags", &animation_id.m_Value, 0, 32);
239257
ImGui::End();
240258

241259
ImGui::Begin("DWM Gradient", 0, window_flags);
@@ -251,19 +269,25 @@ int APIENTRY wWinMain(_In_ HINSTANCE hInstance,
251269
ImGui::ColorPicker4("##picker", (float*)&clear_color, ImGuiColorEditFlags_NoSidePreview | ImGuiColorEditFlags_NoSmallPreview);
252270
ImGui::End();
253271

254-
ImGui::Begin("DWM Animation id", 0, window_flags);
255-
ImGui::SeparatorText("DWM Animation id");
256-
static SmartProperty<INT> animation_id{ 0 };
257-
ImGui::SliderInt("Accent Flags", &animation_id.m_Value, 0, 32);
272+
ImGui::Begin("ImGui Window Bg", 0, window_flags);
273+
ImGui::SeparatorText("ImGuiCol_WindowBgAlpha");
274+
static SmartProperty<float> bg_alpha{ 1 };
275+
ImGui::SliderFloat("BgAlpha", &bg_alpha.m_Value, 1, 0);
276+
if (bg_alpha.update()) {
277+
ImVec4 window_bg = ImGui::GetStyle().Colors[ImGuiCol_WindowBg];
278+
window_bg.w = bg_alpha.m_Value;
279+
ImGui::GetStyle().Colors[ImGuiCol_WindowBg] = window_bg;
280+
}
258281
ImGui::End();
259282

283+
260284
accent_policy.update();
261285
accent_flags.update();
262286
gradient_col.update();
263287
animation_id.update();
264288

265289

266-
static bool init_accents = true; //to apply default initialization
290+
static bool init_accents = false; //to apply default initialization
267291
if (accent_policy.has_changed() || accent_flags.has_changed()
268292
|| gradient_col.has_changed() || animation_id.has_changed()
269293
|| init_accents)
@@ -283,7 +307,7 @@ int APIENTRY wWinMain(_In_ HINSTANCE hInstance,
283307
sizeof(policy)
284308
};
285309

286-
SetWindowCompositionAttribute(window.m_hHWND.get(), &data);
310+
SetWindowCompositionAttribute(window.m_hWND, &data);
287311
}
288312
}
289313

@@ -365,7 +389,7 @@ int APIENTRY wWinMain(_In_ HINSTANCE hInstance,
365389
set_render = true;
366390
}
367391

368-
render(); //render when we leave message loop
392+
render(); // and render when we leave message loop
369393
}
370394

371395
return 0;

0 commit comments

Comments
 (0)