控制台应用程序 VB 如何将屏幕居中
Console Application VB how to center screen
如何让我的应用程序在屏幕中央打开?我知道如何使用 Form 应用程序,但我不知道如何使用控制台,因为它的工作方式不同。
Console.Location = New Point((Screen.PrimaryScreen.WorkingArea.Width - this.Width) / 2,
(Screen.PrimaryScreen.WorkingArea.Height - this.Height) / 2)
Console.SetWindowPosition(0, 0)
试试这个:
Public Shared Sub CenterConsole()
Dim hWin As IntPtr = GetConsoleWindow()
Dim rc As RECT
GetWindowRect(hWin, rc)
Dim scr As Screen = Screen.FromPoint(New Point(rc.left, rc.top))
Dim x As Integer = scr.WorkingArea.Left + (scr.WorkingArea.Width - (rc.right - rc.left)) / 2
Dim y As Integer = scr.WorkingArea.Top + (scr.WorkingArea.Height - (rc.bottom - rc.top)) / 2
MoveWindow(hWin, x, y, rc.right - rc.left, rc.bottom - rc.top, False)
End Sub
Private Structure RECT
Public left, top, right, bottom As Integer
End Structure
<DllImport("kernel32.dll", SetLastError:=True)>
Private Shared Function GetConsoleWindow() As IntPtr
<DllImport("user32.dll", SetLastError:=True)>
Private Shared Function GetWindowRect(ByVal hWnd As IntPtr, <Out> ByRef rc As RECT) As Boolean
<DllImport("user32.dll", SetLastError:=True)>
Private Shared Function MoveWindow(ByVal hWnd As IntPtr, ByVal x As Integer, ByVal y As Integer, ByVal w As Integer, ByVal h As Integer, ByVal repaint As Boolean) As Boolean
当您想将控制台居中时 window 只需像这样调用 CenterConsole sub:
CenterConsole()
与上面相同,修复了错误并添加了第二个中心以展示如何将其移动到第二台显示器。
此外,我想感谢您帮助我解决这个问题。在此之前我没有看到 GetConsoleWindow。
Public Shared Sub CenterConsole()
Dim hWin As IntPtr = GetConsoleWindow()
Dim rc As RECT
GetWindowRect(hWin, rc)
Dim scr As Screen = Screen.FromPoint(New Point(rc.left, rc.top))
Dim x As Integer = scr.WorkingArea.Left + (scr.WorkingArea.Width - (rc.right - rc.left)) / 2
Dim y As Integer = scr.WorkingArea.Top + (scr.WorkingArea.Height - (rc.bottom - rc.top)) / 2
MoveWindow(hWin, x, y, rc.right - rc.left, rc.bottom - rc.top, False)
End Sub
Public Shared Sub CenterConsoleRightMonitor()
Dim hWin As IntPtr = GetConsoleWindow()
Dim rc As RECT
GetWindowRect(hWin, rc)
Dim scr As Screen = Screen.AllScreens(1)
Dim x As Integer = scr.WorkingArea.Left + (scr.WorkingArea.Width - (rc.right - rc.left)) / 2
Dim y As Integer = scr.WorkingArea.Top + (scr.WorkingArea.Height - (rc.bottom - rc.top)) / 2
MoveWindow(hWin, x, y, rc.right - rc.left, rc.bottom - rc.top, False)
End Sub
' Screen.AllScreens(0).Bounds.Width + Screen.AllScreens(1).Bounds.Width
Private Structure RECT
Public left, top, right, bottom As Integer
End Structure
<DllImport("kernel32.dll", SetLastError:=True)>
Private Shared Function GetConsoleWindow() As IntPtr
End Function
<DllImport("user32.dll", SetLastError:=True)>
Private Shared Function GetWindowRect(ByVal hWnd As IntPtr, <Out> ByRef rc As RECT) As Boolean
End Function
<DllImport("user32.dll", SetLastError:=True)>
Private Shared Function MoveWindow(ByVal hWnd As IntPtr, ByVal x As Integer, ByVal y As Integer, ByVal w As Integer, ByVal h As Integer, ByVal repaint As Boolean) As Boolean
End Function
如何让我的应用程序在屏幕中央打开?我知道如何使用 Form 应用程序,但我不知道如何使用控制台,因为它的工作方式不同。
Console.Location = New Point((Screen.PrimaryScreen.WorkingArea.Width - this.Width) / 2,
(Screen.PrimaryScreen.WorkingArea.Height - this.Height) / 2)
Console.SetWindowPosition(0, 0)
试试这个:
Public Shared Sub CenterConsole()
Dim hWin As IntPtr = GetConsoleWindow()
Dim rc As RECT
GetWindowRect(hWin, rc)
Dim scr As Screen = Screen.FromPoint(New Point(rc.left, rc.top))
Dim x As Integer = scr.WorkingArea.Left + (scr.WorkingArea.Width - (rc.right - rc.left)) / 2
Dim y As Integer = scr.WorkingArea.Top + (scr.WorkingArea.Height - (rc.bottom - rc.top)) / 2
MoveWindow(hWin, x, y, rc.right - rc.left, rc.bottom - rc.top, False)
End Sub
Private Structure RECT
Public left, top, right, bottom As Integer
End Structure
<DllImport("kernel32.dll", SetLastError:=True)>
Private Shared Function GetConsoleWindow() As IntPtr
<DllImport("user32.dll", SetLastError:=True)>
Private Shared Function GetWindowRect(ByVal hWnd As IntPtr, <Out> ByRef rc As RECT) As Boolean
<DllImport("user32.dll", SetLastError:=True)>
Private Shared Function MoveWindow(ByVal hWnd As IntPtr, ByVal x As Integer, ByVal y As Integer, ByVal w As Integer, ByVal h As Integer, ByVal repaint As Boolean) As Boolean
当您想将控制台居中时 window 只需像这样调用 CenterConsole sub:
CenterConsole()
与上面相同,修复了错误并添加了第二个中心以展示如何将其移动到第二台显示器。
此外,我想感谢您帮助我解决这个问题。在此之前我没有看到 GetConsoleWindow。
Public Shared Sub CenterConsole()
Dim hWin As IntPtr = GetConsoleWindow()
Dim rc As RECT
GetWindowRect(hWin, rc)
Dim scr As Screen = Screen.FromPoint(New Point(rc.left, rc.top))
Dim x As Integer = scr.WorkingArea.Left + (scr.WorkingArea.Width - (rc.right - rc.left)) / 2
Dim y As Integer = scr.WorkingArea.Top + (scr.WorkingArea.Height - (rc.bottom - rc.top)) / 2
MoveWindow(hWin, x, y, rc.right - rc.left, rc.bottom - rc.top, False)
End Sub
Public Shared Sub CenterConsoleRightMonitor()
Dim hWin As IntPtr = GetConsoleWindow()
Dim rc As RECT
GetWindowRect(hWin, rc)
Dim scr As Screen = Screen.AllScreens(1)
Dim x As Integer = scr.WorkingArea.Left + (scr.WorkingArea.Width - (rc.right - rc.left)) / 2
Dim y As Integer = scr.WorkingArea.Top + (scr.WorkingArea.Height - (rc.bottom - rc.top)) / 2
MoveWindow(hWin, x, y, rc.right - rc.left, rc.bottom - rc.top, False)
End Sub
' Screen.AllScreens(0).Bounds.Width + Screen.AllScreens(1).Bounds.Width
Private Structure RECT
Public left, top, right, bottom As Integer
End Structure
<DllImport("kernel32.dll", SetLastError:=True)>
Private Shared Function GetConsoleWindow() As IntPtr
End Function
<DllImport("user32.dll", SetLastError:=True)>
Private Shared Function GetWindowRect(ByVal hWnd As IntPtr, <Out> ByRef rc As RECT) As Boolean
End Function
<DllImport("user32.dll", SetLastError:=True)>
Private Shared Function MoveWindow(ByVal hWnd As IntPtr, ByVal x As Integer, ByVal y As Integer, ByVal w As Integer, ByVal h As Integer, ByVal repaint As Boolean) As Boolean
End Function