您的位置:首页 > 其它

吕鑫MFC学习系列八

2016-02-13 20:08 330 查看
还是继续学习CWnd函数,增加一些常用的函数使用方法。

上一篇实现到第七个按钮的设置风格这儿。

接下来的添加框架和函数介绍如下面介绍顺序:

第一步,获取和设置窗口信息:

a)GetWindowText和SetWindowText:获取和设置窗口的标题文字。

b)GetStyle和ModifyStyle:获取和设置窗口的基础风格。

c)GetExStyle和ModifyStyleEx:获取和设置窗口的扩展风格

第二步,计时器:

a)SetTimer:设置计时器

b)KillTimer:清除计时器

第三步,窗口操作:

a)ShowWindow:改变窗口显示状态,包括最大化最小化和隐藏等等。

b)EnableWindow:激活或者禁用窗口

c)MoveWindow:移动窗口(父窗口基于坐标系是屏幕坐标系,如果是子窗口坐标系是父窗口的客户区)

d)CenterWindow:居中窗口

e)SetWindowPos:可以同时对窗口的x,y,z轴方向进行修改,常用于前端显示功能的开发。

(如果是子窗口沿着z轴方向调整层叠顺序,如果是父窗口主要用于调整前端显示)

f)BringWindowToTop:不但可以再xy轴方向移动窗口,而且在z轴方向也可以移动

g)SetForegroundWindow:将任务栏内的一个主窗口推到前台

(对主窗口BringWindowToTop也能起到同样的功能)

h)SetActiveWindow:在同一进程内所有窗口中的一个窗口设置为激活状态。

i)FlashWindow:

第四步,窗口状态:

a)IsWindowVisible:判断窗口是否可见或被隐藏。

b)IsIconic:判断是否最小化

c)IsZoomed:判断是否最大化

d)IsWindowEnabled:判断窗口是激活或者禁用状态。

e)GetWindowRect:获取以屏幕坐标系为基础的窗口矩形区域。

f)GetClientRect:获取以对象关联的以其客户区为基础的客户区矩形区域。(left和top必然是0)

g)GetTopWindow:获取当前父窗口内所有子窗口中最底(top)的子窗口。

h)GetForegroundWindow:获取任务栏内目前正在前台的一个主窗口。

i)GetActiveWindow:在同一进程内所有窗口中处于激活状态的窗口。

第五步,窗口关系:

a)GetDlgItem:根据ID获取一个子窗口的对象地址(包含窗口句柄)

b)GetDlgItemText和SetDlgItemText:根据ID获取或设置一个子窗口标题文字

c)GetDlgItemInt和SetDlgItemInt:根据ID获取或设置一个子窗口内显示的数字

d)SetDlgCtrlID和GetDlgCtrlID:根据窗口对象内的句柄获取或设置该窗口的ID。

e)GetParent和IsChild:求出父窗口和判断一个窗口是否为子窗口

f)GetWindow:

g)GetNextWindow:

第六步,刷新函数:

Invalidate和InvalidateRect:

UpdateWindow:

RedrawWindow:

ScreenToClient和ClientToScreen:

最后看一下一些等价关系:

1、IsChild与GetParent:

2、IsIconic与GetStyle()&WS_MINIMIZE:

3、IsZoomed与GetStyle()&WS_MAXIMIZE:

4、IsWindowVisible与GetStyle()&WS_VISIBLE:

5、SetDlgItemText与GetDlgItem(..)->SetWindowText

完成结果:



下面是添加的按钮代码:

void CTestWinDlg::OnBnClickedButton7()
{
DWORD dwExStyle = m_edit.GetExStyle();
m_edit.ModifyStyleEx(0,WS_EX_DLGMODALFRAME);
CRect rect;
m_edit.GetWindowRect(rect);
ScreenToClient(rect);
rect.InflateRect(3, 3);
m_edit.MoveWindow(rect);
//m_edit.MoveWindow(10, 300, 400, 40);
Invalidate();
}
void CTestWinDlg::OnBnClickedButton8()
{
SetTimer(1, 500, NULL);
SetTimer(123, 16, NULL);
}
void CTestWinDlg::OnTimer(UINT_PTR nID)
{
if (nID == 1)
{
CString str;
GetWindowText(str);
if (str == "Timer....")
str = "Setting...";
else
str = "Timer....";
SetWindowText(str);
}
if (nID == 123)
{
CRect rect;
m_cret.GetWindowRect(rect);
ScreenToClient(rect);
rect.OffsetRect(2, 0);
CRect rt;
GetClientRect(rt);
if (rect.right >= rt.right)
{
KillTimer(123);
KillTimer(1);
//	rect.OffsetRect(-rect.left, 0);
}
m_cret.MoveWindow(rect);
}
CDialogEx::OnTimer(nID);
}
void CTestWinDlg::OnBnClickedButton9()
{
CWnd *pCan = GetDlgItem(IDCANCEL);
if (pCan->IsWindowVisible())
pCan->ShowWindow(SW_HIDE);
else
pCan->ShowWindow(SW_SHOW);
//ShowWindow(SW_MINIMIZE);
}
void CTestWinDlg::OnBnClickedButton10()
{
CWnd *pCan = GetDlgItem(IDCANCEL);
pCan->EnableWindow(!pCan->IsWindowEnabled());
/*
if (pCan->IsWindowEnabled())
pCan->EnableWindow(FALSE);
else
pCan->EnableWindow(TRUE);*/
}
void CTestWinDlg::OnBnClickedButton11()
{
CWnd *pWnd = FindWindow(L"Notepad", NULL);
//if (pWnd&&pWnd->GetStyle()&WS_MINIMIZE)
if (pWnd&&pWnd->IsIconic())
pWnd->ShowWindow(SW_MAXIMIZE);
}
void CTestWinDlg::OnBnClickedButton12()
{	//if (GetStyle()&WS_MAXIMIZE)
if (IsZoomed())
this->SetWindowText(_T("是最大化"));
else
this->SetWindowText(_T("非最大化"));
}
void CTestWinDlg::OnBnClickedButton13()
{
CWnd *pCan = GetDlgItem(IDCANCEL);
pCan->CenterWindow();
}
void CTestWinDlg::OnBnClickedButton14()
{
CRect rect;
m_cret.GetWindowRect(rect);
ScreenToClient(rect);
rect.OffsetRect(8, 0);
//m_cret.SetWindowPos(&wndTop, rect.left, rect.top, rect.Width(), rect.Height(), 0);
//m_cret.SetWindowPos(&wndTop,rect.left,rect.top,0,0,SWP_NOSIZE|SWP_NOMOVE);
//Invalidate();
if (GetExStyle()&WS_EX_TOPMOST)
SetWindowPos(&wndNoTopMost, 0, 0, 0, 0, SWP_NOSIZE | SWP_NOMOVE);
else
SetWindowPos(&wndTopMost, 0, 0, 0, 0, SWP_NOSIZE | SWP_NOMOVE);

}
void CTestWinDlg::OnBnClickedButton15()
{
m_list.BringWindowToTop();
Invalidate();
}
void CTestWinDlg::OnBnClickedButton16()
{
CWnd *pWnd = GetTopWindow();
CString str;
pWnd->GetWindowText(str);
SetWindowText(str);
}
void CTestWinDlg::OnBnClickedButton17()
{
CWnd *pWnd = FindWindow(L"Notepad", NULL);
if (pWnd)
//	pWnd->SetForegroundWindow();
pWnd->BringWindowToTop();
}
void CTestWinDlg::OnBnClickedButton18()
{
CWnd *pWnd = FindWindow(L"Notepad", NULL);
if (pWnd)
pWnd->FlashWindow(TRUE);
}
void CTestWinDlg::OnBnClickedButton19()
{
CWnd *pWnd=m_list.GetParent();
if (pWnd == this)
AfxMessageBox(_T("父窗口是对话框"));
}
void CTestWinDlg::OnBnClickedButton20()
{
if (this->IsChild(&m_list))
{
AfxMessageBox(_T("列表控件是给对话框的子窗口"));
}
}
void CTestWinDlg::OnBnClickedButton21()
{
CWnd * pWnd=GetWindow(GW_CHILD);
CString str;
while (pWnd)
{
pWnd->GetWindowText(str);
pWnd = pWnd->GetWindow(GW_HWNDNEXT);
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: