In Rad Studio 12 C++ (Delphi/C++Builder), using VCL.
I'm facing an issue in my application where the minimize button becomes don't working after restoring the main window from the system tray by a mutex is used to bring a previously launched instance to the foreground.
HANDLE hMutex;
hMutex = OpenMutexA(MUTEX_ALL_ACCESS, 0, "Home-work-2025-0x1");
if(!hMutex) hMutex = CreateMutexA(0,0, "Home-work-2025-0x1");
else{
HWND hFirst = ::FindWindow(NULL, L"Home Work");
if(hFirst){
if(IsIconic(hFirst)){
ShowWindow(hFirst, SW_RESTORE);
SetForegroundWindow(hFirst);
}else SetForegroundWindow(hFirst);
}
else return(-2);
return(2);
}
Application->Initialize();
Application->MainFormOnTaskBar = true;
Application->CreateForm(__classid(TForm1), &Form1);
Application->Run();
ReleaseMutex(hMutex);
The window itself restores correctly, but it seems like its state isn't fully updated. Here's what I've tried so far and it don't work:
WINDOWPLACEMENT wp;
wp.length = sizeof(WINDOWPLACEMENT);
GetWindowPlacement(hFirst, &wp);
wp.showCmd = SW_RESTORE;
SetWindowPlacement(hFirst, &wp);
LONG style = GetWindowLong(hFirst, GWL_STYLE);
if (!(style & WS_MINIMIZEBOX) || !(style & WS_SYSMENU)) {
style |= WS_MINIMIZEBOX | WS_SYSMENU;
SetWindowLong(hFirst, GWL_STYLE, style);
}
RedrawWindow(hFirst, NULL, NULL, RDW_INVALIDATE | RDW_UPDATENOW | RDW_ERASE);
UpdateWindow(hFirst);
SendMessage(hFirst, WM_ACTIVATE, WA_ACTIVE, 0);
SetWindowPos(hFirst, HWND_TOP, 0, 0, 0, 0, SWP_SHOWWINDOW | SWP_NOSIZE | SWP_NOMOVE | SWP_NOZORDER);
SetActiveWindow(hFirst);
MSG msg;
while (PeekMessage(&msg, hFirst, 0, 0, PM_REMOVE)) {
TranslateMessage(&msg);
DispatchMessage(&msg);
}
PostMessage(hFirst, WM_SYSCOMMAND,SC_RESTORE, 0);
Important, with crippled min button and restored window, just clicking the "Show/Hide" option in my trayicon context menu, with routine сode:
if(Form1->WindowState == wsMinimized){
Form1->WindowState = wsNormal;
Form1->Show();
}else if(Form1->WindowState == wsNormal){
Form1->WindowState = wsMinimized;
}
seems to "fix" the problem, and the minimize button starts working again.
Update This worked:
void __fastcall TForm1::FormPaint(TObject *Sender)
{
Form1->Show();
}