MFC禁止对话框的移动,改变大小

MFC禁止对话框的移动,改变大小

一、禁止对话框的移动

1)、第一种方法
为这个对话框添加系统消息过虑处理:
.h中:

[cpp]…
afx_msg void OnSysCommand(UINT nID, LPARAM lParam);
…[/cpp]

.cpp中

[cpp]

BEGIN_MESSAGE_MAP(CXXXDlg, CDialog)

ON_WM_SYSCOMMAND()

END_MESSAGE_MAP()
void CXXXDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
if (nID == SC_MOVE || nID==0xF012)
return;
else
CDialog::OnSysCommand(nID, lParam);
}

SC_MOVE就是0xF010。

[/cpp]

2)、第二种方法
.h中:
[cpp]…
afx_msg UINT OnNcHitTest(CPoint point);
…[/cpp]

.cpp中
[cpp]
BEGIN_MESSAGE_MAP(CXXXDlg, CDialog)

ON_WM_NCHITTEST()

END_MESSAGE_MAP()
UINT CXXXDlg::OnNcHitTest(CPoint point)
{
int ret = CDialog::OnNcHitTest(point);

if( HTCAPTION == ret)
return HTCLIENT;

return ret;
}
[/cpp]
二、禁止对话框改变大小【推荐】
1)、第一种方法:

.h中:
[cpp]

afx_msg UINT OnNcHitTest(CPoint point);
…[/cpp]

.cpp中
[cpp]
BEGIN_MESSAGE_MAP(CXXXDlg, CDialog)

ON_WM_NCHITTEST()

END_MESSAGE_MAP()
UINT CXXXDlg::OnNcHitTest(CPoint point)
{
int ret = CDialog::OnNcHitTest(point);

//if语句的前两行是用来禁止改变大小的,最后一行是用来禁止移动的
if(HTTOP == ret || HTBOTTOM == ret || HTLEFT == ret || HTRIGHT == ret
|| HTBOTTOMLEFT == ret || HTBOTTOMRIGHT == ret || HTTOPLEFT == ret || HTTOPRIGHT == ret || HTCAPTION == ret)
return HTCLIENT;
return ret;
}[/cpp]

2)、第二种方法:

直接添加映射nchittest 消息,添加风格,代码如下:

[cpp]ModifyStyle( WS_THICKFRAME, 0, SWP_FRAMECHANGED|SWP_DRAWFRAME );[/cpp]