文章目录
- 窗口类继承关系
- 窗口CWnd类介绍
-
- 核心成员变量
- 由窗口对象获取窗口句柄
- 由窗口句柄获取窗口指针
- 窗口的操作
-
- CWnd::Create
- CWnd::GetWindowText
- CWnd::CloseWindow
- CWnd::DestroyWindow
- CWnd::CenterWindow
- CWnd::FindWindow
- CWnd::FindWindowEx
- CWnd::FlashWindow
- CWnd::GetDesktopWindow
- CWnd::GetStyle
- CWnd::GetExStyle
- CWnd::ModifyStyle
- CWnd::ModifyStyleEx
- CWnd::IsChild
- CWnd::IsIconic
- CWnd::EnableWindow
- CWnd::IsWindowEnabled
- CWnd::IsWindowVisible
- CWnd::IsZoomed
- CWnd::ScreenToClient
- CWnd::MoveWindow
- CWnd::SetWindowPos
- CWnd::GetTopWindow
- CWnd::GetFocus
- CWnd::GetIcon
- CWnd::GetActiveWindow
- 遍历窗口
- 获取对话框数据
窗口类继承关系
CtestMFCDlg <= CDialogEx <= CDialog <= CWnd <=CCmdTarget <= CObject
绝大多数类是从CObject继承的
绝大多数类是从CObject继承的
窗口CWnd类介绍
核心成员变量
m_hWnd:指示附加到此 CWnd 的 HWND。是public属性的成员
CString str;
str.Format(L"当前窗口句柄:%#X", m_hWnd);
SetWindowText(str);
由窗口对象获取窗口句柄
CWnd edit;
str.Format(L"当前窗口句柄:%#X", edit.m_hWnd);//还没开始创建,为0
str.Format(L"当前窗口句柄:%#X", edit.GetSafeHwnd());//以安全的方式获得窗口句柄
SetWindowText(str);
由窗口句柄获取窗口指针
CWnd* pWnd = CWnd::FromHandle(m_hWnd);//通过窗口句柄获得窗口指针
CWnd* pWnd2 = GetDlgItem(IDC_STATIC);//通过ID获得窗口指针
窗口的操作
CWnd::Create
创建并初始化与 CWnd 对象关联的子窗口。
int CMFC窗口类cwndDlg::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
if (CDialogEx::OnCreate(lpCreateStruct) == -1)
return -1;
//创建子窗口
m_wnd.Create(L"Edit", L"", WS_BORDER | WS_CHILD | WS_VISIBLE | ES_CENTER, CRect(10, 10, 100, 100), this, IDC_EDIT, NULL);
//WS开头是通用窗口风格,ES开头是编辑框风格
//创建按钮
m_button.Create(L"Button", L"我的按钮", WS_CHILD | WS_VISIBLE | BS_FLAT, CRect(10, 100, 100, 150), this, IDC_BUTTON, NULL);
//BS开头是按钮风格
m_wnd1.CreateEx(WS_EX_ACCEPTFILES | WS_EX_RIGHT, L"Edit", L"", WS_CHILD | WS_VISIBLE | ES_CENTER, CRect(10, 150, 100, 200), this, IDC_EDIT, NULL);
//比Create多了一个Ex,可以创建扩展风格窗口,WS_EX开头是扩展窗口风格
return 0;
}
CWnd::GetWindowText
返回窗口文本或标题(如果有)。
CString titleStr;
GetWindowText(titleStr);
MessageBox(titleStr);
int n = GetWindowTextLength();
titleStr.Format(L"窗口标题长度:%d", n);
MessageBox(titleStr);
CWnd::CloseWindow
最小化窗口。
void CMFC窗口类cwndDlg::OnBnClickedButton1()
{
//CloseWindow();//隐藏窗口
CWnd* pWnd = GetDlgItem(IDC_BUTTON1);
if (pWnd)
pWnd->CloseWindow();//隐藏当前按钮
}
CWnd::DestroyWindow
销毁附加的 Windows 窗口。
void CMFC窗口类cwndDlg::OnBnClickedButton2()
{
CWnd* pWnd = GetDlgItem(IDC_BUTTON2);
if (pWnd)
pWnd->DestroyWindow();//销毁当前按钮
}
CWnd::CenterWindow
使窗口相对于其父级居中。
void CMFC窗口类cwndDlg::OnBnClickedButton3