[WM][VB][如何變更表單顯示大小]
很多人在開發手機應用程式時都會認為表單是固定尺寸無法變動,接著一定會有人說在編輯器裡面有Form1 屬性視窗有size 可以設定呀,就會變更大小啦看官們你可以試看看設定在跑一下因該是沒辦法變成跟我下圖所示一樣,所以接著要教大家如何來變更表單。
原本表單 變更表單
Step1:開啟vs2008 新增一個vb 智慧型裝置專案,在表單上產一個按鍵 Resize
Step2:撰寫Form1程式碼
Imports System.Runtime.InteropServices
Public Class Form1
Private FormSize As Size
<DllImport("coredll")> _
Public Shared Function MoveWindow(ByVal hWnd As IntPtr, ByVal X As Integer, ByVal Y As Integer, ByVal Width As Integer, ByVal Height As Integer, ByVal Repaint As Boolean) As IntPtr
End Function
<DllImport("coredll")> _
Public Shared Function GetWindowLong(ByVal hWnd As IntPtr, ByVal nItem As Integer) As Integer
End Function
<DllImport("coredll")> _
Public Shared Sub SetWindowLong(ByVal hWnd As IntPtr, ByVal nItem As Integer, ByVal nValue As Integer)
End Sub
<DllImport("coredll")> _
Public Shared Function GetCapture() As IntPtr
End Function
Private Const GWL_STYLE As Integer = -16
Private Const WS_CAPTION As Integer = &HC00000
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
'設定變更表單新尺寸
FormSize = New Size(200, 200)
'執行變更表單
ResizeForm()
End Sub
Private Sub ResizeForm()
'呼叫外部dll提供api來執行我們要的效果
Dim x, y As Integer
x = (Screen.PrimaryScreen.WorkingArea.Width - FormSize.Width) / 2
y = (Screen.PrimaryScreen.WorkingArea.Height - FormSize.Height) / 2 + Screen.PrimaryScreen.WorkingArea.Top
Me.Capture = True
Dim hWnd As IntPtr = GetCapture()
Me.Capture = False
SetWindowLong(hWnd, GWL_STYLE, GetWindowLong(hWnd, GWL_STYLE) Or WS_CAPTION)
MoveWindow(hWnd, x, y, FormSize.Width, FormSize.Height, True)
End Sub
End Class
Public Class Form1
Private FormSize As Size
<DllImport("coredll")> _
Public Shared Function MoveWindow(ByVal hWnd As IntPtr, ByVal X As Integer, ByVal Y As Integer, ByVal Width As Integer, ByVal Height As Integer, ByVal Repaint As Boolean) As IntPtr
End Function
<DllImport("coredll")> _
Public Shared Function GetWindowLong(ByVal hWnd As IntPtr, ByVal nItem As Integer) As Integer
End Function
<DllImport("coredll")> _
Public Shared Sub SetWindowLong(ByVal hWnd As IntPtr, ByVal nItem As Integer, ByVal nValue As Integer)
End Sub
<DllImport("coredll")> _
Public Shared Function GetCapture() As IntPtr
End Function
Private Const GWL_STYLE As Integer = -16
Private Const WS_CAPTION As Integer = &HC00000
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
'設定變更表單新尺寸
FormSize = New Size(200, 200)
'執行變更表單
ResizeForm()
End Sub
Private Sub ResizeForm()
'呼叫外部dll提供api來執行我們要的效果
Dim x, y As Integer
x = (Screen.PrimaryScreen.WorkingArea.Width - FormSize.Width) / 2
y = (Screen.PrimaryScreen.WorkingArea.Height - FormSize.Height) / 2 + Screen.PrimaryScreen.WorkingArea.Top
Me.Capture = True
Dim hWnd As IntPtr = GetCapture()
Me.Capture = False
SetWindowLong(hWnd, GWL_STYLE, GetWindowLong(hWnd, GWL_STYLE) Or WS_CAPTION)
MoveWindow(hWnd, x, y, FormSize.Width, FormSize.Height, True)
End Sub
End Class
Step3:將滑鼠移到功能表按下偵錯\開始偵錯部署應用程式
Step4:按下Resize 鍵,這時候看一下表單是否有變化
Step5: 源碼下載