VC++MFC 对话框和常用控件
- 格式:pdf
- 大小:11.97 MB
- 文档页数:75
一般控件可用/不可用EnableWindow(TRUE);EnableWindow(FALSE);1、Static Text------------静态控件--类CStatic取值/赋值(变量类型为Control)m_lbl.GetWindowText(string);m_lbl.SetWindowText(string);2、Edit Box---------------编辑控件--类CEdit取值/赋值m_txt.GetWindowText(string);m_txt.SetWindowText(string);3、Check Box------------复选控件--类CButton(1)设置选中/未选中m_chk.SetCheck(BST_CHECKED);m_chk.SetCheck(BST_UNCHECKED);(2)判断是否选中int nCur = m_chk.GetCheck();nCur取值为BST_CHECKED/BST_UNCHECKED。
4、Radio Box------------单选控件--类CButton(1)默认选中第一项m_radio.SetCheck(BST_CHECKED);(2)选中组中任一项CWnd::CheckRadioButtonSelects (adds a check mark to) a given radio button in a group and clears (removes a check mark from) all other radio buttons in the group.void CheckRadioButton(int nIDFirstButton, int nIDLastButton, int nIDCheckButton); ParametersnIDFirstButtonSpecifies the integer identifier of the first radio button in the group.nIDLastButtonSpecifies the integer identifier of the last radio button in the group.nIDCheckButtonSpecifies the integer identifier of the radio button to be checked.(3)判断哪一项被选中CWnd::GetCheckedRadioButtonRetrieves the ID of the currently checked radio button in the specified group.int GetCheckedRadioButton(int nIDFirstButton, int nIDLastButton);ParametersnIDFirstButtonSpecifies the integer identifier of the first radio button in the group.nIDLastButtonSpecifies the integer identifier of the last radio button in the group.Return ValueID of the checked radio button, or 0 if none is selected.(4)控件变量类型为Value时,可通过给int型变量赋值0、1、2...选中第1、2、3...个选项。
VC++MFC 常用控件使用方法4.1 Button按钮窗口(控件)在MFC中使用CButton表示,CButton包含了三种样式的按钮,Push Button,Check Box,Radio Box。
所以在利用CButton对象生成按钮窗口时需要指明按钮的风格。
创建按钮:BOOL CButton::Create( LPCTSTR lpszCaption, DWORD dwStyle, const RECT& rect, CWnd* pParentWnd, UINT nID );其中lpszCaption是按钮上显示的文字,dwStyle为按钮风格,除了Windows风格可以使用外(如WS_CHILD|WS_VISUBLE|WS_BORDER)还有按钮专用的一些风格。
BS_AUTOCHECKBOX 检查框,按钮的状态会自动改变 Same as a check box, except thata check mark appears in the check box when the user selects the box; the checkmark disappears the next time the user selects the box.BS_AUTORADIOBUTTON 圆形选择按钮,按钮的状态会自动改变 Same as a radio button, except that when the user selects it, the button automatically highlights itself andremoves the selection from any other radio buttons with the same style in the same group.BS_AUTO3STATE 允许按钮有三种状态即:选中,未选中,未定 Same as a three-state check box, except that the box changes its state when the user selects it.BS_CHECKBOX 检查框 Creates a small square that has text displayed to its right (unless this style is combined with the BS_LEFTTEXT style).BS_DEFPUSHBUTTON 默认普通按钮 Creates a button that has a heavy black border.The user can select this button by pressing the ENTER key. This style enables theuser to quickly select the most likely option (the default option).BS_LEFTTEXT 左对齐文字 When combined with a radio-button or check-box style, the text appears on the left side of the radio button or check box.BS_OWNERDRAW 自绘按钮 Creates an owner-drawn button. The framework calls the DrawItem member function when a visual aspect of the button has changed. Thisstyle must be set when using the CBitmapButton class.BS_PUSHBUTTON 普通按钮 Creates a pushbutton that posts a WM_COMMAND message to the owner window when the user selects the button.BS_RADIOBUTTON 圆形选择按钮 Creates a small circle that has text displayed to its right (unless this style is combined with the BS_LEFTTEXT style). Radio buttons are usually used in groups of related but mutually exclusive choices.BS_3STATE 允许按钮有三种状态即:选中,未选中,未定 Same as a check box, except that the box can be dimmed as well as checked. The dimmed state typically is used toshow that a check box has been disabled.rect为窗口所占据的矩形区域,pParentWnd为父窗口指针,nID为该窗口的ID值。
实验八MFC中的对话框及控件(二)实验目的掌握常用的控件使用方法以及基于对话框的应用程序的开发实验内容1、编辑框的使用2、选择按钮的使用3、下拉列表的使用4、按钮的使用5、基于对话框的应用程序开发一、编辑框的使用[实验步骤]1、新建一个MFC单文档项目;2、在资源选项卡中插入一个自己的对话框3、新建一个对话框模板4、在对话框中添加2个静态文本控件和2个编辑框控件。
注意这2个编辑框控件的ID5、通过Classwizzard添加数据成员。
在Classwizzard弹出的对话框中选择新建你这个对话框的类,取名叫“Cmydlg”,当然你也可以取其他名字选择成员变量选项卡,然后双击某个编辑框的ID,设置对应数据成员名字及类型。
6、在你的工程的view类的头部通过包含对话框类的头文件#include “mydlg.h”包含你所建的对话框类。
注意:如果你改了对话框类的名字,看看相应的项目里新增的那个类的头文件名7、在View类中增加2个变量CString myname;int myage;8、通过Classwizzard增加一个鼠标左键消息响应函数,并在函数中增加如下代码:void CT estView::OnLButtonDown(UINT nFlags, CPoint point){// TODO: Add your message handler code here and/or call defaultCmydlg dlg;if(dlg.DoModal()==IDOK){myname=dlg.m_name;myage=dlg.m_age;Inavalidate();}CView::OnLButtonDown(nFlags, point);}9、在OnDraw函数里增加如下显示内容的代码CString str;str.Format("我的姓名是%s",myname);pDC->TextOut(10,10,str);str.Format("我的年龄是%d",myage);pDC->TextOut(10,30,str);10、编译运行程序,在工作视图区点鼠标左键二、选择按钮的使用[实验步骤]1、新建一个MFC单文档项目;2、在资源选项卡中插入一个自己的对话框3、新建一个对话框模板4、在对话框中添加3个单选框控件。
第五章对话框和常用控件对话框是Windows应用程序中最重要的用户界面元素之一,是与用户交互的重要手段,在程序运行过程中,对话框可用于扑捉用户的输入信息或数据。
对话框是一个特殊类型的窗口,任何对窗口进行的操作(如:移动、最大化、最小化等)都可在对话框中实施,一般来说,在对话框中通过各种控件(如:按钮、编辑框、列表框、组合框等)来和用户进行交互。
控件是在系统内部定义的用于和用户交互的基本单元。
一、对话框的使用Visual C++提供的对话框编辑器能“可视”地进行设计、编辑,并可用ClassWizard为对话框从CDialog基类中派生一个类,MFC的CDialog类封装了用于对话框的显示、关闭等操作的许多功能函数,例如:DoModal函数用来显示模式对话框并返回用户操作的结果。
1、模式对话框(为186附加举例页)所谓模式对话框是指,当对话被弹出时,用户必须在对话框中进行相应的操作,在退出对话框之前,对话框所在的应用程序不能继续执行。
平常我们所见到的对话框大多是模式对话框。
例:模式对话框(通过菜单命令弹出)1)建一个单文档(SDI)应用程序2)创建对话框模板Insert→Resource→选中Dialog→单击New拖过一个静态文本,鼠标对准它,按右键点properties改标题为“新建模式对话框”。
3)鼠标右键对准对话框的任何位置单击,选择properties选项,设置ID为IDD_MYDIALOG 4)给对话框创建类双击新建对话框的任何位置,单击OK,写类名为“CMyDlg”,保证”CDialog”作为该类的基类。
5)创建菜单命令:a)打开资源编辑器的菜单项Menub)双击IDR_MAINFRAMEc)双击右边空白菜单,点开pop_up(让它是空白),在名字处写”弹出对话框(&A)”,ID 处写ID_PUPDIALOG6)将菜单命令连接到主框架程序中,完成ID_PUPDIALOG的消息映射:View→ClassWizard→保证Class name里是CMainFrame,在ObjectIDs里找到ID_PUPDIALOG点黑→Messages里(右边)点COMMAND建立主框架对象方法并加代码:void CMainFrame::OnPupdialog(){ CMyDlg MyDlg;MyDlg.DoModal(); //DoModal()是CDialog类成员函数,通过调用该//函数将显示对话框。
VC++6.0MFC常用控件一般控件可用/不可用EnableWindow(TRUE);EnableWindow(FALSE);1、Static Text------------静态控件--类CStatic取值/赋值(变量类型为Control)m_lbl.GetWindowT ext(string);m_lbl.SetWindowT ext(string);2、Edit Box---------------编辑控件--类CEdit取值/赋值m_txt.GetWindowT ext(string);m_txt.SetWindowT ext(string);3、Check Box------------复选控件--类CButton(1)设置选中/未选中m_chk.SetCheck(BST_CHECKED);m_chk.SetCheck(BST_UNCHECKED);(2)判断是否选中intnCur = m_chk.GetCheck();nCur取值为BST_CHECKED/BST_UNCHECKED。
4、Radio Box------------单选控件--类CButton(1)默认选中第一项m_radio.SetCheck(BST_CHECKED);(2)选中组中任一项CWnd::CheckRadioButtonSelects (adds a check mark to) a given radio button in a group and clears (removes a check mark from) all other radio buttons in the group.void CheckRadioButton(int nIDFirstButton, int nIDLastButton, int nIDCheckButton); ParametersnIDFirstButtonSpecifies the integer identifier of the first radio button in the group.nIDLastButtonSpecifies the integer identifier of the last radio button in the group.nIDCheckButtonSpecifies the integer identifier of the radio button to be checked.(3)判断哪一项被选中CWnd::GetCheckedRadioButtonRetrieves the ID of the currently checked radio button in the specified group.int GetCheckedRadioButton(int nIDFirstButton, int nIDLastButton);ParametersnIDFirstButtonSpecifies the integer identifier of the first radio button in the group.nIDLastButtonSpecifies the integer identifier of the last radio button in the group.Return ValueID of the checked radio button, or 0 if none is selected.(4)控件变量类型为Value时,可通过给int型变量赋值0、1、2...选中第1、2、3...个选项。
1。
mfc单文档结构2。
添加左击鼠标,按下键盘事件3。
消息:窗口消息,控件的通知消息,命令消息第5章对话框和常用控件5.1 对话框的使用5.2 控件的创建和使用方法5.4 通用对话框和消息对话框5.1对话框的使用5.1.1模式和无模式对话框模式对话框指当对话框被弹出,用户必须在对话框中作出相应的操作,在退出对话框之前,对话框所在的应用程序不能继续执行。
无模式对话框指当对话框被弹出后,一直保留在屏幕上,可继续在对话框所在的应用程序中进行其它操作;需要使用对话框时,单击对话框所在的区域即可激活。
在创建和退出对话框窗口时的方式不同:创建时,模式对话框由系统自动分配内存空间,对话框退出时,对话框对象自动删除。
无模式对话框需要来指定内存,退出时还需删除对话框对象。
退出时,两种对话框所使用的终止函数不一样。
模式对话框通过调用CDialog:: EndDialog来终止,无模式对话框则是调用CWnd::DestroyWindow来终止。
函数CDiaolog::OnOK和CDiaolog::OnCancel是调用EndDialog的,因此无模式对话框必须用DestroyWindow来重载OnOK 和OnCancel两个函数。
需要正确删除表示对话框的对象。
对模式对话框,在创建函数返回后即可删除对象。
无模式对话框不是同步的,在创建函数调用后立即返回,因而不知道何时删除对象,但可以通过重载CWnd::PostNcDestroy函数并执行清除操作。
5.1.2使用对话框编辑器打开对话框编辑器将项目工作区窗口切换到ResourceView页面,双击Dialog目录下任意一个对话框ID。
或者,选择“Insert” “Resource”菜单命令(或按快捷键Ctrl+R),选择Dialog项,单击New。
在对话框中添加和编辑控件(1)控件添加的方法在对话框中添加一个控件的方法有下列几种:在控件工具栏中单击某控件,鼠标箭头在对话框内变成“十”字形状;在对话框指定位置单击鼠标左键,再拖动选择框可改变控件的大小和位置。
实验八MFC中的对话框及控件(二)实验目的掌握常用的控件使用方法以及基于对话框的应用程序的开发实验内容1、编辑框的使用2、选择按钮的使用3、下拉列表的使用4、按钮的使用5、基于对话框的应用程序开发一、编辑框的使用[实验步骤]1、新建一个MFC单文档项目;2、在资源选项卡中插入一个自己的对话框3、新建一个对话框模板4、在对话框中添加2个静态文本控件和2个编辑框控件。
注意这2个编辑框控件的ID5、通过Classwizzard添加数据成员。
在Classwizzard弹出的对话框中选择新建你这个对话框的类,取名叫“Cmydlg”,当然你也可以取其他名字选择成员变量选项卡,然后双击某个编辑框的ID,设置对应数据成员名字及类型。
6、在你的工程的view类的头部通过包含对话框类的头文件#include “mydlg.h”包含你所建的对话框类。
注意:如果你改了对话框类的名字,看看相应的项目里新增的那个类的头文件名7、在View类中增加2个变量CString myname;int myage;8、通过Classwizzard增加一个鼠标左键消息响应函数,并在函数中增加如下代码:void CT estView::OnLButtonDown(UINT nFlags, CPoint point){// TODO: Add your message handler code here and/or call defaultCmydlg dlg;if(dlg.DoModal()==IDOK){myname=dlg.m_name;myage=dlg.m_age;Inavalidate();}CView::OnLButtonDown(nFlags, point);}9、在OnDraw函数里增加如下显示内容的代码CString str;str.Format("我的姓名是%s",myname);pDC->TextOut(10,10,str);str.Format("我的年龄是%d",myage);pDC->TextOut(10,30,str);10、编译运行程序,在工作视图区点鼠标左键二、选择按钮的使用[实验步骤]1、新建一个MFC单文档项目;2、在资源选项卡中插入一个自己的对话框3、新建一个对话框模板4、在对话框中添加3个单选框控件。