您好,欢迎访问三七文档
MFC(对话框,孙鑫C++第七讲笔记整理)分类:MFC2012-08-1423:25393人阅读评论(0)收藏举报1.如果在SDI中要调用对话框a.先插入一个对话框资源;b.然后在ClassWizards中为其创建一个类。其目的是比较方便为添加按纽和消息响应函数。c.然后实例化它。在实例化时,必须将其头文件包含进去。2.创建非模态对话框,注意它不能是局部变量。当点击非模态对话框的OnOK按纽时,它并没有关闭,而是隐藏了。需要调用destroyWindow().3.一个对象只能一个按纽。为什么?因为在Wincore.cpp的628行有代码ASSERT(pWnd-m_hWnd==NULL);//onlydoonce而创建后它的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,扩展);staticCRectrectLarge;staticCRectrectSmall;if(rectLarge.IsRectNull()){CRectrectSeparator;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要复选上。WNDPROCprevProc;LRESULTCALLBACKWinSunProc(HWNDhwnd,//handletowindowUINTuMsg,//messageidentifierWPARAMwParam,//firstmessageparameterLPARAMlParam//secondmessageparameter){if(uMsg==WM_CHAR&&wParam==0x0d){//::SetFocus(::GetNextWindow(hwnd,GW_HWNDNEXT));//SetFocus(::GetWindow(hwnd,GW_HWNDNEXT));CStringstr;str.Format(%d,hwnd);AfxMessageBox(str);//,UINTnType=MB_OK,UINTnIDHelp=0);//AfxGetApp()-SetFocus(::GetNextDlgTabItem(::GetParent(hwnd),hwnd,FALSE));return1;}else{returnprevProc(hwnd,uMsg,wParam,lParam);}}BOOLCTestDlg::OnInitDialog(){CDialog::OnInitDialog();//TODAddextrainitializationhereprevProc=(WNDPROC)SetWindowLong(GetDlgItem(IDC_EDIT1)-m_hWnd,GWL_WNDPROC,(LONG)WinSunProc);//设置回调函数returnTRUE;//returnTRUEunlessyousetthefocustoacontrol//EXCEPTION:OCXPropertyPagesshouldreturnFALSE}方法2:在OnOK响应函数中加入代码//GetDlgItem(IDC_EDIT1)-GetNextWindow()-SetFocus();//GetFocus()-GetNextWindow()-SetFocus();//GetFocus()-GetWindow(GW_HWNDNEXT)-SetFocus();GetNextDlgTabItem(GetFocus())-SetFocus();显示一个模态对话框:显示一个非模态对话框:[cpp]viewplaincopyvoidCMyDialogView::OnShowmenue(){//TODO:Addyourcommandhandlercodehere/*CTestDialogcdlg;cdlg.DoModal();*/CTestDialog*cdlg=newCTestDialog();//这里可以设置成成员变量,指针定义,堆栈跟程序一样的声明周期cdlg-Create(IDD_DIALOG1,NULL);cdlg-ShowWindow(SW_NORMAL);}下面两端代码的效果是一样的:[cpp]viewplaincopyvoidCTestDialog::OnButton(){//TODO:Addyourcontrolnotificationhandlercodehereif(!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才能在事件中,改变文本的内容方法一:[cpp]viewplaincopyvoidCTestDialog::OnNumber1(){//TODO:AddyourcontrolnotificationhandlercodehereCStringstr;GetDlgItem(ID_NUMBER1)-GetWindowText(str);if(str==number1:){GetDlgItem(ID_NUMBER1)-SetWindowText(数值1:);}else{GetDlgItem(ID_NUMBER1)-SetWindowText(number1:);}}方法二:[cpp]viewplaincopyintnum1,num2,num3;charch1[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);方法三:[cpp]viewplaincopyTCHARch1[8],ch2[8],ch3[9];GetDlgItemText(IDC_EDIT1,ch1,8);GetDlgItemText(IDC_EDIT2,ch2,8);intnum1=atoi(ch1);intnum2=atoi(ch2);intnum3=num1+num2;itoa(num3,ch3,10);SetDlgItemText(IDC_EDIT3,ch3);方法四:数值关联:[cpp]viewplaincopyUpdateData();//默认参数是TRUE,不直接调用DodataExchange函数m_num3=m_num1+m_num2;UpdateData(FALSE);方法五:控制关联:[cpp]viewplaincopyTCHARch1[9],ch2[9],ch3[9];intnum1,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);方法六:[cpp]viewplaincopyintnum1=GetDlgItemInt(IDC_EDIT1);intnum2=GetDlgItemInt(IDC_EDIT2);intnum3=num1+num2;SetDlgItemInt(IDC_EDIT3,num3);方法七:[cpp]viewplaincopy/*TCHARch1[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);intnum1=atoi(ch1);intnum2=atoi(ch2);intnum3=num1+num2;itoa(num3,ch3,10);::SendMessage(GetDlgItem(IDC
本文标题:MFC第七讲
链接地址:https://www.777doc.com/doc-2882676 .html