VB使用API的例子

 1. 如何消除textbox中按下回车时的beep声?
Private Sub Text1_KeyPress(KeyAscii As Integer)
If KeyAscii = 13 Then
KeyAscii = 0
End If
End Sub
*****************************************************************************
2.Textbox获得焦点时自动选中。
Private Sub Text1_GotFocus()
Text1.SelStart = 0
Text1.SelLength = Len(Text1.Text)
End Sub
*****************************************************************************
3.屏蔽textbox控件自身的右键菜单,并显示自己的菜单。
方法一:
Private Sub Text1_MouseDown(Button As Integer, Shift As Integer, X As Single, Y _
As Single)
If Button = 2 Then
Text1.Enabled = False
Text1.Enabled = True
PopupMenu mymenu
End If
End Sub

方法二:回调函数
module:
Option Explicit
Public OldWindowProc As Long ' 保存默认的窗口函数的地址
Public Const WM_CONTEXTMENU = &H7B ' 当右击文本框时,产生这条消息
Public Declare Function GetWindowLong Lib "user32" Alias "GetWindowLongA" (ByVal hWnd _
As Long, ByVal nIndex As Long) As Long
Public Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hWnd _ As Long, ByVal nIndex As Long, ByVal dwNewLong 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
Public Function SubClass_WndMessage(ByVal hWnd As Long, ByVal Msg As Long, ByVal wp _
As Long, ByVal lp As Long) As Long
' 如果消息不是WM_CONTEXTMENU,就调用默认的窗口函数处理
If Msg <> WM_CONTEXTMENU Then
SubClass_WndMessage = CallWindowProc(OldWindowProc, hWnd, Msg, wp, lp)
Exit Function
End If
SubClass_WndMessage = True
End Function
窗体中:
Private Const GWL_WNDPROC = (-4)
Private Sub Text1_MouseDown(Button As Integer, Shift As Integer, X As Single, Y _
As Single)
If Button = 1 Then Exit Sub
OldWindowProc = GetWindowLong(Text1.hWnd, GWL_WNDPROC) ' 取得窗口函数的地址
' 用SubClass_WndMessage代替窗口函数处理消息
Call SetWindowLong(Text1.hWnd, GWL_WNDPROC, AddressOf SubClass_WndMessage)
End Sub
Private Sub Text1_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single)
If Button = 1 Then Exit Sub
' 恢复窗口的默认函数
Call SetWindowLong(Text1.hWnd, GWL_WNDPROC, OldWindowProc)
PopupMenu mymenu
End Sub
*****************************************************************************
4. 设置TEXTBOX为只读属性
Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd _
As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
Private Declare Function GetWindowLong Lib "user32" Alias "GetWindowLongA" (ByVal hwnd _ As Long, ByVal nIndex As Long) As Long
Private Const GWL_STYLE = (-16)
Private Const EM_SETREADONLY = &HCF
Private Sub Command1_Click()
Dim l As Long
If (GetWindowLong(Text1.hwnd, GWL_STYLE) And &H800) Then
Text1.Text = "This is a read/write text box." '文本窗口是只读窗口,设置为可读写窗口
l = SendMessage(Text1.hwnd, EM_SETREADONLY, False, vbNull)
Text1.BackColor = RGB(255, 255, 255) '将背景设置为白色
Command1.Caption = "Read&Write"
Else
Text1.Text = "This is a readonly text box." '文本窗口是可读写窗口,设置为只读窗口
l = SendMessage(Text1.hwnd, EM_SETREADONLY, True, vbNull)
Text1.BackColor = vbInactiveBorder '将背景设置为灰色
Command1.Caption = "&ReadOnly"
End If
End Sub
*****************************************************************************
5. 利用API函数MessageBox代替MSGBOX函数可以使得Timer控件正常工作

Private Declare Function MessageBox Lib "user32" Alias "MessageBoxA" (ByVal hwnd As _ Long, ByVal lpText As String, ByVal lpCaption As String, ByVal wType As Long) As Long
Private Sub Command1_Click()
MsgBox "时钟变的无效了"
End Sub
Private Sub Command2_Click()
MessageBox Me.hwnd, "时钟正常运行", "hehe", 0
End Sub
Private Sub Timer1_Timer()
Static i As Integer
i = i + 1
Text1.Text = i
End Sub
*****************************************************************************
6. 窗口置顶
Private Declare Function SetWindowPos Lib "user32" (ByVal hwnd As Long, ByVal _ hWndInsertAfter As Long, ByVal X As Long, ByVal Y As Long, ByVal Cx As Long, ByVal Cy _
As Long, ByVal wFlags As Long) As Long
Public Sub SetOnTop(ByVal IsOnTop As Integer)
Dim rtn As Long
If IsOnTop = 1 Then
rtn = SetWindowPos(Form1.hwnd, -1, 0, 0, 0, 0, 3)
Else
rtn = SetWindowPos(Form1.hwnd, -2, 0, 0, 0, 0, 3)
End If
End Sub
Private Sub Command1_Click()
SetOnTop 1 '将窗口置于最上面
End Sub
Private Sub Command2_Click()
SetOnTop 0
End Sub
*****************************************************************************
7.只容许运行一个程序实例(利用互斥体)

选择启动对象为sub main()
module:
Public Declare Function CreateMutex Lib "kernel32" Alias "CreateMutexA" _ (lpMutexAttributes As SECURITY_ATTRIBUTES, ByVal bInitialOwner As Long, ByVal lpName _
As String) As Long
Public Type SECURITY_ATTRIBUTES
nLength As Long
lpSecurityDescriptor As Long
bInheritHandle As Long
End Type
Public Const ERROR_ALREADY_EXISTS = 183&
Private Sub Main()
Dim sa As SECURITY_ATTRIBUTES
sa.bInheritHandle = 1
sa.lpSecurityDescriptor = 0
sa.nLength = Len(sa)
Debug.Print CreateMutex(sa, 1, App.Title) '这一行可千万不能删除啊
Debug.Print Err.LastDllError
If (Err.LastDllError = ERROR_ALREADY_EXISTS) Then
MsgBox "More than one instance"
Else
Form1.Show
End If
End Sub
*****************************************************************************
8.窗体标题栏闪烁
Option Explicit
Private Declare Function FlashWindow Lib "user32" (ByVal hwnd As Long, ByVal bInvert _
As Long) As Long
Private Sub tmrFlash_Timer()
Static mFlash As Boolean
FlashWindow hwnd, Not mFlash
End Sub
*****************************************************************************
8. 拷屏

方法一:利用模拟键盘
Private Declare Sub keybd_event Lib "user32" (ByVal bVk As Byte, ByVal bScan As Byte, _ ByVal dwFlags As Long, ByVal dwExtraInfo As Long)
Const theScreen = 1
Const theForm = 0
Private Sub Command1_Click()
Call keybd_event(vbKeySnapshot, theForm, 0, 0) '若theForm改成theScreen则Copy整个Screen
DoEvents
Picture1.Picture = Clipboard.GetData(vbCFBitmap)
End Sub
*****************************************************************************
9. 为程序注册热键

方法一:修改注册表
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 Declare Function WaitMessage Lib "user32" () As Long
Private Type POINTAPI
x As Long
y As Long
End Type
Private Type Msg
hWnd As Long
Message As Long
wParam As Long
lParam As Long
time As Long
pt As POINTAPI
End Type
' 声明常数
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 HotKey_Fg As Boolean
Private Sub Form_Load()
Dim Message As Msg
'注册 Ctrl+Y 为热键
RegisterHotKey Me.hWnd, &HBFFF&, MOD_CONTROL, vbKeyY
'RegisterHotKey Me.hWnd, &HBFF2&, MOD_CONTROL, vbKeyU
Me.Show
Form1.Hide
'等待处理消息
HotKey_Fg = False
Do While Not HotKey_Fg
'等待消息
WaitMessage
'检查是否热键被按下
If PeekMessage(Message, Me.hWnd, WM_HOTKEY, WM_HOTKEY, PM_REMOVE) Then
Form1.Show 1
End If
'转让控制权,允许操作系统处理其他事件
DoEvents
Loop
End Sub
Private Sub Form_Unload(Cancel As Integer)
HotKey_Fg = True
'撤销热键的注册
Call UnregisterHotKey(Me.hWnd, &HBFFF&)
End Sub

方法二:SendMessage
Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As _ Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
Private Const WM_SETHOTKEY = &H32
Private Const HOTKEYF_SHIFT = &H1
Private Const HOTKEYF_ALT = &H4
Private Sub Form_Load()
Dim l As Long
Dim wHotkey As Long
wHotkey = (HOTKEYF_ALT) * (2 ^ 8) + 65 '定义ALT+A为热键
l = SendMessage(Me.hwnd, WM_SETHOTKEY, wHotkey, 0)
End Sub
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值