Click to See Complete Forum and Search --> : "Send message"-problem
emmzou
Aug 9th, 2000, 04:46 AM
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
IrishJoker
Aug 9th, 2000, 05:52 AM
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...
emmzou
Aug 9th, 2000, 08:44 AM
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
Use FindWindowEx. This will paste text into Notepad:
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
emmzou
Aug 9th, 2000, 09:47 AM
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
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.
ser
Aug 10th, 2000, 02:06 AM
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:
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
emmzou
Aug 10th, 2000, 03:25 AM
THANK YOU Ser
Amazing. That was the trick. Everything works now!!!!
THANKs to everybody
THANKS
Ser. I didn't use vbNullString because I declared both arguments as Any.
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As Any, ByVal lpWindowName As Any) As Long
ser
Aug 10th, 2000, 02:21 PM
Oh, i am very sorry. I didnt realize it when i was looking over the code. My sincerest apologies.
vbforums.com
Copyright Internet.com Inc., All Rights Reserved.