首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > VB >

剪贴板有关问题

2012-08-15 
剪贴板问题想做个用快捷键实现把活动文本窗口的内容进行替换的小程序,问题来了:有时候提示521,剪贴板打不

剪贴板问题
想做个用快捷键实现把活动文本窗口的内容进行替换的小程序,问题来了:
有时候提示521,剪贴板打不开;有时候根本就不替换。
程序中用timer空间,监测是否按下ctrl+w键,如果是:
  1、发送ctrl+A键,全选;
  2、发送ctrl+C键,复制;
  3、读取出剪贴板的内容,进行字符串替换,更新剪贴板内容;
  4、发送ctrl+V键,粘贴到活动窗口。

代码如下:

VB code
Option ExplicitPrivate Declare Function GetAsyncKeyState Lib "user32" (ByVal vKey As Long) As LongPrivate Declare Sub keybd_event Lib "user32" (ByVal bVk As Byte, ByVal bScan As Byte, ByVal dwFlags As Long, ByVal dwExtraInfo As Long)Private Declare Function PostMessage Lib "user32" Alias "PostMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As LongPrivate Declare Function MapVirtualKey Lib "user32" Alias "MapVirtualKeyA" (ByVal wCode As Long, ByVal wMapType As Long) As LongPrivate Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)Const VK_Ctrl = &H11Const KEYEVENTF_EXTENDEDKEY = &H1Const KEYEVENTF_KEYUP = &H2'' Function SelectAll()    keybd_event VK_Ctrl, MapVirtualKey(VK_Ctrl, 0), 0, 0   '按下Ctrl键    keybd_event vbKeyA, MapVirtualKey(VK_Ctrl, 0), 0, 0   '按下Ctrl键    Sleep 100    keybd_event vbKeyA, 0, KEYEVENTF_KEYUP, 0               '松开A键    keybd_event VK_Ctrl, 0, KEYEVENTF_KEYUP, 0              '松开Ctrl键End FunctionFunction Copy()    keybd_event VK_Ctrl, MapVirtualKey(VK_Ctrl, 0), 0, 0   '按下Ctrl键    keybd_event vbKeyC, MapVirtualKey(VK_Ctrl, 0), 0, 0   '按下C键    Sleep 100    keybd_event vbKeyC, 0, KEYEVENTF_KEYUP, 0    keybd_event VK_Ctrl, 0, KEYEVENTF_KEYUP, 0End FunctionFunction Paste()    keybd_event VK_Ctrl, MapVirtualKey(VK_Ctrl, 0), 0, 0   '按下Ctrl键    keybd_event vbKeyV, MapVirtualKey(VK_Ctrl, 0), 0, 0   '按下C键    Sleep 100    keybd_event vbKeyV, 0, KEYEVENTF_KEYUP, 0    keybd_event VK_Ctrl, 0, KEYEVENTF_KEYUP, 0End FunctionFunction StringDeal()    Dim strS As String    Dim Result As String    strS = Clipboard.GetText    DoEvents    Clipboard.Clear    strS = ValueSet(strS, "ORIG_DOMAIN", "UESS")    strS = ValueSet(strS, "ROUTE_TYPE", "00")    strS = ValueSet(strS, "PROCESS_TIME", "20110613104343")    strS = ValueSet(strS, "RSP_TYPE", "0")    strS = ValueSet(strS, "RSP_CODE", "0000")    strS = ValueSet(strS, "OPER_ID", "aiqa0000D")    strS = ValueSet(strS, "PROVINCE_CODE", "74")    strS = ValueSet(strS, "EPARCHY_CODE", "743")    strS = ValueSet(strS, "CITY_CODE", "D00")    strS = ValueSet(strS, "CHANNEL_ID", "D00")    strS = ValueSet(strS, "CHANNEL_TYPE", "1234567")    strS = ValueSet(strS, "ACCESS_TYPE", "00")    strS = ValueSet(strS, "ORDER_TYPE", "00")    strS = ValueSet(strS, "CUTOFFDAY", "00000000")    strS = ValueSet(strS, "OSNDUNS", "0000")    strS = ValueSet(strS, "HSNDUNS", "0000")    strS = ValueSet(strS, "MSG_SENDER", "0000")    strS = ValueSet(strS, "MSG_RECEIVER", "0000")            Clipboard.SetText strSEnd FunctionFunction ValueSet(TxtSource As String, NoteName As String, Value As String) As String    Dim nStart As Long, nEnd As Long    nStart = InStr(TxtSource, "<unib1:" + NoteName)    ValueSet = TxtSource        If nStart = 0 Then Exit Function    nEnd = InStr(TxtSource, "</unib1:" + NoteName)    If nEnd = 0 Then Exit Function        Dim stmp As String    stmp = Mid(TxtSource, nStart, nEnd - nStart)'    Debug.Print stmp    TxtSource = Replace(TxtSource, stmp, "</unib1:" + NoteName + ">" + Value)    ValueSet = TxtSource    Debug.Print TxtSourceEnd FunctionPrivate Sub Timer1_Timer()    If GetAsyncKeyState(vbKeyControl) And GetAsyncKeyState(vbKeyW) Then        Timer1.Enabled = False        Debug.Print "----in---"        SelectAll        Copy        StringDeal        Paste        Timer1.Enabled = TrueEnd IfEnd Sub 



[解决办法]
应该监视剪贴板,剪贴板有指定内容就粘贴,否则就是不需要的内容。
[解决办法]
估计自己解决了.

PS:

按键的监视,还是用全局键盘HOOK吧,实时性好得多.

热点排行