对话框资源的各种属性方法的使用
Dialog
1、 对话框资源(文本)的获取。
//TODO:对话框控件访问七种方式
//(1)GetDlgItem()->Get(Set)WindowText()
//(2)GetDlgItemText()/SetDlgItemText()
//(3)GetDlgItemInt()/SetDlgItemInt()
//(4)将控件和整型变量相关联
//(5)将控件和控件变量相关联
//(6)SendMessage()
//(7)SendDlgItemMessage()
//TODO:对话框控件访问七种方式
//(1)GetDlgItem()->Get(Set)WindowText()获取文本对话框的指针,获取/设置窗体的文字
/*GetDlgItem():Retrieves a pointer to the specified control or child window in a dialog box or other window.
GetWindowText():Copies the CWnd caption title (if it has one) into the buffer pointed to by lpszStringBuf or into the destination string rString.
*/
/*int num1,num2,num3;
char ch1[9],ch2[9],ch3[9];
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);*/
//(2)GetDlgItemText()/SetDlgItemText()
/*CWnd::GetDlgItemText:Call this member function to retrieve the title or text associated with a control in a dialog box.
CWnd::SetDlgItemText:Sets the caption or text of a control owned by a window or dialog box.*/
/*int num1,num2,num3;
char ch1[9],ch2[9],ch3[9];
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);*/
//(3)GetDlgItemInt()/SetDlgItemInt()
/*CWnd::GetDlgItemInt :Retrieves the text of the control identified by nID.
CWnd::SetDlgItemIntSets :the text of a given control in a dialog box to the string representation of a specified integer value.*/
/*int num1,num2,num3;
char ch1[9],ch2[9],ch3[9];
num1=GetDlgItemInt(IDC_EDIT1);
num2=GetDlgItemInt(IDC_EDIT2);
num3=num1+num2;
SetDlgItemInt(IDC_EDIT3,num3);*/
//(4)将控件和整型变量相关联
/*方法:先为Edit控件添加Value变量,通过调用BOOL UpdateData(BOOL bSaveAndValidate = TRUE );
to initialize data in a dialog box, or to retrieve and validate dialog data.(为对话框进行初始化或者重新得到或者验证数据)*/
/*UpdateData(); //initialized (FALSE) or data is being retrieved (TRUE).
m_num3=m_num1+m_num2;
UpdateData(FALSE);*/
//(5)将控件和控件变量相关联
/*方法:先为Edit控件添加Control变量,因此可以更方便获得控件的指针*/
/*int num1,num2,num3;
char ch1[9],ch2[9],ch3[9];
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);*/
//(6)SendMessage()
/*int num1,num2,num3;
char ch1[9],ch2[9],ch3[9];
//::SendMessage(GetDlgItem(IDC_EDIT1)->m_hWnd,WM_GETTEXT,10,(LPARAM)ch1); //方法1
//::SendMessage(m_Edit1.m_hWnd,WM_GETTEXT,10,(LPARAM)ch1); //方法2
//GetDlgItem(IDC_EDIT1)->SendMessage(WM_GETTEXT,10,(LPARAM)ch1); //方法3
m_Edit1.SendMessage(WM_GETTEXT,10,(LPARAM)ch1); //方法4
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);*/
//(7)SendDlgItemMessage()
/*SendDlgItemMessage() :Sends a message to a control.
*/
int num1,num2,num3;
char ch1[9],ch2[9],ch3[9];
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,0,(LPARAM)ch3);
SendDlgItemMessage(IDC_EDIT3,EM_SETSEL,0,-1);
2、 SetFocus属性(2方法)
利用TAB此处利用IDOK的一些属性,在初始化的时候将焦点设置到IDC_EDIT1上,之后利用回车,由于IDOK是默认项目,因此,可以利用这里OnOK消息来处理设置焦点的问题。
void CTestDlg::OnOK()
{
// TODO: 在此添加专用代码和/或调用基类
//GetDlgItem(IDC_EDIT1)->GetNextWindow()->SetFocus();
//GetFocus()->GetNextWindow()->SetFocus();
//GetFocus()->GetWindow(GW_HWNDNEXT)->SetFocus();
GetNextDlgTabItem(GetFocus())->SetFocus();
//CDialog::OnOK();
}
另外可以用窗口过程函数来写这个方法,同时需要修改IDC_EDIT1/ IDC_EDIT2/ IDC_EDIT3的属性中Multiline为TRUE这样的窗口可以接受多行消息,才可以正确响应这个窗口过程函数。
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()
{
//在VC7中需要通过编辑重写类来添加这个项目
CDialog::OnInitDialog();
// TODO: 在此添加额外的初始化
prevProc=(WNDPROC)SetWindowLong(GetDlgItem(IDC_EDIT1)->m_hWnd,GWL_WNDPROC,
(LONG)WinSunProc); //定义一个prevProc来获得返回值WNDPROC,添加一个消息处理函数
return TRUE; // return TRUE unless you set the focus to a control
// 异常: OCX 属性页应返回 FALSE
}
3、 设计伸缩菜单。首先添加一个按钮用于处理这个消息。在需要伸缩的位置用图片控件拉出一条线用做分界线。
可以将其ID号修改为IDC_SEPARATOR,SUNKEN设置为TRUE,线条将下陷。
设计思路:
1、 改变按钮的文字,以配合收缩和扩展的变化
2、 将搜索后的尺寸设定为以左上角为矩形左上角,右下角为分隔线(实质是一个矩形)的右下角的坐标。
3、 使用SetWindowPos()来设定大小。
代码如下:
CString str;
if(GetDlgItemText(IDC_BTN_SEPARATOR,str),str=="收缩<<")
{
SetDlgItemText(IDC_BTN_SEPARATOR,"扩展>>");
}
else
{
SetDlgItemText(IDC_BTN_SEPARATOR,"收缩<<");
}
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);
posted on 2008-07-18 20:12 volnet(可以叫我大V) 阅读(1242) 评论(0) 编辑 收藏 举报