Код: Выделить всё
// Sample.cpp
#ifndef UNICODE
#define UNICODE
#endif
#include "Sample.h"
int WINAPI wWinMain(
_In_ HINSTANCE hInstance,
_In_opt_ HINSTANCE hPrevInstance,
_In_ LPWSTR lpCmdLine,
_In_ int nCmdShow) {
// Register main window class
LPCTSTR mainWndClassName = MAIN_WND_CLASS_NAME;
LPCTSTR mainWndTitle = APP_TITLE;
WNDCLASSEX mainWndClass = {};
mainWndClass.cbSize = sizeof(WNDCLASSEX);
mainWndClass.lpfnWndProc = WindowProc;
mainWndClass.hInstance = hInstance;
mainWndClass.hIcon = (HICON) ICON;
mainWndClass.lpszClassName = mainWndClassName;
RegisterClassEx(&mainWndClass);
// Generate main window
HWND mainWnd = CreateWindowEx(
0,
mainWndClassName,
mainWndTitle,
WS_OVERLAPPEDWINDOW & ~WS_MAXIMIZEBOX & ~WS_THICKFRAME,
CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
HWND_MESSAGE,
NULL,
hInstance,
NULL);
if (mainWnd == NULL) {
return BAD_HWND;
}
// Register hotkeys
// TODO [L]: Refactor
if (!RegisterHotKey(mainWnd,
ID_HK_ALT_CEN_WND_1,
MOD_CONTROL | MOD_SHIFT,
'1')) {
return BAD_KEY;
}
// Start message loop
MSG msg = {};
while (GetMessage(&msg, NULL, 0, 0) > 0) {
TranslateMessage(&msg);
DispatchMessage(&msg);
}
// Unregister hotkeys
UnregisterHotKey(NULL, ID_HK_ALT_CEN_WND_1);
return (int) msg.wParam;
}
Код: Выделить всё
// Sample.h
#pragma once
#include "framework.h"
/* ********** FORWARD DECLARATIONS ********** */
LRESULT CALLBACK WindowProc(HWND, UINT, WPARAM, LPARAM);
// MESSAGE HANDLERS
void HandleHotKeyMessage(HWND, WPARAM, LPARAM);
void HandlePaintMessage(HWND);
// HELPER FUNCTIONS
LONG CenterWindow(HWND,
LONG flag = NULL,
LONG newHWndWidth = 80,
LONG newHWndHeight = 90);
LONG ResizeWindow(HWND, LONG, LONG);
/* ********** FUNCTION DEFINITIONS ********** */
LRESULT CALLBACK WindowProc(HWND hWnd,
UINT uMsg,
WPARAM wParam,
LPARAM lParam) {
switch (uMsg) {
case WM_PAINT: {
HandlePaintMessage(hWnd);
break;
}
case WM_HOTKEY: {
HandleHotKeyMessage(hWnd, wParam, lParam);
break;
}
default: {
return DefWindowProc(hWnd, uMsg, wParam, lParam);
}
}
return SUCCESS;
}
// MESSAGE HANDLERS
void HandleHotKeyMessage(HWND hWnd, WPARAM wParam, LPARAM lParam) {
// TODO [H]: Fix. Prevent desktop from resizing when hotkey is pressed from
// within an active desktop.
HWND foregroundWnd = GetForegroundWindow();
switch (wParam) {
case ID_HK_ALT_CEN_WND_1: {
CenterWindow(foregroundWnd, MOD_WND_SIZE, 20, 30);
break;
}
default: {
break;
}
}
}
void HandlePaintMessage(HWND hWnd) {
PAINTSTRUCT ps = {};
HDC hdc = BeginPaint(hWnd, &ps);
FillRect(hdc, &ps.rcPaint, (HBRUSH) (COLOR_WINDOW + 1));
EndPaint(hWnd, &ps);
}
// HELPER FUNCTIONS
LONG CenterWindow(HWND hWnd,
LONG flag,
LONG newHWndWidth,
LONG newHWndHeight) {
if (hWnd == NULL) {
return BAD_HWND;
}
// Get window dimensions
RECT rect = {};
if (GetWindowRect(hWnd, &rect)) {
LONG hWndWidth, hWndHeight;
LONG workAreaWidth, workAreaHeight;
LONG leftEdgeXCoord, topEdgeYCoord;
// Get work area dimensions
RECT workArea = {};
SystemParametersInfo(SPI_GETWORKAREA, NULL, &workArea, NULL);
workAreaWidth = workArea.right - workArea.left;
workAreaHeight = workArea.bottom - workArea.top;
// Compute centered window dimensions
if (flag == MOD_WND_SIZE) {
hWndWidth = (LONG) std::ceil(workAreaWidth * (newHWndWidth / 100.0));
hWndHeight = (LONG) std::ceil(workAreaHeight * (newHWndHeight / 100.0));
} else {
hWndWidth = rect.right - rect.left;
hWndHeight = rect.bottom - rect.top;
}
// Compute edge coordinates used to center window
leftEdgeXCoord = (workAreaWidth - hWndWidth) / 2;
topEdgeYCoord = (workAreaHeight - hWndHeight) / 2;
// Undo window maximization if necessary
if (IsZoomed(hWnd) && flag == MOD_WND_SIZE) {
ShowWindow(hWnd, SW_RESTORE);
}
// Reposition window
MoveWindow(hWnd,
leftEdgeXCoord,
topEdgeYCoord,
hWndWidth,
hWndHeight,
TRUE);
return SUCCESS;
} else {
return BAD_RECT_READ;
}
}
LONG ResizeWindow(HWND hWnd, LONG widthChange, LONG heightChange) {
if (hWnd == NULL) {
return BAD_HWND;
}
// Get window dimensions
RECT rect = {};
if (GetWindowRect(hWnd, &rect)) {
LONG newHWndWidth, newHWndHeight;
LONG leftEdgeXCoord, topEdgeYCoord;
LONG minWidth, minHeight;
// Compute new window dimensions with change factor taken into account
newHWndWidth = (rect.right - rect.left) + widthChange;
newHWndHeight = (rect.bottom - rect.top) + heightChange;
// Get work area dimensions
RECT workArea = {};
SystemParametersInfo(SPI_GETWORKAREA, NULL, &workArea, NULL);
// Compute minimum window dimensions as a percentage of work area
// dimensions
minWidth = (LONG) std::ceil((workArea.right - workArea.left) * 0.2);
minHeight = (LONG) std::ceil((workArea.bottom - workArea.left) * 0.2);
// Adjust window dimensions if minimum is reached
if (newHWndWidth
Подробнее здесь: [url]https://stackoverflow.com/questions/78747911/how-to-prevent-resizing-desktop-icon-window-with-hotkey-in-windows-application[/url]