VC雕虫小技集(五)

何志丹

1,vc中右键点击一个类或宏,可以察看他的定义,但如何返回初始地方呢? 
install visual assist, and use Alt+left arrow
按菜单中的 后退 (<- 转45度)按钮
好像是又打开了一个文档,关闭它就行了



2,怎样获取系统菜单,并且弹出系统菜单?
void CTestsysmenuDlg::OnOK()
{
// click the OK button to show system menu(masterz)
POINT curPos;
GetCursorPos(&curPos);
SetForegroundWindow();
CMenu* pMenu=GetSystemMenu(false);
int nSelection=pMenu->TrackPopupMenu(TPM_LEFTBUTTON | TPM_LEFTALIGN |
TPM_BOTTOMALIGN|TPM_NONOTIFY|TPM_RETURNCMD, curPos.x, curPos.y, this);
DWORD dwpos= curPos.x + ((curPos.y<<16)&0xffff0000);
SendMessage(WM_SYSCOMMAND,nSelection,dwpos);
CString msg;
msg.Format("x:%d,y:%d; cursor.x=%d,cursor.y=%d",LOWORD(dwpos),HIWORD(dwpos),curPos.x,curPos.y);
OutputDebugString(msg);
}


3, 如何控制键盘上面的3 个灯的亮灭 (NUM LOCK ,CAPS LOCK SRCOLL LOCK)
#include <windows.h>
void SetNumLock( BOOL bState )
{
BYTE keyState[256];
GetKeyboardState((LPBYTE)&keyState);
if( (bState && !(keyState[VK_NUMLOCK] & 1)) ||
(!bState && (keyState[VK_NUMLOCK] & 1)) )
{
// Simulate a key press
keybd_event( VK_NUMLOCK,
0x45,
KEYEVENTF_EXTENDEDKEY | 0,
0 );
// Simulate a key release
keybd_event( VK_NUMLOCK,
0x45,
KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP,
0);
}
}


void main()
{
SetNumLock( TRUE );
}


4, 已经在VC的AppWizard中选择了中文后如何改为英文?
以某文件编辑器打开资源文件,在其中查找"Language",找到后可看到入下所示:
//Chinese (P.R.C) resources
#if !defined(AFX_RESOURCE_DLL) || defined(AFX_TARG_CHS)
#ifdef _WIN32
LANGUAGE LANG_CHINESE,SUBLANG_CHINESE_SIMPLIFIED
#pragma code_page(936)
#endif //_WIN32
这段代码表示当前使用的代码页为936,所代表的语种为简体中文,将以下代码用如下代码替换:
//English (U.S) resources
#if !defined(AFX_RESOURCE_DLL) || defined(AFX_TARG_ENU)
#ifdef _WIN32
LANGUAGE LANG_ENGLISH,SUBLANG_ENGLISH_US
#pragma code_page(1252)
#endif //_WIN32


5,如何有效地判断鼠标点是否在控件的矩形区域内
DWORD dw = GetMessagePos();
CPoint pt(LOWORD(dw),HIWORD(dw));//鼠标的屏幕坐标
CRect r;
GetDlgItem(IDC_EDIT1)->GetClientRect(&r);
GetDlgItem(IDC_EDIT1)->ClientToScreen(&r);
if(r.PtInRect(pt))
AfxMessageBox("在控件内");


6,处理控件的右键,类向导上没有
BOOL CCDialogDlg::PreTranslateMessage(MSG* pMsg)
{
if(WM_MOUSEMOVE == pMsg->message)
if(pMsg->hwnd == GetDlgItem(IDC_BUTTON1)->GetSafeHwnd())
{
UINT nFlag = pMsg->wParam; // 状态,如ctrl是不按下
int xPos = LOWORD(pMsg->lParam); // horizontal position of cursor
int yPos = HIWORD(pMsg->lParam); // vertical position of cursor

}
return CDialog::PreTranslateMessage(pMsg);
}