Log inUsernamePassword
Log me on automatically each visit    
Register
Register
Log in to check your private messages
Log in to check your private messages
Visual Basic Forum for Visual Basic Programmers VB Forum Index » General

Post new topic   Reply to topic
Help with SendMessage - EM_CANPASTE
View previous topic :: View next topic  
Author Message
vb_programmer
Freshman


Joined: 03 Jan 2004
Posts: 31

PostPosted: Jan 16th, 2004 01:32 AM    Post subject: Help with SendMessage - EM_CANPASTE Reply with quote

Hello to All Forum Members ;

I need your Help with SendMessage API to paste Clipboard Text into an edit control of selected Application.

Before I execute the Paste action , I wanted to check whether I can Paste it or not. For this I used EM_CANPASTE message and checked the return value. But even if the hWnd parameter passed through SendMessage is a handle of NotePad (which is of course active and running on my desktop when the message sent) , SendMessage always returns zero ; which means EM_CANPASTE has returned zero. This message returns non-zero if success (says MSDN) ; so then it clearly says that it's not possible to Paste Clipboard material (which is a Plain Text) into NotePad !

I'm sure I'm going wrong somewhere ... but don't know where. Please help to point out the mistake and resolve this issue.

Here is the code ...
Code:

'------------ Code from Module ------------------------------------------------------
Public Const WM_USER As Long = &H400
Public Const EM_CANPASTE As Long = (WM_USER + 50)

Private Declare Function EnumWindows& Lib "user32" (ByVal lpEnumFunc As Long, ByVal lParam 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 IsWindowVisible& Lib "user32" (ByVal hwnd As Long)
Private Declare Function GetParent& Lib "user32" (ByVal hwnd As Long)

Dim sPattern As String, hFind As Long


Function EnumWinProc(ByVal hwnd As Long, ByVal lParam As Long) As Long
    Dim k As Long, sName As String


    If IsWindowVisible(hwnd) And GetParent(hwnd) = 0 Then
        sName = Space$(128)
        k = GetWindowText(hwnd, sName, 128)


        If k > 0 Then
            sName = Left$(sName, k)
            If lParam = 0 Then sName = UCase(sName)
                If sName Like sPattern Then
                    hFind = hwnd
                    EnumWinProc = 0
                    Exit Function
                End If
            End If
        End If

        EnumWinProc = 1

End Function

Public Function FindWindowWild(sWild As String, Optional bMatchCase As Boolean = True) As Long
    sPattern = sWild

    If Not bMatchCase Then sPattern = UCase(sPattern)

    EnumWindows AddressOf EnumWinProc, bMatchCase
    FindWindowWild = hFind

End Function

'------------ Code from Form ------------------------------------------------------

Dim lngNotePadHWND as Long

Private Sub Form_Load()
        lngNotePadHWND = FindWindowWild("Untitled - Notepad")

        'wParam is Clipboard Format (passed zero if check for any)
        lngMsgResult = SendMessage(lngNotePadHWND, EM_CANPASTE, vbCFText, 0)
       
        MsgBox lngMsgResult
End Sub

_________________
----- vb_programmer ------
iNova Creations : Software Development Company !
(www.inovacreations.com)

RutuOnline.com : A Programmer's Resource Center !
(www.rutuonline.filetap.com)


Last edited by vb_programmer on Jan 16th, 2004 01:35 AM; edited 1 time in total
Back to top
View user's profile Send private message Visit poster's website Yahoo Messenger ICQ Number
Avis
Junior Poster


Joined: 07 Oct 2003
Posts: 510
Location: India

PostPosted: Jan 16th, 2004 07:45 AM    Post subject: Reply with quote

So lemme explain what you're trying to do here... you're basically just trying to get the text from clipboard and paste it into the notepad, right ?

If this is what you want to do then there's a smiple way of doing it and that will be to use the Clipboard keyword to get clipboard text. And then use the Shell keyword to open Notepad and then use SendKeys to paste text into the Notepad.

So overall the code will be like this:

Code:
Shell "Notepad.exe", vbNormalFocus
SendKeys Clipboard.GetText, true


The above code may not be correct as i am not having VB right now and this is just a guess but i think technically this should do the job.

If this does not works then just check the words in bold in MSDN and you'll get the point what i am trying to say here.

Thanks!
_________________
Code Snippets, Tutorials, Utilities, Controls

Low cost Web Hosting
Hosting starts at as low as $4 per year!


Always follow posting guidelines
Put your VB code in [vb ] your code [ /vb] tags!
Back to top
View user's profile Send private message Send e-mail Visit poster's website Yahoo Messenger MSN Messenger ICQ Number
vb_programmer
Freshman


Joined: 03 Jan 2004
Posts: 31

PostPosted: Jan 16th, 2004 01:15 PM    Post subject: Reply with quote

:$ Oh ! Will that work ?! If that works then it will mean that SendMessage and big big things fooled me ! =D

I'll try this and I'll come back ...

Thanks for your valuable reply ....
_________________
----- vb_programmer ------
iNova Creations : Software Development Company !
(www.inovacreations.com)

RutuOnline.com : A Programmer's Resource Center !
(www.rutuonline.filetap.com)
Back to top
View user's profile Send private message Visit poster's website Yahoo Messenger ICQ Number
P.T.A.M.
Administrator


Joined: 08 Oct 2003
Posts: 752
Location: Greece

PostPosted: Jan 16th, 2004 06:22 PM    Post subject: Reply with quote

SendMessage can be used but I think you are getting the wrong handle... You are getting the handle of the notepad window, not of the textbox in the notepad window... If you use the FindWindowEx API :

Code:
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


And pass as hWnd1 the handle of notepad you have, as hWnd2 0&, as lpsz1 "edit" and as lpsz2 vbNulklString then you will have the handle of the textbox to check...

Code:
Dim h As Long

h = FindWindowEx(lngNotePadHWND, 0&, "edit", vbNullString)


And then do your checking... I haven't done this to tell you if it'll work but I am sure that your way isn't working cause of the wrong handle Wink
_________________
No one is completely useless. They can at least be an example of what to avoid.
Back to top
View user's profile Send private message Send e-mail Visit poster's website ICQ Number
vb_programmer
Freshman


Joined: 03 Jan 2004
Posts: 31

PostPosted: Jan 16th, 2004 11:42 PM    Post subject: Reply with quote

Yes. You're right. It's only because of wrong Handle. Actually , I avoided SendKeys because my experience with that is not so good.

I found more info on it after your post ... FindWindowEx is doing the stuff for me. But the problem is , "edit" is the class name for NotePad edit box. If it's something else than Text Box Control or RTF Textbox control used in Software , then may be that's the case it's not working with few other tools like FrontPage2000 or DreamWeaver and so on ...

I'll try to resolve this issue. Do you have any other alternative ? One thing your posting has made cleared to me ... I'm passing the handle of Application and not the Editor Control ... hence its coming as "Paste not possible" ...

Thanks for your valuable reply ... I'm trying on it ... please send me any other alternative to do the same ...

I tell you why I need this ...

I've developed a Scrollbar Style Code maker to change the appearance of IE 5.5 onwards browser scrollbar style. Then , when I tool generats complete Style code , I wanted to export to the running HTML Editor software ... That's why I'm looking for this code solution ...

Notepad is just to test the code ... I don't want to bind my code with NotePad only ... instead it should be capable of exporting the code to any running application which has an text editing facility. (or in other words , a textbox or similar control on active window).

Any alternative to achieve this ?
_________________
----- vb_programmer ------
iNova Creations : Software Development Company !
(www.inovacreations.com)

RutuOnline.com : A Programmer's Resource Center !
(www.rutuonline.filetap.com)
Back to top
View user's profile Send private message Visit poster's website Yahoo Messenger ICQ Number
Avis
Junior Poster


Joined: 07 Oct 2003
Posts: 510
Location: India

PostPosted: Jan 17th, 2004 04:01 AM    Post subject: Reply with quote

Hi!

Well i tried this code and it was working pretty fine!

Code:
GetData = Clipboard.GetText
GetHandle = Shell("notepad.exe", vbNormalFocus)
SendKeys GetData, True


Well you can try it out maybe it'll help!
_________________
Code Snippets, Tutorials, Utilities, Controls

Low cost Web Hosting
Hosting starts at as low as $4 per year!


Always follow posting guidelines
Put your VB code in [vb ] your code [ /vb] tags!
Back to top
View user's profile Send private message Send e-mail Visit poster's website Yahoo Messenger MSN Messenger ICQ Number
P.T.A.M.
Administrator


Joined: 08 Oct 2003
Posts: 752
Location: Greece

PostPosted: Jan 17th, 2004 07:32 AM    Post subject: Reply with quote

Avis, as I mentioned the drawback is that is has to be the active window for that to work...

vb_programmer, goto patorjk.com and download the apispy Wink It's a great app for finding handles of controls/windows Wink
_________________
No one is completely useless. They can at least be an example of what to avoid.
Back to top
View user's profile Send private message Send e-mail Visit poster's website ICQ Number
Avis
Junior Poster


Joined: 07 Oct 2003
Posts: 510
Location: India

PostPosted: Jan 17th, 2004 09:07 AM    Post subject: Reply with quote

Hi!

Okay now i understand PTAM. Well if you want to get Windows handles then you better check this out.

http://www.blackbeltvb.com/free/GETWHWND.ZIP

Hope this help!

Thanks!
_________________
Code Snippets, Tutorials, Utilities, Controls

Low cost Web Hosting
Hosting starts at as low as $4 per year!


Always follow posting guidelines
Put your VB code in [vb ] your code [ /vb] tags!
Back to top
View user's profile Send private message Send e-mail Visit poster's website Yahoo Messenger MSN Messenger ICQ Number
vb_programmer
Freshman


Joined: 03 Jan 2004
Posts: 31

PostPosted: Jan 17th, 2004 07:38 PM    Post subject: Reply with quote

BlackBelt Link was very useful as the same for apispy ...

PTAM , is right. The window has to be active. It should have focus. I tried the same with SetFocus API and it worked for me. In that case , I straight away could use Clipboard function.

But then , it remains on the same step ... how to see if I can Paste it or not ?

I enum all the windows and then show the titles in a Listbox. Then user clicks on one of the titles and from that title I use the pre-fetched Handle of that window.

Here , I never come to know whether the window is of any application or just an Explorer window. Hence I wanted to check first EM_CANPASTE.

FindWindowEx function may work ... but I couldn't yet implement it error-free. I'll try ... Thanks for all your valuable reply ...
_________________
----- vb_programmer ------
iNova Creations : Software Development Company !
(www.inovacreations.com)

RutuOnline.com : A Programmer's Resource Center !
(www.rutuonline.filetap.com)
Back to top
View user's profile Send private message Visit poster's website Yahoo Messenger ICQ Number
P.T.A.M.
Administrator


Joined: 08 Oct 2003
Posts: 752
Location: Greece

PostPosted: Jan 18th, 2004 08:36 AM    Post subject: Reply with quote

I couldn't find any documentation on the EM_CANPASTE! Eek I'll keep searching Wink
_________________
No one is completely useless. They can at least be an example of what to avoid.
Back to top
View user's profile Send private message Send e-mail Visit poster's website ICQ Number
vb_programmer
Freshman


Joined: 03 Jan 2004
Posts: 31

PostPosted: Jan 18th, 2004 11:59 AM    Post subject: Reply with quote

Thanks for your Help PTAM ... Well , here is what MSDN has to say about it ...

EM_CANPASTE

The EM_CANPASTE message determines whether a rich edit control can paste a specified clipboard format.

EM_CANPASTE
wParam = (WPARAM) (UINT) uFormat;
lParam = 0;

Parameters

- uFormat
Value identifying the clipboard format to try, or zero to try any format currently on the clipboard.

Return Values
Returns a nonzero value if the clipboard format can be pasted or zero otherwise.
_________________
----- vb_programmer ------
iNova Creations : Software Development Company !
(www.inovacreations.com)

RutuOnline.com : A Programmer's Resource Center !
(www.rutuonline.filetap.com)


Last edited by vb_programmer on Jan 18th, 2004 12:03 PM; edited 1 time in total
Back to top
View user's profile Send private message Visit poster's website Yahoo Messenger ICQ Number
Display posts from previous:   
Post new topic   Reply to topic    Visual Basic Forum for Visual Basic Programmers VB Forum Index » General All times are GMT - 5 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Visual Basic Forum runs phpBB | Forum Template © iOptional
VB Resources | SSL | Visual Basic