Using win32 I create a window like this:
RECT windowRect = {0, 0, width, height};
AdjustWindowRectEx(&windowRect, WS_OVERLAPPED | WS_CAPTION | WS_THICKFRAME, FALSE, WS_EX_APPWINDOW);
mWindowHandle = CreateWindow(wcex.lpszClassName, wcex.lpszClassName, WS_OVERLAPPED | WS_CAPTION | WS_THICKFRAME,
CW_USEDEFAULT, CW_USEDEFAULT, windowRect.right - windowRect.left, windowRect.bottom - windowRect.top,
nullptr, nullptr, mInstanceHandle, nullptr);
For example, right now I have 1920x1080 max, if I pass those values the client area will have 1920x1080, but the overall window size will be too large, dissapearing off-screen.
width and height are arbitary values passed to the function. The effect I want to achieve is that if it is set to the maximum (or above) the resolution of the screen, the whole window will be clamped to fit the screen.
Is there an easy way to do this in win32?
Related
RECT WindowMainRect{ 0, 0, 400, 400 };
HWND WindowMain;
HWND WindowChild;
INT main()
{
AdjustWindowRectEx(&WindowMainRect, WS_SYSMENU, FALSE, 0);
WNDCLASS WindowMainClass;
memset(&WindowMainClass, 0, sizeof(WNDCLASS));
WindowMainClass.lpfnWndProc = WindowMainProcedure;
WindowMainClass.lpszClassName = L"WindowMain";
if (!RegisterClass(&WindowMainClass)) MessageBox(NULL, L"Error", L"Register Class Error", MB_ICONWARNING);
WindowMain = CreateWindowEx(0, L"WindowMain", L"Window", WS_SYSMENU, CW_USEDEFAULT, CW_USEDEFAULT, WindowMainRect.right, WindowMainRect.bottom, NULL, NULL, NULL, NULL);
ShowWindow(WindowMain, TRUE);
WindowChild = CreateWindow(L"Edit", NULL, WS_CHILD | WS_BORDER, 0, 0, 350, 350, WindowMain, NULL, NULL, NULL);
ShowWindow(WindowChild, TRUE);
MSG Msg;
Msg.message = WM_NULL;
while (Msg.message != WM_QUIT)
{
if (PeekMessage(&Msg, NULL, 0, 0, PM_REMOVE))
{
TranslateMessage(&Msg);
DispatchMessage(&Msg);
}
}
}
The code above creates a window by 400 x 400 and adjusted by WS_SYSMENU.
Assumptively, the client area of this window should be 400 x 400 pixels with a 1:1 aspect ratio,
I create a child window at (0,0) with the width of 350, and height of 350.
Although, visibly the client area is not 1:1 and is vertically stretched.
Using photoshop I counted the pixels, I expected it to be above 400 pixels vertically and 400 pixels horizontally,
Although the results returned ~386 pixels in width, and ~394 pixels in height.
How do I fix this and have the client area 1:1?
I have a window, which I want it to behave like a toggle button. Once clicked it will add 4px border and clicking after will make the border disappear. I figured how to make the window behave like a toggle button using BS_PUSHLIKE and Button_SetCheck() but can't seem to figure out how to adjust the border size for this window.
Thanks to all who take their time to help
Maybe you can use MoveWindow to resize the window, and then draw the border yourself, like this,
Draw a borderless window first:
HWND hWnd = CreateWindowW(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW,
100, 100, 800, 600, nullptr, nullptr, hInstance, nullptr);
LONG lStyle = GetWindowLong(hWnd, GWL_STYLE);
lStyle &= ~(WS_CAPTION | WS_THICKFRAME | WS_MINIMIZEBOX | WS_MAXIMIZEBOX | WS_SYSMENU);
SetWindowLong(hWnd, GWL_STYLE, lStyle);
Then handle the window border in the WM_LBUTTONDOWN message:
int num = 0;
case WM_LBUTTONDOWN:
{
RECT rcWind;
HDC dc = GetDC(hWnd);
GetWindowRect(hWnd, &rcWind);
if (num >= 0)
{
num--;
RECT rcClient;
MoveWindow(hWnd, rcWind.left - 4, rcWind.top - 4, 8 + rcWind.right - rcWind.left, 8 + rcWind.bottom - rcWind.top, TRUE);
GetClientRect(hWnd, &rcClient);
HPEN hPen = CreatePen(PS_SOLID, 4, RGB(255, 128, 1));
HGDIOBJ hOldPen = SelectObject(dc, hPen);
Rectangle(dc, rcClient.left, rcClient.top, rcClient.right, rcClient.bottom);
DeleteObject(hPen);
}
else if (num < 0)
{
MoveWindow(hWnd, rcWind.left + 4, rcWind.top + 4, rcWind.right - rcWind.left - 8, rcWind.bottom - rcWind.top - 8, TRUE);
num++;
}
}
break;
When I create my window, it is smaller than the width and height specified, that should be 840x840. The actual window created is only approx 825x782 (not including menus etc). I can't work out why the window is small and have never had this problem before. Thanks in advance.
CreateWindowW(wc.lpszClassName, L"Window",
WS_OVERLAPPEDWINDOW | WS_VISIBLE, 100, 200, 840, 840,
NULL, NULL, NULL, NULL);
The solution is to use AdjustWindowRect function as commented by Simon Mourier, where rect is the size of the client window.
RECT rect = {0, 0, width, height};
AdjustWindowRect(&rect, WS_OVERLAPPEDWINDOW, true);
CreateWindowW(wc.lpszClassName, L"Window",
WS_OVERLAPPEDWINDOW | WS_VISIBLE, 100, 200,
rect.right - rect.left, rect.bottom - rect.top,
NULL, NULL, hPrevInst, NULL);
How to set a window to the right-down corner of the screen (not including the taskbar)? Can I accomplish it with CreateWindowEx? But I only saw CW_USEDEFAULT and there is no CW_ to set it to the corner.
HWND hwnd = CreateWindowEx(
NULL,
DUCKPROC_CLASS_NAME,
DUCKPROC_WINDOW_TIP_NAME,
WS_BORDER| WS_VISIBLE,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
NULL,
NULL,
GetModuleHandle(NULL),
NULL
);
This is an example of placing the window to right bottom corner. (Here I set window's width and height are both 200.)
RECT desktopRect;
if (!GetWindowRect(GetDesktopWindow(), &desktopRect))
return FALSE;
int windowWidth = 200;
int windowHeight = 200;
int posX = desktopRect.right - windowWidth;
int posY = desktopRect.bottom - windowHeight;
HWND hWnd = CreateWindow(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW,
posX, posY, windowWidth, windowHeight, nullptr, nullptr, hInstance, nullptr);
You can use CreateWindowEx but you don't have to because:
Creates an overlapped, pop-up, or child window with an extended window
style; otherwise, this function is identical to the CreateWindow
function.
I am using Window 7 and I wanted to add a transparent border to my window. The same border as we can see on skype for instance.
Window Creation:
hWnd = CreateWindow(L"Example", 0, WS_POPUP, 100, 100, 400, 500, NULL, NULL, hinst, NULL);
Is there a way to add the transparent border to WS_POPUP window without the WS_SYSMENU?