Is there any Windows API that can "normalize" or "maximize" a window? - ruby

I was using Ruby's Win32API to MoveWindow to move a window and resize it.
But if the window is minimized, it won't show.
SetWindowPos works too, and has a flag to hide or show the window, but it is to only make a window visible or invisible, not about minimizing or normalizing.
I also tried SetForegroundWindow and SetActiveWindow and they won't work either. Is there a call to make a window normalized or maximized?

ShowWindow(hwnd, SW_RESTORE) may be what you're looking for. See: MSDN docs

Related

Is It Possible To Change a Window's Size in The Background

Related to, but a more general question than How to Maximize a Window in background?
It is possible to Minimize, Restore, Move and Resize, and/or Maximize an Application Window using the ShowWindow() and SetWindowPos() API's
Using these has the disadvantage of changing the Active Window and Z-Order
Is it possible to make these changes in The Background, so that the changes only become noticable the next time it is Activated?
A soultion using using API's or VB6 is preferred
"Rest assured that things will get worse, before they get a lot worse" - Anon.
SetWindowPos doesn't have to change the z-order or activate the window.
Use the SWP_NOZORDER flag to prevent the z-order from changing.
Use the SWP_NOOWNERZORDER flag to prevent the window's owner's z-order from changing (if the window is owned)
Use the SWP_NOACTIVATE flag to prevent the window from being activated.
These flags (and their values) are documented here.

Disabling the close button on a GLFW window

For my game using GLFW for windowing, I want to prohibit the user from closing the game window using the close button. Is there any cross-platform way to do this?
Use glfwSetWindowCloseCallback to set a callback that always returns GL_FALSE.
You must add glfwWindowHint(GLFW_DECORATED, GLFW_FALSE) before glfwCreateWindow() call, this remove minimize, maximize, close button and borders, the window cannot be resized anymore.

Hiding mouse cursor in multiscreen setup

I am trying to hide the mouse cursor using win32 API ShowCursor(FALSE), but on a multiscreen setup when the mouse gets to the other screen I don't get any mouse updates in windows.
Is there any way I can prevent this?
This is for a fullscreen video game and I don't seem to find any windows api that can do something like this.
From what I understand, your problem is not in hiding the mouse cursor, but in constraining it to your window?
In that case, the ClipCursor function should do the job.
{
RECT windowRect;
GetWindowRect(hWnd, &windowRect);
ClipCursor(&windowRect);
}
For a border-less full-screen window, it should be fine to do that once. You would need to repeat that step if your window's position or size ever changes or the window loses focus.
For game programming, there are likely better methods though, such as DirectInput, which provides an exclusive mouse handling mode (tutorials available) and does all that for you on a lower-level basis.
There are some discussions available about the different ways to handle this, for instance this one on the MSDN forums.
If, on the other hand, you want the cursor to be able to leave your window, and only hide it while it's over your window, you should handle the WM_SETCURSOR message and use SetCursor to hide the cursor.
case WM_SETCURSOR:
SetCursor(NULL);
return TRUE;

Problem when maximizing window in C++

My program needs to arbitrarily maximize any window on the current desktop. I achieve this by calling ShowWindow(hWnd, SW_MAXIMIZE), where hWnd is the HWND of the window I want to maximize. When that line of code executes, the window in question (here, Notepad) looks like this:
Everything seems fine, except for the fact that the window has not been positioned correctly, i.e. the window seems to be a few pixels to low, and the title bar does not look "squashed" like it should. Compared to how it should look when the maximize button is clicked, the problem is clearly visible:
Does anyone know why this behaviour occurs, and what I can do to fix it?
Telling the window to maximize itself might bypass some internal adjustments that the program makes when it maximizes via a system menu command. To emulate clicking on the maximize button, send it a SC_MAXIMIZE command:
SendMessage(hwnd, WM_SYSCOMMAND, SC_MAXIMIZE, 0);
Antoher way to use SetWindowPos(); For example you got HWND handleWnd;
RECT rcWnd;
GetWindowRect(handleWnd,&rcWnd);
SetWindowPos(handleWnd,WHND_TOP,rcWnd.left,rcWnd.top,(rcWnd.right-rcWnd.left),(rcWnd.bottom-rcWnd.top),SWP_SHOWWINDOW);
So you got your previous position, place window on top of Z and show

How to avoid flicker while handling WM_ERASEBKGND in Windows dialog

I have a dialog that resizes. It also has a custom background which I paint in response to a WM_ERASEBKGND call (currently a simple call to FillSolidRect).
When the dialog is resized, there is tremendous flickering going on. To try and reduce the flickering I enumerate all child windows and add them to the clipping region. That seems to help a little -- now the flickering is mostly evident in all of the child controls as they repaint.
How can I make the dialog flicker-free while resizing? I suspect double-buffering must play a part, but I'm not sure how to do that with a dialog with child controls (without making all child controls owner-draw or something like that).
I should note that I'm using C++ (not .NET), and MFC, although pure Win32-based solutions are welcomed :)
NOTE: One thing I tried but which didn't work (not sure why) was:
CDC memDC;
memDC.CreateCompatibleDC(pDC);
memDC.FillSolidRect(rect, backgroundColor);
pDC->BitBlt(0, 0, rect.Width(), rect.Height(), &memDC, 0, 0, SRCCOPY);
Assuming that "FillSolidRect" is the erase of your background then return TRUE from the WM_ERASEBKGND.
To do the double buffering that you are almost doing in your code fragment, you will need to use CreateCompatibleBitmap and select that into your memDC.
Try adding the following line to your OnInitDialog function:
ModifyStyle(0, WS_CLIPCHILDREN, 0);
Do nothing in the WM_ERASEBKGND handling and do the erase as part of your main WM_PAINT. You can either paint smarter so that you only redraw the invalid areas, or more easily, double-buffer the drawing.
By not doing anything in the erase background, you have all your drawing code in one location which should make it easier for others to follow and maintain.
If you are targeting WinXP or higher, you can also use the WS_EX_COMPOSITED style to enable double-buffering by default for top-level windows with this style. Bear in mind this has its own set of limitations -- specifically, no more drawing out of OnPaint cycles using GetDC, etc.
you can set parameter of your call to InvalidateRect method as false. This will prevent you to send WM_ERASEBKGND when the window will redraw.
Double buffering is indeed the only way to make this work.
Child controls will take care of themselves so long as you make sure CLIPCHILDREN.

Resources