我想把屏幕上(包括桌面、程序界面、IE等)的文字中的某一个字符替换为另外的一个字符,请问用VB可否实现。或者能否提供可实现此功能的OCX或DLL

解决方案 »

  1.   

    思路:
    先用findwindow和findwindowex找到窗口句柄,然后用sendmessage发送wm_settext消息
      

  2.   

    用WINDOW类模块~~~~~~~~~~~~~~~~~~~~
    Option Explicit
    'Window Functions
    Private Declare Function GetWindow Lib "user32" (ByVal hwnd As Long, ByVal wCmd As Long) As Long
    Private Declare Function GetActiveWindow Lib "user32" () As Long
    Private Declare Function GetDesktopWindow Lib "user32" () As Long
    Private Declare Function GetNextWindow Lib "user32" Alias "GetWindow" (ByVal hwnd As Long, ByVal wFlag As Long) As Long
    Private Declare Function SetWindowText Lib "user32" Alias "SetWindowTextA" (ByVal hwnd As Long, ByVal lpString As String) As Long
    Private Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" (ByVal hwnd As Long, ByVal lpString As String, ByVal cch As Long) As Long
    Private Declare Function GetWindowRect Lib "user32" (ByVal hwnd As Long, lpRect As RECT) As Long
    Private Declare Function MoveWindow Lib "user32" (ByVal hwnd As Long, ByVal X As Long, ByVal Y As Long, ByVal nWidth As Long, ByVal nHeight As Long, ByVal bRepaint As Long) As Long
    Private Declare Function FlashWindow Lib "user32" (ByVal hwnd As Long, ByVal bInvert As Long) As Long
    Private Declare Function BringWindowToTop Lib "user32" (ByVal hwnd As Long) As Long
    Private Declare Function CloseWindow Lib "user32" (ByVal hwnd As Long) As Long
    Private Declare Function DestroyWindow Lib "user32" (ByVal hwnd As Long) As Long
    Private Declare Function RedrawWindow Lib "user32" (ByVal hwnd As Long, lprcUpdate As RECT, ByVal hrgnUpdate As Long, ByVal fuRedraw As Long) As Long
    Private Declare Function SetForegroundWindow Lib "user32" (ByVal hwnd As Long) As Long'Menu Functions
    Private Declare Function RemoveMenu Lib "user32" (ByVal hMenu As Long, ByVal nPosition As Long, ByVal wFlags As Long) As Long
    Private Declare Function GetMenu Lib "user32" (ByVal hwnd As Long) As Long
    Private Declare Function SetMenu Lib "user32" (ByVal hwnd As Long, ByVal hMenu As Long) As Long
    Private Declare Function DrawMenuBar Lib "user32" (ByVal hwnd As Long) As Long
    Private Declare Function InsertMenu Lib "user32" Alias "InsertMenuA" (ByVal hMenu As Long, ByVal nPosition As Long, ByVal wFlags As Long, ByVal wIDNewItem As Long, ByVal lpNewItem As Any) As Long
    Private Declare Function GetMenuItemCount Lib "user32" (ByVal hMenu As Long) As Long
    Private Declare Function GetSubMenu Lib "user32" (ByVal hMenu As Long, ByVal nPos As Long) As Long
    Private Declare Function GetMenuItemID Lib "user32" (ByVal hMenu As Long, ByVal nPos As Long) As Long
    Private Declare Function GetMenuString Lib "user32" Alias "GetMenuStringA" (ByVal hMenu As Long, ByVal wIDItem As Long, ByVal lpString As String, ByVal nMaxCount As Long, ByVal wFlag As Long) As Long'Other Functions
    Private Declare Function GetClassName Lib "user32" Alias "GetClassNameA" (ByVal hwnd As Long, ByVal lpClassName As String, ByVal nMaxCount As Long) As Long
    Private Declare Function GetWindowDC Lib "user32" (ByVal hwnd As Long) As Long
    Private Declare Function WindowFromPointXY Lib "user32" Alias "WindowFromPoint" (ByVal xPoint As Long, ByVal yPoint As Long) As Long
    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 SetParent Lib "user32" (ByVal hWndChild As Long, ByVal hwndNewParent As Long) As Long
    Private Declare Function DrawEdge Lib "user32" (ByVal hdc As Long, qrc As RECT, ByVal edge As Long, ByVal grfFlags As Long) As Long
    Private Type RECT
            Left As Long
            Top As Long
            Right As Long
            Bottom As Long
    End Type
    Private Const WM_CLOSE = &H10
    ' SetWindowPos() hwndInsertAfter values
    Private Const HWND_TOP = 0
    Private Const HWND_BOTTOM = 1
    Private Const HWND_TOPMOST = -1
    Private Const HWND_NOTOPMOST = -2' GetWindow() Constants
    Private Const GW_HWNDFIRST = 0
    Private Const GW_HWNDLAST = 1
    Private Const GW_HWNDNEXT = 2
    Private Const GW_HWNDPREV = 3
    Private Const GW_OWNER = 4
    Private Const GW_CHILD = 5
    Private Const GW_MAX = 5' ShowWindow() Commands
    Private Const SW_HIDE = 0
    Private Const SW_SHOWNORMAL = 1
    Private Const SW_NORMAL = 1
    Private Const SW_SHOWMINIMIZED = 2
    Private Const SW_SHOWMAXIMIZED = 3
    Private Const SW_MAXIMIZE = 3
    Private Const SW_SHOWNOACTIVATE = 4
    Private Const SW_SHOW = 5
    Private Const SW_MINIMIZE = 6
    Private Const SW_SHOWMINNOACTIVE = 7
    Private Const SW_SHOWNA = 8
    Private Const SW_RESTORE = 9
    Private Const SW_SHOWDEFAULT = 10
    Private Const SW_MAX = 10
    Public hwnd As Long
    Public Function DrawWindowMenuBar() As Long
    DrawWindowMenuBar = DrawMenuBar(hwnd)
    End Function
    Public Function SetWindowMenu(hMenu As Long) As Long
    SetWindowMenu = SetMenu(hwnd, hMenu)
    End Function
    Public Function GetWindowMenu() As Long
    GetWindowMenu = GetMenu(hwnd)
    End Function
    Public Function SetText(sNewText As String) As Long
    SetText = SetWindowText(hwnd, sNewText)
    End Function
    Public Function GetText() As String
    Dim sText As String * 255
    Dim tmpLen As Long
    tmpLen = GetWindowText(hwnd, sText, 255)
    GetText = Left(sText, tmpLen)
    End Function
    Public Function GetWindowClassName() As String
    Dim sClass As String * 255
    Dim tmpLen As Long
    tmpLen = GetClassName(hwnd, sClass, 255)
    GetWindowClassName = Left(sClass, tmpLen)
    End Function
    Public Function GetTop() As Long
    Dim rc As RECT
    GetWindowRect hwnd, rc
    GetTop = rc.Top
    End Function
    Public Function GetLeft() As Long
    Dim rc As RECT
    GetWindowRect hwnd, rc
    GetLeft = rc.Left
    End Function
    Public Function GetWidth() As Long
    Dim rc As RECT
    GetWindowRect hwnd, rc
    GetWidth = rc.Right
    End Function
    Public Function GetHeight() As Long
    Dim rc As RECT
    GetWindowRect hwnd, rc
    GetHeight = rc.Bottom
    End Function
    Public Function GetFromXY(ByVal X As Long, ByVal Y As Long) As Long
    GetFromXY = WindowFromPointXY(X, Y)
    End Function
    Public Function Terminate()
    Terminate = SendMessage(hwnd, WM_CLOSE, 0, 0)
    End Function
    Public Function Minimize()
    Minimize = CloseWindow(hwnd)
    End Function
    Public Function Destroy()
    Destroy = DestroyWindow(hwnd)
    End Function
    Public Function SetWindowParent(hwndNewParent As Long) As Long
    SetWindowParent = SetParent(hwnd, hwndNewParent)
    End Function
    Public Function Flash() As Long
    Flash = FlashWindow(hwnd, 1)
    End Function
    Public Function BringToTop() As Long
    BringToTop = BringWindowToTop(hwnd)
    End Function
    Public Function Move(Left As Long, Top As Long, Width As Long, Height As Long, bRepaint As Boolean)
    MoveWindow hwnd, Left, Top, Width, Height, bRepaint
    End Function
    Public Function GetChildWindow(hwndParent As Long)
    GetChildWindow = GetWindow(hwndParent, GW_CHILD)
    End Function
    Public Function NextWindow(hwnd As Long)
    NextWindow = GetNextWindow(hwnd, GW_HWNDNEXT)
    End Function
    Public Function DesktopWindow() As Long
    DesktopWindow = GetDesktopWindow
    End Function
      

  3.   

    使用=========================================
    Option Explicit
    Private Type POINTAPI
       X As Long
       Y As Long
    End Type
    Private Declare Function GetCursorPos Lib "user32" (lpPoint As POINTAPI) As Long
    Private Declare Function SetCapture Lib "user32" (ByVal hwnd As Long) As Long
    Private Declare Function ReleaseCapture Lib "user32" () As Long
    Private Declare Function WindowFromPointXY Lib "user32" Alias "WindowFromPoint" (ByVal xPoint As Long, ByVal yPoint As Long) As Long
    Dim m_hWnd As Long
    Dim m_Picking As Boolean
    Dim hMenu As Long
    Dim mbDown As Boolean
    Dim wnd As New WindowPrivate Sub cboChild_Click()
    FillProps cboChild.ItemData(cboChild.ListIndex)
    End SubPrivate Sub cmdMinimize_Click()
    wnd.hwnd = Val(txthWnd)
    wnd.Minimize
    End Sub
    Private Sub cmdDestroy_Click()
    wnd.hwnd = Val(txthWnd)
    wnd.Destroy
    End Sub
    Private Sub cmdExportMenu_Click()
    wnd.SetWindowMenu hMenu
    wnd.DrawWindowMenuBar
    End SubPrivate Sub cmdFlash_Click()
    wnd.hwnd = Val(txthWnd)
    wnd.Flash
    End SubPrivate Sub cmdImportMenu_Click()
    wnd.hwnd = Val(txthWnd)
    hMenu = wnd.GetWindowMenu
    wnd.hwnd = Me.hwnd
    wnd.SetWindowMenu hMenu
    wnd.DrawWindowMenuBar
    End SubPrivate Sub cmdRefresh_Click()
    lstWindow.Clear
    Dim sTitle As String, hwnd As Long
    hwnd = wnd.GetChildWindow(wnd.DesktopWindow)
    Do While hwnd <> 0
        wnd.hwnd = hwnd
        sTitle = wnd.GetText()
        If sTitle <> "" Then lstWindow.AddItem Trim(sTitle):  lstWindow.ItemData(lstWindow.NewIndex) = hwnd
        hwnd = wnd.NextWindow(hwnd)
    Loop
    lstWindow.ListIndex = 0
    End SubPrivate Sub cmdReNewSettings_Click()
    wnd.Move Val(txtLeft), Val(txtTop), Val(txtWidth), Val(txtHeight), True
    Me.SetFocus
    End Sub
    Private Sub cmdTerminate_Click()
    wnd.hwnd = Val(txthWnd)
    wnd.Terminate
    End Sub
    Private Sub cmdTransfer_Click()
    wnd.hwnd = Val(txthWnd)
    wnd.SetWindowParent Me.hwnd
    End Sub
    Private Sub cmdBringToTop_Click()
    wnd.hwnd = Val(txthWnd)
    wnd.BringToTop
    End SubPrivate Sub Form_Activate()
    'DrawMenuBar Me.hwnd
    End SubPrivate Sub Form_Load()
    picDrag.Picture = picDrag.DragIcon
    Me.MouseIcon = picDrag.DragIcon
    End SubPrivate Sub lstWindow_Click()
    cboChild.Clear
    Dim sTitle As String, hwnd As Long
    hwnd = wnd.GetChildWindow(lstWindow.ItemData(lstWindow.ListIndex))
    Do While hwnd <> 0
        wnd.hwnd = hwnd
        sTitle = wnd.GetText()
        If sTitle <> "" Then cboChild.AddItem Trim(sTitle): cboChild.ItemData(cboChild.NewIndex) = hwnd
        hwnd = wnd.NextWindow(hwnd)
    Loop
    If cboChild.ListCount > 0 Then cboChild.ListIndex = 0
    FillProps lstWindow.ItemData(lstWindow.ListIndex)
    End SubPrivate Sub picDrag_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
    Me.MousePointer = vbCustom
    Set picDrag.Picture = Nothing
    m_Picking = True
    Call SetCapture(picDrag.hwnd)
    End Sub
    Private Sub picDrag_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
    Static pt As POINTAPI
    'Dim rc As RECT
    Dim hdcTarget As Long
    Dim hdcPrev As Long
    Static hwndTarget As Long
    Static hwndPrev As Long
    If m_Picking = False Then Exit Sub
        Call GetCursorPos(pt)
        hwndTarget = WindowFromPointXY(pt.X, pt.Y)
        'hdcTarget = GetWindowDC(hwndTarget)
        'GetWindowRect hwndTarget, rc
        'rc.Left = 0
        'rc.Top = 0
        If hwndTarget = m_hWnd Then Exit Sub
        'hdcTarget = GetWindowDC(hwndTarget)
        'hdcPrev = GetWindowDC(m_hWnd)
        'GetWindowRect hwndTarget, rc
        'rc.Left = 0
        'rc.Top = 0
        'RedrawWindow m_hWnd, rc, 0, 1
        'DrawEdge hdcPrev, rc, 0, 0
        'DrawEdge hdcTarget, rc, 10, 15
        FillProps hwndTarget
    End Sub
    Private Sub picDrag_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single)
    m_Picking = False
    picDrag.Picture = picDrag.DragIcon
    Me.MousePointer = vbDefault
    Call ReleaseCapture
    End Sub
    Sub FillProps(lWindow As Long)
    Dim sClassName As String
    m_hWnd = lWindow
    wnd.hwnd = lWindow
    txtWindowText.Text = wnd.GetText()
    txthWnd.Text = m_hWnd
    'Get Window's Class Name
    txtClassName.Text = wnd.GetWindowClassName()
    'Get Window Position
    txtTop = wnd.GetTop
    txtLeft = wnd.GetLeft
    txtWidth = wnd.GetWidth
    txtHeight = wnd.GetHeight
    End Sub
    Private Sub txtWindowText_Change()
    If m_Picking = True Then Exit Sub
    wnd.hwnd = Val(txthWnd)
    wnd.SetText txtWindowText.Text
    End Sub
      

  4.   

    其实用这个就可以了!
    Public Function SetText(sNewText As String) As Long
    SetText = SetWindowText(hwnd, sNewText)
    End Function
      

  5.   

    我现在还没有完全调通这个程序,关键是有几个控件的类型有点没搞懂,不知道那些是菜单、哪些是命令按钮,不置可否将重要控件的列表给我?另外,能否简单介绍一下此例程各个控件的作用,Form1上的控件太多,好像有两个文本框、一个列表框,组合框等等,我有点被弄糊涂了。
      

  6.   

    或者,将程序发给我好么?[email protected]