Skip to content

Instantly share code, notes, and snippets.

@Soulstorm50
Created October 29, 2016 09:25
Show Gist options
  • Save Soulstorm50/b43089a5d67732fcb34937e15b0608ea to your computer and use it in GitHub Desktop.
Save Soulstorm50/b43089a5d67732fcb34937e15b0608ea to your computer and use it in GitHub Desktop.
ResizeMoveWindows
#include <tchar.h>
#include <Windows.h>
#include <map>
#pragma comment(linker, "\"/manifestdependency:type='win32' \
name='Microsoft.Windows.Common-Controls' version='6.0.0.0' processorArchitecture='*' \
publicKeyToken='6595b64144ccf1df' language='*'\"")
LRESULT CALLBACK WindowProcedure(HWND, UINT, WPARAM, LPARAM);
VOID CALLBACK TimerProc(HWND hwnd, UINT uMsg, UINT_PTR idEvent, DWORD dwTime);
BOOL CALLBACK EnumWindowsProc(HWND hWindow, LPARAM lParam);
void ChangeWindowPosition(HWND hwnd, int& shiftX, int& shiftY);
void ClearMemory();
int screenWidth = 0;
int screenHeight = 0;
struct winInfo
{
bool isResized = false;
int shiftX = 25;
int shiftY = 0;
};
std::map<HWND, winInfo*> windowCollection;
INT WINAPI _tWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPTSTR szCmdLine, INT nCmdShow)
{
TCHAR szClassWindow[] = TEXT("Win32Application");
screenWidth = GetSystemMetrics(SM_CXSCREEN);
screenHeight = GetSystemMetrics(SM_CYSCREEN);
WNDCLASSEX wc = {};
wc.cbClsExtra = 0;
wc.cbSize = sizeof(wc);
wc.cbWndExtra = 0;
wc.hbrBackground = (HBRUSH)GetStockObject(LTGRAY_BRUSH);
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wc.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
wc.hInstance = hInstance;
wc.lpfnWndProc = WindowProcedure;
wc.lpszClassName = szClassWindow;
wc.lpszMenuName = NULL;
wc.style = CS_HREDRAW | CS_VREDRAW;
INT nExitCode = 0;
if (RegisterClassEx(&wc))
{
HWND hWindow = CreateWindowEx(0, szClassWindow, TEXT("Resize and move windows"),
WS_POPUPWINDOW | WS_CAPTION, 50, 50, 200, 200, NULL, NULL, hInstance, NULL);
ShowWindow(hWindow, nCmdShow);
UpdateWindow(hWindow);
MSG message = {};
EnumWindows(EnumWindowsProc, (LPARAM)hWindow);
SetTimer(hWindow, 1, 500, TimerProc);//чтобы двигать окна
SetTimer(hWindow, 2, 100, TimerProc);//чтобы задать размер окон
while (GetMessage(&message, NULL, 0, 0))
{
TranslateMessage(&message);
DispatchMessage(&message);
}
nExitCode = message.wParam;
}
return nExitCode;
}
VOID CALLBACK TimerProc(HWND hwnd, UINT uMsg, UINT_PTR idEvent, DWORD dwTime)
{
if (idEvent == 1)
{
for (auto item : windowCollection)
{
if (item.second->isResized)
ChangeWindowPosition(item.first, item.second->shiftX, item.second->shiftY);
}
}
if (idEvent == 2)
{
for (auto item : windowCollection)
{
if (!item.second->isResized)
{
MoveWindow(item.first, 0, 0, 200, 200, TRUE);
item.second->isResized = true;
break;
}
}
}
}
void ChangeWindowPosition(HWND hwnd, int& shiftX, int& shiftY)
{
RECT winRect = {};
GetWindowRect(hwnd, &winRect);
if (shiftX > 0 && shiftY == 0)
{
if (winRect.right == screenWidth)
{
shiftX = 0;
shiftY = 25;
}
else if (winRect.right + shiftX > screenWidth)
{
shiftX = screenWidth - winRect.right;
}
}
else if (shiftX == 0 && shiftY > 0)
{
if (winRect.bottom == screenHeight)
{
shiftX = -25;
shiftY = 0;
}
else if (winRect.bottom + shiftY > screenHeight)
{
shiftY = screenHeight - winRect.bottom;
}
}
else if (shiftX < 0 && shiftY == 0)
{
if (winRect.left == 0)
{
shiftX = 0;
shiftY = -25;
}
else if (winRect.left + shiftX < 0)
{
shiftX = -1 * winRect.left;
}
}
else if (shiftX == 0 && shiftY < 0)
{
if (winRect.top == 0)
{
shiftX = 25;
shiftY = 0;
}
else if (winRect.top + shiftY < 0)
{
shiftY = -1 * winRect.top;
}
}
MoveWindow(hwnd, winRect.left + shiftX, winRect.top + shiftY, 200, 200, TRUE);
}
void ClearMemory()
{
for (auto item : windowCollection)
delete item.second;
}
BOOL CALLBACK EnumWindowsProc(HWND hWindow, LPARAM lParam)
{
windowCollection[hWindow] = new winInfo;
return TRUE;
}
LRESULT CALLBACK WindowProcedure(HWND hWindow, UINT nMessage, WPARAM wParam, LPARAM lParam)
{
LRESULT result = 0;
switch (nMessage)
{
case WM_DESTROY:
ClearMemory();
PostQuitMessage(0);
break;
default:
result = DefWindowProc(hWindow, nMessage, wParam, lParam);
break;
}
return result;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment