本文简单地介绍下VS中如何为对话框添加菜单栏工具栏、改变对话框大小并且将对话框拆分为两个视图。
使用工具:VS2008
使用语言:C++
开发步骤:
CRect rectToolbar; m_wndToolBar.GetWindowRect(&rectToolbar);OnCreate中
if(!m_wndToolBar.CreateEx(this,TBSTYLE_FLAT,WS_CHILD|WS_VISIBLE|CBRS_TOP|CBRS_GRIPPER|CBRS_TOOLTIPS|CBRS_FLYBY|CBRS_SIZE_DYNAMIC)||!m_wndToolBar.LoadToolBar(IDR_TOOLBAR1)) { return -1; } RepositionBars(AFX_IDW_CONTROLBAR_FIRST,AFX_IDW_CONTROLBAR_LAST,0);效果如图
RECT rect; GetWindowRect(&rect); ScreenToClient(&rect); rect.top = rect.top + rectToolbar.Height()* 3 + 3; m_pMyFrame->MoveWindow(&rect); m_pMyFrame->ShowWindow(SW_SHOW);
CString strMyClass = AfxRegisterWndClass(CS_VREDRAW | CS_HREDRAW,::LoadCursor(NULL, IDC_ARROW),(HBRUSH)::GetStockObject(WHITE_BRUSH),::LoadIcon(NULL, IDI_APPLICATION)); m_pMyFrame = new CFrameWnd; m_pMyFrame->Create(strMyClass,_T(""), WS_CHILD,CRect(0,0,300,300), this); m_pMyFrame->ShowWindow(SW_SHOW);效果如图
CRect rect; GetClientRect(&rect); CSize size = rect.Size(); CSize sizelist,sizechart; sizelist=sizechart=size; sizelist.cx = size.cx * 1/3; sizechart.cx = size.cx * 2/3; m_cSplitter.CreateStatic(m_pMyFrame,1, 2); m_cSplitter.CreateView(0,0, RUNTIME_CLASS(CMyTreeView), sizelist, NULL); m_cSplitter.CreateView(0,1, RUNTIME_CLASS(CMyListView), sizechart, NULL); m_pMyTreeView = (CMyTreeView*)m_cSplitter.GetPane(0, 0); m_pMyListView = (CMyListView*)m_cSplitter.GetPane(0, 1);
设置Border属性为Resizing
void CSplitterDialogDlg::OnSize(UINT nType, int cx, int cy) { CDialog::OnSize(nType, cx, cy); // TODO: 在此处添加消息处理程序代码 if (m_bShow && (m_wndToolBar.m_hWnd != NULL)) { CRect rectToolbar; m_wndToolBar.GetWindowRect(&rectToolbar); RECT rect; GetWindowRect(&rect); ScreenToClient(&rect); rect.top = rect.top + rectToolbar.Height()* 3 + 3; m_pMyFrame->MoveWindow(&rect); m_pMyFrame->ShowWindow(SW_SHOW); } }在看下效果
void CSplitterDialogDlg::OnMenu() { MessageBox(_T("菜单测试")); } void CSplitterDialogDlg::OnToolBar() { MessageBox(_T("工具栏测试")); }效果如下
// SplitterDialogDlg.h : 头文件 // #pragma once #include "MyTreeView.h" #include "MyListView.h" // CSplitterDialogDlg 对话框 class CSplitterDialogDlg : public CDialog { // 构造 public: CSplitterDialogDlg(CWnd* pParent = NULL); // 标准构造函数 // 对话框数据 enum { IDD = IDD_SPLITTERDIALOG_DIALOG }; protected: virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV 支持 // 实现 protected: HICON m_hIcon; // 生成的消息映射函数 virtual BOOL OnInitDialog(); afx_msg void OnSysCommand(UINT nID, LPARAM lParam); afx_msg void OnPaint(); afx_msg HCURSOR OnQueryDragIcon(); DECLARE_MESSAGE_MAP() public: CToolBar m_wndToolBar; CFrameWnd* m_pMyFrame; CSplitterWnd m_cSplitter; CMyTreeView* m_pMyTreeView; CMyListView* m_pMyListView; BOOL m_bShow; afx_msg int OnCreate(LPCREATESTRUCT lpCreateStruct); afx_msg void OnSize(UINT nType, int cx, int cy); afx_msg void OnMenu(); afx_msg void OnToolBar(); };
// SplitterDialogDlg.cpp : 实现文件 // #include "stdafx.h" #include "SplitterDialog.h" #include "SplitterDialogDlg.h" #ifdef _DEBUG #define new DEBUG_NEW #endif // 用于应用程序“关于”菜单项的 CAboutDlg 对话框 class CAboutDlg : public CDialog { public: CAboutDlg(); // 对话框数据 enum { IDD = IDD_ABOUTBOX }; protected: virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV 支持 // 实现 protected: DECLARE_MESSAGE_MAP() }; CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD) { } void CAboutDlg::DoDataExchange(CDataExchange* pDX) { CDialog::DoDataExchange(pDX); } BEGIN_MESSAGE_MAP(CAboutDlg, CDialog) END_MESSAGE_MAP() // CSplitterDialogDlg 对话框 CSplitterDialogDlg::CSplitterDialogDlg(CWnd* pParent /*=NULL*/) : CDialog(CSplitterDialogDlg::IDD, pParent) { m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME); m_pMyFrame = NULL; m_pMyTreeView = NULL; m_pMyListView = NULL; m_bShow = FALSE; } void CSplitterDialogDlg::DoDataExchange(CDataExchange* pDX) { CDialog::DoDataExchange(pDX); } BEGIN_MESSAGE_MAP(CSplitterDialogDlg, CDialog) ON_WM_SYSCOMMAND() ON_WM_PAINT() ON_WM_QUERYDRAGICON() //}}AFX_MSG_MAP ON_WM_CREATE() ON_WM_SIZE() ON_COMMAND(ID_MENU, &CSplitterDialogDlg::OnMenu) ON_COMMAND(ID_BUTTON_TEST, &CSplitterDialogDlg::OnToolBar) END_MESSAGE_MAP() // CSplitterDialogDlg 消息处理程序 BOOL CSplitterDialogDlg::OnInitDialog() { CDialog::OnInitDialog(); // 将“关于...”菜单项添加到系统菜单中。 // IDM_ABOUTBOX 必须在系统命令范围内。 ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX); ASSERT(IDM_ABOUTBOX < 0xF000); CMenu* pSysMenu = GetSystemMenu(FALSE); if (pSysMenu != NULL) { CString strAboutMenu; strAboutMenu.LoadString(IDS_ABOUTBOX); if (!strAboutMenu.IsEmpty()) { pSysMenu->AppendMenu(MF_SEPARATOR); pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu); } } // 设置此对话框的图标。当应用程序主窗口不是对话框时,框架将自动 // 执行此操作 SetIcon(m_hIcon, TRUE); // 设置大图标 SetIcon(m_hIcon, FALSE); // 设置小图标 // TODO: 在此添加额外的初始化代码 //工具条 CRect rectToolbar; m_wndToolBar.GetWindowRect(&rectToolbar); //设置视图位置和尺寸 RECT rect; GetWindowRect(&rect); ScreenToClient(&rect); rect.top = rect.top + rectToolbar.Height()* 3 + 3; m_pMyFrame->MoveWindow(&rect); m_pMyFrame->ShowWindow(SW_SHOW); //Puts the thread that created the window into the foreground and activates the window. SetForegroundWindow(); m_bShow = TRUE; return TRUE; // 除非将焦点设置到控件,否则返回 TRUE } void CSplitterDialogDlg::OnSysCommand(UINT nID, LPARAM lParam) { if ((nID & 0xFFF0) == IDM_ABOUTBOX) { CAboutDlg dlgAbout; dlgAbout.DoModal(); } else { CDialog::OnSysCommand(nID, lParam); } } // 如果向对话框添加最小化按钮,则需要下面的代码 // 来绘制该图标。对于使用文档/视图模型的 MFC 应用程序, // 这将由框架自动完成。 void CSplitterDialogDlg::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 { CDialog::OnPaint(); } } //当用户拖动最小化窗口时系统调用此函数取得光标 //显示。 HCURSOR CSplitterDialogDlg::OnQueryDragIcon() { return static_cast<HCURSOR>(m_hIcon); } int CSplitterDialogDlg::OnCreate(LPCREATESTRUCT lpCreateStruct) { if (CDialog::OnCreate(lpCreateStruct) == -1) return -1; // TODO: 在此添加您专用的创建代码 //工具条 if(!m_wndToolBar.CreateEx(this,TBSTYLE_FLAT,WS_CHILD|WS_VISIBLE|CBRS_TOP|CBRS_GRIPPER|CBRS_TOOLTIPS|CBRS_FLYBY|CBRS_SIZE_DYNAMIC)||!m_wndToolBar.LoadToolBar(IDR_TOOLBAR1)) { return -1; } RepositionBars(AFX_IDW_CONTROLBAR_FIRST,AFX_IDW_CONTROLBAR_LAST,0); //设置视图位置和尺寸 CString strMyClass = AfxRegisterWndClass(CS_VREDRAW | CS_HREDRAW,::LoadCursor(NULL, IDC_ARROW),(HBRUSH)::GetStockObject(WHITE_BRUSH),::LoadIcon(NULL, IDI_APPLICATION)); m_pMyFrame = new CFrameWnd; m_pMyFrame->Create(strMyClass,_T(""), WS_CHILD,CRect(0,0,300,300), this); m_pMyFrame->ShowWindow(SW_SHOW); //拆分窗口 CRect rect; GetClientRect(&rect); CSize size = rect.Size(); CSize sizelist,sizechart; sizelist=sizechart=size; sizelist.cx = size.cx * 1/3; sizechart.cx = size.cx * 2/3; m_cSplitter.CreateStatic(m_pMyFrame,1, 2); m_cSplitter.CreateView(0,0, RUNTIME_CLASS(CMyTreeView), sizelist, NULL); m_cSplitter.CreateView(0,1, RUNTIME_CLASS(CMyListView), sizechart, NULL); m_pMyTreeView = (CMyTreeView*)m_cSplitter.GetPane(0, 0); m_pMyListView = (CMyListView*)m_cSplitter.GetPane(0, 1); return 0; } void CSplitterDialogDlg::OnSize(UINT nType, int cx, int cy) { CDialog::OnSize(nType, cx, cy); // TODO: 在此处添加消息处理程序代码 if (m_bShow && (m_wndToolBar.m_hWnd != NULL)) { CRect rectToolbar; m_wndToolBar.GetWindowRect(&rectToolbar); RECT rect; GetWindowRect(&rect); ScreenToClient(&rect); rect.top = rect.top + rectToolbar.Height()* 3 + 3; m_pMyFrame->MoveWindow(&rect); m_pMyFrame->ShowWindow(SW_SHOW); } } void CSplitterDialogDlg::OnMenu() { MessageBox(_T("菜单测试")); } void CSplitterDialogDlg::OnToolBar() { MessageBox(_T("工具栏测试")); }