I want make a hidden window visible.
HWND hWnd = FindWindow(NULL, "MyWindowName");
ShowWindow(hWnd, SW_SHOW);
The window is found, but nothing happens. It remains hidden.
What am I doing wrong?
If it matters, the application is made using MFC and it has the following method overwritten:
void CMyClass::OnWindowPosChanging(WINDOWPOS* lpwndpos)
{
lpwndpos->flags &= ~SWP_SHOWWINDOW;
CDialog::OnWindowPosChanging(lpwndpos);
}
I did it.
Apparently you need to modify some flags.
long style= GetWindowLong(hWnd, GWL_STYLE);
style |= WS_VISIBLE;
SetWindowLong(hWnd, GWL_STYLE, style);
SetWindowPos(hWnd, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE | SWP_NOZORDER | SWP_FRAMECHANGED);
And it works.
Related
So I am creating a fullscreen function in win32 c++ doing:
uint8_t isFullscreen = 0;
RECT winRect; //Current Window Rect
RECT nonFullScreenRect; //Rect Not In Full Screen Position (used to restore window to not full screen position when coming out of fullscreen)
uint32_t screen_width = DEFAULT_SCREEN_WIDTH;
uint32_t screen_height = DEFAULT_SCREEN_HEIGHT;
void Fullscreen( HWND WindowHandle )
{
isFullscreen = isFullscreen ^ 1;
if( isFullscreen )
{
//saving off current window rect
nonFullScreenRect.left = winRect.left;
nonFullScreenRect.right = winRect.right;
nonFullScreenRect.bottom = winRect.bottom;
nonFullScreenRect.top = winRect.top;
SetWindowLongPtr( WindowHandle, GWL_STYLE, WS_POPUP | WS_CLIPCHILDREN | WS_CLIPSIBLINGS | WS_VISIBLE ); //causes a resize msg
HMONITOR hmon = MonitorFromWindow(WindowHandle, MONITOR_DEFAULTTONEAREST);
MONITORINFO mi = { sizeof( mi ) };
GetMonitorInfo( hmon, &mi );
screen_width = mi.rcMonitor.right - mi.rcMonitor.left;
screen_height = mi.rcMonitor.bottom - mi.rcMonitor.top;
MoveWindow( WindowHandle, mi.rcMonitor.left, mi.rcMonitor.top, (int32_t)screen_width, (int32_t)screen_height, FALSE );
}
else
{
SetWindowLongPtr( WindowHandle, GWL_STYLE, WS_OVERLAPPEDWINDOW | WS_VISIBLE );
screen_width = nonFullScreenRect.right - nonFullScreenRect.left;
screen_height = nonFullScreenRect.bottom - nonFullScreenRect.top;
MoveWindow( WindowHandle, nonFullScreenRect.left, nonFullScreenRect.top, (int32_t)screen_width, (int32_t)screen_height, FALSE );
}
}
However when it goes fullscreen, the function generates 2 WM_SIZE messages. While when it goes windowed, it generates only 1.
Why is that the case? And how can I make it generate only 1 WM_SIZE message for the proper full screen size?
How can I update an HWND's style and position atomically? asks about it but no one answers it
The reasons I need this is because I am using DirectX12 and on WM_SIZE I wait for all the signals at the end of the command queues before resizing all the swap chain back buffers. And I don't want to have to resize the swap chain twice when switching to fullscreen mode.
case WM_SIZE:
{
screen_width = LOWORD( LParam );
screen_height = HIWORD( LParam );
//DirectX stuff here
}break;
Thanks in advance!
Updated Answer:
The Win32 API allows you to modify parameters of the window one at a time. When a parameter is modified, the API may or may not update the window and trigger a WM_SIZE that will be the size of the window given the current parameters.
Since to have a complete full screen window you need to make at least 2 calls, one to update GWL_STYLE and another to update GWL_EXSTYLE, you have a big chance of getting 2 WM_SIZE calls. One of them will give you the window size without the menu, and the other the full screen window size. It depends in which order you call SetWindowLongPtr, but you'll probably get 2 WM_SIZE and only the second one is "correct", i.e. the one you want in the end.
The more reliable solution to your problem is to use a variable at the top of Main.cpp:
int isTogglingFullScreen = false;
Then inside your full screen toggle code (note where isTogglingFullScreen is being set):
case WM_SYSKEYDOWN:
if (wParam == VK_RETURN && (lParam & 0x60000000) == 0x20000000)
{
// Implements the classic ALT+ENTER fullscreen toggle
if (s_fullscreen)
{
isTogglingFullScreen = true;
SetWindowLongPtr(hWnd, GWL_STYLE, WS_OVERLAPPEDWINDOW);
isTogglingFullScreen = false;
SetWindowLongPtr(hWnd, GWL_EXSTYLE, 0);
int width = 800;
int height = 600;
if (game)
game->GetDefaultSize(width, height);
SetWindowPos(hWnd, HWND_TOP, 0, 0, width, height, SWP_NOMOVE | SWP_NOZORDER | SWP_FRAMECHANGED);
ShowWindow(hWnd, SW_SHOWNORMAL);
}
else
{
isTogglingFullScreen = true;
SetWindowLongPtr(hWnd, GWL_EXSTYLE, WS_EX_TOPMOST);
SetWindowLongPtr(hWnd, GWL_STYLE, WS_POPUP);
SetWindowPos(hWnd, HWND_TOP, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE | SWP_NOZORDER | SWP_FRAMECHANGED);
isTogglingFullScreen = false;
ShowWindow(hWnd, SW_SHOWMAXIMIZED);
}
s_fullscreen = !s_fullscreen;
}
break;
Finally, inside WM_SIZE, change
else if (!s_in_sizemove && game)
{
game->OnWindowSizeChanged(LOWORD(lParam), HIWORD(lParam));
}
to
else if (!s_in_sizemove && game && !isTogglingFullScreen)
{
game->OnWindowSizeChanged(LOWORD(lParam), HIWORD(lParam));
}
This will give you a single call to OnWindowSizeChanged() when you toggle full screen, and the call will be with the correct final size.
--
Old Answer:
If you only want a single WM_SIZE to trigger, when you switch to full screen then you should go for something like this:
SetWindowLongPtr(hWnd, GWL_EXSTYLE, WS_EX_TOPMOST);
SetWindowPos(hWnd, HWND_TOP, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE | SWP_NOZORDER | SWP_FRAMECHANGED);
ShowWindow(hWnd, SW_SHOWMAXIMIZED);
Any SetWindowLongPtr call to GWL_STYLE will trigger a WM_SIZE, so make sure it's only called with GWL_EXSTYLE. For example, if you both set GWL_EXSTYLE to what you want, and reset GWL_STYLE to 0, you'll trigger WM_SIZE twice.
To make it clearer:
Don't use GWL_STYLE in SetWindowLongPtr because it triggers a useless WM_SIZE
ShowWindow will trigger WM_SIZE
The above code will ultimately only trigger WM_SIZE once.
It turns out that YMMV. It's entirely possible that the first time you switch fullscreen, you'll get 2 WM_SIZE. One will be with the original size, and the other with the new size. Subsequent calls will trigger only one WM_SIZE.
Hence, the really bulletproof solution (which I was using anyway before playing around with the SetWindowLongPtr to answer this question, is to validate that the window size has actually changed. Because one thing that I can guarantee in the above call is that you'll never get more than 1 call with the new size. At most you'll get a WM_SIZE call with the old size, which you'll discard by checking that it's the same as the current size.
If you use the DevicesResources template for DX12, you'll see that there's a check in OnWindowSizeChanged() that does nothing if the size hasn't changed.
So i'm trying to write a toggle fullscreen function for my game.
It works.. kinda, problem is the first SetWindowLongPtr() call causes a WM_SIZE message to be queued and sent to my WNDPROC (on MSDN i read changes to the window style are only cached and have to be applied using SetWindowPos() though).
SetWindowLongPtr(m_hWnd, GWL_STYLE, dwStyle);
SetWindowLongPtr(m_hWnd, GWL_EXSTYLE, dwExStyle);
SetWindowPos(m_hWnd, NULL, fullscreen ? 0 : window.left, fullscreen ? 0 : window.top, window.right, window.bottom, SWP_NOZORDER | SWP_FRAMECHANGED);
ShowWindow(m_hWnd, SW_SHOW);
When changing from windowed to fullscreen mode the client area gets resized to the window rect and updated like that (so now 'contains' the bars etc.).
Apart from causing my program to destroy the Vulkan context twice, this makes me save the wrong window dimensions for reverting to windowed mode later on (this is done when WM_SIZE occurs and when the game is in fullscreen mode).
Afterwards it's sized to the correct dimensions by SetWindowPos().
I mean i could certainly just hack in a bool to discard the WM_SIZE message when it should be, but i'm looking for a better and maybe less bodged way.
Is there a function to temporarily disable window messages or just the user def WNDPROC?
This is the function that I use to toggle fullscreen on and off.
I got this function from Raymond Chen.
I don't know if it messes with Vulkan though but it works with OpenGL.
WINDOWPLACEMENT GlobalWindowPosition = {sizeof(GlobalWindowPosition)};
void ToggleFullscreen(HWND WindowHandle)
{
DWORD Style = GetWindowLong(WindowHandle, GWL_STYLE);
if(Style & WS_OVERLAPPEDWINDOW)
{
MONITORINFO MonitorInfo = {sizeof(MonitorInfo)};
if(GetWindowPlacement(WindowHandle, &GlobalWindowPosition) &&
GetMonitorInfo(MonitorFromWindow(WindowHandle, MONITOR_DEFAULTTOPRIMARY),
&MonitorInfo))
{
SetWindowLong(WindowHandle, GWL_STYLE,
Style & ~WS_OVERLAPPEDWINDOW);
SetWindowPos(WindowHandle, HWND_TOP,
MonitorInfo.rcMonitor.left, MonitorInfo.rcMonitor.top,
MonitorInfo.rcMonitor.right - MonitorInfo.rcMonitor.left,
MonitorInfo.rcMonitor.bottom - MonitorInfo.rcMonitor.top,
SWP_NOOWNERZORDER | SWP_FRAMECHANGED);
}
}
else
{
SetWindowLong(WindowHandle, GWL_STYLE, Style | WS_OVERLAPPEDWINDOW);
SetWindowPlacement(WindowHandle, &GlobalWindowPosition);
SetWindowPos(WindowHandle, NULL, 0, 0, 0, 0,
SWP_NOMOVE | SWP_NOSIZE | SWP_NOZORDER |
SWP_NOOWNERZORDER | SWP_FRAMECHANGED);
}
}
Any ideas how to use SetParent()?
The following doesn't seem to work:
fhWnd = FindWindow(_T("Notepad"), NULL);
hWnd = CreateWindowEx(WS_EX_TOPMOST , _T("test"), _T("test"),
WS_POPUP,
20, 20, 400, 400, NULL, NULL, hInst, NULL);
SetParent(hWnd, fhWnd);
DWORD style = GetWindowLong(hWnd, GWL_STYLE);
style = style & ~(WS_POPUP);
style = style | WS_CHILD | WS_VISIBLE | WS_CLIPCHILDREN | WS_CLIPSIBLINGS;
SetWindowLong(hWnd, GWL_STYLE, style);
SetWindowLong(fhWnd, GWL_STYLE, GetWindowLong(fhWnd, GWL_STYLE) | WS_CLIPCHILDREN);
With the above code I have to move the parent window out of the screen and back in again in order to see what's draw on the child window.
Any idea what I am doing wrong?
My code as follow, but the background doesn't transparent also.. What should i do ?
QAxWidget *flash = new QAxWidget(0,0);
flash->resize(200,200);
flash->setControl(QString::fromUtf8("{d27cdb6e-ae6d-11cf-96b8-444553540000}"));
flash->dynamicCall("LoadMovie(long,string)",0,"D:/test.swf");
flash->dynamicCall("WMode", "transparent");
flash->show();
By the way,,In Qt , is there a another way to show swf ? Thank you....
add
flash->setAutoFillBackground(true)
if the movie has a transparent background, you should see widget's original backgorund
try this code
QAxWidget *flash = new QAxWidget(0, 0);
flash->resize(500, 400);
HWND hWnd = (HWND)flash->winId();
LONG lStyle = ::GetWindowLong(hWnd, GWL_STYLE);
lStyle &= ~(WS_CAPTION | WS_THICKFRAME | WS_MINIMIZE | WS_MAXIMIZE | WS_SYSMENU);
::SetWindowLong(hWnd, GWL_STYLE, lStyle);
LONG lExStyle = ::GetWindowLong(hWnd, GWL_EXSTYLE);
::SetWindowLong(hWnd, GWL_EXSTYLE, lExStyle|WS_EX_LAYERED|WS_EX_TOPMOST|WS_EX_TRANSPARENT);
typedef int (WINAPI* LPFUNC)(HWND, COLORREF , BYTE, DWORD);
HINSTANCE hins = ::LoadLibraryW(L"User32.DLL");
if(!hins)
return ;
LPFUNC func2 = (LPFUNC)GetProcAddress(hins,"SetLayeredWindowAttributes");
if(!func2)
return ;
COLORREF clrMask = RGB(255,255,255);
func2(hWnd, clrMask, 0, LWA_COLORKEY);
FreeLibrary(hins);
flash->setControl(QString::fromUtf8("{d27cdb6e-ae6d-11cf-96b8-444553540000}"));
flash->dynamicCall("LoadMovie(long,string)", 0, "d:/9023.swf");
flash->dynamicCall("WMode", "transparent");
flash->show();
show activex with transparent background.
I am trying to create context menu for win32 application using
case WM_RBUTTONDOWN:
{
HMENU hPopupMenu = CreatePopupMenu();
InsertMenu(hPopupMenu, 0, MF_BYPOSITION | MF_STRING, ID_CLOSE, (LPCWSTR)"Exit");
InsertMenu(hPopupMenu, 0, MF_BYPOSITION | MF_STRING, ID_EXIT, (LPCWSTR)"Play");
SetForegroundWindow(hWnd);
TrackPopupMenu(hPopupMenu, TPM_BOTTOMALIGN | TPM_LEFTALIGN, 0, 0, 0, hWnd, NULL);
}
But I always get context menu as shown below
alt text http://img191.imageshack.us/img191/866/70219076.png
I want text exit and play to be displayed in the menu
You can't convert a string literal to wide by casting, you have to declare it as a wide char string. The casting just defeats the compiler warning, it doesn't change the content of the string.
change this
(LPCWSTR)"Exit"
(LPCWSTR)"Play"
to this
_T("Exit")
_T("Play")
or this
L"Exit"
L"Play"
Are you specifying the encoding in the API function definition? I ran into that problem recently and removing the specification fixed the problem.
Following worked for me
case WM_RBUTTONDOWN:
{
HMENU hPopupMenu = CreatePopupMenu();
InsertMenu(hPopupMenu, 0, MF_BYPOSITION | MF_STRING, ID_CLOSE, L"Exit");
InsertMenu(hPopupMenu, 0, MF_BYPOSITION | MF_STRING, ID_EXIT, L"Play");
SetForegroundWindow(hWnd);
TrackPopupMenu(hPopupMenu, TPM_BOTTOMALIGN | TPM_LEFTALIGN, 0, 0, 0, hWnd, NULL);
}