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:

    Then you'll have to re-implement winEvent() to handle the corresponding messages:
    http://doc.qt.digia.com/qt/qwidget.html#winEvent

    It's probably a message of type WM_COMMAND that you are waiting for...



  • Hi MuldeR,

    you are right, this is the only option you have. It's highly OS specific.
    Just a side note: Take care that the title bar stays, if you have an app without a windows title bar, then also the system menu is away.



  • 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 0x0010

    HMENU 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!


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.