How to unable window Close Button at MFC???

CMenu* systemMenu = GetSystemMenu(false);

if(systemMenu)
{
     systemMenu->EnableMenuItem(SC_CLOSE, MF_BYCOMMAND | MF_DISABLED);
}


Above coding, i found that cannot function at OnInitialUpdate().

Is it got any problem abouth this shot?

I would like to unable  window close button when my program load...

TQ!
DannyGan9Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Nass89Commented:
Hi,
What is "systemMenu"?
Just add the following line to OnInitDialog() and the close button will be disabled.

BOOL CDlg9Dlg::OnInitDialog()
{
...
...

      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);
            }
// Add here
            pSysMenu->EnableMenuItem(SC_CLOSE, MF_BYCOMMAND | MF_DISABLED);
      }
...
...
}

Good Luck!
0
AlexFMCommented:
EnableMenuItem is CView class. View doesn't have system menu, GetSystemMenu returns NULL. You need to move this code to the frame class, and use another message handler, like WM_CREATE.
0
DannyGan9Author Commented:
So what is the right way to enable the window close button????

TQ!
0
Nass89Commented:
Hi,
The codes are for dialog based view? Which class are you using?
MDI or SDI or Dialog?
0
OnegaZhangCommented:
int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
      if (CMDIFrameWnd::OnCreate(lpCreateStruct) == -1)
            return -1;
      
      if (!m_wndToolBar.CreateEx(this, TBSTYLE_FLAT, WS_CHILD | WS_VISIBLE | CBRS_TOP
            | CBRS_GRIPPER | CBRS_TOOLTIPS | CBRS_FLYBY | CBRS_SIZE_DYNAMIC) ||
            !m_wndToolBar.LoadToolBar(IDR_MAINFRAME))
      {
            TRACE0("Failed to create toolbar\n");
            return -1;      // fail to create
      }

      if (!m_wndStatusBar.Create(this) ||
            !m_wndStatusBar.SetIndicators(indicators,
              sizeof(indicators)/sizeof(UINT)))
      {
            TRACE0("Failed to create status bar\n");
            return -1;      // fail to create
      }

      // TODO: Delete these three lines if you don't want the toolbar to
      //  be dockable
      m_wndToolBar.EnableDocking(CBRS_ALIGN_ANY);
      EnableDocking(CBRS_ALIGN_ANY);
      DockControlBar(&m_wndToolBar);
      CMenu *pmenu = GetSystemMenu(FALSE);
      pmenu->EnableMenuItem(SC_CLOSE, MF_BYCOMMAND | MF_DISABLED|MF_GRAYED);
      return 0;
}
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
System Programming

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.