VB做的无框窗体只能在电脑屏幕范围内移动
拖动一个VB做的无框窗体在电脑屏幕范围内移动,不管分辨率是多少,都不能移动到屏幕外面去,这样的代码要怎么写呢?代码越少越好,不过不要全部都是文字解释这个问题,不然的话我问...
拖动一个VB做的无框窗体在电脑屏幕范围内移动,不管分辨率是多少,都不能移动到屏幕外面去,这样的代码要怎么写呢?代码越少越好,不过不要全部都是文字解释这个问题,不然的话我问这个问题就没意义了,谢谢
比如说:我正在玩的[圣斗士OL],这个游戏就是一个无边框窗口,再怎么拖动游戏窗口,它始终都在电脑屏幕内活动,想拖出屏幕外面就会被阻止掉,怎么也拖不到外面去 展开
比如说:我正在玩的[圣斗士OL],这个游戏就是一个无边框窗口,再怎么拖动游戏窗口,它始终都在电脑屏幕内活动,想拖出屏幕外面就会被阻止掉,怎么也拖不到外面去 展开
展开全部
Option Explicit
Dim MovX As Long, MovY As Long
Private Const CS_DROPSHADOW = &H20000
Private Const GCL_STYLE = (-26)
Private Sub Form_MouseDown(Button As Integer, Shift As Integer, _
X As Single, Y As Single)
If Button = 1 Then
MovX = X: MovY = Y
End If
End Sub
Private Sub Form_MouseMove(Button As Integer, Shift As Integer, _
X As Single, Y As Single)
If Button = 1 Then
Me.Move Me.Left + (X - MovX), Me.Top + (Y - MovY)
End If
End Sub
————————————————以上是移动无框窗体代码———————————————
至于你说的简洁代码,干脆就建一个模板,然后棚棚直接模板代码。还有引用不能移出屏幕链升则,我人为硬挨就像QQ一样,移动到角落就自动为吸附式,其实道理都一样,都要先用个timer监视,然后设置时间设置200毫秒,代码如下
If Screen.ActiveForm.Top < 0 Then
Screen.ActiveForm.Top = 0
End If
If Screen.ActiveForm.Left < 0 Then
Screen.ActiveForm.Left = 0
End If
If Screen.ActiveForm.Top + Screen.ActiveForm.Height > Screen.Height Then
Screen.ActiveForm.Top = Screen.Height - Screen.ActiveForm.Height
End If
If Screen.ActiveForm.Left + Screen.ActiveForm.Width > Screen.Width Then
Screen.ActiveForm.Left = Screen.Width - Screen.ActiveForm.Width
End If
—————————————————这是笑埋timer1的代码—————————————————
Dim MovX As Long, MovY As Long
Private Const CS_DROPSHADOW = &H20000
Private Const GCL_STYLE = (-26)
Private Sub Form_MouseDown(Button As Integer, Shift As Integer, _
X As Single, Y As Single)
If Button = 1 Then
MovX = X: MovY = Y
End If
End Sub
Private Sub Form_MouseMove(Button As Integer, Shift As Integer, _
X As Single, Y As Single)
If Button = 1 Then
Me.Move Me.Left + (X - MovX), Me.Top + (Y - MovY)
End If
End Sub
————————————————以上是移动无框窗体代码———————————————
至于你说的简洁代码,干脆就建一个模板,然后棚棚直接模板代码。还有引用不能移出屏幕链升则,我人为硬挨就像QQ一样,移动到角落就自动为吸附式,其实道理都一样,都要先用个timer监视,然后设置时间设置200毫秒,代码如下
If Screen.ActiveForm.Top < 0 Then
Screen.ActiveForm.Top = 0
End If
If Screen.ActiveForm.Left < 0 Then
Screen.ActiveForm.Left = 0
End If
If Screen.ActiveForm.Top + Screen.ActiveForm.Height > Screen.Height Then
Screen.ActiveForm.Top = Screen.Height - Screen.ActiveForm.Height
End If
If Screen.ActiveForm.Left + Screen.ActiveForm.Width > Screen.Width Then
Screen.ActiveForm.Left = Screen.Width - Screen.ActiveForm.Width
End If
—————————————————这是笑埋timer1的代码—————————————————
展开全部
'建一个模块
Public Const GWL_WNDPROC = -4
Public Const WM_SIZE = &H5
Public Const WM_MOVE = &H3
Public Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hWnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
Public Declare Function CallWindowProc Lib "user32" Alias "CallWindowProcA" (ByVal lpPrevWndFunc As Long, ByVal hWnd As Long, ByVal Msg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
Public Declare Function GetWindowLong Lib "user32" Alias "GetWindowLongA" (ByVal hWnd As Long, ByVal nIndex As Long) As Long
Public OldWindowProc As Long '用来保存系统默认的窗口消息处理函数的地址
Public Sub HookForm(F As Form)
OldWindowProc = GetWindowLong(F.hWnd, GWL_WNDPROC)
Call SetWindowLong(F.hWnd, GWL_WNDPROC, AddressOf NewWindowProc)
End Sub
Public Sub UnHookForm(F As Form)
SetWindowLong F.hWnd, GWL_WNDPROC, OldWindowProc
End Sub
'自蚂姿定义的消息旁物庆处理函数
Public Function NewWindowProc(ByVal hWnd As Long, ByVal Msg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
On Error Resume Next
If Msg = WM_MOVE Then
If Screen.ActiveForm.Top < 0 Then Screen.ActiveForm.Top = 0
If Screen.ActiveForm.Left < 0 Then Screen.ActiveForm.Left = 0
If Screen.ActiveForm.Top + Screen.ActiveForm.Height > Screen.Height Then Screen.ActiveForm.Top = Screen.Height - Screen.ActiveForm.Height
If Screen.ActiveForm.Left + Screen.ActiveForm.Width > Screen.Width Then Screen.ActiveForm.Left = Screen.Width - Screen.ActiveForm.Width
Else
'调用默认窗口消息处理函数
NewWindowProc = CallWindowProc(OldWindowProc, hWnd, Msg, wParam, lParam)
End If
End Function
'在运握窗体里
Private Sub Form_Load()
HookForm Me
End Sub
Private Sub Form_Unload(Cancel As Integer)
UnHookForm Me
End Sub
Public Const GWL_WNDPROC = -4
Public Const WM_SIZE = &H5
Public Const WM_MOVE = &H3
Public Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hWnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
Public Declare Function CallWindowProc Lib "user32" Alias "CallWindowProcA" (ByVal lpPrevWndFunc As Long, ByVal hWnd As Long, ByVal Msg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
Public Declare Function GetWindowLong Lib "user32" Alias "GetWindowLongA" (ByVal hWnd As Long, ByVal nIndex As Long) As Long
Public OldWindowProc As Long '用来保存系统默认的窗口消息处理函数的地址
Public Sub HookForm(F As Form)
OldWindowProc = GetWindowLong(F.hWnd, GWL_WNDPROC)
Call SetWindowLong(F.hWnd, GWL_WNDPROC, AddressOf NewWindowProc)
End Sub
Public Sub UnHookForm(F As Form)
SetWindowLong F.hWnd, GWL_WNDPROC, OldWindowProc
End Sub
'自蚂姿定义的消息旁物庆处理函数
Public Function NewWindowProc(ByVal hWnd As Long, ByVal Msg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
On Error Resume Next
If Msg = WM_MOVE Then
If Screen.ActiveForm.Top < 0 Then Screen.ActiveForm.Top = 0
If Screen.ActiveForm.Left < 0 Then Screen.ActiveForm.Left = 0
If Screen.ActiveForm.Top + Screen.ActiveForm.Height > Screen.Height Then Screen.ActiveForm.Top = Screen.Height - Screen.ActiveForm.Height
If Screen.ActiveForm.Left + Screen.ActiveForm.Width > Screen.Width Then Screen.ActiveForm.Left = Screen.Width - Screen.ActiveForm.Width
Else
'调用默认窗口消息处理函数
NewWindowProc = CallWindowProc(OldWindowProc, hWnd, Msg, wParam, lParam)
End If
End Function
'在运握窗体里
Private Sub Form_Load()
HookForm Me
End Sub
Private Sub Form_Unload(Cancel As Integer)
UnHookForm Me
End Sub
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
判断窗口的 top. left. 属性
如果窗口的 x<0 就是上边超出屏幕, y<0就是在左边超出屏幕,me.x+me.width>screen.width就是右边超出屏幕,me.y + me.height > screen.height就是在下边超出屏幕。
对应的,如果上边超出销橘陆了,就让 me.x =0;左边超出了,就让 me.y=0;右边伍芹超出了,就亏顷让 me.x=screen.width - me.width ;下边超出了,就让 me.y = screen.height - me.height
如果窗口的 x<0 就是上边超出屏幕, y<0就是在左边超出屏幕,me.x+me.width>screen.width就是右边超出屏幕,me.y + me.height > screen.height就是在下边超出屏幕。
对应的,如果上边超出销橘陆了,就让 me.x =0;左边超出了,就让 me.y=0;右边伍芹超出了,就亏顷让 me.x=screen.width - me.width ;下边超出了,就让 me.y = screen.height - me.height
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询