會(huì)員登錄 - 用戶注冊(cè) - 網(wǎng)站地圖 Office中國(guó)(office-cn.net),專業(yè)Office論壇
當(dāng)前位置:主頁 > 技巧 > Access技巧 > 窗體控件 > 正文

窗體自動(dòng)居中代碼

時(shí)間:2004-11-23 17:26 來源:AccessQQ 作者:huanghai… 閱讀:

使窗體居中顯示代碼
access中的窗體雖然設(shè)置了自動(dòng)居中,但是打開后總是有點(diǎn)不居中的感覺,加下如下代碼可以做到真正居中顯示

Private Sub Form_Load()
DoCmd.Echo False
Dim x, y As Integer
DoCmd.Maximize
x = Me.WindowWidth
y = Me.WindowHeight
DoCmd.Restore
DoCmd.Echo True
Move (x - Me.WindowWidth) / 2, (y - Me.WindowHeight) / 2
End Sub

下列代碼適用于accessXP以上

'使用方法:

'Private Sub Form_Load()
'    moveFormToCenter Me '居中
'End Sub


'Private Sub Form_Load()
'    moveFormToCenter Me, 3000, 2000  '調(diào)整窗體大小并居中
'End Sub

Option Compare Database
Option Explicit

Type RECT
    x1 As Long
    y1 As Long
    x2 As Long
    y2 As Long
End Type

Public Declare Function GetClientRect Lib "user32" (ByVal hwnd As Long, lpRect As RECT) As Long

'菜單欄高:22
'工具欄高:26
'狀態(tài)欄高:20

Public Function moveFormToCenter(ByRef Frm As Form, Optional ByVal longFormWidth As Long = 0, Optional ByVal longFormHeight As Long = 0)

    Dim lngW, lngH As Long

    lngW = GetaccessClientWidth() - 4    '-4為測(cè)試微調(diào)值
    lngW = lngW * 15

    lngH = GetaccessClientHeight() - 4    '-4為測(cè)試微調(diào)值
    'lngH = lngH - (22 * 1)  '一個(gè)菜單欄
    lngH = lngH - (26 * 1)  '一個(gè)工具欄
    'lngH = lngH - (20 * 1) '一個(gè)狀態(tài)欄
    lngH = lngH * 15

    If longFormWidth + longFormHeight = 0 Then
        Frm.Move (lngW - Frm.WindowWidth) / 2, (lngH - Frm.WindowHeight) / 2
    End If

    If longFormWidth > 0 And longFormHeight > 0 Then
        Frm.Move (lngW - longFormWidth) / 2, (lngH - longFormHeight) / 2, longFormWidth, longFormHeight
    End If

End Function

Public Function GetaccessClientWidth() As Integer
    Dim R As RECT
    Dim hwnd As Long
    Dim RetVal As Long

    hwnd = Application.hWndaccessApp

    RetVal = GetClientRect(hwnd, R)
    'Debug.Print R.x2
    'Debug.Print R.x1
    GetaccessClientWidth = R.x2 - R.x1

End Function
Public Function GetaccessClientHeight() As Integer
    Dim R As RECT
    Dim hwnd As Long
    Dim RetVal As Long

    hwnd = Application.hWndaccessApp

    RetVal = GetClientRect(hwnd, R)
    'Debug.Print R.y2
    'Debug.Print R.y1
    GetaccessClientHeight = R.y2 - R.y1

End Function

(責(zé)任編輯:admin)

頂一下
(0)
0%
踩一下
(0)
0%
發(fā)表評(píng)論
請(qǐng)自覺遵守互聯(lián)網(wǎng)相關(guān)的政策法規(guī),嚴(yán)禁發(fā)布色情、暴力、反動(dòng)的言論。
評(píng)價(jià):