10月23日新股:弹出光驱快捷键怎么编程
VB--如何弹出和关闭光驱托盘? [ 日期:2004-10-28 ] [ 来自:本站原创 ]Option Explicit
Private Declare Function mciSendString Lib "winmm.dll" Alias "mciSendStringA" (ByVal lpstrCommand As String, ByVal lpstrReturnString As String, ByVal uReturnLength As Long, ByVal hwndCallback As Long) As Long
Private Sub Command1_Click()
mciExecute "set cdaudio door open" '弹出光驱
Label2.Caption = "弹 出"
End Sub
Private Sub Command2_Click()
Label2.Caption = "关 闭"
mciExecute "set cdaudio door closed" '合上光驱
Unload Me
End
End Sub
用API函数定义热键一法
定义热键的方法,在此把心得写出来,请各位网友指正!
三个API函数是必须的:
RegisterHotKey —— 用于定义热键
UnregisterHotKey —— 用于注销热键
PeekMessage —— 用于检测热键(尚有其它的主要用途)
这里举个实例:程序启动后窗体不可见,按下 Ctrl+F12 窗体弹出来。本例仅需要一个窗体,在 PWin98、VB6.0企业版下调试通过。
\'申明 API 函数
Private Declare Function RegisterHotKey Lib "user32" (ByVal hWnd As Long, ByVal id As Long, ByVal fsModifiers As Long, ByVal vk As Long) As Long
Private Declare Function UnregisterHotKey Lib "user32" (ByVal hWnd As Long, ByVal id As Long) As Long
Private Declare Function PeekMessage Lib "user32" Alias "PeekMessageA" (lpMsg As Msg, ByVal hWnd As Long, ByVal wMsgFilterMin As Long, ByVal wMsgFilterMax As Long, ByVal wRemoveMsg As Long) As Long
Private bCancel As Boolean \'用于热键有效与否的变量
\'全局变量
Private Const MOD_ALT = &H1
Private Const MOD_CONTROL = &H2
Private Const MOD_SHIFT = &H4
Private Const PM_REMOVE = &H1
Private Const WM_HOTKEY = &H312
Private Type Msg \'定义Message类型
hWnd As Long
Message As Long
wParam As Long
lParam As Long
time As Long
End Type
Private Sub proMsg() \'声明进程
Dim Message As Msg
\' 循环直到 bCancel 为真
Do While Not bCancel
\'检测到热键被按下则令窗体隐藏
If PeekMessage(Message, Me.hWnd, WM_HOTKEY, WM_HOTKEY, PM_REMOVE) Then
Me.Visible = True
End If
DoEvents
Loop
End Sub
Private Sub Form_Load() \'窗体加载时初始化程序
Dim ret As Long
bCancel = False
\'定义热键为Ctrl+F12
ret = RegisterHotKey(Me.hWnd, &HBFFF&, MOD_CONTROL, vbKeyF12)
Me.Visible = False \'窗体不可见
proMsg
End Sub
\'退出时注销热键
Private Sub Form_Unload(Cancel As Integer)
bCancel = True
Call UnregisterHotKey(Me.hWnd, &HBFFF&)
End Sub
tjpanda88 [2003-1-20 14:39:00]
这个方法的确可以,但是太耗系统资源了,有没有更好的方法,我可以要让这个程序一直运行着。
tygh2001 [2003-1-20 16:32:00]
呵呵,也可以搞一个Timer控件,时时检测了
Dailey.Ding [2003-1-20 16:34:00]
不可能的.利用时间控件会存在盲区的
小心 [2003-1-20 16:43:00]
那就是API更好喽
fishkids [2003-1-20 18:24:00]
说实话,我现在不太喜欢用Timer控件,正在寻求更好的方法
xi [2003-1-20 18:43:00]
\'你可以用这个来检测键.
\'连WIN键也可以获得,鼠标也可以!
for i=0 to 500
if getasynckeystate(i)<0 then print I
next
尾巴的狐狸 [2003-1-20 19:00:00]
我觉得2楼的方法不错啊,这个方法很占用系统资源么?不会吧?
tjpanda88 [2003-1-21 13:11:00]
8楼朋友的方法我已经试过了,这种方法放在timer控件里的效果的确不错,可是我现在要让程序识别一个组合键。
2楼朋友的方法我也试过,的确可以识别组合键,可是同时运行别的程序速度就明显的慢了。
如果那位大侠有更好的方法,希望指教!
不知道
但你可以用超级兔子
有这么麻烦吗?
include winmm.inc
includelib winmm.lib
.DATA
szOutCdrom db "Set cdaudio door open wait",0
szInCdrom db "Set cdaudio door closed wait",0
.CODE
//弹出
invoke mciSendString,addr szOutCdrom,NULL,0,hWnd
//关闭
invoke mciSendString,addr szInCdrom,NULL,0,hWnd
使用别的语言应该是这样的方法:
调用winmm中的一个函数:mciSendString,第一个参数分别是:"Set cdaudio door open wait"或者"Set cdaudio door closed wait",第二个是null,第三个是0,最后一个是hwnd。
//我手头没有什么编程环境,只有masm格式是以前写的正确的。