资源ID(如文本框控件ID:IDC_textbox)、句柄(如窗口句柄HWND)、对象指针(如编辑框类指针CEdit* edit1)的概念、使用场景和相互转换:
![b07aefe4d45ba495078a23b82c66b809.png](https://i-blog.csdnimg.cn/blog_migrate/29ae2c86b1276229ea32cfd2ca64740b.jpeg)
资源的数据保存到内存后,需要通过一个物理地址或虚拟地址被访问到,这个地址的外在表现形式可以是ID、对象指针、或作为特殊结构体指针的句柄。
例1:
void Sys::OnButton48() {CString str;str = "控件句柄与指针:";str += "1 pWnd->GetSafeHwnd();";CWnd * pWnd = GetDlgItem(IDC_textbox);if (pWnd){HWND hWnd = pWnd->GetSafeHwnd();rdc4.Attach(hWnd); //CRichEditCtrl rdc4;rdc4.SetWindowText(str);}rdc4.Detach();str+="2 CWnd *p = CWnd::FromHandle(hWnd2);";HWND hWnd2=::GetDlgItem(m_hWnd, IDC_textbox);CWnd *p = CWnd::FromHandle(hWnd2);p->SetWindowText(str);}
上述按钮单击事件在富编辑框中输入:
控件句柄与指针:1 pWnd->GetSafeHwnd();2 CWnd *p = CWnd::FromHandle(hWnd2);
例2:
void Sys::OnButton47Attach() {CString str;str = "通过函数建立控件窗口和变量的关联:";str += "1 rdc1.SubclassDlgItem(IDC_textbox,this);";rdc1.SubclassDlgItem(IDC_textbox,this); //CRichEditCtrl rdc1;rdc1.SetWindowText(str);rdc1.UnsubclassWindow();str += "2 rdc2.SubclassWimdow(hrdc);";HWND hrdc;GetDlgItem(IDC_textbox,&hrdc);rdc2.SubclassWindow(hrdc);rdc2.SetWindowText(str);rdc2.UnsubclassWindow();str += "3 rdc2.Attach(hrdc);";HWND hrdc2;GetDlgItem(IDC_textbox,&hrdc2);rdc3.Attach(hrdc);rdc3.SetWindowText(str);rdc3.Detach();}
上述按钮单击事件在富编辑框中输入:
通过函数建立控件窗口和变量的关联:1 rdc1.SubclassDlgItem(IDC_textbox,this);2 rdc2.SubclassWimdow(hrdc);3 rdc2.Attach(hrdc);
-End-