怎样使窗口保持在屏幕的中央
编号:QA003183
建立日期: 2000年7月20日 最后修改日期:2000年7月25日
所属类别:
zhoujl:
操作系统: win98
编程工具: VisualBasic5
问题: 怎样使窗口保持在屏幕的中央?
水平: 中级
回答:
你可以设置窗体的StartUpPosition属性为2,这样窗口一开始就出现在屏幕中央。VB缺乏一个事件来检测用户是否移动窗口,你可以参考QA000727 "在msgblst中如何处理指向结构变量的地址"所介绍的方法来禁止用户移动窗口。
kvdvm的意见:
禁止窗口移动可以使用:
Form1.Moveable=false
sean的意见:
VOID
CenterWindow(
HWND hwnd
)
{
RECT rect;
LONG dx, dy;
LONG dxParent, dyParent;
LONG Style;
// Get window rect
GetWindowRect(hwnd, &rect);
dx = rect.right - rect.left;
dy = rect.bottom - rect.top;
// Get parent rect
Style = GetWindowLong(hwnd, GWL_STYLE);
if ((Style & WS_CHILD) == 0) {
// Return the desktop windows size (size of main screen)
dxParent = GetSystemMetrics(SM_CXSCREEN);
dyParent = GetSystemMetrics(SM_CYSCREEN);
} else {
HWND hwndParent;
RECT rectParent;
hwndParent = GetParent(hwnd);
if (hwndParent == NULL) {
hwndParent = GetDesktopWindow();
}
GetWindowRect(hwndParent, &rectParent);
dxParent = rectParent.right - rectParent.left;
dyParent = rectParent.bottom - rectParent.top;
}
// Centre the child in the parent
rect.left = (dxParent - dx) / 2;
rect.top = (dyParent - dy) / 3;
// Move the child into position
SetWindowPos(hwnd, HWND_TOPMOST, rect.left, rect.top, 0, 0, SWP_NOSIZE);
SetForegroundWindow(hwnd);
}
此问题由李海回答。
| |
|
|
| |
|
|