vc-study-7
来源:互联网 发布:通用网络加速 编辑:程序博客网 时间:2024/06/13 06:31
对话框的编程(1)
CDialog
两种类型的对话框
模态对话框 非模态对话框
========================
创建新的类
CTextDog
对话框的测试
1:创建对话框
创建一个非模态对话框
Create
CTextDlg dlg;//需要创建一个全局变量
dlg.Create(IDD_DIALOG1,this);
dlg.ShowWindow(SW_SHOW);
CTextDlg *pDlg=new CTextDlg();
pDlg->Create(IDD_DIALOG,this);
pDlg->ShowWindow(SW_SHOW);
非模态的对话框 需要针对OnOK() 函数
对话框的创建
动态增加 和删除 按钮!!!!!
动态增加一个按钮
也可以将BOOL值定义为一个静态的成员变量static
CButton m_btn;
BOOL m_bIsCreate 初始化为false
void CTextDlg::OnBtnAdd()
{
if(m_bIsCreate==FALSE)
{
m_btn.Create("黑刃",BS_DEFPUSHBUTTON | WS_VISIBLE | WS_CHILD,
CRect(0,0,100,100),this,1111);
m_bIsCreat=TRUE ;
}
else
{
m_btn.DestroyWindow();
m_bIsCreat=FALSE;
}
}
====================================
另一种方法
MFC 方法
if(!m_btn.m_hWnd)
{
m_btn.Create("黑刃",BS_DEFPUSHBUTTON | WS_VISIBLE | WS_CHILD,
CRect(0,0,100,100),this,1111);
}
else
{
m_btn.DestroyWindow();
}
==================================
改变静态文本内容
静态编辑对话框 注意要改一下 ID
静态文本框 不能接受 命令点击消息
Notify
CString str;
if(GetDlgItem(IDC_NUMBER1)->GetWindowText(str),str=="Number1:")
{
GetDlgItem(IDC_NUMBER1)->SetWindowText("数值1:");
}
else
{
GetDlgItem(IDC_NUMBER1)->SetWindowText("Number1:");
}
====================================
加法器 『 对话框控件访问的7种方式』
-------------------------------------------
int num1,num2,num3;
char ch1[10],ch2[10],ch3[10];
GetDlgItem(IDC_EDIT1)->GetWindowText(ch1,10);
GetDlgItem(IDC_EDIT2)->GetWindowText(ch2,10);
num1=atoi(ch1);
num2=atoi(ch2);
num3=num1+num2;
itoa(num3,ch3,10);
GetDlgItem(IDC_EDIT3)->SetWindowText(ch3);
-------------------------------------------
GetDlgItemText
int num1,num2,num3;
char ch1[10],ch2[10],ch3[10];
GetDlgItemText(IDC_EDIT1,ch1,10);
GetDlgItemText(IDC_EDIT2,ch2,10);
num1=atoi(ch1);
num2=atoi(ch2);
num3=num1+num2;
itoa(num3,ch3,10);
SetDlgItemText(IDC_EDIT3,ch3);
-----------------------------------------------
GetDlgItemInt
int num1,num2,num3;
num1=GetDlgItemInt(IDC_EDIT1);
num2=GetDlgItemInt(IDC_EDIT2);
num3=num1+num2;
SetDlgItemInt(IDC_EDIT3,num3);
------------------------------------------------
分别关联成员变量
定义各个添加的组件的成员变量
【可以对定义的变量进行取值范围的限制】
然后 直接调用
UpdateData();
num3=num1+num2; //没有获取到数值
UpdateData(FALSE);
------------------------------------------------
增加 三个 control 控件变量
int num1,num2,num3;
char ch1[10],ch2[10],ch3[10];
m_edit1.GetWindowText(ch1,10);
m_edit2.GetWindowText(ch2,10);
num1=atoi(ch1);
num2=atoi(ch2);
num3=num1+num2;
itoa(num3,ch3,10);
m_edit3.SetWindowText(ch3);
------------------------------------------------
===================================================
int num1,num2,num3;
char ch1[10],ch2[10],ch3[10];
//::SendMessage(GetDlgItem(IDC_EDIT1)->m_hWnd,WM_GETTEXT,10,(LOPARAM)ch1);
//::SendMessagem_dit1.m_hWnd,WM_GETTEXT,10,(LPARAM)ch1);
GetDlgItem(IDC_EDIT1)->SendMessage(WM_GETTEXT,10,(LPARAM)ch1);
m_edit1.SendMessage(WM_GETTEXT,10,(LPARAM)ch1);
m_edit2.SendMessage(WM_GETTEXT,10,(LPARAM)ch2);
num1=atoi(ch1);
num2=atoi(ch2);
num3=num1+num2;
itoa(num3,ch3,10);
m_edit3.SendMessage(WM_SETTEXT,0,(LPARAM)ch3);
====================================================
int num1,num2,num3;
char ch1[10],ch2[10],ch3[10];
SendDlgItemMessage(IDC_EDIT1,WM_GETTEXT,10,(LPARAM)ch1);
SendDlgItemMessage(IDC_EDIT2,WM_GETTEXT,10,(LPARAM)ch2);
num1=atoi(ch1);
num2=atoi(ch2);
num3=num1+num2;
itoa(num3,ch3,10);
SendDlgItemMessage(IDC_EDIT3,WM_SETTEXT,10,(LPARAM)ch3);
SendDlgItemMessage(IDC_EDIT3,EM_SETSEL,1,3);
m_edit3.SetFocus();
***********************************
***********************************
If the start is 0 and the end is –1,
all the text in the edit control is selected.
If the start is –1, any current selection is
deselected.
***********************************
***********************************
====================================================
对话框的收缩与扩展的功能
增加button 收缩<<
添加图像控件为分隔符号: 选择下陷的状态
button2()
{
CString str;
if(GetDlgItemText(IDC_BUTTON2,str),str=="收缩<<")
{
SetDlgItemText(IDC_BUTTON2,"拓展>>");
}
else
{
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;
recrSmall.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);
}
}
-------------------------------------------
OnOk 的设置 回车键的运用
对OnOK 函数的操作
截获 对 Enter 键 的响应
void CTestDlg::OnOK()
{
// TODO: Add extra validation here
//CDialog::OnOK();
}
-------------------------------------------
SetWindowLong
GetNextWindow
GetWindow
GetNextDlgTabItem
修改窗口过程函数
【编辑框设置为接受 『多行』】
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
}
==============================================
缺省按钮的响应函数
『去掉 多行响应』
GetNextWindow;
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();
}
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
设置缺省按扭 可以设置 按钮的属性为缺省
onok 按钮的 ID 为 IDOK 响应回车键
- vc-study-7
- VC study
- VC++ study websites
- vc-study-1
- vc-study-2
- vc-study-3
- vc-study-4
- vc-study-5
- vc-study-6
- vc-study-8
- My study of VC++ ( Paint refered )
- C/C++/VC MFC----char & int---study
- awk study(7)
- Vim Tutorial Study(7)
- Study JQuery《7、插件》
- mysql study 7
- study-7用户管理
- study
- 算法的力量(转)
- vc-study-3
- vc-study-4
- vc-study-5
- vc-study-6
- vc-study-7
- 用c++函数结构计算时间差
- 经典查询
- vc-study-8
- 类的调用
- 数据结构——顺序表
- 数据结构——单链表
- 数据结构——单链表
- Ant安装并调试gwt程序流程