1:cwnd封在了所有和窗口相关的类,他有一个全局变量,保存了类窗口的一个句柄m_hWnd;
cdc类封装了所有和绘图相关的操作;他也有m_hDC句柄;
2:当前所操作的类的一个对象指针就是this,要想获得父类窗口的指针GetParent(),返回CWnd*指针
3:
第七课
1
void CMyboleView::OnDialog()
{
// TODO: Add your command handler code here
CTestDlg dlg;
dlg.DoModal();
/* CTestDlg pDlg=new CTestDlg();
pDlg.Create(IDD_DIALOG1,this);
pDlg.ShowWindow(SW_SHOW);*/ <span style="background-color: rgb(255, 255, 0);">//为什么非模态对话框显示不出来,因为pdlg是局部变量,因为是非模态,要出这个函数,此时局部变量自动销毁。模态对话框不出这个函数就//没事</span>
}
注意:非模态对话,要重载onok按钮
解决方法①把ctestdlg定义成该类的成员变量,②或者定义ctestdlg指针,此时在堆上分派内存,只有在程序结束时才一起结束
void CMyboleView::OnDialog()
{
// TODO: Add your command handler code here
CTestDlg dlg;
dlg.DoModal();
/* CTestDlg *pDlg=new CTestDlg();
pDlg->Create(IDD_DIALOG1,this);
pDlg->ShowWindow(SW_SHOW);*/
}
2
void CTestDlg::OnBtnAdd()
{
// TODO: Add your control notification handler code here
/* static BOOL bIsCreate=FALSE; //如果不是static时,每次进入这个函数时,都会初始化为false,要不想让他每次都初始化,就可以定义为static
// if(m_bIsCreate==FALSE)
if(bIsCreate==FALSE)
{
m_btn.Create("维新",BS_DEFPUSHBUTTON | WS_VISIBLE | WS_CHILD, //动态创建按钮(按钮也是窗口,CButton类)的方法,创建之后m_btn就和该按钮绑定了
CRect(0,0,100,100),this,123); //再点击就会出现内存分配错误
//m_bIsCreate=TRUE;
bIsCreate=TRUE;
}
else
{
m_btn.DestroyWindow();
//m_bIsCreate=FALSE;
bIsCreate=FALSE;
}*/
}
if(!m_btn.m_hWnd) //任何窗口都有m_hwnd成员变量,当窗口对象跟窗口相关联时,此时该句柄就有值了
{
m_btn.Create("维新",BS_DEFPUSHBUTTON | WS_VISIBLE | WS_CHILD,
CRect(0,0,100,100),this,123);
}
else
{
m_btn.DestroyWindow();
}*/
3通过id获取一个控件的指针,和获取控件上的文本
void CTestDlg::OnNumber1()
{
// TODO: Add your control notification handler code here
CString str;
if(<span style="background-color: rgb(255, 255, 0);">GetDlgItem(IDC_NUMBER1)</span>-><span style="background-color: rgb(255, 255, 0);">GetWindowText(str)</span>,str=="Number1:")
{
GetDlgItem(IDC_NUMBER1)->SetWindowText("数值1:");
}
else
{
GetDlgItem(IDC_NUMBER1)->SetWindowText("Number1:");
}
}
4:控件关联一个变量,操作变量的值,就是操作控件对应的值
5 SendMessage用法
//::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);
m_edit2.SendMessage(WM_GETTEXT,10,(LPARAM)ch2);
6 SendDlgItemMessage给子控件发送消息
SendDlgItemMessage(IDC_EDIT1,WM_GETTEXT,10,(LPARAM)ch1);
SendDlgItemMessage(IDC_EDIT2,WM_GETTEXT,10,(LPARAM)ch2);
SendDlgItemMessage(IDC_EDIT3,WM_SETTEXT,0,(LPARAM)ch3);
SendDlgItemMessage(IDC_EDIT3,EM_SETSEL,0,-1);
7:SetFocus设置焦点位置
8设置对窗口(对话框)的大小、位置、z序,还有他的文本内容
void CTestDlg::OnButton2()
{
// TODO: Add your control notification handler code here
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;
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);
}
}
9:重载窗口的过程的调用,每个窗口都有一个窗口过程
BOOL CTestDlg::OnInitDialog()
{
CDialog::OnInitDialog();
// TODO: Add extra initialization here
prevProc=(WNDPROC)<span style="background-color: rgb(255, 255, 0);">SetWindowLong</span>(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
}
10:SetWindowLong有很多用途
11: