Results 1 to 10 of 10

Thread: "Send message"-problem

  1. #1

    Thread Starter
    New Member
    Join Date
    Jul 2000
    Posts
    14

    Question

    I am new in VB and I would greatly appreciate any help.
    I have tried to access the menu of NOTEPAD (I use it to display HTML code) from within my application using the "send message" API function. Although I succeded to send commands and access all the menu items of the NOTEPAD, this didn't work with "Cut", "Copy" and "Paste" of the "Edit" menu. Is there something special with those?
    Is there any alternative way to manipulate (Select, copy, etc) the text displayed in the NOTEPAD? ("Sendkeys" do not work in an already opened instance of notepad. Am I wrong?)

    Many thanks in advance



  2. #2
    Lively Member
    Join Date
    Jul 2000
    Location
    Ireland, Dublin
    Posts
    76

    Talking

    When sending the message to Notepad, I think if you
    use the constants WM_CUT, WM_COPY, WM_PASTE then it
    might work. I haven't tried it yet, so please let me know if it works.

    Hope it helps

    IJ...
    ---------------------------------
    TopMan
    ---------------------------------

  3. #3

    Thread Starter
    New Member
    Join Date
    Jul 2000
    Posts
    14
    Unfortunately it didn't work. The WM_COPY appears to send the message to a specific Edit CONTROL . In the case of NOTEPAD this must be the TextBox of NOTEPAD but I don't know how to find its Handle. Any idea??

    THANKS

  4. #4
    Guest
    Use FindWindowEx. This will paste text into Notepad:
    Code:
    Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As Any, ByVal lpWindowName As Any) As Long
    Private Declare Function FindWindowEx Lib "user32" Alias "FindWindowExA" (ByVal hWnd1 As Long, ByVal hWnd2 As Long, ByVal lpsz1 As Any, ByVal lpsz2 As Any) 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
    Const WM_COPY = &H301
    Const WM_CUT = &H300
    Const WM_PASTE = &H302
    Dim hParent As Long
    Dim hChild As Long
    
    Private Sub Command1_Click()
    
        'Get the Handle of Notepad
        hParent = FindWindow("Notepad", 0&)
        'Get the Handle of the Edit Control
        hChild = FindWindowEx(hParent, 0&, "Edit", 0&)
        'Send "paste" a message to the Edit Control.
        SendMessage hChild, WM_PASTE, 0&, 0&
        
    End Sub

  5. #5

    Thread Starter
    New Member
    Join Date
    Jul 2000
    Posts
    14

    SendMessage-stillproblem

    THANKS to everybody
    But still nothing has worked!

    Probably Megatron, the class of Edit control of NOTEPAD is not "Edit" because the hChild is returned 0. Any other idea?

    Thanks in advance


  6. #6
    Guest
    The class of the Notepad conrol is Edit. I even double checked it with a Class-Spy tool and tested it on my machine and it worked fine.

    Try making a brand new project and add a CommandButton with the above code into it. Also, make sure that Notepad is openend before you run your App and also make sure that you have some text on the clipboard.




  7. #7
    New Member
    Join Date
    Aug 2000
    Posts
    4

    Question

    Where are your heads?? Your not supposed to use 0& when the function asks for a string. Use vbNullString. That should fix your problem. Im not going to go into the WM_PASTE constant because i dont use it. Well here is the way i would do it:
    Code:
    Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
    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
    Private Declare Function SendMessageByString Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As String) As Long
    Const WM_SETTEXT = &HC
    
    Private Sub Command1_Click()
      'Dim variables
    Dim notepad As Long, editx As Long
      'Find the hWnd of the notepad window
    notepad = FindWindow("notepad", vbNullString)
      'Find the hWnd of the notepad textbox
    editx = FindWindowEx(notepad, 0&, "edit", vbNullString)
      'Set the textbox's text
    SendMessageByString editx, WM_SETTEXT, 0, "Code Fixed By: ser"
    End Sub

  8. #8

    Thread Starter
    New Member
    Join Date
    Jul 2000
    Posts
    14
    THANK YOU Ser

    Amazing. That was the trick. Everything works now!!!!

    THANKs to everybody
    THANKS

  9. #9
    Guest
    Ser. I didn't use vbNullString because I declared both arguments as Any.
    Code:
    Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As Any, ByVal lpWindowName As Any) As Long

  10. #10
    New Member
    Join Date
    Aug 2000
    Posts
    4
    Oh, i am very sorry. I didnt realize it when i was looking over the code. My sincerest apologies.
    Please exuse any mispeled words

    Tell me and I forget.
    Show me and I remember.
    Teach me and I learn.

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