1个回答
展开全部
这个我给你一个思路 熟悉API函数的都知道 如果能获取一个窗体的句柄 那么肯定可以调用API函数来移动这个窗体 所以可以这么做 首先调用MessageBox这个API函数来弹出一个消息框 然后在用SetWindowPos来将这个窗口移动到指定位置即可! 代码如下 : (首先在窗体上添加2个Timer控件,一个按钮,) Private Declare Function GetWindowRect Lib "user32" (ByVal hwnd As Long, lpRect As RECT) As Long Private Declare Function MessageBox Lib "user32" Alias "MessageBoxA" (ByVal hwnd As Long, ByVal lpText As String, ByVal lpCaption As String, ByVal wType As Long) As Long Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long Private Type RECT Left As Long Top As Long Right As Long Bottom As Long End Type Private 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 Private Const SWP_NOSIZE = &H1& Private Const SWP_NOZORDER = &H4& Private Const HWND_TOP = 0& Private Sub Command1_Click() 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 以上代码能实现弹出的对话框始终在窗体中间 希望能帮助到你 不懂请追问。
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询