CDialog::DoModal virtual int DoModal( ); CDialog::Create BOOL Create( UINT nIDTemplate, CWnd* pParentWnd = NULL ); CWnd::ShowWindow BOOL ShowWindow ( int nCmdShow ); CButton::Create BOOL Create( LPCTSTR lpszCaption, DWORD dwStyle, const RECT& rect, CWnd* pParentWnd, UINT nID ); /*demo1 (对话框的创建)*/ //创建模态对话框 CTestDialog dialog; dialog.DoModal();//程序会在这里暂停执行,dialog声明周期并没有结束 //创建非模态对话框 CTestDialog *pDlg = new CTestDialog(); pDlg->Create(IDD_DIALOG1,this);//程序不会在这里停留 pDlg->ShowWindow(SW_SHOW);
/*demo2 (动态产生按钮) 在对话框中添加button,并添加消息响应 */ void CTestDialog::OnBtnAdd() { if(!m_btn.m_hWnd) { m_btn.Create("MFC",BS_DEFPUSHBUTTON | WS_VISIBLE | WS_CHILD,CRect(0,0,100,100),this,123); } else { m_btn.DestroyWindow(); } }
CRect::IsRectEmpty BOOL IsRectEmpty( ) const; CWnd::GetWindowRect void GetWindowRect( LPRECT lpRect ) const; CWindow::SetWindowPos BOOL SetWindowPos( HWND hWndInsertAfter, int x, int y, int cx, int cy, UINT nFlags ); BOOL SetWindowPos( HWND hWndInsertAfter, LPCRECT lpRect, UINT nFlags ); /*demo3 (实现可收缩和扩展的对话框) 在对话框上放置一个按钮,并添加消息响应函数, 每次点击按钮,都会改变按钮的caption,并且进行收缩和扩展。 在对话框放置一个图像控件,并拉成一条直线, 作为分隔的标记,属性中勾选sunken,设置为不可见。 */ void CTestDialog::OnButton1() { // TODO: Add your control notification handler code here CString str; if(GetDlgItem(IDC_BUTTON1)->GetWindowText(str),str=="收缩<<") { GetDlgItem(IDC_BUTTON1)->SetWindowText("扩展>>"); } else { GetDlgItem(IDC_BUTTON1)->SetWindowText("收缩<<"); } static CRect rectLarge; static CRect rectSmall; if(rectLarge.IsRectEmpty()) { CRect rectSeparator; GetWindowRect(&rectLarge); GetDlgItem(IDC_SEPARATOR)->GetWindowRect(&rectSeparator); rectSmall = rectLarge; rectSmall.bottom = rectSeparator.bottom; } if(str=="收缩<<") { //收缩 SetWindowPos(NULL, 0, 0, rectSmall.Width(), rectSmall.Height(), SWP_NOMOVE|SWP_NOZORDER); } else { //扩展 SetWindowPos(NULL, 0, 0, rectLarge.Width(), rectLarge.Height(), SWP_NOMOVE|SWP_NOZORDER); } }
//全局函数只能用Win32 SDK SetWindowLong//Win32 SDK 该函数改变指定窗口的属性 LONG SetWindowLong( HWND hWnd, int nIndex, LONG dwNewLong); SetFocus//Win32 SDK 设置焦点 HWND SetFocus(HWND hWnd // handle to window); GetNextWindow HWND GetNextWindow(HWND hWnd, /*handle to current window */UINT wCmd /*direction*/); /* demo4 (修改已经创建的编辑框的窗口过程函数,使其接收回车消息,焦点下移) 在对话框中创建一个编辑框。属性中设置多行 重写OnOK函数,使敲击回车时窗口不关闭。 增加对话框对WM_INITDIALOG消息的处理。 在CTestDialog中EditCode: */ WNDPROC prevProc;//先前窗口回调函数 //回调函数的定义可以查看MSDN的WNDCLASS结构 LRESULT CALLBACK myProc(HWND hwnd, // handle to window UINT uMsg, // message identifier WPARAM wParam, // first message parameter LPARAM lParam // second message parameter ) { if(uMsg==WM_CHAR && wParam==0x0d) { //如果是字符消息,并且是回车,则移动焦点到下一个窗口 //获取下一个控件句柄的三个方法 //::SetFocus(::GetNextWindow(hwnd,GW_HWNDNEXT));//法1 //::SetFocus(::GetWindow(hwnd,GW_HWNDNEXT));//法2 ::SetFocus(::GetNextDlgTabItem(::GetParent(hwnd),hwnd,FALSE));//法3 return 1; } else { //否则执行原来的回调函数 return prevProc(hwnd, uMsg, wParam,lParam ); } } BOOL CTestDialog::OnInitDialog() { CDialog::OnInitDialog(); prevProc = (WNDPROC)SetWindowLong(GetDlgItem(IDC_EDIT1)->m_hWnd,GWL_WNDPROC,(LONG)myProc); // TODO: Add extra initialization here return TRUE; // return TRUE unless you set the focus to a control // EXCEPTION: OCX Property Pages should return FALSE } void CTestDialog::OnOK() { // TODO: Add extra validation here // CDialog::OnOK(); }
/* demo5 (多个编辑框都支持回车焦点下移) */ void CTestDialog::OnOK() { // TODO: Add extra validation here //GetDlgItem(IDC_EDIT1)->GetNextWindow()->SetFocus();//效果同设置编辑框的回调函数,不能传递焦点 //GetFocus()->GetNextWindow()->SetFocus(); GetNextDlgTabItem(GetFocus())->SetFocus(); // CDialog::OnOK(); }
demo6(使控件关联新的类对象,设计“逃跑”按钮) //对话框中有两个按钮,但鼠标指针移动到一个按钮之上,按钮会隐藏,并让另一个显示。 //创建基于对话框的程序,并在对话框上添加两个Button。 //创建一个新类CMyButton,继承与CButton,并在新类中捕获WM_MOUSEMOVE消息。 //使两个按钮控件分别关联一个CMyButton类对象。 void CMybutton::OnMouseMove(UINT nFlags, CPoint point) { // TODO: Add your message handler code here and/or call default this->ShowWindow(SW_HIDE); if( this->GetDlgCtrlID() == IDC_BUTTON1) { this->GetParent()->GetDlgItem(IDC_BUTTON2)->ShowWindow(SW_SHOW); } else { this->GetParent()->GetDlgItem(IDC_BUTTON1)->ShowWindow(SW_SHOW); } CButton::OnMouseMove(nFlags, point); }