学习啦>学习电脑>电脑硬件知识>键盘鼠标>

VB怎么更改鼠标指针样式

时间: 本达868 分享

  VB设置鼠标的指针样式,可设置窗口中的鼠标样式和系统中的鼠标指针样式,通过操作窗口中的菜单,你就可以很方便的改变鼠标样式,下面是学习啦小编给大家整理的一些相关解决方法步骤,希望对大家有帮助!

  VB怎么更改鼠标指针样式

  这是窗体代码:

  01VERSION 5.00

  02Object = "{F9043C88-F6F2-101A-A3C9-08002B2F49FB}#1.2#0"; "Comdlg32.ocx"

  03Begin VB.Form Form1

  04 Caption = "鼠标样式设置器"

  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 = "设置鼠标样式"

  22 Begin VB.Menu SetSysMouse

  23 Caption = "设置系统鼠标样式"

  24 End

  25 Begin VB.Menu BackSysMouse

  26 Caption = "恢复系统鼠标样式"

  27 End

  28 Begin VB.Menu SetFormMouse

  29 Caption = "设置窗体鼠标样式"

  30 End

  31 Begin VB.Menu BackFormMouse

  32 Caption = "恢复窗体鼠标样式"

  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 '标准

  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() '恢复窗体鼠标的样式

  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() '恢复系统鼠标的样式

  76 SystemParametersInfo SPI_SETCURSORS, 0, 0, SPIF_SENDWININICHANGE

  77End Sub

  78Private Sub SetFormMouse_Click() '设置窗体的鼠标样式

  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() '设置系统鼠标样式

  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

  程序运行截图如下图所示:

  相关拓展:vb如何控制鼠标

  主要是要通过两个方面:

  (一)对鼠标的停留位置做出判断,也就是得到鼠标在屏幕上停留的位置。

  (二)将鼠标的移动到所确定的位置上。 而实现这个功能则要使用到SetCursorPos这个函数,此函数的功能是设定鼠标位置。

  这个函数的声明如下:

  Public Declare Function SetCursorPos Lib "user32" (ByVal x As Long, ByVal y As Long) As Long

  而仅仅有这个函数是不够的,还需要定义一个type格式的自定义变量。定义为:

  Public Type POINTAPI

  x As Long

  y As Long

  End Type

  它用于存放鼠标的位置(屏幕上的位置)。

  但是一个新的问题又出现了:鼠标到底放在哪里呢?也就是如何获得屏幕上的位置。

  这个问题就要用到另一个函数:GetCursorPos,它的功能是获得屏幕上鼠标的坐标。

  它的声明如下:

  Public Declare Function GetCursorPos Lib "user32" (lpPoint As POINTAPI) As Long

  这样就可以通过GetCursorPos函数获得鼠标的位置,存放到一个POINTAPI变量中,再通过SetCursorPos函数来设置鼠标的位置。

  这样就可以十分顺利的来控制鼠标了!

1357862