vb.net把鼠标固定 vb鼠标停留显示提示信息

VB.NET怎么可以限制窗体不能被鼠标随便拖动位置很简单vb.net把鼠标固定 , 通过WindowsAPIvb.net把鼠标固定,删除窗体菜单项就行了
首先在窗体类中声明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 lngHwnd0 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 lngHwnd0 Then
lngHmenu = GetSystemMenu(lngHwnd, True)
RemoveMenu(lngHmenu, 0, MF_BYPOSITION) '这里的0与禁止代码中的数值同步vb.net把鼠标固定 , 原因时虽然表面上删除了菜单项,实则为隐藏了菜单项,各个菜单的索引值并没有变 , 所以0依然代表初始菜单的第一个菜单项,即被删除的那个菜单项
End If
End Sub
然后如果vb.net把鼠标固定你的窗口菜单是动态变化的,建议声明常数:
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

推荐阅读