2013-10-17 125 views
1

我有一個程序,要求窗體顯示在屏幕的右下角。我已經做了一些研究,它說設置me.location =會鎖定窗體位置,但它似乎沒有工作。代碼如下:在VB中鎖定Windows窗體位置


Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load 
     'Position form to lower right hand corner of screen 

    Me.Visible = True 
    Dim x As Integer 
    Dim y As Integer 
    x = Screen.PrimaryScreen.WorkingArea.Width 
    y = Screen.PrimaryScreen.WorkingArea.Height - Me.Height 

    Do Until x = Screen.PrimaryScreen.WorkingArea.Width - Me.Width 
     x = x - 1 
     Me.Location = New Point(x, y) 
    Loop 

End Sub 

我需要的形式有最小化,關閉按鈕,並鎖定到右下角,當它沒有最小化或關閉。

我使用VB 2010 Express

乾杯。

+0

UPDATE: ,因爲我已經在節目中的第二種形式只是凍結添加相同的代碼爲第二形態。這個代碼肯定有一些東西。我想知道爲什麼表單1可以定位表單(但用戶可以自由移動它),表單2導致程序凍結。 – DaveyLions

回答

3

試試這個:

更新:

一個更好的解決辦法是:

Public Class Form1 

' Just to set and store custom locations 
Dim Corners As New Dictionary(Of String, Point) 

' Flag to make/unmake form moveable 
Private bMoveable As Boolean = True 

Private Declare Function EnableMenuItem Lib "user32.dll" Alias "EnableMenuItem" (ByVal hMenu As IntPtr, ByVal uIDEnableItem As Int32, ByVal uEnable As Int32) As Int32 

Public Overridable Property Moveable() As Boolean 
    Get 
     Return bMoveable 
    End Get 
    Set(ByVal Value As Boolean) 
     If bMoveable <> Value Then 
      bMoveable = Value 
     End If 
    End Set 
End Property 

Protected Overrides Sub WndProc(ByRef m As Message) 

    If m.Msg = &H117& Then 
     'Handles popup of system menu. 
     If m.LParam.ToInt32 \ 65536 <> 0 Then 'divide by 65536 to get hiword. 
      Dim AbleFlags As Int32 = &H0& 
      If Not Moveable Then AbleFlags = &H2& Or &H1& 
      EnableMenuItem(m.WParam, &HF010&, &H0& Or AbleFlags) 
     End If 
    End If 

    If Not Moveable Then 
     'Cancels any attempt to drag the window by it's caption. 
     If m.Msg = &HA1 Then If m.WParam.ToInt32 = &H2 Then Return 
     'Redundant but cancels any clicks on the Move system menu item. 
     If m.Msg = &H112 Then If (m.WParam.ToInt32 And &HFFF0) = &HF010& Then Return 
    End If 

    'Return control to base message handler. 
    MyBase.WndProc(m) 

End Sub 

Private Sub Form1_Shown(sender As System.Object, e As System.EventArgs) Handles MyBase.Shown 

    ' Add a custom location to the dictionary 
    Corners.Add("BottomRight", _ 
     New Point(Screen.PrimaryScreen.WorkingArea.Width - Me.Width, _ 
       Screen.PrimaryScreen.WorkingArea.Height - Me.Height)) 

    ' Move the form 
    Me.Location = Corners("BottomRight") 

    ' Make it unmoveable from there! 
    Me.Moveable = False 

End Sub 

End Class 
+0

現在驗證。感謝您的答覆! – DaveyLions

+1

這是完美的!感謝您擁有我的UPVOTE。誰被推翻,一定沒有看到更新! – DaveyLions

+0

@DaveyLions謝謝你這麼多,並且隨時問你是否有些代碼不清楚。 – ElektroStudios

0

你可以嘗試這樣的事:

Private Sub Form1_Move(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Move 
    Me.Location = New Point(30, 30) 
End Sub 
+0

邁克,謝謝你,我只是要問,我現在要試試這個,(30,30)的意義是什麼?我們不需要計算角落基於提供屏幕分辨率的位置嗎? – DaveyLions

+0

沒有工作。它將表單鎖定在30,30而不是右側角落 – DaveyLions

0

嘗試了這一點...

Imports System.Runtime.InteropServices 
Public Class Form1 

    Private LockPt As Point 
    Private Const WM_MOVING As Integer = &H216 

    Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load 
     Me.MaximizeBox = False 
     Me.FormBorderStyle = Windows.Forms.FormBorderStyle.FixedSingle 

     LockPt.X = Screen.PrimaryScreen.WorkingArea.Width - Me.Width 
     LockPt.Y = Screen.PrimaryScreen.WorkingArea.Height - Me.Height 
     Me.StartPosition = FormStartPosition.Manual 
     Me.Location = New Point(Screen.PrimaryScreen.WorkingArea.Width, LockPt.Y) 
    End Sub 

    Private Sub Form1_Shown(sender As Object, e As System.EventArgs) Handles Me.Shown 
     While Me.Location.X > Screen.PrimaryScreen.WorkingArea.Width - Me.Width 
      Me.Location = New Point(Me.Location.X - 1, LockPt.Y) 
     End While 
    End Sub 

    Private Structure RECT 
     Public Left As Integer 
     Public Top As Integer 
     Public Right As Integer 
     Public Bottom As Integer 
    End Structure 

    Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message) 
     Select Case m.Msg 
      Case WM_MOVING 
       Dim rc As RECT = Marshal.PtrToStructure(m.LParam, GetType(RECT)) 
       rc.Left = LockPt.X 
       rc.Top = LockPt.Y 
       rc.Right = rc.Left + Me.Width 
       rc.Bottom = rc.Top + Me.Height 
       Marshal.StructureToPtr(rc, m.LParam, True) 
     End Select 

     MyBase.WndProc(m) 
    End Sub 

End Class