Paste a texte into the current window

C

CR

Hi,

I need to make a little c# program which copies a text into the
clipboard and that pastes this data into the active window.

I copy the data like this

Clipboard.SetText("mystring");

but i don't know how to paste this data in the active window (notepad,
word for example)

thanks for your help (excuse my english, i am french)
 
C

CR

Clipboard.GetText

I know this, but I would like to affect the value of Clipboard.GetText
to the active object of my window. With visual basic it should be
something like this

Screen.ActiveControl = Clipboard.GetText()

I don't know the equivalent of this in C#.

I also have an other problem: when I launch my program, my windows
becomes inactive. how could i force it to stay active?
 
V

VJ

this.ActiveControl() where this is the Form. .

I know this, but I would like to affect the value of Clipboard.GetText
to the active object of my window. With visual basic it should be
something like this

Screen.ActiveControl = Clipboard.GetText()

I don't know the equivalent of this in C#.

I also have an other problem: when I launch my program, my windows
becomes inactive. how could i force it to stay active?
 
C

CR

this.ActiveControl() where this is the Form.

Thanks a lot, but it still doesn't work. I do not use any form. I am
using a console application. My active window could be any
application : notepad, ie...
I have the error 'this' is not valid in a static property or method
with this

[STAThread]
static void Main()
{
Clipboard.SetText("00");
this.ActiveControl.Text = Clipboard.GetText();
}}


Should I use the GetForegroundWindow API function?
 
V

VJ

yes it will work for Windows only, my expertise is in Windows. If you want
for console, you can try to google or use the MSDN to see what options are
there. If I find time later today, I will research and post.

VJ
>> this.ActiveControl() where this is the Form.

Thanks a lot, but it still doesn't work. I do not use any form. I am
using a console application. My active window could be any
application : notepad, ie...
I have the error 'this' is not valid in a static property or method
with this

[STAThread]
static void Main()
{
Clipboard.SetText("00");
this.ActiveControl.Text = Clipboard.GetText();
}}


Should I use the GetForegroundWindow API function?
 
C

CR

yes it will work for Windows only, my expertise is in Windows. If you want
for console, you can try to google or use the MSDN to see what options are
there. If I find time later today, I will research and post.

Thanks, it will really help me.
I found this but for vb :'((

hWnd_Target = GetForegroundWindow
Calling_Pid = GetWindowThreadProcessId(hWnd_Target, 0)
AttachThreadInput App.ThreadID, GetWindowThreadProcessId(hWnd_Target,
0), True

SendMessage GetFocus, WM_PASTE, 0, ByVal 0&
AttachThreadInput App.ThreadID, GetWindowThreadProcessId(hWnd_Target,
0), False
 
V

VJ

Some people have posted links for vb to c-sharp conversion. You can just
Google convert VB to C-Sharp, you will find plenty that do it for free...
the below code is simple, its been 2 years I wrote any VB code, the below
looks simple enough for conversion. I don't want to attempt and give you a
bad conversion.

You are looking to automatically paste into active window, from your code?
Is that what you are trying?... interesting... never attempted something
like that. Not sure how that will happen across process.. Also as thought in
that line, assuming you are doing across process, you will fall into CAS i.e
Code access security, and if you are target Vista ( even some strict XP
environments) and above its pretty strict environment.. you have to make
sure your Console App has security clearance to do such... The below code
might be denied permission. I am not a expert in CAS or inter-process
communication. If that is what you are looking. Please post in
Microsoft.public.dotnet.security and get any doubts cleared.

VJ
 
C

CR

You are looking to automatically paste into active window, from your code?
Is that what you are trying?... interesting... never attempted something
like that. Not sure how that will happen across process..

Yes!!! It is exactly what i'm trying to do! Unfortunately, I have
vista, and as you said, i guess i will have some security problems..
If my code works, I will tell you if it runs under vista.
 
V

VJ

Ahh... the vista world.. Ok let me know, would like to learn always..

if you are developing on Vista, one inital good test would be run the
application as regular user ( non-admin ) and see what happens.

VJ

>> You are looking to automatically paste into active window, from your
>> Is that what you are trying?... interesting... never attempted something
>> like that. Not sure how that will happen across process..

Yes!!! It is exactly what i'm trying to do! Unfortunately, I have
vista, and as you said, i guess i will have some security problems..
If my code works, I will tell you if it runs under vista.
[/QUOTE]
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top