Skip to content

Instantly share code, notes, and snippets.

@nikanos
Created November 4, 2022 22:12
Show Gist options
  • Save nikanos/71457f821d000f51c2d65b5449434fec to your computer and use it in GitHub Desktop.
Save nikanos/71457f821d000f51c2d65b5449434fec to your computer and use it in GitHub Desktop.
C Win32 snippet to create a non-movable window by handling the WM_MOVING message
#include <windows.h>
LRESULT CALLBACK WinProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam);
TCHAR WinClassName[] = TEXT("NoMoveWinClass");
int WINAPI WinMain(
_In_ HINSTANCE hInstance,
_In_opt_ HINSTANCE hPrevInstance,
_In_ LPSTR lpCmdLine,
_In_ int nShowCmd)
{
HWND hwnd;
MSG msg;
WNDCLASSEX wcl;
wcl.cbClsExtra = 0;
wcl.cbSize = sizeof(WNDCLASSEX);
wcl.cbWndExtra = 0;
wcl.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
wcl.hCursor = LoadCursor(NULL, IDC_ARROW);
wcl.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wcl.hIconSm = NULL;
wcl.hInstance = hInstance;
wcl.lpfnWndProc = WinProc;
wcl.lpszClassName = WinClassName;
wcl.lpszMenuName = NULL;
wcl.style = 0;
if (!RegisterClassEx(&wcl))
return EXIT_FAILURE;
hwnd = CreateWindow(
WinClassName,
TEXT("Non-Movable Window"),
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
NULL,
NULL,
hInstance,
NULL);
ShowWindow(hwnd, nShowCmd);
UpdateWindow(hwnd);
while (GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return EXIT_SUCCESS;
}
LRESULT CALLBACK WinProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
static RECT wrect;
switch (message)
{
case WM_DESTROY:
PostQuitMessage(0);
break;
case WM_CREATE:
case WM_SIZE:
GetWindowRect(hwnd, &wrect);
break;
case WM_MOVING:
*(RECT*)lParam = wrect;
return TRUE; /*An application should return TRUE if it processes this message.*/
default:
return DefWindowProc(hwnd, message, wParam, lParam);
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment