Results 1 to 26 of 26

Thread: [RESOLVED] Change caption or text of running Application

  1. #1

    Thread Starter
    Lively Member cyberd's Avatar
    Join Date
    Jul 2008
    Location
    GREECE
    Posts
    108

    Resolved [RESOLVED] Change caption or text of running Application

    Hi,

    for once again i am trying something difficult...

    Well i want my VB6 program see the current application running at my desktop (that the easy part) and with one click change for example the title of running application (Application - EXE - that is not mine)...

    For example if Windows Calculator running... with one click to my program at the button change title of "Calculator" - i want to turn the title of this program to something else like "C-a-l-c-u-l-a-t-o-r"...

    is that possible to change the captions or text of a running application ?

    and how ?

    Thanks in advance... :-)

  2. #2

    Thread Starter
    Lively Member cyberd's Avatar
    Join Date
    Jul 2008
    Location
    GREECE
    Posts
    108

    Re: Change caption or text of running Application

    ...i ve found that code.... that changes text of Start button of Windows XP...

    vb Code:
    1. Private Const WM_SETTEXT = &HC
    2. Private Const WM_GETTEXT = &HD
    3. Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
    4. Private Declare Function FindWindowEx Lib "user32" Alias "FindWindowExA" (ByVal hWnd1 As Long, ByVal hWnd2 As Long, ByVal lpsz1 As String, ByVal lpsz2 As String) As Long
    5. Private Declare Function SendMessageSTRING Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As String) As Long
    6.  
    7. Public Sub SetStartCaption(str As String)
    8.    Dim StartBar As Long
    9.    Dim StartBarText As Long
    10.    Dim sCaption As String
    11.    StartBar = FindWindow("Shell_TrayWnd", vbNullString)
    12.    StartBarText = FindWindowEx(StartBar, 0&, "button", vbNullString)
    13.    Debug.Print StartBarText
    14.    sCaption = Left(str, 5)
    15.    SendMessageSTRING StartBarText, WM_SETTEXT, 256, sCaption
    16.    Exit Sub
    17. End Sub

    code found here

    but i don;t know how to "cycle" the controls to see caption/texts before change...

  3. #3
    Frenzied Member
    Join Date
    Mar 2009
    Posts
    1,182

    Re: Change caption or text of running Application

    Code:
    'System & API - How to Change Other Window's Caption
    
    Option Explicit
    
    'Add two Text Boxes and a Command Button to your form.
    'Insert the caption of the window you want to change in Text1.
    'Insert the new caption in Text2.
    
    Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, _
        ByVal lpWindowName As String) 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 Const WM_SETTEXT = &HC
    
    Private Sub Command1_Click()
        Dim target_hwnd As Long
        Dim target_name As String
        Dim new_caption As String
        target_name = Text1.Text
        target_hwnd = FindWindow(vbNullString, target_name)
        If target_hwnd = 0 Then
            MsgBox "Cannot find target"
            Exit Sub
        End If
        new_caption = Text2.Text
        SendMessage target_hwnd, WM_SETTEXT, 0, ByVal new_caption
    End Sub
    
    Private Sub Form_Load()
        Text1.Text = "My Computer"
        Text2.Text = "New Caption"
    End Sub
    Good Luck

  4. #4

    Thread Starter
    Lively Member cyberd's Avatar
    Join Date
    Jul 2008
    Location
    GREECE
    Posts
    108

    Re: Change caption or text of running Application

    @vb5prgrmr thank for your answer.. but i didn't mean that ... exactly... i want my programs reads the Controls of Calculator... the title that may be in German / Greek or any other Language... and change it with the text i want...

    In generic words i want to read all controls and caption of other application.

    if that code was possible will be ok for me .. but it isnt:
    vb Code:
    1. k=0
    2. for x=1 to otherapplication.controls.count
    3.  if otherapplication.control(x).typeof is label then
    4.        if otherappliction.control(x).caption="Calculator" then
    5.                otherapplication.control(x).caption="C-A-l-C-u-l-a-t-o-r"
    6.                 k=k+1
    7.        end if
    8.  end if
    9. next x
    10. debug.print "From " & otherapplication.controls.count & " change only " & k

    i want to cycle all the controls of the other Application... at least that i can view...

  5. #5
    Frenzied Member
    Join Date
    Mar 2009
    Posts
    1,182

    Re: Change caption or text of running Application

    Ok, you will need to add the API EnumChildWindows to enumerate through the controls of the target application and then use the SendMessage API.

    Good Luck

  6. #6

    Thread Starter
    Lively Member cyberd's Avatar
    Join Date
    Jul 2008
    Location
    GREECE
    Posts
    108

    Re: Change caption or text of running Application

    @vb5prgrmgr can you give the code or the example of how to do it ?

  7. #7
    Frenzied Member
    Join Date
    Mar 2009
    Posts
    1,182

    Re: Change caption or text of running Application

    You can find some example code here

    http://www.acky.net/forums/index.php?showtopic=7497

    or search the web for enumerate child windows

  8. #8

    Thread Starter
    Lively Member cyberd's Avatar
    Join Date
    Jul 2008
    Location
    GREECE
    Posts
    108

    Re: Change caption or text of running Application

    thanx

  9. #9

    Thread Starter
    Lively Member cyberd's Avatar
    Join Date
    Jul 2008
    Location
    GREECE
    Posts
    108

    Re: Change caption or text of running Application

    vb Code:
    1. Option Explicit
    2.  
    3. Public Const WM_SETTEXT = &HC
    4. Public Const WM_GETTEXT = &HD
    5. Public Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
    6. Public Declare Function FindWindowEx Lib "user32" Alias "FindWindowExA" (ByVal hWnd1 As Long, ByVal hWnd2 As Long, ByVal lpsz1 As String, ByVal lpsz2 As String) As Long
    7. Public Declare Function SendMessageSTRING Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As String) As Long
    8. Public Declare Function EnumChildWindows Lib "user32" (ByVal hWndParent As Long, ByVal lpEnumFunc As Long, ByVal lParam As Long) As Long
    9. Public Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" (ByVal hwnd As Long, ByVal lpString As String, ByVal cch As Long) As Long
    10.  
    11. Public Function EnumChildProc(ByVal hwnd As Long, ByVal lParam As Long) As Long
    12.    Static winnum As Integer
    13.    winnum = winnum + 1
    14.    ReDim winhandle(winnum) As Long
    15.    winhandle(winnum) = hwnd
    16.    EnumChildProc = 1
    17. End Function
    18.  
    19. '-------------------
    20. '-Form code:
    21.  
    22.  
    23.  
    24. Public Sub SetStartCaption(str As String)
    25.    Dim StartBar As Long
    26.    Dim StartBarText As Long
    27.    Dim sCaption As String
    28.    StartBar = FindWindow("Shell_TrayWnd", vbNullString)
    29.    StartBarText = FindWindowEx(StartBar, 0&, "button", vbNullString)
    30.    
    31.    Debug.Print StartBarText
    32.    
    33.    sCaption = Left(str, 5)
    34.    SendMessageSTRING StartBarText, WM_SETTEXT, 256, sCaption
    35.    Exit Sub
    36. End Sub
    37.  
    38. Private Sub Form_Load()
    39.    
    40.    Dim pHandle As Long
    41.    Dim bHandle As Long, h As Long, i As Long, length As Long
    42.    Dim caption As String
    43.    
    44.    pHandle = FindWindow(vbNullString, "Áñéèìïìç÷áíÞ")
    45.    h = EnumChildWindows(pHandle, AddressOf EnumChildProc, 0)
    46.    bHandle = 0
    47.    
    48.    If (h = 1) Then
    49.        For i = 1 To (UBound(winhandle) - LBound(winhandle))
    50.        
    51.            caption = Space$(1024)
    52.            length = GetWindowText(winhandle(i), caption, Len(caption))
    53.            caption = Left$(caption, length)
    54.            
    55.            Debug.Print caption, length
    56.            
    57.            If caption = "OK" Then
    58.                bHandle = winhandle(i)
    59.                Exit For
    60.            End If
    61.            
    62.        Next i
    63.    End If
    64.    
    65. End Sub

    I am getting error at ubound(winhandle) why ?
    vb Code:
    1. [I]For i = 1 To (UBound(winhandle) - LBound(winhandle))[/I]
    Type mismatch.

  10. #10
    Frenzied Member
    Join Date
    Mar 2009
    Posts
    1,182

    Re: Change caption or text of running Application

    to begin with I do not see where winhandle is declared, is it declared?

  11. #11

    Thread Starter
    Lively Member cyberd's Avatar
    Join Date
    Jul 2008
    Location
    GREECE
    Posts
    108

    Re: Change caption or text of running Application

    vb Code:
    1. ReDim winhandle(winnum) As Long   winhandle(winnum) = hwnd

    what do you suggest ?

  12. #12
    Fanatic Member
    Join Date
    Aug 2007
    Posts
    912

    Re: Change caption or text of running Application

    Quote Originally Posted by cyberd
    Hi,

    for once again i am trying something difficult...

    Well i want my VB6 program see the current application running at my desktop (that the easy part) and with one click change for example the title of running application (Application - EXE - that is not mine)...

    For example if Windows Calculator running... with one click to my program at the button change title of "Calculator" - i want to turn the title of this program to something else like "C-a-l-c-u-l-a-t-o-r"...

    is that possible to change the captions or text of a running application ?

    and how ?

    Thanks in advance... :-)
    Code:
    Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
    Private Declare Function SetWindowText Lib "user32" Alias "SetWindowTextA" (ByVal hwnd As Long, ByVal lpString As String) As Long
    
    Private Sub Form_Load()
    Dim H As Long
    H = FindWindow(vbNullString, "Calculator")
    If H > 0 Then
        SetWindowText H, "C-a-l-c-u-l-a-t-o-r"
    Else
        MsgBox "Calculator not found"
    End If
    
    End Sub

  13. #13

    Thread Starter
    Lively Member cyberd's Avatar
    Join Date
    Jul 2008
    Location
    GREECE
    Posts
    108

    Re: Change caption or text of running Application

    @chunk you didn't read after 1st post ? - i want to cycle all controls of form...
    thanks anyway but i have already this answer..

  14. #14
    Frenzied Member
    Join Date
    Mar 2009
    Posts
    1,182

    Re: Change caption or text of running Application

    proper declaration would be say at form level
    Code:
    dim winhandle() as Long
    and then keep the rest of your code as is

    Good Luck

  15. #15

    Thread Starter
    Lively Member cyberd's Avatar
    Join Date
    Jul 2008
    Location
    GREECE
    Posts
    108

    Re: Change caption or text of running Application

    hmm... seems ok.. i am continue the project... seems no error.. thanx again

  16. #16
    Frenzied Member
    Join Date
    Mar 2009
    Posts
    1,182

    Re: Change caption or text of running Application

    Not a problem

    Good Luck

  17. #17

    Thread Starter
    Lively Member cyberd's Avatar
    Join Date
    Jul 2008
    Location
    GREECE
    Posts
    108

    Re: Change caption or text of running Application

    caugh.. hmmm..
    what about the menus of forms how i can enumerate them... ? is it possible ?

    (the title and the buttons, and captions enumerated by the code i ve write - but menus... not...)

  18. #18
    Frenzied Member
    Join Date
    Mar 2009
    Posts
    1,182

    Re: Change caption or text of running Application

    Hmmm.... Not sure. Try to send via sendmessage "&File" to main app and see if the file menu drops (that is if it has a file menu item). I would think that during your enumeration you should recieve a handle to a window with no caption but its class should be something like ToolbarWindow32.

    Just used SPY++ on IE and it reported that the captionless window (the menu bar) does have a child.

    Good Luck

  19. #19
    Fanatic Member
    Join Date
    Aug 2007
    Posts
    912

    Re: Change caption or text of running Application

    Quote Originally Posted by cyberd
    caugh.. hmmm..
    what about the menus of forms how i can enumerate them... ? is it possible ?

    (the title and the buttons, and captions enumerated by the code i ve write - but menus... not...)
    i dont this this is possible. If this is possible then i would also like to learn it
    because it will always show 32676 something like that on menus

  20. #20
    Frenzied Member
    Join Date
    Mar 2009
    Posts
    1,182

    Re: Change caption or text of running Application

    Okay, with a little research, I found some more API's for you...
    SetForegroundWindow (not really needed but will allow you to see if the rest of the API's work)

    In help look up GetSystemMenu
    At the bottom look into the Menu Functions and the WM_SYSCOMMAND>Keyboard Accelerator Messages

    Good Luck

  21. #21
    Frenzied Member
    Join Date
    Dec 2007
    Posts
    1,072

    Re: Change caption or text of running Application

    Try this code to turn a string like "ABCD" into "A-B-C-D"

    Code:
    Public Function dashString(sInput As String) As String
    Dim tempArray() As String, i As Integer
        ReDim tempArray(1 To Len(sInput))
        For i = 1 To Len(sInput)
            tempArray(i) = Mid$(sInput, i, 1)
        Next i
        dashString = Join(tempArray, "-")
    End Function

  22. #22

    Thread Starter
    Lively Member cyberd's Avatar
    Join Date
    Jul 2008
    Location
    GREECE
    Posts
    108

    Re: Change caption or text of running Application

    @Zach_VB6 please read the topic .. thanks again..

    @vb5prgrmr ...can't get it.. can you give a simple example...

  23. #23
    Frenzied Member
    Join Date
    Mar 2009
    Posts
    1,182

    Re: Change caption or text of running Application

    Don't know if I have one... Will search systems... If anybody else perhaps has an example please post...

  24. #24

    Thread Starter
    Lively Member cyberd's Avatar
    Join Date
    Jul 2008
    Location
    GREECE
    Posts
    108

    Re: Change caption or text of running Application

    did anyone found something... (?)...

  25. #25
    Frenzied Member
    Join Date
    Mar 2009
    Posts
    1,182

    Re: Change caption or text of running Application

    No not yet.. I have to work sometimes...

    Perhaps, that you are now on a bit of a different subject, start a new thread with something like

    How to enumerate seperate program's menu items

    Or whatever

  26. #26

    Thread Starter
    Lively Member cyberd's Avatar
    Join Date
    Jul 2008
    Location
    GREECE
    Posts
    108

    Re: [RESOLVED] Change caption or text of running Application

    ok thanks, you have right

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width