Send copy to active window

I have a vb.net app in which I have setup a "hotkey" where when the user 
types that key combo, something is supposed to happen.  The hotkey part is 
working and fires perfectly.  What this is supposed to do, is copy whatever 
text is selected, no matter where the user is in the system, either my app, 
notepad, whatever, and copy that text to the clipboard.

I have tried SendMessage API, VB's SendKeys, and It only seems to work 
sometimes.  I would prefer to use SendMessage with a WM_COPY but can't get 
it to work at all, so I've been using SendKeys, sending "^c" with sporatic 
results.

Any suggestions?

Thanks,
Alan 


0
Alan
11/12/2009 9:04:17 PM
dotnet.languages.vb 830 articles. 0 followers. Follow

6 Replies
908 Views

Similar Articles

[PageSpeed] 58

Unforunately I can't help, but when you find out, please be sure to tell 
ptpwjp (ptpwjp@op.pl)  and ShariqDON (gr8shariq@live.com) - you will have 
two friends for life, as they are both very keen for an answer to the same 
problem.

"Alan Sloan" <NoSpam@comcast.net> wrote in message 
news:eCpB0u9YKHA.4920@TK2MSFTNGP04.phx.gbl...
>I have a vb.net app in which I have setup a "hotkey" where when the user 
>types that key combo, something is supposed to happen.  The hotkey part is 
>working and fires perfectly.  What this is supposed to do, is copy whatever 
>text is selected, no matter where the user is in the system, either my app, 
>notepad, whatever, and copy that text to the clipboard.
>
> I have tried SendMessage API, VB's SendKeys, and It only seems to work 
> sometimes.  I would prefer to use SendMessage with a WM_COPY but can't get 
> it to work at all, so I've been using SendKeys, sending "^c" with sporatic 
> results.
>
> Any suggestions?
>
> Thanks,
> Alan
> 

0
James
11/13/2009 4:06:15 AM
"Alan Sloan" <NoSpam@comcast.net> wrote in message 
news:eCpB0u9YKHA.4920@TK2MSFTNGP04.phx.gbl...
>I have a vb.net app in which I have setup a "hotkey" where when the user 
>types that key combo, something is supposed to happen.  The hotkey part is 
>working and fires perfectly.  What this is supposed to do, is copy whatever 
>text is selected, no matter where the user is in the system, either my app, 
>notepad, whatever, and copy that text to the clipboard.
>
> I have tried SendMessage API, VB's SendKeys, and It only seems to work 
> sometimes.  I would prefer to use SendMessage with a WM_COPY but can't get 
> it to work at all, so I've been using SendKeys, sending "^c" with sporatic 
> results.

Below is code to get the window with the focus, and copies the selection to 
the clipboard. GetForegroundWindow() gets the main window handle, like 
Notepad's main window, not the text box control within it. There is another 
function that would get the text box handle, GetFocus(), but it require more 
works to make it work with other processes. The code below uses 
GetGUIThreadInfo(), then gets the text box handle using hwndFocus member.

To try the sample, add a Timer, and a TextBox to Form1. Set the TextBox's 
Mutliline property to True, and set the Timer interval to 5000. Run the 
project, then start Notepad, type some text then select it.

Option Explicit On

Public Class Form1

    Private Declare Function GetForegroundWindow Lib "user32" () As Integer
    Private Declare Function GetWindowThreadProcessId Lib "user32" (ByVal 
hWnd As Integer, ByRef lpdwProcessId As Integer) As Integer
    Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" 
(ByVal hWnd As Integer, ByVal wMsg As Integer, ByVal wParam As Integer, 
ByVal lParam As Integer) As Integer
    Private Declare Function GetGUIThreadInfo Lib "user32.dll" (ByVal 
idThread As Integer, ByRef pgui As GUITHREADINFO) As Integer
    Private Const WM_COPY As Integer = &H301
    Private Structure RECT
        Dim Left_Renamed As Integer
        Dim Top As Integer
        Dim Right_Renamed As Integer
        Dim Bottom As Integer
    End Structure
    Private Structure GUITHREADINFO
        Dim cbSize As Integer
        Dim flags As Integer
        Dim hwndActive As Integer
        Dim hwndFocus As Integer
        Dim hwndCapture As Integer
        Dim hwndMenuOwner As Integer
        Dim hwndMoveSize As Integer
        Dim hwndCaret As Integer
        Dim rcCaret As RECT
    End Structure

    Private Function GetFocusWindow() As Integer
        Dim h As Integer
        Dim ThreadID As Integer
        Dim gui As GUITHREADINFO
        Dim ret As Integer

        h = GetForegroundWindow()
        Console.WriteLine("GetFocusWindow: ForegroundWindow = " & Hex(h))
        ThreadID = GetWindowThreadProcessId(h, 0)
        gui.cbSize = Len(gui)
        ret = GetGUIThreadInfo(ThreadID, gui)
        If ret = 0 Then
            Console.WriteLine("GetFocusWindow: GetGUIThreadInfo failed. 
LastDllError = " & Err.LastDllError)
            Exit Function
        End If
        Console.WriteLine("GetFocusWindow: hwndFocus = " & 
Hex(gui.hwndFocus))
        GetFocusWindow = gui.hwndFocus
    End Function

    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As 
System.EventArgs) Handles Timer1.Tick
        Me.Text = Hex(GetFocusWindow())
        SendCopyToWindow(GetFocusWindow())
        TextBox1.Text = Clipboard.GetText
    End Sub

    Private Sub SendCopyToWindow(ByVal hWnd As Integer)
        SendMessage(hWnd, WM_COPY, 0, 0)
    End Sub

End Class



0
Nobody
11/13/2009 1:04:04 PM
Nobody schrieb:
> Below is code to get the window with the focus, 

Nowadays, 64 bit OSes are not rare not find. Therefore I'd be careful when posting
these declarations that only work on 32 bit systems.


--
Armin
0
Armin
11/13/2009 2:10:32 PM
"Armin Zingler" <az.nospam@freenet.de> wrote in message 
news:uZymWtGZKHA.1816@TK2MSFTNGP04.phx.gbl...
> Nobody schrieb:
>> Below is code to get the window with the focus,
>
> Nowadays, 64 bit OSes are not rare not find. Therefore I'd be careful when 
> posting
> these declarations that only work on 32 bit systems.

If there was a .Net library function, then it should be used, but I can't 
find any. 64-Bit OS'es support 32-Bit applications. Obviously, the code need 
some work if one wants to compile the app as 64-Bit.


0
Nobody
11/13/2009 2:40:02 PM
On 2009-11-13, Nobody <nobody@nobody.com> wrote:
> "Armin Zingler" <az.nospam@freenet.de> wrote in message 
> news:uZymWtGZKHA.1816@TK2MSFTNGP04.phx.gbl...
>> Nobody schrieb:
>>> Below is code to get the window with the focus,
>>
>> Nowadays, 64 bit OSes are not rare not find. Therefore I'd be careful when 
>> posting
>> these declarations that only work on 32 bit systems.
>
> If there was a .Net library function, then it should be used, but I can't 
> find any. 64-Bit OS'es support 32-Bit applications. Obviously, the code need 
> some work if one wants to compile the app as 64-Bit.
>
>

The main problem is your declares...  Your handles should be declared IntPtr -
as IntPtr is automatically the size of the system ptr type (32-bit on 32 bit
windows, 64-bit on 64-bit windows).  And you should not alias your functions
to the A versions of the functions.  I would drop the alias all together and
either use Unicode or Auto on the declare.

Win64 for the most part is Win32 with 64-bit pointers.  Using IntPtr for you
handles and ptr types should go a long way in making your code 64-bit
compatible.

-- 
Tom Shelton
0
Tom
11/13/2009 3:23:54 PM
That worked beautifully!!  I took it out of the timer and put it in my 
hotkey pressed routine and it works every time!!

Thanks a ton!

"Nobody" <nobody@nobody.com> wrote in message 
news:uJNYVMGZKHA.3712@TK2MSFTNGP06.phx.gbl...
> "Alan Sloan" <NoSpam@comcast.net> wrote in message 
> news:eCpB0u9YKHA.4920@TK2MSFTNGP04.phx.gbl...
>>I have a vb.net app in which I have setup a "hotkey" where when the user 
>>types that key combo, something is supposed to happen.  The hotkey part is 
>>working and fires perfectly.  What this is supposed to do, is copy 
>>whatever text is selected, no matter where the user is in the system, 
>>either my app, notepad, whatever, and copy that text to the clipboard.
>>
>> I have tried SendMessage API, VB's SendKeys, and It only seems to work 
>> sometimes.  I would prefer to use SendMessage with a WM_COPY but can't 
>> get it to work at all, so I've been using SendKeys, sending "^c" with 
>> sporatic results.
>
> Below is code to get the window with the focus, and copies the selection 
> to the clipboard. GetForegroundWindow() gets the main window handle, like 
> Notepad's main window, not the text box control within it. There is 
> another function that would get the text box handle, GetFocus(), but it 
> require more works to make it work with other processes. The code below 
> uses GetGUIThreadInfo(), then gets the text box handle using hwndFocus 
> member.
>
> To try the sample, add a Timer, and a TextBox to Form1. Set the TextBox's 
> Mutliline property to True, and set the Timer interval to 5000. Run the 
> project, then start Notepad, type some text then select it.
>
> Option Explicit On
>
> Public Class Form1
>
>    Private Declare Function GetForegroundWindow Lib "user32" () As Integer
>    Private Declare Function GetWindowThreadProcessId Lib "user32" (ByVal 
> hWnd As Integer, ByRef lpdwProcessId As Integer) As Integer
>    Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" 
> (ByVal hWnd As Integer, ByVal wMsg As Integer, ByVal wParam As Integer, 
> ByVal lParam As Integer) As Integer
>    Private Declare Function GetGUIThreadInfo Lib "user32.dll" (ByVal 
> idThread As Integer, ByRef pgui As GUITHREADINFO) As Integer
>    Private Const WM_COPY As Integer = &H301
>    Private Structure RECT
>        Dim Left_Renamed As Integer
>        Dim Top As Integer
>        Dim Right_Renamed As Integer
>        Dim Bottom As Integer
>    End Structure
>    Private Structure GUITHREADINFO
>        Dim cbSize As Integer
>        Dim flags As Integer
>        Dim hwndActive As Integer
>        Dim hwndFocus As Integer
>        Dim hwndCapture As Integer
>        Dim hwndMenuOwner As Integer
>        Dim hwndMoveSize As Integer
>        Dim hwndCaret As Integer
>        Dim rcCaret As RECT
>    End Structure
>
>    Private Function GetFocusWindow() As Integer
>        Dim h As Integer
>        Dim ThreadID As Integer
>        Dim gui As GUITHREADINFO
>        Dim ret As Integer
>
>        h = GetForegroundWindow()
>        Console.WriteLine("GetFocusWindow: ForegroundWindow = " & Hex(h))
>        ThreadID = GetWindowThreadProcessId(h, 0)
>        gui.cbSize = Len(gui)
>        ret = GetGUIThreadInfo(ThreadID, gui)
>        If ret = 0 Then
>            Console.WriteLine("GetFocusWindow: GetGUIThreadInfo failed. 
> LastDllError = " & Err.LastDllError)
>            Exit Function
>        End If
>        Console.WriteLine("GetFocusWindow: hwndFocus = " & 
> Hex(gui.hwndFocus))
>        GetFocusWindow = gui.hwndFocus
>    End Function
>
>    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As 
> System.EventArgs) Handles Timer1.Tick
>        Me.Text = Hex(GetFocusWindow())
>        SendCopyToWindow(GetFocusWindow())
>        TextBox1.Text = Clipboard.GetText
>    End Sub
>
>    Private Sub SendCopyToWindow(ByVal hWnd As Integer)
>        SendMessage(hWnd, WM_COPY, 0, 0)
>    End Sub
>
> End Class
>
>
> 


0
Alan
11/13/2009 7:24:35 PM
Reply:

Similar Artilces:

How do I send copies emails without sending attachments?
I send emails with attachments to our bank. My boss wants a copy of the email but not a copy of the attachment. How do I do that? Steve Bramblett wrote: > I send emails with attachments to our bank. My boss wants a copy of the > email but not a copy of the attachment. How do I do that? Each recipient gets the SAME copy of the e-mail. If you want different recipients to get different versions of your e-mail, you have to send different e-mails. ...

No message in outbox
I tried to send a message with 1Mb attachment to 40 recipients in the bcc field. It gave me an error message from the isp's server, the message was NOT sent (I've checked), it did NOT arrive in Sent Items, but neither is it in the Outbox any more. I can send and receive email successfully. Problem is, every few minutes Outlook tells me it's sending message 1 of 1, it fails and gives me the same error message. Yet there is no message in the Outbox. But what happened to the original message when it first failed to send? I guess it's 'stuck' somewhere but I can...

Sending mail from selected email address
Hi All, As I have 4 email accounts set up in Outlook, I understand that I can send mail from each or any account. The accounts are listed under tools / Send and Receive / then each of the accounts. How can I choose which account to send from so that the recipient see's who I want it to be from?? Hope that explains it. Regards Alan On Sat, 16 Apr 2005 21:10:48 -1000, The Fatman <albaxter@optushome.com.au> wrote: > Hi All, > > As I have 4 email accounts set up in Outlook, I understand that I can > send > mail from each or any account. > > The acco...

Exchange/Active Directory and backing up the system
So we have exchange, We actually have it replicated to a lcr machine and we also do NT backups of the database every 3 hours. So we hav all our bases covered, or so we thought. Yesterday we had an issue with our Domain controller. Turns out, much to my surprise, that Exchange went down when I could not connect to the domain controller. So now I have a entirely new area of disaster recovery I need to understand and account for that I never knew existed. How do I protect my exchange server so that, in the event my domain controller were to die... I am covered? Thanks On Wed, ...

Sending a macro to another PC-how?
How do you send a macro you've written to another PC so that they can save it in their Personal.xls file (at Program Files\Microsoft Office\Office\XLStart)? You can export a macro and save it, to do this from your workbook, right-click the workbook's icon and pick View Code. This icon is to the left of the "File" menu this will open the VBA editor, click on the module you want to save and go to file and export file, then save it to a floppy or what ever you backup to, then import it to the new workbook. -- Paul B Always backup your data before trying something new Using ...

introducion to windows
it baisicly stinks you have to right click left click use funny spinny thing along eith that it is compuletly out wiereding it is bug prone and not very plessent to it is horibly expensive i wich i was curently sitting un a mac the folder structure makes no sense un a windows computer you can find all the files but it just dossent make sense all in all windows stinks oh and ps msn messanger isent all that great iether its no better if not wourse than yohoo messanger and from what i have seen from yahoo there better than microsoft in everything they do yet stink in everything they do and pc lap...

Sending mail from remote location
my mail works great while at home. when I go on the road I can receive ail but not send. I get the following error message: Subject 'SKIDOO MACH Z 800LT (long track / long tunnel) (Jackson)', Account: 'Jeff', Server: 'smtp.charter.net', Protocol: SMTP, Server Response: '571 imp11 smtp.charter.net OTcuMTQ3LjE2LjIzMA== You must connect from Charter IP space. E1110', Port: 25, Secure(SSL): No, Server Error: 571, Error Number: 0x800CCC61 It worked on my last laptop and I have not been able to figure it out on this one. I can't remember what I...

Send and Recieve Fix
For a long time I had a problem with my Send/Recieve function in Outlook. This happended in both Outlook 2000 and my current version - 2007. I found I had to re-boot my computer to perform a Send/Recieve in Outlook. Otherwise the sequence would start then stop and say it could not perform due to an error. After extensive Internet and News Group research I decided it was time to run a check on disk errors. This Windows XP facility seemed to have fixed my Outlook problem. I did this about a few months ago now and I have since had no problems with my send and recieve. Unfortunately I do have ...

How do I copy a horizontal ruler in Word?
I just need to copy text in a document to another part of the same document, including the ruler format, so I don't have to reset the ruler every time. Surely there's a way, but I've searched for hours trying to find it. In earlier versions of Word, we could just hold down the shift or control key, I don't remember which. If by "ruler format" you mean the margin and tab settings, the ruler shows the ones applied to whichever paragraph the cursor happens to be in. You duplicate them by assigning the same paragraph style in each place. On Nov 23, 5:39=...

windows xp logon with personal application
hey cn ne 1 help me to start my application before login in windows xp ,and want my application to logon to user account i want to use. ...

Unable to send?
Hi, I've got MOOL, and i"m usign ti with outlook. The emails that I'm sending are just "stuck" in the outbox? Can anyone give me an idea of what's going on? Rex Did it ever work? Are you able to receive? -- Aloha, -Ben- Ben M. Schorr, OneNote-MVP Roland Schorr & Tower http://www.rolandschorr.com Microsoft OneNote FAQ: http://www.factplace.com/onenotefaq.htm **I apologize but I am unable to respond to direct requests for assistance. Please post questions and replies here in the newsgroup. Mahalo! "Rekks" <rex_flynn@hotmail.com> wrote ...

Sending messages in Outlook
We have a user that everytime that she sends an email she will get booted out of Outlook. The email does go through, but she will have to get out of Outlook. When she goes back into Outlook, everything is fine until she sends another message. Any help would be appreciated. ...

No Permission to Send/Receive Email???
I just replaced our server hardware, upgraded to WS 2k3 Enterprise and created a new domain. I transferred the client files and desktop on our desk/laptop computers over to the new domain. We do NOT have Exchange installed yet, but use Outlook 2002 with the email etc. saved on the client. One one client machine (mine), the email, calendar, etc are there, but when I hit the Send/Receive button, I get a message saying that I don't have permission to perform this operation. Also when I try and change the automatic check for email option it tells me I don't have permission. ...

CRM 3.0 Distribute Campaign Activity Error
I'm trying to do a word mail merge to a marketing list which is part of a campaign. I can create the Campaign Activity (Channel is "Letter via Mail Merge"). When I click on "open word" on the "Distribute Campaign Activity" screen after having selected the type of "template" I get an error Line: 132 Char: 1 Error: Access is denied Code: 0 URL: http://<server name/_grid/cmds/dlg_canmailmerge.aspx?iObyType=4402&Total=1&sIds=<GUID>&sAction=letterviamailmerge I have system administration role, mail merge has no issues on any other ...

Send Do Not Send
When I attempt to email via a print server - OutLook Express displays the message "A program is attempting to email on your behalf" Send or Do Not Send - How do you prevent this from happening? And just automatically send? ...

Exchange 2003, Windows Server 2003, Data Centre Environment
Interesting title to a somewhat uninteresting post. I've just signed up for this, using the Windows Live Mila desktop beta, and just want to test things are working ok! Hello! Adam Laslett wrote: > Interesting title to a somewhat uninteresting post. > I've just signed up for this, using the Windows Live Mila desktop beta, > and just want to test things are working ok! > > Hello! > SOB. ...

Wizard Application
Hi All, I have a Wizard Application which has three Property Sheet and Each of Different size and 1st page has only 3 radio button and so I need the fonts of this page to be much bigger but second & third page has lot of contents so i cannot use the same font in all page & will be using default fonts size. How Dyamically changing the font on a Wizard Application. 1) Is it Possible to Resize the window for each page with respect to its size in wizard appln. 2) How to dymanically create a font and write the static text in wiz app. Appriciate you to come back to me at the earliest and ...

How to find the owner of the active excel workbook
Dear Smart People, I'm sure this has been asked before but I'm not able to find the answer. In Visual Basic, how may I find the name or system ID of the owner of the currently active workbook? I apologize if this is an "old" question but I've not been able to gleen the answer. Thank you! Janet Take a look at the BuiltInDocumentProperties to see if you can find what you want there. I'm not sure you can get the "Owner", but you can find who last modified the document. -- HTH, Barb Reinhardt "Janet Panighetti" w...

Sending on behalf... Exchange 2003
Hi everyone, Is there a better way to give a few users the ability to send mails also from the info@domain.com mailbox without having them logout and login to a different Outlook 2003 profile... Or without the text sending on behalf of... So my question is, Is there an easy way that a user can select another e-mail address and send mails out...? Thanks a lot in advance!! Regards, BY On Thu, 05 Oct 2006 12:57:12 GMT, "BY" <nospam@mail.com> wrote: >Hi everyone, > >Is there a better way to give a few users the ability to send mails also >from the info@dom...

opening Windows Excel 2003 files with my Mac Excel 2008
Version: 2008 Operating System: Mac OS X 10.5 (Leopard) Processor: intel I cannot open the Windows Excel 2003 files sent to me by my accountant. I have Mac Excel 2008. It defaults to the Numbers program, part of Iworks. I need the full functions of Excel to work on the files...not Numbers. thanks I suspect that this is the same problem I have found with Keynote/ PowerPoint. Hold the control key down while you press the mouse on the file you are trying to open. Then pick "Open with" and choose Excel. If you have a two (or more button mouse you can also use "right click" ...

What was Outlook sending?
Hi, I downloaded my messages using the option "send and receive all". However, the "Show Progress" box indicates that I was also 'sending something'. But I had nothing to send! I look in the "Sent items' folder and nothing was sent at this time. What was Outlook 2003 doing? Santista The usual response here is, "You have a virus". It could be that a virus program is using your Outlook to send spam and/or a copy of itself to people in your address book. I hope this is not the case, but please have the foresight (aftersight?) to look into ...

Outlook Can't Send Messages
This morning I fired up Outlook 2007 (running under Windows 7). It retrieved all new emails. I then tried to post a new email, (not a reply). However, the nano-second that I clicked on "Send" I received a reply from "System Administrator". It said this: Your message did not reach some or all of the intended recipients. Subject: The Train Meltdown Sent: 27-Jul-10 1:18 PM The following recipient(s) cannot be reached: xxxxx@xxxxx.com.au on 27-Jul-10 1:18 PM None of your e-mail accounts could send to this recipient. I have 3 active email ac...

'SEND' just sends it to the OUTBOX
Outlook 2007 on WinXP Home SP3. When I click 'SEND' when replying to an email it does not SEND but puts it in the OUTBOX. I have to click SEND/RECEIVE from the main page to actually SEND the email. Is there a setting I am missing? Thank you. "Peter" <lysdexic@hotmail.com> wrote in message news:eoOd1BM$KHA.5536@TK2MSFTNGP02.phx.gbl... > Outlook 2007 on WinXP Home SP3. When I click 'SEND' when replying to an > email it does not SEND but puts it in the OUTBOX. I have to click > SEND/RECEIVE from the main page to actually SEND the emai...

Rules Wizard and Send/Receive error
A couple of days ago, I received an alert from Norton Antivirus software that the file "belt.cab" was corrupted but could not be deleted. Fearing the worst, I manually deleted the file. Since then, Outlook appears to be completely disabled, with the symptoms being that several commands have disappeared from the Tools menu including Send/Receive (both), Rules Wizard, Out of Office Assistant. I can compose an email, but it will not send. I have checked my email accounts and all appears to be in order. Even after a complete reinstall, I cannot send/receive email. The on...

sending Access files
Is there a quick and easy way to send access files without Outlook blocking them. I exchange files with a limited selection of people so am happy that what I am sending and receiving is OK. I know that I can use WizZip but the knowledge database also suggests changing the .mdb extension. I don't know how to do this. Do I just right click over the file and choose open with Word? Or maybe there is a simpler way. Thanks When you send a file type that on Microsoft's potentially dangerous list, you get a message informing you that the recipient may not be able to access the file on...