├── CMakeLists.txt ├── LICENSE.txt ├── README.md └── src ├── BorderlessWindow.cpp ├── BorderlessWindow.hpp └── main.cpp /CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.15) 2 | 3 | project(BorderlessWindow) 4 | 5 | # WIN32 for a /subsystem:windows program... 6 | add_executable(BorderlessWindow WIN32 7 | src/main.cpp 8 | src/BorderlessWindow.cpp 9 | ) 10 | 11 | # but with 'main' entry point 12 | target_link_options(BorderlessWindow PRIVATE /entry:mainCRTStartup) 13 | target_link_libraries(BorderlessWindow PRIVATE dwmapi) 14 | target_compile_options(BorderlessWindow PRIVATE /diagnostics:caret /permissive- /W4) 15 | target_compile_definitions(BorderlessWindow PRIVATE UNICODE _UNICODE NOMINMAX) 16 | -------------------------------------------------------------------------------- /LICENSE.txt: -------------------------------------------------------------------------------- 1 | Creative Commons Legal Code 2 | 3 | CC0 1.0 Universal 4 | 5 | CREATIVE COMMONS CORPORATION IS NOT A LAW FIRM AND DOES NOT PROVIDE 6 | LEGAL SERVICES. DISTRIBUTION OF THIS DOCUMENT DOES NOT CREATE AN 7 | ATTORNEY-CLIENT RELATIONSHIP. CREATIVE COMMONS PROVIDES THIS 8 | INFORMATION ON AN "AS-IS" BASIS. CREATIVE COMMONS MAKES NO WARRANTIES 9 | REGARDING THE USE OF THIS DOCUMENT OR THE INFORMATION OR WORKS 10 | PROVIDED HEREUNDER, AND DISCLAIMS LIABILITY FOR DAMAGES RESULTING FROM 11 | THE USE OF THIS DOCUMENT OR THE INFORMATION OR WORKS PROVIDED 12 | HEREUNDER. 13 | 14 | Statement of Purpose 15 | 16 | The laws of most jurisdictions throughout the world automatically confer 17 | exclusive Copyright and Related Rights (defined below) upon the creator 18 | and subsequent owner(s) (each and all, an "owner") of an original work of 19 | authorship and/or a database (each, a "Work"). 20 | 21 | Certain owners wish to permanently relinquish those rights to a Work for 22 | the purpose of contributing to a commons of creative, cultural and 23 | scientific works ("Commons") that the public can reliably and without fear 24 | of later claims of infringement build upon, modify, incorporate in other 25 | works, reuse and redistribute as freely as possible in any form whatsoever 26 | and for any purposes, including without limitation commercial purposes. 27 | These owners may contribute to the Commons to promote the ideal of a free 28 | culture and the further production of creative, cultural and scientific 29 | works, or to gain reputation or greater distribution for their Work in 30 | part through the use and efforts of others. 31 | 32 | For these and/or other purposes and motivations, and without any 33 | expectation of additional consideration or compensation, the person 34 | associating CC0 with a Work (the "Affirmer"), to the extent that he or she 35 | is an owner of Copyright and Related Rights in the Work, voluntarily 36 | elects to apply CC0 to the Work and publicly distribute the Work under its 37 | terms, with knowledge of his or her Copyright and Related Rights in the 38 | Work and the meaning and intended legal effect of CC0 on those rights. 39 | 40 | 1. Copyright and Related Rights. A Work made available under CC0 may be 41 | protected by copyright and related or neighboring rights ("Copyright and 42 | Related Rights"). Copyright and Related Rights include, but are not 43 | limited to, the following: 44 | 45 | i. the right to reproduce, adapt, distribute, perform, display, 46 | communicate, and translate a Work; 47 | ii. moral rights retained by the original author(s) and/or performer(s); 48 | iii. publicity and privacy rights pertaining to a person's image or 49 | likeness depicted in a Work; 50 | iv. rights protecting against unfair competition in regards to a Work, 51 | subject to the limitations in paragraph 4(a), below; 52 | v. rights protecting the extraction, dissemination, use and reuse of data 53 | in a Work; 54 | vi. database rights (such as those arising under Directive 96/9/EC of the 55 | European Parliament and of the Council of 11 March 1996 on the legal 56 | protection of databases, and under any national implementation 57 | thereof, including any amended or successor version of such 58 | directive); and 59 | vii. other similar, equivalent or corresponding rights throughout the 60 | world based on applicable law or treaty, and any national 61 | implementations thereof. 62 | 63 | 2. Waiver. To the greatest extent permitted by, but not in contravention 64 | of, applicable law, Affirmer hereby overtly, fully, permanently, 65 | irrevocably and unconditionally waives, abandons, and surrenders all of 66 | Affirmer's Copyright and Related Rights and associated claims and causes 67 | of action, whether now known or unknown (including existing as well as 68 | future claims and causes of action), in the Work (i) in all territories 69 | worldwide, (ii) for the maximum duration provided by applicable law or 70 | treaty (including future time extensions), (iii) in any current or future 71 | medium and for any number of copies, and (iv) for any purpose whatsoever, 72 | including without limitation commercial, advertising or promotional 73 | purposes (the "Waiver"). Affirmer makes the Waiver for the benefit of each 74 | member of the public at large and to the detriment of Affirmer's heirs and 75 | successors, fully intending that such Waiver shall not be subject to 76 | revocation, rescission, cancellation, termination, or any other legal or 77 | equitable action to disrupt the quiet enjoyment of the Work by the public 78 | as contemplated by Affirmer's express Statement of Purpose. 79 | 80 | 3. Public License Fallback. Should any part of the Waiver for any reason 81 | be judged legally invalid or ineffective under applicable law, then the 82 | Waiver shall be preserved to the maximum extent permitted taking into 83 | account Affirmer's express Statement of Purpose. In addition, to the 84 | extent the Waiver is so judged Affirmer hereby grants to each affected 85 | person a royalty-free, non transferable, non sublicensable, non exclusive, 86 | irrevocable and unconditional license to exercise Affirmer's Copyright and 87 | Related Rights in the Work (i) in all territories worldwide, (ii) for the 88 | maximum duration provided by applicable law or treaty (including future 89 | time extensions), (iii) in any current or future medium and for any number 90 | of copies, and (iv) for any purpose whatsoever, including without 91 | limitation commercial, advertising or promotional purposes (the 92 | "License"). The License shall be deemed effective as of the date CC0 was 93 | applied by Affirmer to the Work. Should any part of the License for any 94 | reason be judged legally invalid or ineffective under applicable law, such 95 | partial invalidity or ineffectiveness shall not invalidate the remainder 96 | of the License, and in such case Affirmer hereby affirms that he or she 97 | will not (i) exercise any of his or her remaining Copyright and Related 98 | Rights in the Work or (ii) assert any associated claims and causes of 99 | action with respect to the Work, in either case contrary to Affirmer's 100 | express Statement of Purpose. 101 | 102 | 4. Limitations and Disclaimers. 103 | 104 | a. No trademark or patent rights held by Affirmer are waived, abandoned, 105 | surrendered, licensed or otherwise affected by this document. 106 | b. Affirmer offers the Work as-is and makes no representations or 107 | warranties of any kind concerning the Work, express, implied, 108 | statutory or otherwise, including without limitation warranties of 109 | title, merchantability, fitness for a particular purpose, non 110 | infringement, or the absence of latent or other defects, accuracy, or 111 | the present or absence of errors, whether or not discoverable, all to 112 | the greatest extent permissible under applicable law. 113 | c. Affirmer disclaims responsibility for clearing rights of other persons 114 | that may apply to the Work or any use thereof, including without 115 | limitation any person's Copyright and Related Rights in the Work. 116 | Further, Affirmer disclaims responsibility for obtaining any necessary 117 | consents, permissions or other rights required for any use of the 118 | Work. 119 | d. Affirmer understands and acknowledges that Creative Commons is not a 120 | party to this document and has no duty or obligation with respect to 121 | this CC0 or use of the Work. 122 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | BorderlessWindow 2 | ================ 3 | 4 | This sample application demonstrates the necessary WinAPI calls and window 5 | messages to handle to produce a resizable, borderless window (similar to Steam, GitHub for Windows). 6 | 7 | 8 | 9 | ![sample](https://cloud.githubusercontent.com/assets/3340026/20039089/69b3b2f6-a43e-11e6-8a44-21599a34ceba.png) 10 | 11 | The borderless window can be resized, moved, and also supports all the Aero features a regular window has: 12 | 13 | - snapping to desktop halves/quadrants 14 | - shake to minimize all other windows 15 | - animations when minimizing, maximizing, restoring, snapping 16 | - native soft shadow around the window 17 | 18 | The sample should work on Windows 7, 8/8.1 and 10. 19 | 20 | What this example does *not* do: 21 | 22 | - Draw anything to the client area. You will need to fill the entire window with an opaque color, or the window frame may be visible inside your client area in borderless mode. 23 | In my use case I simply fill the D3D backbuffer covering the window's client area. 24 | - Calculate proper client/window size in windowed mode. You will need to use [AdjustWindowRect](https://msdn.microsoft.com/en-us/library/windows/desktop/ms632665(v=vs.85).aspx) and friends to calculate the correct window size for a desired client area size. 25 | 26 | Keybinds: 27 | 28 | - F8 enables/disables dragging in the borderless window to move it 29 | - F9 enables/disables resizing the borderless window 30 | - F10 toggles between borderless and windowed mode 31 | - F11 toggles the aero shadow when in borderless mode 32 | -------------------------------------------------------------------------------- /src/BorderlessWindow.cpp: -------------------------------------------------------------------------------- 1 | #include "BorderLessWindow.hpp" 2 | 3 | #include 4 | #include 5 | 6 | #include 7 | #include 8 | #include 9 | 10 | namespace { 11 | // we cannot just use WS_POPUP style 12 | // WS_THICKFRAME: without this the window cannot be resized and so aero snap, de-maximizing and minimizing won't work 13 | // WS_SYSMENU: enables the context menu with the move, close, maximize, minize... commands (shift + right-click on the task bar item) 14 | // WS_CAPTION: enables aero minimize animation/transition 15 | // WS_MAXIMIZEBOX, WS_MINIMIZEBOX: enable minimize/maximize 16 | enum class Style : DWORD { 17 | windowed = WS_OVERLAPPEDWINDOW | WS_THICKFRAME | WS_CAPTION | WS_SYSMENU | WS_MINIMIZEBOX | WS_MAXIMIZEBOX, 18 | aero_borderless = WS_POPUP | WS_THICKFRAME | WS_CAPTION | WS_SYSMENU | WS_MAXIMIZEBOX | WS_MINIMIZEBOX, 19 | basic_borderless = WS_POPUP | WS_THICKFRAME | WS_SYSMENU | WS_MAXIMIZEBOX | WS_MINIMIZEBOX 20 | }; 21 | 22 | auto maximized(HWND hwnd) -> bool { 23 | WINDOWPLACEMENT placement; 24 | if (!::GetWindowPlacement(hwnd, &placement)) { 25 | return false; 26 | } 27 | 28 | return placement.showCmd == SW_MAXIMIZE; 29 | } 30 | 31 | /* Adjust client rect to not spill over monitor edges when maximized. 32 | * rect(in/out): in: proposed window rect, out: calculated client rect 33 | * Does nothing if the window is not maximized. 34 | */ 35 | auto adjust_maximized_client_rect(HWND window, RECT& rect) -> void { 36 | if (!maximized(window)) { 37 | return; 38 | } 39 | 40 | auto monitor = ::MonitorFromWindow(window, MONITOR_DEFAULTTONULL); 41 | if (!monitor) { 42 | return; 43 | } 44 | 45 | MONITORINFO monitor_info{}; 46 | monitor_info.cbSize = sizeof(monitor_info); 47 | if (!::GetMonitorInfoW(monitor, &monitor_info)) { 48 | return; 49 | } 50 | 51 | // when maximized, make the client area fill just the monitor (without task bar) rect, 52 | // not the whole window rect which extends beyond the monitor. 53 | rect = monitor_info.rcWork; 54 | } 55 | 56 | auto last_error(const std::string& message) -> std::system_error { 57 | return std::system_error( 58 | std::error_code(::GetLastError(), std::system_category()), 59 | message 60 | ); 61 | } 62 | 63 | auto window_class(WNDPROC wndproc) -> const wchar_t* { 64 | static const wchar_t* window_class_name = [&] { 65 | WNDCLASSEXW wcx{}; 66 | wcx.cbSize = sizeof(wcx); 67 | wcx.style = CS_HREDRAW | CS_VREDRAW; 68 | wcx.hInstance = nullptr; 69 | wcx.lpfnWndProc = wndproc; 70 | wcx.lpszClassName = L"BorderlessWindowClass"; 71 | wcx.hbrBackground = reinterpret_cast(COLOR_WINDOW + 1); 72 | wcx.hCursor = ::LoadCursorW(nullptr, IDC_ARROW); 73 | const ATOM result = ::RegisterClassExW(&wcx); 74 | if (!result) { 75 | throw last_error("failed to register window class"); 76 | } 77 | return wcx.lpszClassName; 78 | }(); 79 | return window_class_name; 80 | } 81 | 82 | auto composition_enabled() -> bool { 83 | BOOL composition_enabled = FALSE; 84 | bool success = ::DwmIsCompositionEnabled(&composition_enabled) == S_OK; 85 | return composition_enabled && success; 86 | } 87 | 88 | auto select_borderless_style() -> Style { 89 | return composition_enabled() ? Style::aero_borderless : Style::basic_borderless; 90 | } 91 | 92 | auto set_shadow(HWND handle, bool enabled) -> void { 93 | if (composition_enabled()) { 94 | static const MARGINS shadow_state[2]{ { 0,0,0,0 },{ 1,1,1,1 } }; 95 | ::DwmExtendFrameIntoClientArea(handle, &shadow_state[enabled]); 96 | } 97 | } 98 | 99 | auto create_window(WNDPROC wndproc, void* userdata) -> HWND { 100 | auto handle = CreateWindowExW( 101 | 0, window_class(wndproc), L"Borderless Window", 102 | static_cast(Style::aero_borderless), CW_USEDEFAULT, CW_USEDEFAULT, 103 | 1280, 720, nullptr, nullptr, nullptr, userdata 104 | ); 105 | if (!handle) { 106 | throw last_error("failed to create window"); 107 | } 108 | return handle; 109 | } 110 | } 111 | 112 | BorderlessWindow::BorderlessWindow() 113 | : handle{create_window(&BorderlessWindow::WndProc, this)} 114 | { 115 | set_borderless(borderless); 116 | set_borderless_shadow(borderless_shadow); 117 | ::ShowWindow(handle, SW_SHOW); 118 | } 119 | 120 | void BorderlessWindow::set_borderless(bool enabled) { 121 | Style new_style = (enabled) ? select_borderless_style() : Style::windowed; 122 | Style old_style = static_cast