VC implements a method that makes the close button grey and unavailable

  • 2020-04-02 02:35:02
  • OfStack

For VC project programs to run in some cases, there will be a ban on the user through the close button to close the window on the title bar, you will find that by this time the program's close button is grey, not with, also can't close the window from the task bar, the menu is grey, same very good banned window function, if you want to shut down, can press on the keyboard shortcuts "ALT + F4", or through the end of the task manager task. The following section illustrates the core code file for this feature.

The specific function code of disabling the close button is as follows:


#include "stdafx.h"
#include "Test.h"
#include "MainFrm.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#endif

// CMainFrame
IMPLEMENT_DYNCREATE(CMainFrame, CFrameWnd)
BEGIN_MESSAGE_MAP(CMainFrame, CFrameWnd)
 ON_WM_CREATE()
END_MESSAGE_MAP()

static UINT indicators[] =
{
 ID_SEPARATOR,      //Status line indicator
 ID_INDICATOR_CAPS,
 ID_INDICATOR_NUM,
 ID_INDICATOR_SCRL,
};


//CMainFrame construction/destruction

CMainFrame::CMainFrame()
{
 //TODO: add the member initialization code here
}

CMainFrame::~CMainFrame()
{
}

int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
 if (CFrameWnd::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(" Could not create toolbar n");
 return -1;   //Failed to create
 }

 if (!m_wndStatusBar.Create(this) ||
 !m_wndStatusBar.SetIndicators(indicators,
  sizeof(indicators)/sizeof(UINT)))
 {
 TRACE0(" The status bar could not be created n");
 return -1;   //Failed to create
 }
 //TODO: delete these three lines if you do not need the toolbar to dock
 m_wndToolBar.EnableDocking(CBRS_ALIGN_ANY);
 EnableDocking(CBRS_ALIGN_ANY);
 DockControlBar(&m_wndToolBar);
 //Get system menu
 CMenu *pMenu=GetSystemMenu(FALSE);
 //Get system menu The number of 
 int Count=pMenu->GetMenuItemCount();
 //Gets the ID of the close menu
 UINT ID=pMenu->GetMenuItemID(Count-1);
 //Do not close the menu
 pMenu->EnableMenuItem(ID,MF_GRAYED);
 return 0;
}

BOOL CMainFrame::PreCreateWindow(CREATESTRUCT& cs)
{
 if( !CFrameWnd::PreCreateWindow(cs) )
 return FALSE;
 //TODO: modify the window class or by modifying the CREATESTRUCT cs at this point
 //style

 return TRUE;
}

//CMainFrame diagnosis
#ifdef _DEBUG
void CMainFrame::AssertValid() const
{
 CFrameWnd::AssertValid();
}

void CMainFrame::Dump(CDumpContext& dc) const
{
 CFrameWnd::Dump(dc);
}

#endif //_DEBUG
// CMainFrame  Message handler 

Related articles: