VC实现让关闭按钮成灰色不可用的方法

2025-05-27 0 97

对于有些情况下运行的VC项目程序来说,会有禁止用户通过标题栏上的关闭按钮关闭窗口的情况,你会发现,这时候程序的关闭按钮变成灰色不可用,从任务栏处也无法关闭窗口,菜单同样是灰色的,很好的禁止了窗口关闭功能,如果想关闭,可以按键盘上的快捷键“ALT+F4”,或者通过任务管理器结束任务。下面就来说明一下这个功能的核心代码文件。

禁用关闭按钮的具体功能代码如下:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92
#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, // 状态行指示器

ID_INDICATOR_CAPS,

ID_INDICATOR_NUM,

ID_INDICATOR_SCRL,

};

// CMainFrame 构造/销毁

CMainFrame::CMainFrame()

{

// TODO:在此添加成员初始化代码

}

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("未能创建工具栏\\n");

return -1; // 未能创建

}

if (!m_wndStatusBar.Create(this) ||

!m_wndStatusBar.SetIndicators(indicators,

sizeof(indicators)/sizeof(UINT)))

{

TRACE0("未能创建状态栏\\n");

return -1; // 未能创建

}

// TODO: 如果不需要工具栏可停靠,则删除这三行

m_wndToolBar.EnableDocking(CBRS_ALIGN_ANY);

EnableDocking(CBRS_ALIGN_ANY);

DockControlBar(&m_wndToolBar);

//取得系统菜单

CMenu *pMenu=GetSystemMenu(FALSE);

//取得系统菜单数量

int Count=pMenu->GetMenuItemCount();

//取得关闭菜单的ID

UINT ID=pMenu->GetMenuItemID(Count-1);

//禁止关闭菜单

pMenu->EnableMenuItem(ID,MF_GRAYED);

return 0;

}

BOOL CMainFrame::PreCreateWindow(CREATESTRUCT& cs)

{

if( !CFrameWnd::PreCreateWindow(cs) )

return FALSE;

// TODO: 在此处通过修改 CREATESTRUCT cs 来修改窗口类或

// 样式

return TRUE;

}

// CMainFrame 诊断

#ifdef _DEBUG

void CMainFrame::AssertValid() const

{

CFrameWnd::AssertValid();

}

void CMainFrame::Dump(CDumpContext& dc) const

{

CFrameWnd::Dump(dc);

}

#endif //_DEBUG

// CMainFrame 消息处理程序

收藏 (0) 打赏

感谢您的支持,我会继续努力的!

打开微信/支付宝扫一扫,即可进行扫码打赏哦,分享从这里开始,精彩与您同在
点赞 (0)

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。

快网idc优惠网 建站教程 VC实现让关闭按钮成灰色不可用的方法 https://www.kuaiidc.com/75886.html

相关文章

发表评论
暂无评论