如果讓我做,我會(huì)按如下方法
創(chuàng)新互聯(lián)秉承實(shí)現(xiàn)全網(wǎng)價(jià)值營銷的理念,以專業(yè)定制企業(yè)官網(wǎng),網(wǎng)站設(shè)計(jì)、網(wǎng)站制作,微信平臺(tái)小程序開發(fā),網(wǎng)頁設(shè)計(jì)制作,手機(jī)網(wǎng)站開發(fā),全網(wǎng)整合營銷推廣幫助傳統(tǒng)企業(yè)實(shí)現(xiàn)“互聯(lián)網(wǎng)+”轉(zhuǎn)型升級(jí)專業(yè)定制企業(yè)官網(wǎng),公司注重人才、技術(shù)和管理,匯聚了一批優(yōu)秀的互聯(lián)網(wǎng)技術(shù)人才,對(duì)客戶都以感恩的心態(tài)奉獻(xiàn)自己的專業(yè)和所長。
在窗體的MouseMove事件里做如下功能判斷
1、用GetCursorPos得到當(dāng)前鼠標(biāo)的位置(X,Y)
2、用GetWindowRect得到窗體的位置(Left,Top,Right,Bottom)
3、判斷當(dāng)前的鼠標(biāo)位置是否在窗體中(X=Left
and
X=Right
and
Y=Top
and
Y=Bottom)
這里有一個(gè)關(guān)鍵的地方,當(dāng)鼠標(biāo)在窗體外時(shí),如何觸發(fā)MouseMove事件,你要用到SetCapture和ReleaseCapture.
當(dāng)然你也可以用timer_time事件循環(huán)判斷了。不過我不建議你用timer事件
很簡單,通過WindowsAPI,刪除窗體菜單項(xiàng)就行了
首先在窗體類中聲明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參數(shù)常量:
Const MF_BYPOSITION As Integer = H400
然后在窗體類中寫入過程:
Private Sub UserForm_Initialize_stopmove() '禁止窗體移動(dòng)
? 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代表菜單中從上往下數(shù)第一個(gè)菜單項(xiàng)
? End If
End Sub
Private Sub UserForm_Initialize_onmove() '恢復(fù)窗體移動(dòng)
? 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與禁止代碼中的數(shù)值同步,原因時(shí)雖然表面上刪除了菜單項(xiàng),實(shí)則為隱藏了菜單項(xiàng),各個(gè)菜單的索引值并沒有變,所以0依然代表初始菜單的第一個(gè)菜單項(xiàng),即被刪除的那個(gè)菜單項(xiàng)
? End If
End Sub
然后如果你的窗口菜單是動(dòng)態(tài)變化的,建議聲明常數(shù):
Const MF_BYPOSITION As Integer = H0
然后使用相關(guān)的Windows功能的常數(shù)進(jìn)行刪除菜單。常數(shù)需要自行查看winuser.h頭文件
如果找不到該頭文件,可以看這里:網(wǎng)頁鏈接
其余信息詳見MSDN:網(wǎng)頁鏈接
最后說一下,不建議前面網(wǎng)友說的重寫WndProc的方法,因?yàn)檫@樣攔截標(biāo)題欄點(diǎn)擊消息會(huì)導(dǎo)致窗體本身的菜單也無法顯示出來,有損窗體功能,并且像雙擊左上角圖標(biāo)關(guān)閉窗體這樣的功能也會(huì)跟著攔截消息的操作一起被吞掉。
幾乎找不到真正VB.NET的api,都是自己將VB的api改成VB.NET的api,我的理解是聲明中對(duì)應(yīng)輸入是H的地方保留Long,其他的Long改成Integer,既然VB.NET連MSDN都找不到較全的api說明,不保證api的用法在VB.NET其他版本中適用。
給你個(gè)好玩的例子,包括鼠標(biāo)的隱藏(Ctrl_↓)、顯示(Ctrl_↑),移動(dòng)、點(diǎn)擊
在Form1上放上2個(gè)TextBox,其中TextBox1.Multiline = True,并且面積大些,2個(gè)Button,運(yùn)行后在TextBox1中點(diǎn)擊一下會(huì)記錄位置,移開鼠標(biāo),點(diǎn)擊移動(dòng)或點(diǎn)擊Button,可模擬在TextBox1中的點(diǎn)擊
Public Class Form1
Public My_MousePosition As Point
'模擬鼠標(biāo)移動(dòng)、點(diǎn)擊
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)
'定義光標(biāo)的位置
Declare Function SetCursorPos Lib "user32" Alias "SetCursorPos" (ByVal x As Integer, ByVal y As Integer) As Integer
'鼠標(biāo)的隱藏
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 = "點(diǎn)擊"
Button2.Text = "移動(dòng)"
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