当前位置:编程学习 > VB >>

vb的热键问题

就是我们班级用幻灯片教学

然后还用到一个抽取全班名字的小程序(老师提问用滴),

然后想在幻灯片播放时侯按下热键HOME或别的热键.后出现这小程序

就是图层1是小程序   图层2是幻灯片全屏

问一下用到什么方法  或者是源码

 

悬赏我都拼了血本了  求求了

 

追问:可以弄源代码  我研究研究 

答案:这个在powerpoint里是不好弄的,其实可以用vb写,完全可以和powerpoint结合起来,我简单写了一下,你新建一个vb程序,在窗体上放一个label,一个timer和一个按钮,timer的interval设置为1,enable设置为false

写入以下代码:

Dim a(0 To 9) As String

Private Sub Command1_Click()
    Timer1.Enabled = Not Timer1.Enabled
End Sub

Private Sub Form_Load()
    SetHotkey Me.hwnd, 1, 0, vbKeyHome, "Add"
    a(0) = "张三"
    a(1) = "李四"
    a(2) = "王五"
    a(3) = "刘六"
    a(4) = "牛七"
    a(5) = "马八"
    a(6) = "杨九"
    a(7) = "苟十"
    a(8) = "朱十一"
    a(9) = "吕十二"
End Sub


Private Sub Form_Unload(Cancel As Integer)
    SetHotkey Me.hwnd, 1, 0, 0, "Del"
End Sub

Private Sub Timer1_Timer()
    Dim i As Integer
    i = Int(Rnd * 10)
    Label1.Caption = a(i)
    Label1.Tag = i
End Sub
新建一个模块,写入以下代码


Private Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
Private Declare Function GetWindowLong Lib "user32" Alias "GetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long) As Long
Private Declare Function CallWindowProc Lib "user32" Alias "CallWindowProcA" (ByVal lpPrevWndFunc As Long, ByVal hwnd As Long, ByVal Msg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
Private Declare Function RegisterHotKey Lib "user32" (ByVal hwnd As Long, ByVal id As Long, ByVal fskey_Modifiers As Long, ByVal vk As Long) As Long
Private Declare Function UnregisterHotKey Lib "user32" (ByVal hwnd As Long, ByVal id As Long) As Long
Public Declare Function ShowWindow Lib "user32" (ByVal hwnd As Long, ByVal nCmdShow As Long) As Long
Public Declare Sub keybd_event Lib "user32" (ByVal bVk As Byte, ByVal Scan As Byte, ByVal dwFlags As Long, ByVal dwExtraInfo As Long)
Public Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
Public Declare Function SetForegroundWindow Lib "user32" (ByVal hwnd As Long) As Long


Const SW_HIDE = 0
Const SW_SHOW = 5
Const WM_HOTKEY = &H312
Const MOD_ALT = &H1
Const MOD_CONTROL = &H2
Const MOD_SHIFT = &H4
Const GWL_WNDPROC = (-4) '窗口函数的地址
Dim FormIsShowing As Boolean
Dim key_preWinProc As Long '用来保存窗口信息
Dim key_IsWinAddress As Boolean '是否取得窗口信息的判断

 

 

Function keyWndproc(ByVal hwnd As Long, ByVal Msg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long

    If Msg = WM_HOTKEY Then
        Select Case wParam 'wParam 值就是 key_idHotKey
            Case 1 '激活 多 个热键后,多 个热键所对应的操作,大家在其他的程序中,只要修改此处就可以了
               FormIsShowing = Not FormIsShowing
               If FormIsShowing Then
                    ShowWindow hwnd, SW_HIDE
               Else
                    ShowWindow hwnd, SW_SHOW
                    'SendMessage hwnd, &H32, 0, 0
                    SetForegroundWindow hwnd
               End If
            'Case 2
               'MsgBox "hide"
               'ShowWindow hwnd, SW_HIDE
        End Select
    End If
   
    ' 将消息传送给指定的窗口
    keyWndproc = CallWindowProc(key_preWinProc, hwnd, Msg, wParam, lParam)

End Function

Function SetHotkey(ByVal hwnd As Long, ByVal KeyId As Long, ByVal keyControlKey As Long, ByVal keyNormalKey As Long, ByVal Action As String)
    If key_IsWinAddress = False Then '判断是否需要取得窗口信息,如果重复取得,再最后恢复窗口时,将会造成程序死掉
    ' 记录原来的window程序地址
        key_preWinProc = GetWindowLong(hwnd, GWL_WNDPROC)
        ' 用自定义程序代替原来的window程序
        SetWindowLong hwnd, GWL_WNDPROC, AddressOf keyWndproc
    End If
    Select Case Action
    Case "Add"
        RegisterHotKey hwnd, KeyId, keyControlKey, keyNormalKey '向窗口注册系统热键
        key_IsWinAddress = True '不需要再取得窗口信息
    Case "Del"
        SetWindowLong hwnd, GWL_WNDPROC, key_preWinProc '恢复窗口信息
        UnregisterHotKey hwnd, KeyId '取消系统热键
        key_IsWinAddress = False '可以再次取得窗口信息
    End Select
End Function

 

这样就成了,主要的思路是注册系统热键

第一个是用Rnd 函数随机一个数,每个数对应一个同学,很简单
第二个是调用了另外一个个程序

有三种方法 :) ShellExecute函数 WinExec函数 Shell函数 1.ShellExecute VB声明 Declare Function ShellExecute Lib “shell32.dll“ Alias “ShellExecuteA“ (ByVal hwnd As Long, ByVal lpOperation As String, ByVal lpFile As String, ByVal lpParameters As String, ByVal lpDirectory As String, ByVal nShowCmd As Long) As Long 说明 查找与指定文件关联在一起的程序的文件名 返回值 Long,非零表示成功,零表示失败。会设置GetLastError [参数表] 参数 类型及说明 hwnd Long,指定一个窗口的句柄,有时候,windows程序有必要在创建自己的主窗口前显示一个消息框 lpOperation String,指定字串“open”来打开lpFlie文档,或指定“Print”来打印它 lpFile String,想用关联程序打印或打开一个程序名或文件名 lpParameters String,如lpszFlie是可执行文件,则这个字串包含传递给执行程序的参数 lpDirectory String,想使用的完整路径 nShowCmd Long,定义了如何显示启动程序的常数值。参考ShowWindow函数的nCmdShow参数 2.WinExec VB声明 Declare Function WinExec Lib “kernel32“ Alias “WinExec“ (ByVal lpCmdLine As String, ByVal nCmdShow As Long) As Long 说明 运行指定的程序 返回值 Long,大于32表示成功,请参考FindExecutable函数 [参数表] 参数 类型及说明 lpCmdLine String,包含要执行的命令行 nCmdShow Long,定义了以怎样的形式启动程序的常数值。参考ShowWindow函数的nCmdShow参数 注解 请参考对CreateProcess函数的说明,了解在目录中查找指定文件的顺序 3.Shell函数 执行一个可执行文件,返回一个 Variant (Double),如果成功的话,代表这个程序的任务 ID,若不成功,则会返回 0。 语法 Shell(pathname[,windowstyle]) Shell 函数的语法含有下面这些命名参数: 部分 描述 pathname 必要参数。Variant (String),要执行的程序名,以及任何必需的参数或命令行变量,可能还包括目录或文件夹,以及驱动器。 Windowstyle 可选参数。Variant (Integer),表示在程序运行时窗口的样式。如果 windowstyle 省略,则程序是以具有焦点的最小化窗口来执行的。 windowstyle 命名参数有以下这些值: 常

上一个:VB写恶搞程序
下一个:VB拦截message消息

CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,