1 对话框
1.01 模态对话框
//创建对话框对象
CDemoDlg* pDlg = new CDemoDlg();
//显示对话框窗口
pDlg->DoModal();
//删除对话框对象
delete pDlg;
void CDemoDlg::OnCloseDlg()
{
//关闭对话框
EndDialog(IDOK);
}
/* 消息对话框 */
AfxMessageBox(_T("AfxMessageBox函数创建的消息对话框"));
MessageBox(_T("MessageBox函数创建的消息对话框"), _T("Demo"), MB_ICONEXCLAMATION);
1.02 非模态对话框
//创建对话框对象
CDemoDlg* pDlg = new CDemoDlg();
//创建对话框窗口
pDlg->Create(IDD_DEMO, this);
//显示对话框窗口
pDlg->ShowWindow(SW_SHOW);
//居中显示
pDlg->CenterWindow();
void CDemoDlg::OnCloseDlg()
{
//关闭非模态对话框,销毁对话框窗口
DestroyWindow();
}
1.03 拓展对话框大小
void CDemoDlg::OnExpand()
{
m_bExpand = !m_bExpand;
//获得窗口大小
CRect rect;
GetWindowRect(rect);
if (m_bExpand)
{
rect.SetRect(rect.left, rect.top, rect.right + 100, rect.bottom);
SetDlgItemText(IDC_TEST, _T("<<"));
}
else
{
rect.SetRect(rect.left, rect.top, rect.right - 100, rect.bottom);
SetDlgItemText(IDC_TEST, _T(">>"));
}
//设置窗口大小
MoveWindow(rect);
}
1.04 主窗口主动隐藏
void CDemoDlg::OnWindowPosChanging(WINDOWPOS* lpwndpos)
{
//删除SWP_SHOWWINDOW选项
lpwndpos->flags &= ~SWP_SHOWWINDOW;
CDialog::OnWindowPosChanging(lpwndpos);
}
1.05 窗口全屏
//获得屏幕长度和高度
int cx = GetSystemMetrics(SM_CXSCREEN);
int cy = GetSystemMetrics(SM_CYSCREEN);
//设置对话框位置和大小
SetWindowPos(NULL, 0, 0, cx, cy, SWP_NOZORDER);
1.05 窗口背景
/* 创建画刷 */
HBRUSH m_hBrush = CreateSolidBrush(RGB(0x99, 0xCC, 0xFF));
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;
}
/* 设置文本前景色和背景色 */
HBRUSH Cclass12Dlg::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
{
HBRUSH hbr = CDialog::OnCtlColor(pDC, pWnd, nCtlColor);
if (pWnd->GetDlgCtrlID() == IDC_EDIT1)
{
//设置文本前景色和背景色
pDC->SetTextColor(RGB(0xFF, 0x00, 0x00));
pDC->SetBkColor(RGB(0x99, 0xCC, 0xFF));
return m_hBrush;
}
return hbr;
}
1.05 对话框中输出文本
void Cclass12Dlg::OnPaint()
{
if (IsIconic())
{
CPaintDC dc(this); // 用于绘制的设备上下文
SendMessage(WM_ICONERASEBKGND, reinterpret_cast<WPARAM>(dc.GetSafeHdc()), 0);
// 使图标在工作区矩形中居中
int cxIcon = GetSystemMetrics(SM_CXICON);
int cyIcon = GetSystemMetrics(SM_CYICON);
CRect rect;
GetClientRect(&rect);
int x = (rect.Width() - cxIcon + 1) / 2;
int y = (rect.Height() - cyIcon + 1) / 2;
// 绘制图标
dc.DrawIcon(x, y, m_hIcon);
}
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();
}
}
1.06 对话框中添加菜单
新增菜单资源IDR_MENU1
定义菜单对象
CMenu m_Menu;
/* 窗口初始化加载 */
//加载菜单资源
m_Menu.LoadMenu(IDR_MENU1);
//设置当前菜单
SetMenu(&m_Menu);
1.07 对话框中添加工具栏
/* 创建工具栏资源 */
/* 创建工具栏对象 */
CToolBar m_wndToolBar;
//创建工具栏
if (!m_wndToolBar.Create(this))
{
return FALSE;
}
//加载工具栏资源
if (!m_wndToolBar.LoadToolBar(IDR_TOOLBAR1))
{
return FALSE;
}
//重新定位工具栏
RepositionBars(AFX_IDW_CONTROLBAR_FIRST, AFX_IDW_CONTROLBAR_LAST, 0);
1.08 对话框中添加状态栏
//创建状态栏窗口
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);
1.08 对话框中加载视图
/* CDemoView 继承 CView */
void CDemoView::OnDraw(CDC* pDC)
{
CRect rect;
GetClientRect(rect);
pDC->SetTextAlign(TA_CENTER);
pDC->TextOut(rect.Width() / 2, rect.Height() / 2, _T("Hello World !"));
}
/* 创建视图 */
CCreateContext context;
context.m_pNewViewClass = RUNTIME_CLASS(CDemoView);
context.m_pCurrentDoc = NULL;
context.m_pNewDocTemplate = NULL;
context.m_pLastView = NULL;
context.m_pCurrentFrame = (CFrameWnd*)this;
m_pView = (CDemoView*)context.m_pNewViewClass->CreateObject();
if (!m_pView->Create(NULL,NULL ,AFX_WS_DEFAULT_VIEW, CRect(0, 0, 0, 0), this, AFX_IDW_PANE_FIRST, &context))
{
return FALSE;
}
CRect rect;
GetClientRect(rect);
m_pView->MoveWindow(rect);
void CDemoDlg::OnSize(UINT nType, int cx, int cy)
{
CDialog::OnSize(nType, cx, cy);
if (m_pView != NULL)
{
CRect rect;
GetClientRect(rect);
m_pView->MoveWindow(rect);
}
}
1.09 截获按键消息
BOOL CDemoDlg::PreTranslateMessage(MSG* pMsg)
{
//将按键为ENTER的WM_KEYDOWN消息转换成按键为TAB的消息
if (pMsg->message == WM_KEYDOWN)
{
if (pMsg->wParam == VK_RETURN)
{
pMsg->wParam = VK_TAB;
}
}
return CDialog::PreTranslateMessage(pMsg);
}
1.10 窗口停靠
/* 初始化位置和大小 */
{
//对话框的宽和高
m_nWidth = 220;
m_nHeigh = 550;
//获得桌面工作区大小
CRect rect;
SystemParametersInfo(SPI_GETWORKAREA, 0, &rect, 0);
//对话框的初始位置
int x = rect.left + rect.Width() / 2 - m_nWidth / 2;
int y = rect.top + rect.Height() / 2 - m_nHeigh / 2;
MoveWindow(x, y, m_nWidth, m_nHeigh);
}
void CDemoDlg::OnWindowPosChanging(WINDOWPOS FAR* lpwndpos)
{
CDialog::OnWindowPosChanging(lpwndpos);
//获得桌面工作区大小
CRect rect;
SystemParametersInfo(SPI_GETWORKAREA, 0, &rect, 0);
//左停靠
if (abs(lpwndpos->x - rect.left) <= 50)
{
lpwndpos->x = rect.left;
lpwndpos->y = rect.top;
lpwndpos->cy = rect.Height();
}
//右停靠
else if (abs(lpwndpos->x + lpwndpos->cx-rect.right) <= 50)
{
lpwndpos->x = rect.right - lpwndpos->cx;
lpwndpos->y = rect.top;
lpwndpos->cy = rect.Height();
}
else
{
lpwndpos->cy = m_nHeigh;
}
if (lpwndpos->y < rect.top)
{
lpwndpos->y = rect.top;
}
}
1.11 窗体无边框,能够随意拖动
/* 捕捉鼠标点击的坐标,然后将其HTCLIENT结果偷换成HTCAPTION,这样就可以让系统误以为鼠标左键点击的是标题栏,就进入了拖拽模式了 */
UINT CDemoDlg::OnNcHitTest(CPoint point)
{
UINT nValue = CDialog::OnNcHitTest(point);
//返回HTCAPTION
if (nValue == HTCLIENT)
{
nValue = HTCAPTION;
}
return nValue;
}