我有一个程序需要在屏幕的右下角显示表单。我做了一些研究,它说设置me.location =将锁定表单位置,但它似乎没有工作。代码如下:
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 :(得分: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
答案 1 :(得分:2)
为什么不检查这篇文章(Make form always stick to desktop like Win 7 gadget (VB.net)),我回答了一个非常相似的问题。
答案 2 :(得分: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
答案 3 :(得分: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