How to modify the System Menu to execute a function
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9

Thread: How to modify the System Menu to execute a function

  1. #1
    Join Date
    May 2002
    Posts
    1,727

    How to modify the System Menu to execute a function

    I wish to add the item 'Website' to an MFC Dialog app System Menu so that it can activate the following method:
    Code:
    void CMyDlg::OnWebpage() 
    {
    	ShellExecute( NULL, NULL, _T("http://www.codeguru.com"), NULL, NULL, 0 );
    	
    }// OnWebpage()
    I have tried this:
    Code:
    #define IDM_WEBSITE  (WM_USER + 101)
    //..
    		if (!strAboutMenu.IsEmpty())
    		{
    			pSysMenu->AppendMenu(MF_SEPARATOR);
    			pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
    			pSysMenu->AppendMenuW(MF_STRING, IDM_WEBSITE, _T("Website"));
    		}
    //..
    void CMyDlg::OnSysCommand(UINT nID, LPARAM lParam)
    {
    	if ((nID & 0xFFF0) == IDM_ABOUTBOX)
    	{
    		CAboutDlg dlgAbout;
    		dlgAbout.DoModal();
    	}
    	else if ((nID & 0xFFF0) == IDM_WEBSITE)
    	{
    		OnWebpage();
    	}
    	else
    	{
    		CDialogEx::OnSysCommand(nID, lParam);
    	}
    }
    The code compiles without error but and the 'website' item appears on the System Menu but it doesnt do anything.

    I've inspected the following but don't really understand how it works and it seems to me there should be a simpler way to accomplish my end.

    Modifying the System Menu By John Simmons 26 Jan 2007
    http://www.codeproject.com/Articles/...he-System-Menu
    Any help appreciated.
    Last edited by Mike Pliam; February 11th, 2013 at 11:46 PM.
    mpliam

  2. #2
    Join Date
    Nov 2000
    Location
    Voronezh, Russia
    Posts
    6,024

    Re: How to modify the System Menu to execute a function

    Sample attached. It demonstrates handling for both frame and dialog windows. Don't be surprised as the demo automatically activates system menus.
    Attached Files Attached Files
    Last edited by Igor Vartanov; February 12th, 2013 at 03:22 AM.
    Best regards,
    Igor

  3. #3
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Wallisellen (ZH), Switzerland
    Posts
    17,472

    Re: How to modify the System Menu to execute a function

    Mike, look at the code the framework generated for a standard VC++ 6.0 MFC dialog based application:
    Code:
    	// Add "About..." menu item to system menu.
    
    	// IDM_ABOUTBOX must be in the system command range.
    	ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
    	ASSERT(IDM_ABOUTBOX < 0xF000);
    
    	CMenu* pSysMenu = GetSystemMenu(FALSE);
    	if (pSysMenu != NULL)
    	{
    		CString strAboutMenu;
    		strAboutMenu.LoadString(IDS_ABOUTBOX);
    		if (!strAboutMenu.IsEmpty())
    		{
    			pSysMenu->AppendMenu(MF_SEPARATOR);
    			pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
    		}
    	}
    Do you see a difference between this code and yours?
    Victor Nijegorodov

  4. #4
    Arjay's Avatar
    Arjay is online now Moderator / MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    11,363

    Re: How to modify the System Menu to execute a function

    Quote Originally Posted by VictorN View Post
    Mike, look at the code the framework generated for a standard VC++ 6.0 MFC dialog based application:
    Code:
    	// Add "About..." menu item to system menu.
    
    	// IDM_ABOUTBOX must be in the system command range.
    	ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
    	ASSERT(IDM_ABOUTBOX < 0xF000);
    
    	CMenu* pSysMenu = GetSystemMenu(FALSE);
    	if (pSysMenu != NULL)
    	{
    		CString strAboutMenu;
    		strAboutMenu.LoadString(IDS_ABOUTBOX);
    		if (!strAboutMenu.IsEmpty())
    		{
    			pSysMenu->AppendMenu(MF_SEPARATOR);
    			pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
    		}
    	}
    Do you see a difference between this code and yours?
    Victor, one thing that's always been cool about MFC is the source code is available. Don't know how to do something in your code?... just take a peak at the MFC source code and see how it's done there. What a fantastic learning tool.

  5. #5
    Join Date
    May 2002
    Posts
    1,727

    Re: How to modify the System Menu to execute a function

    Thank you for your kind help. I have it working now. Arjay, you are appropriate to point out that the answer was right in front of me, in the MFC source code. My problem was simply that my definitions for the items I wished to add were not in an accepable range. Once that was corrected everything worked out.
    mpliam

  6. #6
    Arjay's Avatar
    Arjay is online now Moderator / MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    11,363

    Re: How to modify the System Menu to execute a function

    Quote Originally Posted by Mike Pliam View Post
    Thank you for your kind help. I have it working now. Arjay, you are appropriate to point out that the answer was right in front of me, in the MFC source code.
    Not trying to school here. When working on a new problem in MFC, I find it helpful to know that MFC if already does it, I usually try to track it down in the source to see how I can do it, or at least to give me a starting point. Sometimes, it's easy to miss some subtlety such as an out of range menu id like in this case.

  7. #7
    Join Date
    May 2002
    Posts
    1,727

    Re: How to modify the System Menu to execute a function

    One final question related to the System Menu of a Dialog app. Is it possible to remove the Move and Minimize items from the menu WITHOUT eliminating that functionality completely when user drags the title bar or clicks the minimize button?
    mpliam

  8. #8
    Join Date
    Nov 2000
    Location
    Voronezh, Russia
    Posts
    6,024

    Re: How to modify the System Menu to execute a function

    I would recommend to leave those there as long as you're going to keep this standard functionality for your dialog, as you never know your end users' habits. You must have very good reasons to start modifying standard UI elements.
    Best regards,
    Igor

  9. #9
    Join Date
    May 2002
    Posts
    1,727

    Re: How to modify the System Menu to execute a function

    I agree. But I came across a little app that does exactly that and it seems to work just fine and the System Menu of the dialog is very clean and customized without minimize and move, yet the app minimize and move functionality is preserved, so there must be a way to do it. Anyway, you're right and I've just left it the way it is. Thanks for your thoughts, Igor.
    mpliam

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center