How to add a custom menu item to the windows system menu
-
Hello,
I am writing an application without menu bar.
Is it possible to add a custom menu item (like "About...") to the system menu of the application window?
On Windows this is usually done by the default MFC implementation.Thank you for your help!
-
I think this is too much OS-specific and you'll have to use the Win32 API directly.
See also:
- http://msdn.microsoft.com/en-us/library/windows/desktop/ms647985(v=vs.85).aspx
- http://msdn.microsoft.com/en-us/library/windows/desktop/ms647616(v=vs.85).aspx
Then you'll have to re-implement winEvent() to handle the corresponding messages:
http://doc.qt.digia.com/qt/qwidget.html#winEventIt's probably a message of type WM_COMMAND that you are waiting for...
-
Hello,
thank you very much for your answers.
Knowing the right approach everything becomes quite easy.
Of cause this is highly OS specific...Just for the case that someone else wants to add the "About" menu item into the Windows system menu here is a short code snippet:
@#include "windows.h"
MyWidget::MyWidget() : QMainWindow()
{
...// IDM_ABOUTBOX must be in the system command range
// (IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX)
// and (IDM_ABOUTBOX < 0xF000)
#define IDM_ABOUTBOX 0x0010HMENU hMenu = ::GetSystemMenu(winId(), FALSE);
if (hMenu != NULL)
{
::AppendMenuA(hMenu, MF_SEPARATOR, 0, 0);
::AppendMenuA(hMenu, MF_STRING, IDM_ABOUTBOX, "About MyApp...");
}...
}bool MyWidget::winEvent(MSG *m, long *result)
{
if (m->message == WM_SYSCOMMAND)
{
if ((m->wParam & 0xfff0) == IDM_ABOUTBOX)
{
*result = 0;// open About dialog about(); return (true); }
}
return (false);
}
@ -
This example doesn't build:
@mainwindow.obj:-1: error: LNK2019: unresolved external symbol __imp__AppendMenuW@16 referenced in function "public: __thiscall MainWindow::MainWindow(class QWidget *)" (??0MainWindow@@QAE@PAVQWidget@@@Z)@
Have you used this?
-
You have to add the following line in the project file:
@LIBS += -lUser32@
This is required for GetSystemMenu and AppendMenu.
Hope this helps!
-
How stupid of me! Of course, it's Win32 - Thanks so much!