记录vc编程的知识点。由于vc知识点小而零碎,所以使用小锦集记录较合适。
1. 设置对话框的颜色或背景:
OnEraseBkgnd():适合修改对话框背景
OnPaint():适合完成复杂的显示操作
OnCtlColor():适合修改对话框上控件的颜色
调用顺序:对话框初始化完毕,显示时调用OnSize()->OnEraseBkgnd()->OnPaint()->OnCtlColor()
2. Windows窗口刷新机制:
当窗口出现无效区域时,刷新显示无效区域。
1)当窗口没有无效区域,即使收到WM_PAINT消息,程序也不作处理;
2)当窗口有无效区域,收到WM_PAINT消息后,调用OnPaint()刷新窗口。
主动刷行窗口:1:RedrawWindow;2:Invalidate(TRUE); (UpdateWindow())。
以上两条参考:http://www.cnblogs.com/lidabo/archive/2012/07/03/2574702.html
3. 屏蔽Statusbar默认提示:
1).h添加声明:afx_msg LRESULT OnSetMessageString(WPARAM, LPARAM);
2)添加消息映射:ON_MESSAGE(WM_SETMESSAGESTRING, OnSetMessageString)
3)实现函数:
{
LRESULT lr = CFrameWnd::OnSetMessageString(wParam, lParam);
if ((wParam == AFX_IDS_IDLEMESSAGE)
|| (wParam == ID_VIEW_TOOLBAR) || (wParam == ID_VIEW_STATUS_BAR) || (wParam == ID_APP_ABOUT))
{
SetMessageText(L"");
return S_OK;
}
return lr;
}
4. 动态改变菜单项在状态栏中的提示:
1)通过ClassWizard添加重载函数:virtual void
GetMessageString(UINT nID, CString& rMessage) const;
2)完善实现如下:
{
// TODO: Add your specialized code here and/or call the base class
if(nID == ID_EXAM_NEW) // 按ID修改提示
{
rMessage = "Create new file!";
return;
}
return CFrameWnd::GetMessageString(nID, rMessage);
}
5. 指定 BROWSEINFOW 的初始路径
// brief : 指定 BROWSEINFOW 的初始路径
int CALLBACK BrowseCallbackProc(HWND hwnd, UINT uMsg, LPARAM lParam, LPARAM lpData)
{
if (uMsg == BFFM_INITIALIZED )
{
::SendMessage(hwnd, BFFM_SETSELECTION, TRUE, lpData);
}
return 0;
}
void SelectDir()
{
BROWSEINFOW bi;
bi.hwndOwner = this->m_hWnd;
bi.pidlRoot = NULL;
bi.pszDisplayName = wszPath; // Pointer to a buffer to receive the display name of the folder selected by the user.
bi.lpszTitle = L"请选择目录:"; // Pointer to a null-terminated string that is displayed above the tree view control in the dialog box.
bi.ulFlags = 0;
bi.lpfn = BrowseCallbackProc;
bi.lParam = (LPARAM)GetImageDir();
bi.iImage = 0;
//弹出选择目录对话框
LPITEMIDLIST lp = SHBrowseForFolderW(&bi);
if(lp && SHGetPathFromIDList(lp, wszPath))
{
// wszPath
}
}
6. 获取屏幕矩阵
int nScreenCX = ::GetDeviceCaps(GetDC()->m_hDC, HORZRES);
int nScreenCY = ::GetDeviceCaps(GetDC()->m_hDC, VERTRES);
// 获取除 系统任务栏 外的矩阵
RECT rect;
SystemParametersInfo(SPI_GETWORKAREA, 0, &rect, 0);
// 获取除 vs2010任务栏 + 系统任务栏 外的矩阵
int nScreenCX = ::GetSystemMetrics(SM_CXFULLSCREEN);
int nScreenCY = ::GetSystemMetrics(SM_CYFULLSCREEN);
7. 消息窗口,隐藏的、用于接收消息,设置方法
//Message-Only Windows:
// A message-only window enables you to send and receive messages.
// It is not visible, has no z-order, cannot be enumerated, and does not receive broadcast messages.
// The window simply dispatches messages.