MFC API——》OnSysCommand

WM_SYSCOMMANDWM_COMMAND——》前面一个是系统消息、后面一个是窗口消息

一、

       OnSysCommand()这个函数主要是截获控制命令的,msdn上的解释如下:


  The framework calls this member function when the user selects a command from the Control menu, or when the user selects the Maximize or the Minimize button.


  尤其是最大化和最小化窗口的时候,比如现在软件的流行的点关闭按钮,不是退出而是隐藏的情况,就可以在这里来实现,


  void CMainFrame::OnSysCommand(UINT nID, LPARAM lParam)


  {


  if (nID == SC_CLOSE)


  ShowWindow(SW_HIDE);


  .....


  }


  就是这样来实现的,


  注意和PreTranslateMessage的区别


  PreTranslateMessage是用来截获消息的,msdn的解释如下 


  Used by class cwinapp to translate window messages before they are dispatched to the TranslateMessage and DispatchMessage Windows functions.


  virtual BOOL PreTranslateMessage(


  MSG* pMsg


  );


  nID


  SC_CLOSE Close the CWnd object. //关闭


  SC_MAXIMIZE (or SC_ZOOM) Maximize the CWnd object.//最大化


  SC_MINIMIZE (or SC_ICON) Minimize the CWnd object.//最小化


  SC_RESTORE Restore window to normal position and size.//还原


  SC_SIZE Size the CWnd object. //大小


  SC_MOVE Move the CWnd object.//移动

二、


通过OnSysCommand关闭程序控制
在MainFrame利用WINDOWS的消息循环机制,拦截关闭窗口消息:
1:
BEGIN_MESSAGE_MAP(CMainFrame, CFrameWnd)
//{{AFX_MSG_MAP(CMainFrame)
//手动添加:
ON_MESSAGE(WM_SYSCOMMAND,OnSysCommand)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
2:
void CMainFrame::OnSysCommand(UINT nID,LPARAM lParam)
{
if((nID & 0xFFFFFFF0) ==SC_CLOSE)
{
if(AfxMessageBox("Warning: Wollen Sie die Pozess wicklich abbrechen?",MB_OKCANCEL)!=IDOK) 
return;
//如果关闭程序 do something:.......... 

CWnd::OnSysCommand(nID,lParam);
}

 

void CMainWindow::OnSysCommand (UINT nID, LPARAM lParam)
{
    UINT nMaskedID = nID & 0xFFF0;

    if (nMaskedID == IDM_SYSMENU_FULL_WINDOW) {
        m_bFullWindow = m_bFullWindow ? 0 : 1;
        SetTitleBarState ();
        return;
    }
    else if (nMaskedID == IDM_SYSMENU_STAY_ON_TOP) {
        m_bStayOnTop = m_bStayOnTop ? 0 : 1;
        SetTopMostState ();
        return;
    }
    CFrameWnd::OnSysCommand (nID, lParam);
}

 


3.在CDialog中
void CDBinfoSmall::OnSysCommand(UINT nID, LPARAM lParam) 

//AfxMessageBox("mouse call");
if((nID & 0xFFFFFFF0) == SC_CLOSE) 

if(AfxMessageBox("Warning: Wollen Sie die Pozess wicklich abbrechen?",MB_YESNO) == IDNO) 

//AfxMessageBox( GetCommandLine() );
return;
}
else

//---- use the View pointobject, close the m_DaoDatabase; 
CMDIFrameWnd *pFrame = (CMDIFrameWnd*)AfxGetApp()->m_pMainWnd;
// Get the active MDI child window.
CMDIChildWnd *pChild = (CMDIChildWnd *) pFrame->GetActiveFrame();
// or CMDIChildWnd *pChild = pFrame->MDIGetActive();
// Get the active view attached to the active MDI child window.
CDbinfoView *pView = (CDbinfoView *) pChild->GetActiveView();
//close the source db m_DaoDatabase by using pView
if( pView->m_DaoDatabase.IsOpen())
{
pView->m_DaoDatabase.Close();
//
//AfxMessageBox("m_DaoDatabase.Close();");
}
if (plc.existFile(plc.InstallDir + "\\Database\\apollo.ldb"))
DeleteFile(plc.InstallDir + "\\Database\\apollo.ldb");
//close the target db in cdbinfolistct,
//pView->m_pListCt.DB_Close(); //runtime error!!
//----
if( !TerminateProcess(GetCurrentProcess(),0) ) 
AfxMessageBox("Fehlermeldung: Programm kann nicht terminiert werden, bitte versuchen Sie Task-Manager!");
}
}
/
//
// * Making other messages be answered, e.SC_MOVE 
//
/
CDialog::OnSysCommand(nID,lParam); 
}
4.OnSysCommand() 与 OnLButtonDown()在CDialog中响应区别
OnSysCommand 响应了对对话框标题栏的鼠标点击
OnLButtonDown响应了对对话框框体的鼠标点击(不包括标题栏)


三、用鼠标单击窗口标题条以外区域移动窗口 

移动标准窗口是通过用鼠标单击窗口标题条来实现的,

 

但对于没有标题条的窗口, 
就需要用鼠标单击窗口标题条以外区域来移动窗口。有两种方法可以达到这一目标。 
方法一:当窗口确定鼠标位置时,Windows向窗口发送WM_NCHITTEST消息,可以处 
理该消息,使得只要鼠标在窗口内,Windows便认为鼠标在标题条上。这需要重载 
CWnd类处理WM_NCHITTEST消息的OnNcHitTest函数,在函数中调用父类的该函数, 
如果返回HTCLIENT,说明鼠标在窗口客户区内,使重载函数返回HTCAPTION, 
使Windows误认为鼠标处于标题条上。 
下例是使用该方法的实际代码: 
UINT CEllipseWndDlg::OnNcHitTest(CPoint point) 

// 取得鼠标所在的窗口区域 
UINT nHitTest = CDialog::OnNcHitTest(point); 
// 如果鼠标在窗口客户区,则返回标题条代号给Windows 
// 使Windows按鼠标在标题条上类进行处理,即可单击移动窗口 
return (nHitTest==HTCLIENT) ? HTCAPTION : nHitTest; 

方法二:当用户在窗口客户区按下鼠标左键时,使Windows认为鼠标是在标题条上, 
即在处理WM_LBUTTONDOWN消息的处理函数OnLButtonDown中发送一个wParam参数为 
HTCAPTION,lParam为当前坐标的WM_NCLBUTTONDOWN消息。 
下面是使用该方法的实际代码: 
void CEllipseWndDlg::OnLButtonDown(UINT nFlags, CPoint point) 

// 调用父类处理函数完成基本操作 
CDialog::OnLButtonDown(nFlags, point); 
// 发送WM_NCLBUTTONDOWN消息 
// 使Windows认为鼠标在标题条上 
PostMessage(WM_NCLBUTTONDOWN, 
HTCAPTION, 
MAKELPARAM(point.x, point.y)); 

}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值