请问拥有一个窗口的句柄后如何是这个窗口位于屏幕的正中央?

flyingpig2003 2004-10-18 12:20:16
谢谢!
...全文
124 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
kugou123 2004-10-18
  • 打赏
  • 举报
回复
取得桌面大小,然后用MoveWindow改变窗口位置
mynamelj 2004-10-18
  • 打赏
  • 举报
回复
你首先要获得了窗口句柄,然后再用GetWindowRect去获得它的矩形尺寸!
竹君子 2004-10-18
  • 打赏
  • 举报
回复
void CenterWindow( CWnd* pAlternateOwner = NULL );
薛定谔之死猫 2004-10-18
  • 打赏
  • 举报
回复
类似代码
nWidth = (rChild.right - rChild.left);
nHeight = (rChild.bottom - rChild.top);

// calculate parent window center point
pCenter.x = rParent.left+((rParent.right
- rParent.left)/2);
pCenter.y = rParent.top+((rParent.bottom
- rParent.top)/2);

// calculate message box starting point
pStart.x = (pCenter.x - (nWidth/2));
pStart.y = (pCenter.y - (nHeight/2));

// adjust if message box is off desktop
if(pStart.x < 0) pStart.x = 0;
if(pStart.y < 0) pStart.y = 0;
if(pStart.x + nWidth > rDesktop.right)
pStart.x = rDesktop.right - nWidth;
if(pStart.y + nHeight > rDesktop.bottom)
pStart.y = rDesktop.bottom - nHeight;

// move message box
MoveWindow(hChildWnd,
pStart.x, pStart.y,
nWidth, nHeight,
FALSE);

具体的例子
http://www.codeguru.com/Cpp/W-P/win32/messagebox/article.php/c4541/
薛定谔之死猫 2004-10-18
  • 打赏
  • 举报
回复
得到桌面窗口句柄->得到桌面窗口Rect->得到该窗口Rect->计算后SetWindowPos
flyingpig2003 2004-10-18
  • 打赏
  • 举报
回复
各位,关键是我没法取得哪个窗口的rect,取出来的数据是错的!
Mr-Chen 2004-10-18
  • 打赏
  • 举报
回复
SetWindowPos
mynamelj 2004-10-18
  • 打赏
  • 举报
回复
算出来不就得了吗?
HWND hChild; //Child window handle.
HWND hParent; //Parent window handle.
CSize size;
CRect ChildRect;
CRect ParentRect;
GetWindowRect(hChild,&ChildRect);
GetWindowRect(hParent,&ParentRect);

size.cx = (ParentRect.right - ChildRect.right)/2;
size.cy = (ParentRect.bottom - ChildRect.bottom)/2;
Jimmy_Xia 2004-10-18
  • 打赏
  • 举报
回复
CenterWindow(GetDesktopWindow())
dropingleaf 2004-10-18
  • 打赏
  • 举报
回复
void CenterWindow( CWnd* pAlternateOwner = NULL );
这个是子窗体位于主窗体的中央吧
mynamelj 2004-10-18
  • 打赏
  • 举报
回复
void CenterWindow(
CWnd* pAlternateOwner = NULL
);
//这个函数也可以帮你实现
mynamelj 2004-10-18
  • 打赏
  • 举报
回复
创建一个非模态窗口:

CSize size;
CRect ChildRect;
CRect ParentRect;

CDialog* pDlg = new CDialog();
pDlg->Create(IDD_MYDIALOG,this); //从资源中上装入对话框

pDlg->GetWindowRect(&ChildRect);
GetWindowRect(&ParentRect);

size.cx = (ParentRect.right - ChildRect.right)/2;
size.cy = (ParentRect.bottom - ChildRect.bottom)/2;

pDlg->MoveWindow(size.cx,size.cx,ChildRect.right,ChildRect.bottom,TRUE);
PDlg->ShowWindow(SW_SHOWNORMAL);

16,472

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC相关问题讨论
社区管理员
  • 基础类社区
  • Web++
  • encoderlee
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

        VC/MFC社区版块或许是CSDN最“古老”的版块了,记忆之中,与CSDN的年龄几乎差不多。随着时间的推移,MFC技术渐渐的偏离了开发主流,若干年之后的今天,当我们面对着微软的这个经典之笔,内心充满着敬意,那些曾经的记忆,可以说代表着二十年前曾经的辉煌……
        向经典致敬,或许是老一代程序员内心里面难以释怀的感受。互联网大行其道的今天,我们期待着MFC技术能够恢复其曾经的辉煌,或许这个期待会永远成为一种“梦想”,或许一切皆有可能……
        我们希望这个版块可以很好的适配Web时代,期待更好的互联网技术能够使得MFC技术框架得以重现活力,……

试试用AI创作助手写篇文章吧