Just a proof of concept, this will add an check option "Remember Size/Pos" to Window system menu.
#include <stdio.h>
#include <windows.h>
#define MENU_SEPARATOR_POS 5
#define MENU_SEPARATOR_ID 0x0D10
#define MENU_REMEMBER_ID 0x0C10
#define MENU_REMEMBER_POS 6
#define MENU_REMEMBER_CAPTION "Remember Size/Pos"
#define INFO_FORMAT "x%dy%dw%dh%dm%d"
char *iniPath = NULL;
void WriteData(HWND hwnd, BOOL save) {
char filePath[MAX_PATH];
if(GetModuleFileName(NULL, filePath, MAX_PATH)) {
RECT rect;
char cls[MAX_PATH];
char value[MAX_PATH];
BOOL max = GetWindowLong(hwnd, GWL_STYLE) & WS_MAXIMIZE;
GetWindowRect(hwnd, &rect);
GetClassName(hwnd, cls, MAX_PATH);
sprintf(value, INFO_FORMAT, (int)rect.left, (int)rect.top, (int)(rect.right - rect.left), (int)(rect.bottom - rect.top), max ? 1 : 0);
WritePrivateProfileString(filePath, save ? cls : NULL, value, iniPath);
}
}
BOOL ReadData(HWND hwnd, int *iX, int *iY, int *iW, int *iH, BOOL *bMax) {
char filePath[MAX_PATH];
if(GetModuleFileName(NULL, filePath, MAX_PATH)) {
char value[MAX_PATH];
char cls[MAX_PATH];
GetClassName(hwnd, cls, MAX_PATH);
if(GetPrivateProfileString(filePath, cls, "", value, MAX_PATH, iniPath) && value[0] != '\0') {
sscanf(value, INFO_FORMAT, iX, iY, iW, iH, bMax);
return TRUE;
}
}
return FALSE;
}
__declspec(dllexport) LRESULT CALLBACK CBTProc(int nCode, WPARAM wParam, LPARAM lParam) {
if(nCode == HCBT_CREATEWND) {
CBT_CREATEWND *cbt = (CBT_CREATEWND*)lParam;
HMENU hm = GetSystemMenu((HWND)wParam, FALSE);
if(hm != NULL) {
BOOL max = FALSE;
UINT flags = ReadData((HWND)wParam, &cbt->lpcs->x, &cbt->lpcs->y, &cbt->lpcs->cx, &cbt->lpcs->cy, &max) ? MF_CHECKED : MF_UNCHECKED;
if(max)
cbt->lpcs->style = cbt->lpcs->style | WS_MAXIMIZE;
InsertMenu(hm, MENU_SEPARATOR_POS, MF_BYPOSITION | MF_SEPARATOR, MENU_SEPARATOR_ID, NULL);
InsertMenu(hm, MENU_REMEMBER_POS, MF_BYPOSITION | flags, MENU_REMEMBER_ID, MENU_REMEMBER_CAPTION);
}
} else if(nCode == HCBT_DESTROYWND/*nCode == HCBT_MOVESIZE || nCode == HCBT_MINMAX*/) {
HMENU hm = GetSystemMenu((HWND)wParam, FALSE);
if(hm != NULL)
WriteData((HWND)wParam, GetMenuState(hm, MENU_REMEMBER_ID, MF_BYCOMMAND) & MF_CHECKED);
}
return CallNextHookEx(NULL, nCode, wParam, lParam);
}
BOOL dup = FALSE;
__declspec(dllexport) LRESULT CALLBACK GetMsgProc(int nCode, WPARAM wParam, LPARAM lParam) {
MSG *msg = (MSG*)lParam;
if(msg->message == WM_SYSCOMMAND && msg->wParam == MENU_REMEMBER_ID) {
if(!dup) {
HMENU hm = GetSystemMenu(msg->hwnd, FALSE);
UINT flags = GetMenuState(hm, MENU_REMEMBER_ID, MF_BYCOMMAND) & MF_CHECKED ? MF_UNCHECKED : MF_CHECKED;
ModifyMenu(hm, MENU_REMEMBER_ID, MF_BYCOMMAND | flags, MENU_REMEMBER_ID, MENU_REMEMBER_CAPTION);
}
dup = !dup;
}
return CallNextHookEx(NULL, nCode, wParam, lParam);
}
__declspec(dllexport) int Hook() {
HHOOK cbtHook = NULL;
HHOOK wndHook = NULL;
HINSTANCE hDll = GetModuleHandle("spr.dll");
HOOKPROC proc;
if(hDll == NULL)
return -1;
proc = (HOOKPROC)GetProcAddress(hDll, "CBTProc");
cbtHook = SetWindowsHookEx(WH_CBT, proc, hDll, 0);
if(cbtHook == NULL)
return -2;
proc = (HOOKPROC)GetProcAddress(hDll, "GetMsgProc");
wndHook = SetWindowsHookEx(WH_GETMESSAGE, proc, hDll, 0);
if(wndHook == NULL)
return -3;
Sleep(INFINITE);
UnhookWindowsHookEx(cbtHook);
UnhookWindowsHookEx(wndHook);
return 1;
}
__declspec(dllexport) void CALLBACK Load(HWND hwnd, HINSTANCE hinst, LPSTR lpszCmdLine, int nCmdShow) {
Hook();
}
BOOL APIENTRY DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved) {
if(fdwReason == DLL_PROCESS_ATTACH) {
int size;
iniPath = malloc(MAX_PATH);
size = GetModuleFileName(hinstDLL, iniPath, MAX_PATH);
iniPath[size - 3] = 'i';
iniPath[size - 2] = 'n';
iniPath[size - 1] = 'i';
} else if(fdwReason == DLL_PROCESS_DETACH) {
free(iniPath);
}
return TRUE;
}
Compile with
gcc -shared -o spr.dll main.c -Wl,-- kill-at -s -Os
Run with
rundll32 spr.dll,Load
After that, new windows will have the option added and load their size/positions.
To disable, just kill the process. A side effect is that the existing windows will not have the menu removed, this is no big deal, as the option simply does nothing.
Be warned that the code has not been fully tested and can cause problems or unwanted side effects.