在 开 发VB 程 序 时, 一 般 希 望 将 表 单 放 在 屏 幕 可 利 用 区 域 的 正 中 央, 实 现 上 可 以 利 用Move (Screen.Width - Width) \ 2, (Screen.Height - Height) \ 2 的 方 法 来 实 现。 但 是 当 用 户 使 用Windows 95 或 NT 操 作 系 统 时, 在 屏 幕 的 底 端 会 有 一 任 务 条, 上 述 的 实 现 方 法 并 未 考 虑 该 任 务 条 所 占 的 空 间, 表 单 实 际 并 未 处 于 屏 幕 可 利 用 区 域 的 正 中 央。 下 面 的 代 码 段 实 现 了 在 每 次 启 动 应 用 程 序 时, 无 论 屏 幕 是 否 有 任 务 条, 表 单 都 会 处 于 屏 幕 可 利 用 区 域 的 正 中 央。 在 工 程 中 增 添 一 模 块, 在 模 块 中 加 上 如 下 的 代 码: Option Explicit Private Const SPI_GETWORKAREA = 48 Private Declare Function SystemParametersInfo& Lib "User32" Alias "SystemParametersInfoA" (ByVal uAction As Long,ByVal uParam As Long, lpvParam As Any, ByVal fuWinIni As Long) Private Type RECT Left As Long Top As Long Right As Long Bottom As Long End Type Public Function CenterForm32 (frm As Form) Dim ScreenWidth&, ScreenHeight&, ScreenLeft&, ScreenTop& Dim DesktopArea As RECT Call SystemParametersInfo (SPI_GETWORKAREA, 0, DesktopArea, 0) ScreenHeight = (DesktopArea.Bottom - DesktopArea.Top) * Screen.TwipsPerPixelY ScreenWidth = (DesktopArea.Right - DesktopArea.Left) * Screen.TwipsPerPixelX ScreenLeft = DesktopArea.Left * Screen.TwipsPerPixelX ScreenTop = DesktopArea.Top * Screen.TwipsPerPixelY frm.Move (ScreenWidth - frm.Width)\ 2 + ScreenLeft, (ScreenHeight - frm.Height) \ 2 + ScreenTop End Function 要 调 用CenterForm32 函 数, 可 在 表 单 的Load 事 件 中 增 添 代 码CenterForm32 Me 即 可。 以 上 代 码 在VB4/32,VB5 中 实 现。
|