Skip to content

Instantly share code, notes, and snippets.

@hcs64 hcs64/main.cpp
Last active Aug 28, 2018

Embed
What would you like to do?
Fullscreen test
#include <windows.h>
#include <shobjidl.h>
void MarkFullscreen(HWND hWnd, bool fullscreen);
void
Fullscreen(HWND hWnd, bool removeFrame, bool fillScreen, bool less1Width, bool mark)
{
if (removeFrame) {
LONG_PTR style = GetWindowLong(hWnd, GWL_STYLE);
SetWindowLongPtr(hWnd, GWL_STYLE, style & ~(WS_CAPTION | WS_THICKFRAME));
}
if (fillScreen) {
/// Get monitor size
HMONITOR hMon = MonitorFromWindow(hWnd, MONITOR_DEFAULTTONEAREST);
MONITORINFO info = { 0 };
info.cbSize = sizeof(info);
GetMonitorInfo(hMon, &info);
// Resize to fill the monitor
RECT sr(info.rcMonitor);
int width = sr.right - sr.left;
int height = sr.bottom - sr.top;
if (less1Width) width -= 1;
SetWindowPos(hWnd, NULL,
sr.left, sr.top,
width, height,
SWP_NOZORDER | SWP_NOACTIVATE | SWP_FRAMECHANGED);
}
if (mark) {
MarkFullscreen(hWnd, true);
}
}
void
Reset(HWND hWnd, RECT sr)
{
LONG_PTR style = GetWindowLong(hWnd, GWL_STYLE);
SetWindowLongPtr(hWnd, GWL_STYLE, style | WS_CAPTION | WS_THICKFRAME);
int width = sr.right - sr.left;
int height = sr.bottom - sr.top;
SetWindowPos(hWnd, NULL,
sr.left, sr.top,
width, height,
SWP_NOZORDER | SWP_NOACTIVATE | SWP_FRAMECHANGED);
MarkFullscreen(hWnd, false);
}
void MarkFullscreen(HWND hWnd, bool fullscreen)
{
// Mark as a fullscreen window to overlap the taskbar
ITaskbarList *list = nullptr;
ITaskbarList2 *list2 = nullptr;
if (!SUCCEEDED(CoInitialize(0))) return;
SUCCEEDED(
CoCreateInstance(CLSID_TaskbarList, nullptr, CLSCTX_INPROC_SERVER, IID_ITaskbarList, (LPVOID*)&list)
) && SUCCEEDED(
list->HrInit()
) && SUCCEEDED(
list->QueryInterface(IID_ITaskbarList2, (LPVOID*)&list2)
) && SUCCEEDED(
list2->MarkFullscreenWindow(hWnd, fullscreen ? TRUE : FALSE)
);
if (list2) list2->Release();
if (list) list->Release();
CoUninitialize();
}
HBRUSH g_red = NULL;
HINSTANCE g_hInstance = NULL;
LRESULT CALLBACK
WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
static bool extendClientArea = false;
static RECT savedPos = { 0 };
static HWND hRemoveFrame = NULL;
static HWND hFillScreen = NULL;
static HWND hLess1Button = NULL;
static HWND hMark = NULL;
static HWND hExtendClientArea = NULL;
static HWND hGoButton = NULL;
static HWND hResetButton = NULL;
const int removeFrame = 1;
const int fillScreen = 2;
const int less1Button = 3;
const int markButton = 4;
const int ECAButton = 5;
const int goButton = 6;
const int resetButton = 7;
switch (msg)
{
case WM_CREATE:
{
const DWORD buttonStyle = WS_VISIBLE | WS_CHILD | BS_DEFPUSHBUTTON;
const DWORD checkStyle = WS_VISIBLE | WS_CHILD | BS_AUTOCHECKBOX;
hRemoveFrame = CreateWindow("BUTTON", "Remove Frame", checkStyle, 15, 15, 150, 25, hWnd, (HMENU)removeFrame, g_hInstance, NULL);
hFillScreen = CreateWindow("BUTTON", "Fill Screen", checkStyle, 15, 45, 150, 25, hWnd, (HMENU)fillScreen, g_hInstance, NULL);
hLess1Button = CreateWindow("BUTTON", "Width - 1", checkStyle, 40, 75, 125, 25, hWnd, (HMENU)less1Button, g_hInstance, NULL);
hMark = CreateWindow("BUTTON", "Mark Fullscreen", checkStyle, 15, 105, 150, 25, hWnd, (HMENU)markButton, g_hInstance, NULL);
hExtendClientArea = CreateWindow("BUTTON", "Extend Client Area", checkStyle, 15, 135, 150, 25, hWnd, (HMENU)ECAButton, g_hInstance, NULL);
hGoButton = CreateWindow("BUTTON", "Go", buttonStyle, 15, 165, 150, 25, hWnd, (HMENU)goButton, g_hInstance, NULL);
hResetButton = CreateWindow("BUTTON", "Reset", buttonStyle, 15, 195, 150, 25, hWnd, (HMENU)resetButton, g_hInstance, NULL);
CheckDlgButton(hWnd, removeFrame, BST_CHECKED);
CheckDlgButton(hWnd, fillScreen, BST_CHECKED);
CheckDlgButton(hWnd, less1Button, BST_UNCHECKED);
CheckDlgButton(hWnd, markButton, BST_CHECKED);
CheckDlgButton(hWnd, ECAButton, BST_UNCHECKED);
GetWindowRect(hWnd, &savedPos);
break;
}
case WM_NCCALCSIZE:
if (extendClientArea) {
return 0L;
}
else {
break;
}
case WM_COMMAND:
if (LOWORD(wParam) == goButton && HIWORD(wParam) == BN_CLICKED) {
Fullscreen(hWnd,
IsDlgButtonChecked(hWnd, removeFrame) == BST_CHECKED,
IsDlgButtonChecked(hWnd, fillScreen) == BST_CHECKED,
IsDlgButtonChecked(hWnd, less1Button) == BST_CHECKED,
IsDlgButtonChecked(hWnd, markButton) == BST_CHECKED);
return 0L;
} else if (LOWORD(wParam) == resetButton && HIWORD(wParam) == BN_CLICKED) {
Reset(hWnd, savedPos);
extendClientArea = false;
CheckDlgButton(hWnd, ECAButton, BST_UNCHECKED);
SetWindowPos(hWnd, NULL, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE | SWP_NOZORDER | SWP_NOACTIVATE | SWP_FRAMECHANGED);
return 0L;
}
else if (LOWORD(wParam) == ECAButton) {
bool newValue = extendClientArea;
if (HIWORD(wParam) == BN_CLICKED) {
extendClientArea = IsDlgButtonChecked(hWnd, ECAButton) == BST_CHECKED;
}
if (newValue != extendClientArea) {
SetWindowPos(hWnd, NULL, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE | SWP_NOZORDER | SWP_NOACTIVATE | SWP_FRAMECHANGED);
}
}
break;
case WM_PAINT:
{
// Outline the window in red
PAINTSTRUCT ps;
HDC hdc = BeginPaint(hWnd, &ps);
RECT r;
GetClientRect(hWnd, &r);
FillRect(hdc, &r, g_red);
r.top += 10; r.left += 10; r.right -= 10; r.bottom -= 10;
FillRect(hdc, &r, (HBRUSH)COLOR_WINDOW);
EndPaint(hWnd, &ps);
return 0L;
}
case WM_SIZE:
RedrawWindow(hWnd, NULL, NULL, RDW_INVALIDATE | RDW_INTERNALPAINT);
break;
case WM_CLOSE:
PostQuitMessage(0);
return 0L;
default:
break;
}
return DefWindowProc(hWnd, msg, wParam, lParam);
}
int CALLBACK
WinMain(HINSTANCE hInstance, HINSTANCE, LPSTR, int)
{
g_red = CreateSolidBrush(RGB(255, 0, 0));
g_hInstance = hInstance;
WNDCLASS wc = { 0 };
wc.lpfnWndProc = WndProc;
wc.hInstance = hInstance;
wc.hbrBackground = (HBRUSH)COLOR_BACKGROUND;
wc.hCursor = (HCURSOR)LoadCursor(NULL, IDC_ARROW);
wc.lpszClassName = "Fullscreen Test";
RegisterClass(&wc);
HWND hWnd = CreateWindow(
wc.lpszClassName,
"Fullscreen Test",
WS_OVERLAPPEDWINDOW | WS_CLIPCHILDREN | WS_VISIBLE,
0, 0, 250, 300,
NULL, NULL,
hInstance,
NULL);
MSG msg;
while (GetMessage(&msg, NULL, 0, 0) > 0) {
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.