vc入门宝典(十)

类别:VC语言 点击:0 评论:0 推荐:

消息

何志丹

PreTranslateMessage函数顾名思义,就是在消息被翻译之前,做的一些事.我们可以屏蔽一个键或一个命令.
下面是一些常用消息的相关信息.这些信息放pMsg中.
WM_KEYDOWN
nVirtKey = (int) wParam;  /*虚键值,和ascll码有许多相同的地方 */
lKeyData = lParam;        /*和硬件有关 */   
WM_CHAR
chCharCode = (TCHAR) wParam;   
lKeyData = lParam;             
WM_COMMAND
wNotifyCode = HIWORD(wParam);
wID = LOWORD(wParam);        
hwndCtl = (HWND) lParam;     
WM_LBUTTONDOWN
fwKeys = wParam;        //
xPos = LOWORD(lParam);  //  鼠标横坐标
yPos = HIWORD(lParam);  //  鼠标纵坐标
 fwKeys
MK_CONTROL MK_LBUTTON MK_MBUTTON MK_RBUTTON  MK_SHIFT
WM_MOUSEMOVE
fwKeys = wParam;       
xPos = LOWORD(lParam);  
yPos = HIWORD(lParam); 
 
下面这个例子演示了,PreTranslateMessage的常用用法.
BOOL CPreTranslateDlg::PreTranslateMessage(MSG* pMsg)
{
 if(WM_KEYDOWN == pMsg->message )
  if(0x30 == (int) pMsg->wParam)
  {
   return true;               /*禁止在编辑框中输入0(ascll 0x30)*/
  }                                

 if(WM_CHAR == pMsg->message )
  if('A' == (TCHAR) pMsg->wParam)
  {
   return true;               /*禁止在编辑框中输入'A'*/
  }
  
 if(WM_MOUSEMOVE == pMsg->message) 
  if(MK_CONTROL & pMsg->wParam)  /*当ctrl被按下鼠标移动时,显示相关信息*/
  {
   int xPos = LOWORD(pMsg->lParam); 
   int yPos = HIWORD(pMsg->lParam);
   CString str ;
   str.Format("鼠标的坐标%d %d",xPos,yPos);
   AfxMessageBox(str);
  }

 if(WM_COMMAND == pMsg->message)
  if(ID_1 == LOWORD(pMsg->wParam))//禁止菜单项ID_1,注意按钮发送的是BN_CLICKED
  {
   return true;
  }
 return CDialog::PreTranslateMessage(pMsg);
}

本文地址:http://com.8s8s.com/it/it1633.htm