现在的位置: 首页 > 综合 > 正文

WM_SYSCOMMAND Notification(WM_SYSCOMMAND 系统命令消息)应用

2013年03月31日 ⁄ 综合 ⁄ 共 2006字 ⁄ 字号 评论关闭

 WM_SYSCOMMAND消息是MFC预定义好的一组命令消息,框架有默认的处理方式,比如点击窗口右上方的关闭按钮,发送SC_CLOSE消息,用以关闭窗口。

系统命令消息包括以下几种:

SC_CLOSE
Closes the window.
SC_CONTEXTHELP
Changes the cursor to a question mark with a pointer. If the user then clicks a control in the dialog box, the control receives a message.
SC_DEFAULT
Selects the default item; the user double-clicked the window menu.
SC_HOTKEY
Activates the window associated with the application-specified hot key. The lParam parameter identifies the window to activate.
SC_HSCROLL
Scrolls horizontally.
SC_KEYMENU
Retrieves the window menu as a result of a keystroke. For more information, see the Remarks section.
SC_MAXIMIZE
Maximizes the window.
SC_MINIMIZE
Minimizes the window.
SC_MONITORPOWER
Sets the state of the display. This command supports devices that have power-saving features, such as a battery-powered personal computer.

The lParam parameter can have the following values:

1 - the display is going to low power

2 - the display is being shut off

SC_MOUSEMENU
Retrieves the window menu as a result of a mouse click.
SC_MOVE
Moves the window.
SC_NEXTWINDOW
Moves to the next window.
SC_PREVWINDOW
Moves to the previous window.
SC_RESTORE
Restores the window to its normal position and size.
SC_SCREENSAVE
Executes the screen saver application specified in the [boot] section of the System.ini file.
SC_SIZE
Sizes the window.
SC_TASKLIST
Activates the Start menu.
SC_VSCROLL
Scrolls vertically.

下面应用SC_SIZE消息和SC_MOVE 消息在窗口上动态改变一个按钮控件,实践发现,这些消息值再加上一个值会

有不同的效果:

BOOL CSysCommandTestDlg::PreTranslateMessage(MSG* pMsg)
{
 // TODO: 在此添加专用代码和/或调用基类
 CWnd *pWnd = GetDlgItem(IDC_BUTTON1);//得到此按钮窗口指针
 if ((pMsg->message==WM_LBUTTONDOWN) && (pMsg->hwnd == pWnd->m_hWnd))
 {
  //
  pWnd->SendMessage( WM_SYSCOMMAND,SC_MOVE+1,0);//按鼠标左键移动窗口
  return TRUE;
 }
 else if ((pMsg->message==WM_RBUTTONDOWN) && (pMsg->hwnd == pWnd->m_hWnd))
 {
  //按鼠标右键改变窗口大小,加上不同的值代表按不同的方向改变
  pWnd->SendMessage( WM_SYSCOMMAND,SC_SIZE+2,0
  return TRUE;
 }
 
 return CDialog::PreTranslateMessage(pMsg);
}

以下代码将导致右上方的关闭按钮无效,OnClose函数也没有进入:

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

{

	// TODO: 在此添加消息处理程序代码和/或调用默认值

	if (nID == SC_CLOSE)

	{

		return;

	}



	CMDIFrameWnd::OnSysCommand(nID, lParam);

}
由此可以说明点击右上方的关闭按钮后,先是发送系统消息SC_CLOSE,在OnSysCommand响应函数中,再发送
WM_CLOSE消息。

抱歉!评论已关闭.