VB怎么更改鼠標(biāo)指針樣式
VB設(shè)置鼠標(biāo)的指針樣式,可設(shè)置窗口中的鼠標(biāo)樣式和系統(tǒng)中的鼠標(biāo)指針樣式,通過(guò)操作窗口中的菜單,你就可以很方便的改變鼠標(biāo)樣式,下面是學(xué)習(xí)啦小編給大家整理的一些相關(guān)解決方法步驟,希望對(duì)大家有幫助!
VB怎么更改鼠標(biāo)指針樣式
這是窗體代碼:
01VERSION 5.00
02Object = "{F9043C88-F6F2-101A-A3C9-08002B2F49FB}#1.2#0"; "Comdlg32.ocx"
03Begin VB.Form Form1
04 Caption = "鼠標(biāo)樣式設(shè)置器"
05 ClientHeight = 3210
06 ClientLeft = 165
07 ClientTop = 735
08 ClientWidth = 5490
09 LinkTopic = "Form1"
10 ScaleHeight = 3210
11 ScaleWidth = 5490
12 StartUpPosition = 3 '窗口缺省
13 Begin MSComDlg.CommonDialog CommonDialog1
14 Left = 2640
15 Top = -45
16 _ExtentX = 847
17 _ExtentY = 847
18 _Version = 393216
19 End
20 Begin VB.Menu SetMouse
21 Caption = "設(shè)置鼠標(biāo)樣式"
22 Begin VB.Menu SetSysMouse
23 Caption = "設(shè)置系統(tǒng)鼠標(biāo)樣式"
24 End
25 Begin VB.Menu BackSysMouse
26 Caption = "恢復(fù)系統(tǒng)鼠標(biāo)樣式"
27 End
28 Begin VB.Menu SetFormMouse
29 Caption = "設(shè)置窗體鼠標(biāo)樣式"
30 End
31 Begin VB.Menu BackFormMouse
32 Caption = "恢復(fù)窗體鼠標(biāo)樣式"
33 End
34 End
35End
36Attribute VB_Name = "Form1"
37Attribute VB_GlobalNameSpace = False
38Attribute VB_Creatable = False
39Attribute VB_PredeclaredId = True
40Attribute VB_Exposed = False
41Option Explicit
42Const OCR_NORMAL = 32512
43Const IDC_ARROW = 32512&
44Const SPI_SETCURSORS = 87
45Const SPIF_SENDWININICHANGE = &H2
46Const OCR_NORAAC = 32512 '標(biāo)準(zhǔn)
47Const GCL_HCURSOR = (-12)
48' const OCR_APPSTARTING = 32650 '小的沙漏
49' const OCR_HAND = 32649 '手
50' const OCR_NO = 32648 '圓
51' const OCR_SIZEALL = 32646 '十
52' const OCR_WAIT = 32514 '沙漏
53Private Declare Function LoadCursorFromFile Lib "user32" Alias "LoadCursorFromFileA" (ByVal lpFileName As String)As Long
54Private Declare Function SetSystemCursor Lib "user32" (ByVal hcur As Long, _
55 ByVal id As Long) As Long
56Private Declare Function SystemParametersInfo Lib "user32" Alias "SystemParametersInfoA" (ByVal uAction As Long, _
57 ByVal uParam As Long, _
58 ByRef lpvParam As Any, _
59 ByVal fuWinIni As Long _
60 ) As Long
61Private Declare Function SetClassLong Lib "user32" Alias "SetClassLongA" (ByVal hwnd As Long, _
62 ByVal nIndex As Long, _
63 ByVal dwNewLong As Long _
64 ) As Long
65Private Declare Function GetSystemDirectory Lib "kernel32" Alias "GetSystemDirectoryA" (ByVal lpBuffer As String, _
66 ByVal nSize As Long _
67 ) As Long
68Dim hCursor As Long
69Private Sub BackFormMouse_Click() '恢復(fù)窗體鼠標(biāo)的樣式
70 Dim sPath As String * 260 '定義路徑
71 GetSystemDirectory sPath, Len(sPath)
72 hCursor = LoadCursorFromFile(sPath)
73 SetClassLong Me.hwnd, GCL_HCURSOR, hCursor
74End Sub
75Private Sub BackSysMouse_Click() '恢復(fù)系統(tǒng)鼠標(biāo)的樣式
76 SystemParametersInfo SPI_SETCURSORS, 0, 0, SPIF_SENDWININICHANGE
77End Sub
78Private Sub SetFormMouse_Click() '設(shè)置窗體的鼠標(biāo)樣式
79 CommonDialog1.Filter = "CUR文件(*.cur)|*.cur|ANI文件(*.ani)|*.ani|所有文件|(*.*)"
80 CommonDialog1.ShowOpen
81 CommonDialog1.CancelError = False
82 If CommonDialog1.FileName <> "" Then
83 hCursor = LoadCursorFromFile(CommonDialog1.FileName)
84 SetClassLong Me.hwnd, GCL_HCURSOR, hCursor
85 End If
86End Sub
87Private Sub SetSysMouse_Click() '設(shè)置系統(tǒng)鼠標(biāo)樣式
88 CommonDialog1.Filter = "CUR文件(*.cur)|*.cur|ANI文件(*.ani)|*.ani|所有文件|(*.*)"
89 CommonDialog1.ShowOpen
90 CommonDialog1.CancelError = False
91 If CommonDialog1.FileName <> "" Then
92 hCursor = LoadCursorFromFile(CommonDialog1.FileName)
93 Call SetSystemCursor(hCursor, OCR_NORMAL)
94 End If
95End Sub
程序運(yùn)行截圖如下圖所示:
相關(guān)拓展:vb如何控制鼠標(biāo)
主要是要通過(guò)兩個(gè)方面:
(一)對(duì)鼠標(biāo)的停留位置做出判斷,也就是得到鼠標(biāo)在屏幕上停留的位置。
(二)將鼠標(biāo)的移動(dòng)到所確定的位置上。 而實(shí)現(xiàn)這個(gè)功能則要使用到SetCursorPos這個(gè)函數(shù),此函數(shù)的功能是設(shè)定鼠標(biāo)位置。
這個(gè)函數(shù)的聲明如下:
Public Declare Function SetCursorPos Lib "user32" (ByVal x As Long, ByVal y As Long) As Long
而僅僅有這個(gè)函數(shù)是不夠的,還需要定義一個(gè)type格式的自定義變量。定義為:
Public Type POINTAPI
x As Long
y As Long
End Type
它用于存放鼠標(biāo)的位置(屏幕上的位置)。
但是一個(gè)新的問(wèn)題又出現(xiàn)了:鼠標(biāo)到底放在哪里呢?也就是如何獲得屏幕上的位置。
這個(gè)問(wèn)題就要用到另一個(gè)函數(shù):GetCursorPos,它的功能是獲得屏幕上鼠標(biāo)的坐標(biāo)。
它的聲明如下:
Public Declare Function GetCursorPos Lib "user32" (lpPoint As POINTAPI) As Long
這樣就可以通過(guò)GetCursorPos函數(shù)獲得鼠標(biāo)的位置,存放到一個(gè)POINTAPI變量中,再通過(guò)SetCursorPos函數(shù)來(lái)設(shè)置鼠標(biāo)的位置。
這樣就可以十分順利的來(lái)控制鼠標(biāo)了!