1.如果在SDI中要调用对话框
a.先插入一个对话框资源;
b.然后在ClassWizards中为其创建一个类。其目的是比较方便为添加按纽和消息响应函数。
c.然后实例化它。在实例化时,必须将其头文件包含进去。
2.创建非模态对话框,注意它不能是局部变量。当点击非模态对话框的OnOK按纽时,它并没有关闭,而是隐藏了。需要调用destroyWindow().
3.一个对象只能一个按纽。为什么?因为在Wincore.cpp的628行有代码 ASSERT(pWnd->m_hWnd == NULL); // only do once而创建后它的m_hWnd就不为0了。此处ASSERT的用法是如果括号里面不为真,则程序崩溃。
4.如何为静态文本框增加消息响应?首先将IDC_STATIC改名。同时还需要将Notify特性复选中。
5.完成加法功能。
a.GetDlgItem();
b.GetDlgItemText();
c.GetDlgItemInt();
d.将IDC_EDIT1关联CEDIT类型变量
e.将IDC_EDIT1关联int型变量。注意调用 UpdateData();
f. //::SendMessage(GetDlgItem(IDC_EDIT1)->m_hWnd,WM_GETTEXT,10,(LPARAM)ch1);
//::SendMessage(m_edit1.m_hWnd,WM_GETTEXT,10,(LPARAM)ch1);
//GetDlgItem(IDC_EDIT1)->SendMessage(WM_GETTEXT,10,(LPARAM)ch1);
m_edit1.SendMessage(WM_GETTEXT,10,(LPARAM)ch1);
g. SendDlgItemMessage(IDC_EDIT1,WM_GETTEXT,10,(LPARAM)ch1);
SendDlgItemMessage(IDC_EDIT2,WM_GETTEXT,10,(LPARAM)ch2);
6.点击按纽改变窗口尺寸
if(GetDlgItemText(IDC_BUTTON2,str),str=="收缩<<")
{
SetDlgItemText(IDC_BUTTON2,"扩展>>");
static CRect rectLarge;
static CRect rectSmall;
if(rectLarge.IsRectNull())
{
CRect rectSeparator;
GetWindowRect(&rectLarge);
GetDlgItem(IDC_SEPARATOR)->GetWindowRect(&rectSeparator); rectSmall.left=rectLarge.left;
rectSmall.top=rectLarge.top;
rectSmall.right=rectLarge.right;
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);
}
7.回车时将输入焦点移动到下一个控件
SetWindowLong()改变窗口的属性。
方法1:
改变控件的回调函数,注意IDC_EDIT1的MultiLine要复选上。
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));
CString str;
str.Format("%d",hwnd);
AfxMessageBox(str);//, UINT nType = MB_OK, UINT nIDHelp = 0 );
// AfxGetApp()->
SetFocus(::GetNextDlgTabItem(::GetParent(hwnd),hwnd,FALSE));
return 1;
}
else
{
return prevProc(hwnd,uMsg,wParam,lParam);
}
}
BOOL CTestDlg::OnInitDialog()
{
CDialog::OnInitDialog();
// TOD 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
}
方法2:
在OnOK响应函数中加入代码
//GetDlgItem(IDC_EDIT1)->GetNextWindow()->SetFocus();
//GetFocus()->GetNextWindow()->SetFocus();
//GetFocus()->GetWindow(GW_HWNDNEXT)->SetFocus();
GetNextDlgTabItem(GetFocus())->SetFocus();
显示一个模态对话框:
显示一个非模态对话框:
- void CMyDialogView::OnShowmenue()
- {
- // TODO: Add your command handler code here
-
- /*CTestDialog cdlg;
- cdlg.DoModal();*/
-
- CTestDialog *cdlg=new CTestDialog();//这里可以设置成成员变量,指针定义,堆栈跟程序一样的声明周期
- cdlg->Create(IDD_DIALOG1,NULL);
- cdlg->ShowWindow(SW_NORMAL);
-
-
- }
下面两端代码的效果是一样的:
- void CTestDialog::OnButton()
- {
- // TODO: Add your control notification handler code here
-
- if(!bIsCreate)
- {
- m_btn.Create("按钮",BS_PUSHBUTTON|WS_VISIBLE|WS_CHILD,CRect(0,0,100,80),this,123);
- bIsCreate=TRUE;
- }
- else
- {
- m_btn.DestroyWindow();
- bIsCreate=FALSE;
- }
-
- /*if(!m_btn.m_hWnd)
- {
-
- m_btn.Create("按钮",BS_PUSHBUTTON|WS_VISIBLE|WS_CHILD,CRect(0,0,100,80),this,123);
- }
- else
- {
- m_btn.DestroyWindow();
- }*/
- }
几种访问控件的方式:
拉控件:
利用
调整位置
改变了静态文本的ID,才能添加事件
修改了Notify才能在事件中,改变文本的内容
方法一:
- void CTestDialog::OnNumber1()
- {
- // TODO: Add your control notification handler code here
- CString str;
- GetDlgItem(ID_NUMBER1)->GetWindowText(str);
- if(str=="number1:")
- {
- GetDlgItem(ID_NUMBER1)->SetWindowText("数值1:");
- }
- else
- {
- GetDlgItem(ID_NUMBER1)->SetWindowText("number1:");
- }
- }
方法二:
- int num1,num2,num3;
- char ch1[8],ch2[8],ch3[9];
- GetDlgItem(IDC_EDIT1)->GetWindowText(ch1,8);
- GetDlgItem(IDC_EDIT2)->GetWindowText(ch2,8);
-
- num1=atoi(ch1);//将字符串转化为整数
- num2=atoi(ch2);
- num3=num1+num2;
-
- itoa(num3,ch3,10);//将整数转化为字符串
-
- GetDlgItem(IDC_EDIT3)->SetWindowText(ch3);
方法三:
- TCHAR ch1[8],ch2[8],ch3[9];
- GetDlgItemText(IDC_EDIT1,ch1,8);
- GetDlgItemText(IDC_EDIT2,ch2,8);
-
- int num1=atoi(ch1);
- int num2=atoi(ch2);
- int num3=num1+num2;
-
- itoa(num3,ch3,10);
-
- SetDlgItemText(IDC_EDIT3,ch3);
方法四:
数值关联:
- UpdateData();//默认参数是TRUE,不直接调用DodataExchange函数
- m_num3=m_num1+m_num2;
- UpdateData(FALSE);
方法五:
控制关联:
- TCHAR ch1[9],ch2[9],ch3[9];
- int num1,num2,num3;
- m_edit1.GetWindowText(ch1,9);
- m_edit2.GetWindowText(ch2,9);
-
- num1=atoi(ch1);
- num2=atoi(ch2);
-
- num3=num1+num2;
-
- itoa(num3,ch3,10);
-
- m_edit3.SetWindowText(ch3);
方法六:
- int num1=GetDlgItemInt(IDC_EDIT1);
- int num2=GetDlgItemInt(IDC_EDIT2);
- int num3=num1+num2;
-
- SetDlgItemInt(IDC_EDIT3,num3);
方法七:
- /*TCHAR ch1[10],ch2[10],ch3[10];
- ::SendMessage(GetDlgItem(IDC_EDIT1)->m_hWnd,WM_GETTEXT,10,(LPARAM)ch1);
- ::SendMessage(GetDlgItem(IDC_EDIT2)->m_hWnd,WM_GETTEXT,10,(LPARAM)ch2);
-
- int num1=atoi(ch1);
- int num2=atoi(ch2);
- int num3=num1+num2;
-
- itoa(num3,ch3,10);
-
- ::SendMessage(GetDlgItem(IDC_EDIT3)->m_hWnd,WM_SETTEXT,0,(LPARAM)ch3);*/
-
-
- /*TCHAR ch1[10],ch2[10],ch3[10];
- GetDlgItem(IDC_EDIT1)->SendMessage(WM_GETTEXT,10,(LPARAM)ch1);
- GetDlgItem(IDC_EDIT2)->SendMessage(WM_GETTEXT,10,(LPARAM)ch2);
-
- int num1=atoi(ch1);
- int num2=atoi(ch2);
- int num3=num1+num2;
-
- itoa(num3,ch3,10);
-
- SendDlgItemMessage(IDC_EDIT3,WM_SETTEXT,0,(LPARAM)ch3);
- SendDlgItemMessage(IDC_EDIT3,EM_SETSEL,0,2);
- GetDlgItem(IDC_EDIT3)->SetFocus();*/
-
-
- TCHAR ch1[10],ch2[10],ch3[10];
- SendDlgItemMessage(IDC_EDIT1,WM_GETTEXT,10,(LPARAM)ch1);
- SendDlgItemMessage(IDC_EDIT2,WM_GETTEXT,10,(LPARAM)ch2);
-
- int num1=atoi(ch1);
- int num2=atoi(ch2);
- int num3=num1+num2;
-
- itoa(num3,ch3,10);
-
- SendDlgItemMessage(IDC_EDIT3,WM_SETTEXT,0,(LPARAM)ch3);
- SendDlgItemMessage(IDC_EDIT3,EM_SETSEL,0,2);
- GetDlgItem(IDC_EDIT3)->SetFocus();
伸缩效果:
拉入一个picture:
- void CTestDialog::OnShensuo()
- {
- // TODO: Add your control notification handler code here
- CString cst;
- if(GetDlgItemText(ID_SHENSUO,cst),cst=="缩小<<")
- {
- SetDlgItemText(ID_SHENSUO,"扩展>>");
- }
- else
- {
- SetDlgItemText(ID_SHENSUO,"缩小<<");
- }
-
- static CRect crectLarge,crectSmall;
-
- if(crectLarge.IsRectNull())
- {
- GetWindowRect(&crectLarge);
- CRect ctemp;
-
- GetDlgItem(IDC_SEPARATOR)->GetWindowRect(&ctemp);
-
- crectSmall.left=crectLarge.left;
- crectSmall.top=crectLarge.top;
- crectSmall.right=crectLarge.right;
- crectSmall.bottom=ctemp.bottom;
-
- }
-
- if(cst=="缩小<<")
- {
- SetWindowPos(NULL,0,0,crectSmall.Width(),crectSmall.Height(),SWP_NOZORDER|SWP_NOMOVE);
- }
- else
- {
-
- SetWindowPos(NULL,0,0,crectLarge.Width(),crectLarge.Height(),SWP_NOZORDER|SWP_NOMOVE);
- }
-
- }
回车响应SetFocus()只能一次 multiline
- WNDPROC prevProc;
- LRESULT CALLBACK DialogProc(
- 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 CTestDialog::OnInitDialog()
- {
- CDialog::OnInitDialog();
-
-
- // TODO: Add extra initialization here
- prevProc=(WNDPROC)SetWindowLong(GetDlgItem(IDC_EDIT1)->m_hWnd,GWL_WNDPROC,(LONG)DialogProc);
-
-
- return TRUE; // return TRUE unless you set the focus to a control
- // EXCEPTION: OCX Property Pages should return FALSE
- }
- void CTestDlg::OnOK()
- {
- // TODO: Add extra validation here
- //GetDlgItem(IDC_EDIT1)->GetNextWindow()->SetFocus();
- //GetFocus()->GetNextWindow()->SetFocus();
- //GetFocus()->GetWindow(GW_HWNDNEXT)->SetFocus();
- GetNextDlgTabItem(GetFocus())->SetFocus();
- // CDialog::OnOK();
- }