PDA

View Full Version : Taskbar menu (Windows) takes two clicks to display properly



pablojr
4th November 2009, 18:15
I have an application based on a QDialog that's created like this

MyDialog::MyDialog(QWidget *parent, Qt::WFlags flags) : QDialog(parent, flags | Qt::Dialog | Qt::CustomizeWindowHint | Qt::MSWindowsFixedSizeDialogHint | Qt::WindowSystemMenuHint)

and I want the system menu in Windows (when right-clicking the application in Windows taskbar) to show only the option Minimize and Restore depending on application's status (minimized or not).

So I reimplemented the winEvent() method to change the options using operating system calls (see code below). The problem is that first time I right-click the taskbar, system menu is still drawn with default options. Only after second click I get the expected result.

Any help is welcome!

Thanks. Pablo

void MyDialog::winEvent(MSG *message, long *result)
{
// We want to deal with messages about system menu only
if (message->message == WM_INITMENUPOPUP)
{
if (HIWORD(message->lParam))
{
// Get the menu handle
HMENU hMenu = ::GetSystemMenu((HWND) this->winId(), FALSE);
// Disable all actions, except minimize and close
::EnableMenuItem(hMenu, SC_MOVE, MF_BYCOMMAND | MF_GRAYED);
::EnableMenuItem(hMenu, SC_SIZE, MF_BYCOMMAND | MF_GRAYED);
::EnableMenuItem(hMenu, SC_MAXIMIZE, MF_BYCOMMAND | MF_GRAYED);
// Restore and minimize depends on application status
::EnableMenuItem(hMenu, SC_RESTORE, MF_BYCOMMAND | (this->isMinimized() ? MF_ENABLED : MF_GRAYED));
::EnableMenuItem(hMenu, SC_MINIMIZE, MF_BYCOMMAND | (this->isMinimized() ? MF_GRAYED : MF_ENABLED));
// set return value
*result = 0;
return true;
}
}
return false;
}