孙鑫Lesson7 Dialog

本章节主要是对话框的创建与显示,同时在对话框中进行一些操作,比如添加、收缩、确定、取消等
MFC:

CObject:CmdTarget:Cwn:Cdialog

CDialog类是在屏幕上显示的对话框基类。对话框有两类:模态对话框和非模态对话框。模态对话框在应用继续进行之前必须关闭。非模态对话框允许用户执行另外的操作而不必取消或删除该对话框。

virtual INT_PTR DoModal( );
Call this member function to invoke the modal dialog box and return the dialog-box result when done.
An int value that specifies the value of the nResult parameter that was passed to the CDialog::EndDialog member function, which is used to close the dialog box. The return value is –1 if the function could not create the dialog box, or IDABORT if some other error occurred, in which case the Output window will contain error information from GetLastError.
返回值:
整数值,指定了传递给CDialog::EndDialog 的nResult参数值。该函数用于关闭对话框。如果函数不能创建对话框,则返回-1;如果出现其它错误,则返回IDABORT。
调用该成员函数使用模态对话框并返回对话框结果。当对话框处于活动状态时,该函数处理与用户的交互。这使得对话框是模态的,使用户在关闭对话框之前不能与其它窗口交互。如果用户单击了对话框中的按钮,如OK或Cancel,那么消息处理函数如OnOK或OnCancel被调用,从而关闭对话框。缺省的OnOK成员函数会对对话框数据进行有效性检验和更新,并关闭它得到结果IDOK。缺省OnCancel函数关闭对话框得到结果IDCANCEL,而不对对话框数据检验或更新,可以覆盖这些消息函数并改变它们的行为。注意 目前PreTransMessage被调用来处理模态对话框的消息。

代码段:

//视图中添加此响应函数,在菜单栏中点击对话框,然后创建并显示对话框

void CMyboleView::OnDialog()
{
// TODO: 在此添加命令处理程序代码
//CTestDlg dlg;
//dlg.DoModal();


CTestDlg *pDlg=new CTestDlg();
pDlg->Create(IDD_DIALOG1,this);
pDlg->ShowWindow(SW_SHOW);
}


//在testdlg中添加相应

IMPLEMENT_DYNAMIC(CTestDlg, CDialogEx)

//初始化三个文本编辑输入值为0
CTestDlg::CTestDlg(CWnd* pParent /*=NULL*/)
: CDialogEx(CTestDlg::IDD, pParent)
{
m_blsCreate=FALSE;
m_num1 = 0;
m_num2 = 0;
m_num3 = 0;
}

CTestDlg::~CTestDlg()
{
}

//数据交换,这个函数不能被直接调用,都是通过updateData调用
void CTestDlg::DoDataExchange(CDataExchange* pDX)
{
CDialogEx::DoDataExchange(pDX);
DDX_Text(pDX, IDC_EDIT1, m_num1);
DDV_MinMaxInt(pDX, m_num1, 0, 100);
DDX_Text(pDX, IDC_EDIT2, m_num2);
DDV_MinMaxInt(pDX, m_num2, 0, 100);
DDX_Text(pDX, IDC_EDIT3, m_num3);
DDV_MinMaxInt(pDX, m_num3, 0, 1000);
DDX_Control(pDX, IDC_EDIT1, m_edit1);
DDX_Control(pDX, IDC_EDIT2, m_edit2);
DDX_Control(pDX, IDC_EDIT3, m_edit3);
}

BEGIN_MESSAGE_MAP(CTestDlg, CDialogEx)
ON_BN_CLICKED(IDC_BTN_ADD, &CTestDlg::OnClickedBtnAdd)
ON_STN_CLICKED(IDC_NUMBER1, &CTestDlg::OnClickedNumber1)
ON_BN_CLICKED(IDC_BTN2, &CTestDlg::OnBnClickedBtn2)
ON_BN_CLICKED(IDOK, &CTestDlg::OnBnClickedOk)
END_MESSAGE_MAP()

// CTestDlg 消息处理程序

//点击add按钮时,响应把第一个编辑框与第二个编辑框中的数据相加的和输入第三个编辑框中
void CTestDlg::OnClickedBtnAdd()
{
// TODO: 在此添加控件通知处理程序代码
/* if(m_blsCreate==FALSE)
{
m_btn.Create(_T("XXX"),BS_DEFPUSHBUTTON | WS_VISIBLE | WS_CHILD,
CRect(0,0,100,100),this,123);
m_blsCreate=TRUE;
}
else
{
m_btn.DestroyWindow();
m_blsCreate=FALSE;
}
*/


/*if(!m_btn.m_hWnd)
{
m_btn.Create(_T("rlj"),BS_DEFPUSHBUTTON | WS_VISIBLE | WS_CHILD,
CRect(0,0,100,100),this,123);
}
else
{
m_btn.DestroyWindow();
}
*/

/*int num1,num2,num3;
wchar_t ch1[100],ch2[100],ch3[100];
GetDlgItem(IDC_EDIT1)->GetWindowTextW((LPTSTR)ch1,100);
GetDlgItem(IDC_EDIT2)->GetWindowTextW((LPTSTR)ch2,100);

num1=_wtoi(ch1);
num2=_wtoi(ch2);
num3=num1+num2;
//itoa(num3,ch3,100);
wsprintf(ch3,_T("%d"),num3);
GetDlgItem(IDC_EDIT3)->SetWindowTextW((LPTSTR)(ch3));*/

int num1,num2,num3;
wchar_t ch1[10],ch2[10],ch3[10];

GetDlgItemText(IDC_EDIT1,(LPTSTR)ch1,10);
GetDlgItemText(IDC_EDIT2,(LPTSTR)ch2,10);

num1=_wtoi(ch1);
num2=_wtoi(ch2);
num3=num1+num2;
wsprintf(ch3,_T("%d"),num3);

SetDlgItemText(IDC_EDIT3,(LPTSTR)ch3);

/*int num1,num2,num3;
num1=GetDlgItemInt(IDC_EDIT1);
num2=GetDlgItemInt(IDC_EDIT2);
num3=num1+num2;
SetDlgItemInt(IDC_EDIT3,num3);*/

/*UpdateData();
m_num3=m_num1+m_num2;
UpdateData(FALSE);*/

/*int num1,num2,num3;
wchar_t ch1[10],ch2[10],ch3[10];
m_edit1.GetWindowTextW((LPTSTR)ch1,10);
m_edit2.GetWindowTextW((LPTSTR)ch2,10);

num1=_wtoi(ch1);
num2=_wtoi(ch2);
num3=num1+num2;
sprintf(ch3,_T("%d"),num3);
m_edit3.SetWindowTextW((LPTSTR)ch3);*/

/*int num1,num2,num3;
wchar_t ch1[10],ch2[10],ch3[10];
//::SendMessageA(GetDlgItem(IDC_EDIT1)->m_hWnd,WM_GETTEXT,10,(LPARAM)ch1);
//::SendMessageA(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);
m_edit2.SendMessage(WM_GETTEXT,10,(LPARAM)ch2);

num1=_wtoi(ch1);
num2=_wtoi(ch2);
num3=num1+num2;
sprintf(ch3,_T("%d"),num3);
m_edit3.SendMessage(WM_SETTEXT,10,(LPARAM)ch3);*/

/*int num1,num2,num3;
wchar_t ch1[10],ch2[10],ch3[10];
SendDlgItemMessage(IDC_EDIT1,WM_GETTEXT,10,(LPARAM)_T(ch1));
SendDlgItemMessage(IDC_EDIT2,WM_GETTEXT,10,(LPARAM)_T(ch2));
num1=_wtoi(ch1);
num2=_wtoi(ch2);
num3=num1+num2;
sprintf(ch3,_T("%d"),num3);
SendDlgItemMessage(IDC_EDIT3,WM_GETTEXT,10,(LPARAM)_T(ch3));
SendDlgItemMessage(IDC_EDIT3,EM_SETSEL,0,-1);
m_edit3.GetFocus();*/
}

//双击文本框nummber1时的响应操作,改变文本框值
void CTestDlg::OnClickedNumber1()
{
// TODO: 在此添加控件通知处理程序代码
CString str;
if(GetDlgItem(IDC_NUMBER1)->GetWindowTextW(str),str=="Number1:")
{
GetDlgItem(IDC_NUMBER1)->SetWindowTextW(_T("数值1:"));
}
else
{
GetDlgItem(IDC_NUMBER1)->SetWindowTextW(_T("Number1:"));
}
}

//点击收缩按键时,窗口一部份收缩起来,按钮显示扩展。点击扩展时,窗口展开,按钮显示收缩
void CTestDlg::OnBnClickedBtn2()
{
// TODO: 在此添加控件通知处理程序代码
CString str;
GetDlgItemText(IDC_BTN2,str);
if(str=="收缩<<")
{
SetDlgItemText(IDC_BTN2,(LPTSTR)_T("扩展>>"));
}
else
{
SetDlgItemText(IDC_BTN2,(LPTSTR)_T("收缩<<"));
}

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);
}

}


//点击确定按钮时把焦点放在此按钮上
void CTestDlg::OnBnClickedOk()
{
// TODO: 在此添加控件通知处理程序代码
//GetDlgItem(IDC_EDIT1)->GetNextWindow()->SetFocus();
//GetFocus()->GetNextWindow()->SetFocus();
//GetFocus()->GetNextWindow(GW_HWNDNEXT)->SetFocus();
GetNextDlgTabItem(GetFocus())->SetFocus();

//CDialogEx::OnOK();
}

//过程调用函数
WNDPROC preProc;
LRESULT CALLBACK WinSunProc(
  __in  HWND hwnd,
  __in  UINT uMsg,
  __in  WPARAM wParam,
  __in  LPARAM lParam
)
{
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 preProc(hwnd,uMsg,wParam,lParam);
}
}


BOOL CTestDlg::OnInitDialog()
{
CDialogEx::OnInitDialog();

// TODO:  在此添加额外的初始化

preProc=(WNDPROC)SetWindowLong(GetDlgItem(IDC_EDIT1)->m_hWnd,GWL_WNDPROC,(LONG)WinSunProc);
return TRUE;  // return TRUE unless you set the focus to a control
// 异常: OCX 属性页应返回 FALSE
}






  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值