使用Win95的动画光标

来源:互联网 发布:mac怎样取消开机密码 编辑:程序博客网 时间:2024/06/06 01:27
<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 728x15, 创建于 08-4-23MSDN */google_ad_slot = "3624277373";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 160x600, 创建于 08-4-23MSDN */google_ad_slot = "4367022601";google_ad_width = 160;google_ad_height = 600;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>

-在Windows 95系统中具有一个Windows 3.x所不具备的特性――支持动画的光标文件。你可以在Windows 95目录中的Cursors子目录下看到这些动画的光标文件,它们均具有扩展名*.ANI。在程序中使用相应的动画光标能够极大地改善程序的外观,本文介绍了如何在你的Visual Basic应用程序中使用Windows 95所附带的动画光标文件。

使用动画光标文件

----要在Visual Basic的应用程序中使用Windows 95所附带的动画光标,你需要使用下列Windows 应用程序编程接口(API)函数:

  • LoadCursorFormFile,用于从磁盘上载入光标文件;

  • ClipeCursor,用于将光标限制在一个固定的矩形区域内;

  • GetWindowRect,用于获取该矩形区域,在下面的样例程序中就是程序主窗体的本身;

  • SetClassLong,用于设置和提取窗口类的数据,以使光标被显示在窗体上;

  • GetClassLong函数,在退出应用程序之前,需要将应用程序的缺省光标设置回程序执行以前的光标,所以需要在程序运行时首先对以前的光标状态进行备份,这项工作由该函数来完成;

  • DestroyCursor,在正确显示了光标之后,需要使用该函数来取消载入的光标。

样例程序

----下面的样例程序将在窗体区域内显示出C:Win95CURSORS目录下的APPSTART.ANI动画光标文件,如果你的Windows 95路径不同的话,你需要修改样例程序以正确显示出动画光标

  1. 在Visual Basic中开始一个新的工程,采用缺省的方法建立Form1。

  2. 在Form1上创建一个命令按钮控件,采用缺省的方法建立Command1。将它的Caption属性设置为“显示动画光标”。

  3. 在Form1上创建第二个命令按钮控件,采用缺省的方法建立Command2。将它的Caption属性设置为“恢复缺省光标”。

  4. 创建一个新的模块,采用缺省的方法建立Module1.Bas。将如下的声明,类型和常量语句添加到Module1.Bas的通用声明部分中:


    Option Explicit
    Type RECT
            Left As Long
            Top As Long
            Right As Long
            Bottom As Long
    End Type
    Public Const GCL_HCURSOR = (-12)
    Declare Function ClipCursor Lib "user32" (lpRect As Any) As Long
    Declare Function DestroyCursor Lib "user32" (ByVal hCursor As Long) As Long
    Declare Function LoadCursorFromFile Lib "user32" Alias "LoadCursorFromFileA"
    (ByVal lpFileName As String) As Long
    Declare Function GetWindowRect Lib "user32" (ByVal hwnd As Long,
    lpRect As RECT) As Long
    Declare Function SetClassLong Lib "user32" Alias "SetClassLongA"
    (ByVal hwnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
    Declare Function GetClassLong Lib "user32" Alias "GetClassLongA"
    (ByVal hwnd As Long, ByVal nIndex As Long) As Long

    注意上面的声明语句需要书写在一行内。

  5. 将如下的语句添加到Form1的通用声明部分中:


    Option Explicit
    Dim mhBaseCursor As Long
    Dim mhAniCursor As Long

  6. 将如下的代码添加到Form1的Form_Load事件中:


    Private Sub Form_Load()
        Dim lResult As Long
       
        mhBaseCursor = GetClassLong((Me.hwnd), GCL_HCURSOR)
    End Sub

  7. 将如下的代码添加到Command1的单击事件中:


    Private Sub Command1_Click()
        Dim lResult As Long
        Dim RT_FormArea As RECT
       
        mhAniCursor = LoadCursorFromFile("c:Win95cursorsappstart.ani")
       
        lResult = SetClassLong((Me.hwnd), GCL_HCURSOR, mhAniCursor)
       
        lResult = GetWindowRect((Me.hwnd), RT_FormArea)
       
        lResult = ClipCursor(RT_FormArea)
    End Sub

  8. 将如下的代码添加到Command2的单击事件中:


    Private Sub Command2_Click()
        Dim lResult As Long
        Dim RT_ScreenArea As RECT
       
        RT_ScreenArea.Top = 0
        RT_ScreenArea.Left = 0
        RT_ScreenArea.Bottom = Screen.Height Screen.TwipsPerPixelX
        RT_ScreenArea.Right = Screen.Width Screen.TwipsPerPixelY
        lResult = ClipCursor(RT_ScreenArea)
        lResult = SetClassLong((Me.hwnd), GCL_HCURSOR, mhBaseCursor)
        lResult = DestroyCursor(mhAniCursor)
    End Sub

    ----通过按下F5键来运行该程序,单击“显示动画光标”命令按钮,则在窗体的范围内光标变成为APPSTART.ANI光标,并且光标被限制在窗体的范围内。单击“恢复缺省光标”命令按钮,则窗体中的光标被恢复为缺省的光标。注意,在退出该样例程序之前,需要单击“恢复缺省光标”命令按钮,否则将不能在系统中正确进行其它操作。


<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 728x15, 创建于 08-4-23MSDN */google_ad_slot = "3624277373";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 160x600, 创建于 08-4-23MSDN */google_ad_slot = "4367022601";google_ad_width = 160;google_ad_height = 600;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
原创粉丝点击