vb.net锁定鼠标位置 vb锁定窗口大小

vb做的程序怎么锁定键盘鼠标

如果让我做,我会按如下方法

创新互联秉承实现全网价值营销的理念,以专业定制企业官网,网站设计、网站制作,微信平台小程序开发,网页设计制作,手机网站开发,全网整合营销推广帮助传统企业实现“互联网+”转型升级专业定制企业官网,公司注重人才、技术和管理,汇聚了一批优秀的互联网技术人才,对客户都以感恩的心态奉献自己的专业和所长。

在窗体的MouseMove事件里做如下功能判断

1、用GetCursorPos得到当前鼠标的位置(X,Y)

2、用GetWindowRect得到窗体的位置(Left,Top,Right,Bottom)

3、判断当前的鼠标位置是否在窗体中(X=Left

and

X=Right

and

Y=Top

and

Y=Bottom)

这里有一个关键的地方,当鼠标在窗体外时,如何触发MouseMove事件,你要用到SetCapture和ReleaseCapture.

当然你也可以用timer_time事件循环判断了。不过我不建议你用timer事件

VB.NET怎么可以限制窗体不能被鼠标随便拖动位置

很简单,通过WindowsAPI,删除窗体菜单项就行了

首先在窗体类中声明API:

Declare Function GetSystemMenu Lib "user32" (ByVal hWnd As IntPtr, ByVal bRevert As Boolean) As IntPtr

Declare Function RemoveMenu Lib "user32" (ByVal lngHmenu As IntPtr, ByVal nPosition As Integer, ByVal wFlags As Integer) As Integer

其次声明API参数常量:

Const MF_BYPOSITION As Integer = H400

然后在窗体类中写入过程:

Private Sub UserForm_Initialize_stopmove() '禁止窗体移动

  Dim lngHwnd As IntPtr

  Dim lngHmenu As IntPtr

  lngHwnd = Me.Handle

  If lngHwnd 0 Then

      lngHmenu = GetSystemMenu(lngHwnd, False)

      RemoveMenu(lngHmenu, 0, MF_BYPOSITION) '这里的0代表菜单中从上往下数第一个菜单项

  End If

End Sub

Private Sub UserForm_Initialize_onmove() '恢复窗体移动

  Dim lngHwnd As IntPtr

  Dim lngHmenu As IntPtr

  lngHwnd = Me.Handle

  If lngHwnd 0 Then

      lngHmenu = GetSystemMenu(lngHwnd, True)

RemoveMenu(lngHmenu, 0, MF_BYPOSITION) '这里的0与禁止代码中的数值同步,原因时虽然表面上删除了菜单项,实则为隐藏了菜单项,各个菜单的索引值并没有变,所以0依然代表初始菜单的第一个菜单项,即被删除的那个菜单项

  End If

End Sub

然后如果你的窗口菜单是动态变化的,建议声明常数:

Const MF_BYPOSITION As Integer = H0

然后使用相关的Windows功能的常数进行删除菜单。常数需要自行查看winuser.h头文件

如果找不到该头文件,可以看这里:网页链接

其余信息详见MSDN:网页链接

最后说一下,不建议前面网友说的重写WndProc的方法,因为这样拦截标题栏点击消息会导致窗体本身的菜单也无法显示出来,有损窗体功能,并且像双击左上角图标关闭窗体这样的功能也会跟着拦截消息的操作一起被吞掉。

VB.NET改变鼠标位置和让鼠标消失

几乎找不到真正VB.NET的api,都是自己将VB的api改成VB.NET的api,我的理解是声明中对应输入是H的地方保留Long,其他的Long改成Integer,既然VB.NET连MSDN都找不到较全的api说明,不保证api的用法在VB.NET其他版本中适用。

给你个好玩的例子,包括鼠标的隐藏(Ctrl_↓)、显示(Ctrl_↑),移动、点击

在Form1上放上2个TextBox,其中TextBox1.Multiline = True,并且面积大些,2个Button,运行后在TextBox1中点击一下会记录位置,移开鼠标,点击移动或点击Button,可模拟在TextBox1中的点击

Public Class Form1

Public My_MousePosition As Point

'模拟鼠标移动、点击

Declare Sub mouse_event Lib "user32" (ByVal dwFlags As Long, ByVal dx As Integer, ByVal dy As Integer, ByVal cButtons As Long, ByVal dwExtraInfo As Long)

'定义光标的位置

Declare Function SetCursorPos Lib "user32" Alias "SetCursorPos" (ByVal x As Integer, ByVal y As Integer) As Integer

'鼠标的隐藏

Declare Function ShowCursor Lib "user32" Alias "ShowCursor" (ByVal bShow As Integer) As Integer

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

TextBox1.Multiline = True

Button1.Text = "点击"

Button2.Text = "移动"

Me.KeyPreview = True

End Sub

Private Sub Form1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown

If e.Control And e.KeyCode = Keys.Down Then ShowCursor(0)

If e.Control And e.KeyCode = Keys.Up Then ShowCursor(1)

End Sub

Private Sub Form1_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseMove

TextBox2.Text = MousePosition.X " " MousePosition.Y

End Sub

Private Sub TextBox1_MouseClick(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles TextBox1.MouseClick

My_MousePosition = MousePosition

TextBox1.Text = My_MousePosition.X " " My_MousePosition.Y vbCrLf

End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

SetCursorPos(My_MousePosition.X, My_MousePosition.Y)

mouse_event(H2, 0, 0, 0, 0)

mouse_event(H4, 0, 0, 0, 0)

End Sub

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click

SetCursorPos(My_MousePosition.X, My_MousePosition.Y)

End Sub

End Class

其中

mouse_event Lib "user32" (ByVal dwFlags As Long

的dwFlags的代码是

Const MOUSEEVENTF_LEFTDOWN = H2

Const MOUSEEVENTF_LEFTUP = H4

Const MOUSEEVENTF_MIDDLEDOWN = H20

Const MOUSEEVENTF_MIDDLEUP = H40

Const MOUSEEVENTF_MOVE = H1

Const MOUSEEVENTF_ABSOLUTE = H8000

Const MOUSEEVENTF_RIGHTDOWN = H8

Const MOUSEEVENTF_RIGHTUP = H10


当前名称:vb.net锁定鼠标位置 vb锁定窗口大小
分享地址:http://myzitong.com/article/doepoog.html