VB如何设置弹出窗口的位置?

2025-03-04 09:05:54
推荐回答(4个)
回答1:

如果你整个程序窗口比较少的话~不用写代码 直接把你想弹出的窗口 在 窗口布局上 拖到你想在桌面出现的位置就可以了

回答2:

直接在 VB 的右下角,,有个电脑形状的东东,在那里调下位置就行了.....

回答3:

在程序里设置。
form1.show
form1.left=screen.width - form1.width
form1.top = screen.height - form1.height

回答4:

VB自己设定消息框的显示位置
现在就来做一个在程序主窗口居中显示的消息框。 在Module模块中添加以下API函数、类型以及常量的声明:
Public Declare Function GetWindowRect Lib "user32" (ByVal hwnd As Long, lpRect As RECT) As Long
Public Type RECT Left As Long Top As Long Right As Long Bottom As Long End Type
Public Declare Function SetWindowPos Lib "user32" (ByVal hwnd As Long, ByVal hWndInsertAfter As Long, ByVal x As Long, ByVal y As Long, ByVal cx As Long, ByVal cy As Long, ByVal wFlags As Long) As Long
Public Const SWP_NOSIZE As Long = &H1& Public Const SWP_NOZORDER As Long = &H4& Public Const HWND_TOP As Long = 0&
在刚才的窗体上再添加一个Command按钮和一个Timer控件,代码如下: Private Sub Command2_Click()
' 这里我们将自己设置消息框的位置
' 打开计时器,设定10毫秒后改变消息框的位置 Timer2.Interval = 10 Timer2.Enabled = True
' 调用 messagebox API 函数
MessageBox Me.hwnd, "你见过不在屏幕中央的消息框吗?", "改变位置的 MessageBox", vbQuestion End Sub
Private Sub Timer2_Timer()
Dim hMsgBox As Long, xPoint As Long, yPoint As Long Dim stMsgBoxRect As RECT, stParentRect As RECT
' 找到消息框窗口
hMsgBox = FindWindow("#32770", "改变位置的 MessageBox")
' 如果找到窗口就移动它 If hMsgBox Then

' 得到消息框和父窗口的矩形位置
GetWindowRect hMsgBox, stMsgBoxRect GetWindowRect Me.hwnd, stParentRect
' 计算位置,以便把消息框放到窗体的中央
xPoint = stParentRect.Left + (((stParentRect.Right - stParentRect.Left) \ 2) - ((stMsgBoxRect.Right - stMsgBoxRect.Left) \ 2))
yPoint = stParentRect.Top + (((stParentRect.Bottom - stParentRect.Top) \ 2) - ((stMsgBoxRect.Bottom - stMsgBoxRect.Top) \ 2))
' 这里还要确定消息框的显示位置不会超出屏幕 If xPoint < 0 Then xPoint = 0 If yPoint < 0 Then yPoint = 0
If (xPoint + (stMsgBoxRect.Right - stMsgBoxRect.Left)) > (Screen.Width \ Screen.TwipsPerPixelX) Then
xPoint = (Screen.Width \ Screen.TwipsPerPixelX) - (stMsgBoxRect.Right - stMsgBoxRect.Left) End If
If (yPoint + (stMsgBoxRect.Bottom - stMsgBoxRect.Top)) > (Screen.Height \ Screen.TwipsPerPixelY) Then
yPoint = (Screen.Height \ Screen.TwipsPerPixelY) - (stMsgBoxRect.Bottom - stMsgBoxRect.Top) End If
' 移动位置
SetWindowPos hMsgBox, HWND_TOP, xPoint, yPoint, 0, 0, SWP_NOZORDER Or SWP_NOSIZE End If
' 关闭计时器
Timer2.Enabled = False End Sub