Attribute VB_Name = "modCenterWindow" Option Explicit '============================================================================================================= ' ' cCenterWindow Module ' -------------------- ' ' Created By : Kevin Wilson ' http://www.TheVBZone.com ( The VB Zone ) ' http://www.TheVBZone.net ( The VB Zone .net ) ' ' Last Update : April 01, 2000 ' ' VB Versions : 5.0 / 6.0 ' ' Requires : NOTHING ' ' Description : This class module was created to easily center any window that you provide the window's ' titlebar caption or class name for in the middle of your screen. ' ' Example Use : ' ' CenterWindow "Untitled - Notepad" ' '============================================================================================================= ' ' LEGAL: ' ' You are free to use this code as long as you keep the above heading information intact and unchanged. Credit ' given where credit is due. Also, it is not required, but it would be appreciated if you would mention ' somewhere in your compiled program that that your program makes use of code written and distributed by ' Kevin Wilson (www.TheVBZone.com). Feel free to link to this code via your web site or articles. ' ' You may NOT take this code and pass it off as your own. You may NOT distribute this code on your own server ' or web site. You may NOT take code created by Kevin Wilson (www.TheVBZone.com) and use it to create products, ' utilities, or applications that directly compete with products, utilities, and applications created by Kevin ' Wilson, TheVBZone.com, or Wilson Media. You may NOT take this code and sell it for profit without first ' obtaining the written consent of the author Kevin Wilson. ' ' These conditions are subject to change at the discretion of the owner Kevin Wilson at any time without ' warning or notice. Copyright© by Kevin Wilson. All rights reserved. ' '============================================================================================================= Public Type RECT Left As Long Top As Long Right As Long Bottom As Long End Type Public Const SW_HIDE = 0 Public Const SW_MAXIMIZE = 3 Public Const SW_MINIMIZE = 6 Public Const SW_RESTORE = 9 Public Const SW_SHOW = 5 Public Const SW_SHOWDEFAULT = 10 Public Const SW_SHOWMAXIMIZED = 3 Public Const SW_SHOWMINIMIZED = 2 Public Const SW_SHOWMINNOACTIVE = 7 Public Const SW_SHOWNA = 8 Public Const SW_SHOWNOACTIVATE = 4 Public Const SW_SHOWNORMAL = 1 Public Declare Function GetWindowRect Lib "USER32" (ByVal hWnd As Long, lpRect As RECT) As Long Public Declare Function FindWindow Lib "USER32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long Public Declare Function MoveWindow Lib "USER32" (ByVal hWnd As Long, ByVal X As Long, ByVal Y As Long, ByVal nWidth As Long, ByVal nHeight As Long, ByVal bRepaint As Long) As Long Public Declare Function ShowWindow Lib "USER32" (ByVal hWnd As Long, ByVal nCmdShow As Long) As Long ' Center the specified window Public Function CenterWindow(Optional TitlebarCaption As String = "", Optional ClassName As String = "") On Error Resume Next Dim TheHandle As Long Dim ReturnValue As Long Dim W_Top As Integer Dim W_Left As Integer Dim W_Width As Integer Dim W_Height As Integer Dim rct As RECT If ClassName = "" And TitlebarCaption = "" Then Exit Function ElseIf ClassName = "" Then TheHandle = FindWindow(vbNullString, TitlebarCaption) Else TheHandle = FindWindow(ClassName, vbNullString) End If If TheHandle = 0 Then Exit Function End If ReturnValue = GetWindowRect(TheHandle, rct) If ReturnValue = 0 Then ' Error Exit Function End If W_Top = rct.Top W_Left = rct.Left W_Height = rct.Bottom - rct.Top W_Width = rct.Right - rct.Left W_Top = ((Screen.Height / Screen.TwipsPerPixelY) - W_Height) / 2 W_Left = ((Screen.Width / Screen.TwipsPerPixelX) - W_Width) / 2 ShowWindow TheHandle, SW_SHOW ShowWindow TheHandle, SW_RESTORE MoveWindow TheHandle, W_Left, W_Top, W_Width, W_Height, True End Function