4.14 如何改变对话框的背景颜色
- OnCtlColor
- CTLCOLOR_DLG
HBRUSH CDemoDlg::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
{
HBRUSH hbr = CDialog::OnCtlColor(pDC, pWnd, nCtlColor);
//改变对话框的背景颜色
if (nCtlColor == CTLCOLOR_DLG)
{
hbr = m_hBrush;
}
return hbr;
}
4.15 如何改变对话框中控件的颜色
根据控件Id更改,这个事件一直触发
HBRUSH CDemoDlg::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
{
HBRUSH hbr = CDialog::OnCtlColor(pDC, pWnd, nCtlColor);
if (pWnd->GetDlgCtrlID() == IDC_EDIT)
{
//设置文本前景色和背景色
pDC->SetTextColor(RGB(0xFF, 0x00, 0x00));
pDC->SetBkColor(RGB(0x99, 0xCC, 0xFF));
return m_hBrush;
}
return hbr;
}
4.16 如何在对话框中输出文本
在OnPaint事件中使用CPaintDC的TextOut方法
void CDemoDlg::OnPaint()
{
if (IsIconic())
{
}
else
{
//获得客户区大小
CRect rect;
GetClientRect(rect);
CPaintDC dc(this);
//设置背景模式
dc.SetBkMode(TRANSPARENT);
//设置文本对齐方式
dc.SetTextAlign(TA_CENTER | TA_BASELINE);
//输出文本
dc.TextOut(rect.Width() / 2, rect.Height() / 2, _T("Hello World!"));
CDialog::OnPaint();
}
}
4.17 如何在对话框中加载菜单
使用CMenu对象的LoadMenu方法加载资源,然后调用SetMenu方法设置菜单
//加载菜单资源
m_Menu.LoadMenu(IDR_MENU);
//设置当前菜单
SetMenu(&m_Menu);
4.18 如何在对话框中加载工具栏
使用CToolBar相关方法
//创建工具栏
if (!m_wndToolBar.Create(this))
{
return FALSE;
}
//加载工具栏资源
if (!m_wndToolBar.LoadToolBar(IDR_TOOLBAR))
{
return FALSE;
}
//获得旧客户区坐标
CRect rcOldClient;
GetClientRect(rcOldClient);
//获得新客户区坐标
CRect rcNewClient;
RepositionBars(AFX_IDW_CONTROLBAR_FIRST, AFX_IDW_CONTROLBAR_LAST,
0, reposQuery, rcNewClient);
//移动主窗口
CRect rcMain;
GetWindowRect(rcMain);
rcMain.right += rcOldClient.Width() - rcNewClient.Width();
rcMain.bottom += rcOldClient.Height() - rcNewClient.Height();
MoveWindow(rcMain, FALSE);
//移动所有子窗口
CRect rcChild;
CPoint point(rcNewClient.left - rcOldClient.left, rcNewClient.top - rcOldClient.top);
CWnd* pChildWnd = GetWindow(GW_CHILD);
while (pChildWnd != NULL)
{
pChildWnd->GetWindowRect(rcChild);
ScreenToClient(rcChild);
rcChild.OffsetRect(point);
pChildWnd->MoveWindow(rcChild, FALSE);
pChildWnd = pChildWnd->GetNextWindow();
}
//重新定位工具栏
RepositionBars(AFX_IDW_CONTROLBAR_FIRST, AFX_IDW_CONTROLBAR_LAST, 0);
4.19 如何在对话框中加载状态栏
类似的,使用CStatusBar
//创建状态栏窗口
if (!m_wndStatusBar.Create(this))
{
return -1;
}
//ID数组
UINT IDArray[2];
for (int n = 0; n < 2; n++)
{
IDArray[n] = 10000 + n;
}
//设置状态栏指示器
m_wndStatusBar.SetIndicators(IDArray, sizeof(IDArray) / sizeof(UINT));
//设置窗格宽度
m_wndStatusBar.SetPaneInfo(0, IDArray[0], SBPS_NORMAL, 100);
m_wndStatusBar.SetPaneInfo(1, IDArray[1], SBPS_STRETCH, 0);
//设置窗格文本
m_wndStatusBar.SetPaneText(0, _T("状态栏:"));
m_wndStatusBar.SetPaneText(1, _T(""));
//获得旧客户区坐标
CRect rcOldClient;
GetClientRect(rcOldClient);
//获得新客户区坐标
CRect rcNewClient;
RepositionBars(AFX_IDW_CONTROLBAR_FIRST, AFX_IDW_CONTROLBAR_LAST,
0, reposQuery, rcNewClient);
//移动主窗口
CRect rcMain;
GetWindowRect(rcMain);
rcMain.right += rcOldClient.Width() - rcNewClient.Width();
rcMain.bottom += rcOldClient.Height() - rcNewClient.Height();
MoveWindow(rcMain, FALSE);
//移动所有子窗口
CRect rcChild;
CPoint point(rcNewClient.left - rcOldClient.left, rcNewClient.top - rcOldClient.top);
CWnd* pChildWnd = GetWindow(GW_CHILD);
while (pChildWnd != NULL)
{
pChildWnd->GetWindowRect(rcChild);
ScreenToClient(rcChild);
rcChild.OffsetRect(point);
pChildWnd->MoveWindow(rcChild, FALSE);
pChildWnd = pChildWnd->GetNextWindow();
}
//重新定位状态栏
RepositionBars(AFX_IDW_CONTROLBAR_FIRST, AFX_IDW_CONTROLBAR_LAST, 0);