问题虽小,难度很大?MFC 打开系统对话框可以使其居中吗?

时间:2021-08-19 17:34:18
背景: vs2005/2008  MFC 应用程序开发 。C++ 
开发一个应用程序,在有些功能上发现通过dos 命令调用windows 自带的设置窗口较好。
所以就在应用程序窗口上增加了按钮,通过执行下面的语句 命令打开系统窗口。
	ShellExecute(NULL, NULL, L"desk.cpl", NULL, NULL, SW_SHOW);
//打开windows 自带的系统设置窗口。cmd-> dest.cpl 打开可查看


问题:
发现1、打开的窗口不居中?  我想将其居中且在我的父窗口没关闭的情况下,其始终在桌面的
最前面?

各位高手: 能指导一二吗?

感激。

7 个解决方案

#1


坐等结帖......

#2


貌似Widnows自己打开的时候都不是固定位置的,观望中。。。

#3


MoveWindow
The MoveWindow function changes the position and dimensions of the specified window. For a top-level window, the position and dimensions are relative to the upper-left corner of the screen. For a child window, they are relative to the upper-left corner of the parent window's client area. 

BOOL MoveWindow(
  HWND hWnd,      // handle to window
  int X,          // horizontal position
  int Y,          // vertical position
  int nWidth,     // width
  int nHeight,    // height
  BOOL bRepaint   // repaint flag
);
 
Parameters
hWnd 
Handle to the window. 

Specifies the new position of the left side of the window. 

Specifies the new position of the top of the window. 
nWidth 
Specifies the new width of the window. 
nHeight 
Specifies the new height of the window. 
bRepaint 
Specifies whether the window is to be repainted. If this parameter is TRUE, the window receives aWM_PAINT message. If the parameter is FALSE, no repainting of any kind occurs. This applies to the client area, the nonclient area (including the title bar and scroll bars), and any part of the parent window uncovered as a result of moving a child window. If this parameter is FALSE, the application must explicitly invalidate or redraw any parts of the window and parent window that need redrawing. 
Return Values
If the function succeeds, the return value is nonzero.

If the function fails, the return value is zero. To get extended error information, callGetLastError.

Remarks
If the bRepaint parameter is TRUE, the system sends the WM_PAINT message to the window procedure immediately after moving the window (that is, the MoveWindow function calls the UpdateWindow function). If bRepaint is FALSE, the system places the WM_PAINT message in the message queue associated with the window. The message loop dispatches the WM_PAINT message only after dispatching all other messages in the queue. 

MoveWindow sends WM_WINDOWPOSCHANGING, WM_WINDOWPOSCHANGED, WM_MOVE, WM_SIZE, and WM_NCCALCSIZE messages to the window. 

#4


被3楼真相了~~!!

#5


CWnd::CenterWindow

#6


结贴是种文明。总结:上述3楼的方法只针对自己新建的对话框有效,针对windows 自带的对话框不起作用,也许使用钩子等可行,但是现在我还没有找到方法,先结贴。会的人麻烦告诉我下。谢谢

#7


引用 6 楼 zhchongyao 的回复:
结贴是种文明。总结:上述3楼的方法只针对自己新建的对话框有效,针对windows 自带的对话框不起作用,也许使用钩子等可行,但是现在我还没有找到方法,先结贴。会的人麻烦告诉我下。谢谢

不会遍历所有窗口或查找指定标题窗口的句柄hWnd吗?

#1


坐等结帖......

#2


貌似Widnows自己打开的时候都不是固定位置的,观望中。。。

#3


MoveWindow
The MoveWindow function changes the position and dimensions of the specified window. For a top-level window, the position and dimensions are relative to the upper-left corner of the screen. For a child window, they are relative to the upper-left corner of the parent window's client area. 

BOOL MoveWindow(
  HWND hWnd,      // handle to window
  int X,          // horizontal position
  int Y,          // vertical position
  int nWidth,     // width
  int nHeight,    // height
  BOOL bRepaint   // repaint flag
);
 
Parameters
hWnd 
Handle to the window. 

Specifies the new position of the left side of the window. 

Specifies the new position of the top of the window. 
nWidth 
Specifies the new width of the window. 
nHeight 
Specifies the new height of the window. 
bRepaint 
Specifies whether the window is to be repainted. If this parameter is TRUE, the window receives aWM_PAINT message. If the parameter is FALSE, no repainting of any kind occurs. This applies to the client area, the nonclient area (including the title bar and scroll bars), and any part of the parent window uncovered as a result of moving a child window. If this parameter is FALSE, the application must explicitly invalidate or redraw any parts of the window and parent window that need redrawing. 
Return Values
If the function succeeds, the return value is nonzero.

If the function fails, the return value is zero. To get extended error information, callGetLastError.

Remarks
If the bRepaint parameter is TRUE, the system sends the WM_PAINT message to the window procedure immediately after moving the window (that is, the MoveWindow function calls the UpdateWindow function). If bRepaint is FALSE, the system places the WM_PAINT message in the message queue associated with the window. The message loop dispatches the WM_PAINT message only after dispatching all other messages in the queue. 

MoveWindow sends WM_WINDOWPOSCHANGING, WM_WINDOWPOSCHANGED, WM_MOVE, WM_SIZE, and WM_NCCALCSIZE messages to the window. 

#4


被3楼真相了~~!!

#5


CWnd::CenterWindow

#6


结贴是种文明。总结:上述3楼的方法只针对自己新建的对话框有效,针对windows 自带的对话框不起作用,也许使用钩子等可行,但是现在我还没有找到方法,先结贴。会的人麻烦告诉我下。谢谢

#7


引用 6 楼 zhchongyao 的回复:
结贴是种文明。总结:上述3楼的方法只针对自己新建的对话框有效,针对windows 自带的对话框不起作用,也许使用钩子等可行,但是现在我还没有找到方法,先结贴。会的人麻烦告诉我下。谢谢

不会遍历所有窗口或查找指定标题窗口的句柄hWnd吗?