MFC::窗口
https://learn.microsoft.com/zh-cn/cpp/?view=msvc-170
1.窗口
1.01 获得当前窗口的句柄
//获得当前窗口的句柄
HWND hWnd = GetSafeHwnd();
//通过HWND获得CWnd指针
CWnd* pWnd = CWnd::FromHandle(hWnd);
CString strText = _T("");
strText.Format("pWnd=0x%X\n this=0x%X\n", pWnd, this);
AfxMessageBox(strText);
1.02 获得应用程序指针
//获得应用程序指针
CDemo01App* pApp = (CDemo01App*)AfxGetApp();
//获得主窗口指针
CWnd* pMainWnd = pApp->m_pMainWnd;
CString strText = _T("");
strText.Format("pMainWnd=0x%X\nthis=0x%X\n", pMainWnd, this);
AfxMessageBox(strText);
1.03 窗口最大最小化
//最大化窗口
SendMessage(WM_SYSCOMMAND, SC_MAXIMIZE, 0);
//最小化窗口
SendMessage(WM_SYSCOMMAND, SC_MINIMIZE, 0);
//恢复窗口
SendMessage(WM_SYSCOMMAND, SC_RESTORE, 0);
//关闭窗口
SendMessage(WM_CLOSE, 0, 0);
1.04 窗口大小和位置
//设置窗口的大小和位置
SetWindowPos(NULL, 0, 0, 320, 200, SWP_NOZORDER);
//设置窗口的大小和位置
MoveWindow(0, 200, 200, 320);
//居中显示窗口
CenterWindow();
//设置窗口的层次
SetWindowPos(&wndTopMost, 0, 0, 0, 0, SWP_NOSIZE | SWP_NOMOVE);
1.05 窗口图标和风格
//加载图标
HICON hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
//设置图标
SetIcon(hIcon, FALSE);
//获得窗口标题
GetWindowText(strText);
//设置窗口标题
SetWindowText(strText);
//删除标题栏风格
ModifyStyle(WS_CAPTION, 0, SWP_FRAMECHANGED);
//添加标题栏风格
ModifyStyle(0, WS_CAPTION, SWP_FRAMECHANGED);
1.06 获取窗口
// 移动动鼠标获,取鼠标所在的窗口
void CDemoDlg::OnMouseMove(UINT nFlags, CPoint point)
{
//获得指定点的窗口
CWnd* pWnd = WindowFromPoint(point);
if(pWnd != NULL)
{
if (IsChild(pWnd))
{
CString strText = _T("");
pWnd->GetWindowText(strText);
SetWindowText(strText);
}
}
CDialog::OnMouseMove(nFlags, point);
}
//消息截获
PreTranslateMessage BOOL CDemoDlg::PreTranslateMessage(MSG* pMsg)
{
if(pMsg->message == WM_MOUSEMOVE)
{
CPoint point(LOWORD(pMsg->lParam), HIWORD(pMsg->lParam));
//客户区坐标转换为屏幕坐标
::ClientToScreen(pMsg->hwnd, &point);
OnMouseMove(0, point);
}
return CDialog::PreTranslateMessage(pMsg);
}
1.07 多边形
CRect rect;
GetClientRect(rect);
//创建矩形区域CRgn rgn;
rgn.CreateRectRgn(rect.left, rect.top, rect.right, rect.bottom);
//创建椭圆形区域
rgn.CreateEllipticRgn(0, 0, rect.Width(), rect.Height());
//创建圆矩形区域
rgn.CreateRoundRectRgn(0, 0, rect.Width(), rect.Height(), rect.Width()/2, rect.Height()/2);
//创建多边形区域
CRgn rgn;
CPoint point[6];
point[0].x = 0;
point[0].y = rect.Height() / 2;
point[1].x = rect.Width() / 3;
point[1].y = 0;
point[2].x = 2* rect.Width() / 3;
point[2].y = 0;
point[3].x = rect.Width();
point[3].y = rect.Height() / 2;
point[4].x = 2* rect.Width() / 3;
point[4].y = rect.Height();
point[5].x = rect.Width() / 3;
point[5].y = rect.Height();
rgn.CreatePolygonRgn(point, 6, ALTERNATE);
//设置窗口的区域
SetWindowRgn((HRGN)rgn, TRUE);
1.08 透明窗口
//窗口透明
//添加WS_EX_LAYERED(0x80000)扩展风格
ModifyStyleEx(0, 0x80000);
//加载User32.DLL动态链接库
HMODULE hModule = LoadLibrary("User32.DLL");
if(hModule != NULL)
{
typedef BOOL(WINAPI *FUNC)(HWND, COLORREF, BYTE, DWORD);
//获得SetLayeredWindowAttributes函数指针
FUNC func = (FUNC)GetProcAddress(hModule, "SetLayeredWindowAttributes");
if(func != NULL)
{
func(GetSafeHwnd(), 0, 128, 2);
}
FreeLibrary(hModule);
}
1.09 窗口闪烁
//窗口从一种状态闪烁到另一种状态
FlashWindow(TRUE);
//窗口返回原始状态
FlashWindow(FALSE);
// 图片窗口
BOOL CDemoDlg::OnEraseBkgnd(CDC* pDC)
{
CRect rect;
GetWindowRect(&rect);
CBitmap bmp;
bmp.LoadBitmap(IDB_BITMAP);
CDC dc;
dc.CreateCompatibleDC(pDC);
dc.SelectObject(&bmp);
pDC->BitBlt(0, 0, rect.Width(), rect.Height(), &dc, 0, 0, SRCCOPY);
return TRUE;
//return CDialog::OnEraseBkgnd(pDC);
}
1.10 窗口任务栏
//获得任务栏窗口
CWnd* pWnd = CWnd::FindWindow(_T("Shell_TrayWnd"), NULL);
//发送ID为0x1F5(Win + M)的WM_HOTKEY消息
pWnd->SendMessage(WM_HOTKEY, 0x1F5);
//获得窗口大小CRect rect;
pWnd->GetClientRect(rect);
//隐藏窗口
if(pWnd->IsWindowVisible())
{
pWnd->ShowWindow(SW_HIDE);
}
//显示窗口
if(!pWnd->IsWindowVisible())
{
pWnd->ShowWindow(SW_SHOW);
}
1.10 枚举桌面所有顶层窗口
// 枚举桌面所有顶层窗口
void CDemoDlg::OnTest1()
{
CListCtrl* pList = (CListCtrl*)GetDlgItem(IDC_LIST);
pList->DeleteAllItems();
pList->SetRedraw(FALSE);
//获得桌面窗口
CWnd* pDesktopWnd = CWnd::GetDesktopWindow();
//获得第一个子窗口
CWnd* pWnd = pDesktopWnd->GetWindow(GW_CHILD);
while(pWnd != NULL)
{
intnItem = pList->GetItemCount();
//获得窗口类名CString strClassName = _T("");
::GetClassName(pWnd->GetSafeHwnd(), strClassName.GetBuffer(256), 256);
strClassName.ReleaseBuffer();
pList->InsertItem(nItem, strClassName);
//获得窗口标题CString strWindowText= _T("");
::GetWindowText(pWnd->GetSafeHwnd(), strWindowText.GetBuffer(256), 256);
strWindowText.ReleaseBuffer();
pList->SetItemText(nItem, 1, strWindowText);
//继续下一个子窗口 pWnd = pWnd->GetWindow(GW_HWNDNEXT);
}
pList->SetRedraw(TRUE);
}
void CDemoDlg::OnTest2()
{
CListCtrl* pList = (CListCtrl*)GetDlgItem(IDC_LIST);
pList->DeleteAllItems();
pList->SetRedraw(FALSE);
//枚举窗口
::EnumWindows(EnumWndProc, (LPARAM)pList);
pList->SetRedraw(TRUE);
}
BOOL CALLBACK EnumWndProc(HWND hwnd,LPARAM lParam)
{
if(hwnd == NULL)
{
return FALSE;
}
CListCtrl* pList = (CListCtrl*)lParam;
intnItem = pList->GetItemCount();
//获得窗口类名CString strClassName = _T("");
::GetClassName(hwnd, strClassName.GetBuffer(256), 256);
strClassName.ReleaseBuffer();
pList->InsertItem(nItem, strClassName);
//获得窗口标题CString strWindowText= _T("");
::GetWindowText(hwnd, strWindowText.GetBuffer(256), 256);
strWindowText.ReleaseBuffer();
pList->SetItemText(nItem, 1, strWindowText);
return TRUE;
}