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);
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?
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?
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?
I develop a c++ application with MinGW compiler.
I changed the CreateWindow() functions default font, something like this:
HFONT font;
font = CreateFont(15, 0, 0, 0, FW_NORMAL, FALSE, FALSE, FALSE, DEFAULT_CHARSET, 0, 0, 0, 0, "Tahoma");
HWND btn;
btn = CreateWindowEx(BS_PUSHBUTTON, "BUTTON", "Title",
WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON,
0, 0, 0, 0,
hwnd,
(HMENU)ID_BUTTON,
GetModuleHandle(NULL),
0);
SendMessage(btn, WM_SETFONT, (WPARAM) font, TRUE);
But I created menu:
HMENU hMenu, hSubMenu;
hMenu = CreateMenu();
hSubMenu = CreatePopupMenu();
AppendMenuW(hSubMenu, MF_STRING, ID_FILE_OPEN_BTN, "Open" );
AppendMenuW(hMenu, MF_STRING | MF_POPUP, (UINT)hSubMenu, "File" );
But I dont know how to change the font in the AppendMenuW(), can anybody help me?