- 创建一个对话框并且和一个类相关联,并且通过点击一个菜单项进行显示
代码1 void CDlg1View::OnDialog()
2 {
3 // TODO: Add your command handler code here
4 /*CTestDlg dlg;
5 dlg.DoModal();*/
6
7 CTestDlg *dlg = new CTestDlg();
8 dlg->Create(IDD_DIALOG1, this);
9 dlg->ShowWindow(SW_SHOW);
10 }
上面显示了两种创建并且显示Dialog的方法,第一种是创建一个模态对话框,模态对话框是指只要该对话框没有被销毁,其他的控件都不能用;第二种方法是创建一个非模态对话框,但是这种创建方式又一个漏洞,因为没有办法释放dlg占用的内存。可以在CTestDlg中实现OnOK()函数,在其中调用DestroyWindow()函数。
- 使用7种方式实现对话框中控件的使用
-
代码1 void CTestDlg::OnBtnAdd()
2 {
3 // TODO: Add your control notification handler code here
4 /* static BOOL bIsCreate=FALSE;
5 // if(m_bIsCreate==FALSE)
6 if(bIsCreate==FALSE)
7 {
8 m_btn.Create("维新",BS_DEFPUSHBUTTON | WS_VISIBLE | WS_CHILD,
9 CRect(0,0,100,100),this,123);
10 //m_bIsCreate=TRUE;
11 bIsCreate=TRUE;
12 }
13 else
14 {
15 m_btn.DestroyWindow();
16 //m_bIsCreate=FALSE;
17 bIsCreate=FALSE;
18 }*/
19 /* if(!m_btn.m_hWnd)
20 {
21 m_btn.Create("维新",BS_DEFPUSHBUTTON | WS_VISIBLE | WS_CHILD,
22 CRect(0,0,100,100),this,123);
23 }
24 else
25 {
26 m_btn.DestroyWindow();
27 }*/
28 /* int num1,num2,num3;
29 char ch1[10],ch2[10],ch3[10];
30
31 GetDlgItem(IDC_EDIT1)->GetWindowText(ch1,10);
32 GetDlgItem(IDC_EDIT2)->GetWindowText(ch2,10);
33
34 num1=atoi(ch1);
35 num2=atoi(ch2);
36 num3=num1+num2;
37 itoa(num3,ch3,10);
38
39 GetDlgItem(IDC_EDIT3)->SetWindowText(ch3);*/
40
41 /* int num1,num2,num3;
42 char ch1[10],ch2[10],ch3[10];
43
44 GetDlgItemText(IDC_EDIT1,ch1,10);
45 GetDlgItemText(IDC_EDIT2,ch2,10);
46
47 num1=atoi(ch1);
48 num2=atoi(ch2);
49 num3=num1+num2;
50 itoa(num3,ch3,10);
51
52 SetDlgItemText(IDC_EDIT3,ch3);*/
53
54 /* int num1,num2,num3;
55 num1=GetDlgItemInt(IDC_EDIT1);
56 num2=GetDlgItemInt(IDC_EDIT2);
57
58 num3=num1+num2;
59 SetDlgItemInt(IDC_EDIT3,num3);*/
60
61 /*UpdateData();
62 m_num3=m_num1+m_num2;
63 UpdateData(FALSE);*/
64
65 /* int num1,num2,num3;
66 char ch1[10],ch2[10],ch3[10];
67
68 m_edit1.GetWindowText(ch1,10);
69 m_edit2.GetWindowText(ch2,10);
70
71 num1=atoi(ch1);
72 num2=atoi(ch2);
73 num3=num1+num2;
74 itoa(num3,ch3,10);
75
76 m_edit3.SetWindowText(ch3);*/
77
78 /* int num1,num2,num3;
79 char ch1[10],ch2[10],ch3[10];
80
81 //::SendMessage(GetDlgItem(IDC_EDIT1)->m_hWnd,WM_GETTEXT,10,(LPARAM)ch1);
82 //::SendMessage(m_edit1.m_hWnd,WM_GETTEXT,10,(LPARAM)ch1);
83 //GetDlgItem(IDC_EDIT1)->SendMessage(WM_GETTEXT,10,(LPARAM)ch1);
84 m_edit1SendMessage.(WM_GETTEXT,10,(LPARAM)ch1);
85 m_edit2.SendMessage(WM_GETTEXT,10,(LPARAM)ch2);
86
87 num1=atoi(ch1);
88 num2=atoi(ch2);
89 num3=num1+num2;
90 itoa(num3,ch3,10);
91
92 m_edit3.SendMessage(WM_SETTEXT,0,(LPARAM)ch3);*/
93
94 int num1,num2,num3;
95 char ch1[10],ch2[10],ch3[10];
96
97 SendDlgItemMessage(IDC_EDIT1,WM_GETTEXT,10,(LPARAM)ch1);
98 SendDlgItemMessage(IDC_EDIT2,WM_GETTEXT,10,(LPARAM)ch2);
99
100 num1=atoi(ch1);
101 num2=atoi(ch2);
102 num3=num1+num2;
103 itoa(num3,ch3,10);
104
105 SendDlgItemMessage(IDC_EDIT3,WM_SETTEXT,0,(LPARAM)ch3);
106 SendDlgItemMessage(IDC_EDIT3,EM_SETSEL,0,-1);
107 m_edit3.SetFocus();
108 } - 用覆盖窗口过程函数函数的方法实现按下回车键之后,焦点从一个控件到另一个控件
-
WNDPROC prevProc; LRESULT CALLBACK WinSunProc( 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)); //SetFocus(::GetWindow(hwnd,GW_HWNDNEXT)); SetFocus(::GetNextDlgTabItem(::GetParent(hwnd),hwnd,FALSE)); return 1; } else { return prevProc(hwnd,uMsg,wParam,lParam); } } BOOL CTestDlg::OnInitDialog() { CDialog::OnInitDialog(); // TODO: Add extra initialization here prevProc=(WNDPROC)SetWindowLong(GetDlgItem(IDC_EDIT1)->m_hWnd,GWL_WNDPROC, (LONG)WinSunProc); return TRUE; // return TRUE unless you set the focus to a control // EXCEPTION: OCX Property Pages should return FALSE }
这个没太看明白是怎么回事,先放到这儿吧! - 利用默认控件能接收回车键按下这个消息的特性实现焦点的转移
-
//GetFocus()->GetNextWindow()->SetFocus(); //GetFocus()->GetWindow(GW_HWNDNEXT)->SetFocus(); GetNextDlgTabItem(GetFocus())->SetFocus(); //CDialog::OnOK();
一定要把基类的OnOK函数注释掉,前两行的代码会导致错误。 - 通过一个按钮实现对话框的变大变小
代码
1 void CTestDlg::OnButton2()
2 {
3 // TODO: Add your control notification handler code here
4 CString str;
5 if(GetDlgItemText(IDC_BUTTON2, str), "变小" == str)
6 SetDlgItemText(IDC_BUTTON2, "变大");
7 else
8 SetDlgItemText(IDC_BUTTON2, "变小");
9
10 static CRect largeRec;
11 static CRect smallRec;
12
13 if(largeRec.IsRectNull()){
14 CRect rectSeparator;
15 this->GetWindowRect(&largeRec);
16 GetDlgItem(IDC_SEPARATOR)->GetWindowRect(&rectSeparator);
17
18 smallRec.left = largeRec.left;
19 smallRec.top = largeRec.top;
20 smallRec.right = rectSeparator.right;
21 smallRec.bottom = rectSeparator.bottom;
22 }
23
24 if(str == "变小")
25 SetWindowPos(NULL, 0, 0, smallRec.Width(), smallRec.Height(), SWP_NOMOVE | SWP_NOZORDER);
26 else
27 SetWindowPos(NULL, 0, 0, largeRec.Width(), largeRec.Height(), SWP_NOMOVE | SWP_NOZORDER);
28 }
这个地方的Z字序不是很懂哦。