473,417 Members | 1,554 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,417 software developers and data experts.

Handle to main window.

Hi to all,

is there a way to get the window handle of the main window of an application
or process ?
For example, if someone opens Microsoft Word, he gets a window so he/she can
write text.
Spy++ gives that this window is called _Wwg.

How we can get a handle to this window assuming that we do not know
beforehand the name of the process,
we just want to find the handle of every application's main window ?
(or the window that currently has the focus/caret).

Is this possible ??

Thanks a lot for any help,

objectref
Nov 16 '05 #1
11 5056
Hi objectref,

..NET doesn't provide enumaration of windwos. However you can always use
PInvoke and call one of the windows enumeration APIs.

The only thing that you can do with .NET classes is to call
Process.GetProcesses() method to get array of all processes currently
started on the machine. Then for each process you can read the
MainWindowHandle property.

However, in this way you can read one of the process' toplevel windows.
Process may have more then one main windows. For example look at Windows and
Internet Explorer application. They have only one process and a separate
thread for each GUI. For these application you'll get only one of the main
windows. Another example could be .NET application that runs more then one
application domains, which of which has its own GUI.

--

Stoitcho Goutsev (100) [C# MVP]
"objectref" <ob*******@mediatrel.com> wrote in message
news:%2***************@TK2MSFTNGP11.phx.gbl...
Hi to all,

is there a way to get the window handle of the main window of an application or process ?
For example, if someone opens Microsoft Word, he gets a window so he/she can write text.
Spy++ gives that this window is called _Wwg.

How we can get a handle to this window assuming that we do not know
beforehand the name of the process,
we just want to find the handle of every application's main window ?
(or the window that currently has the focus/caret).

Is this possible ??

Thanks a lot for any help,

objectref

Nov 16 '05 #2
Hi Stoitcho,

i know all this and that's why i am calling EnumChildWindows based on the
MainWindowHandle of each process.
The problem is that i want to call PostMessage and send some characters in
the window that have the focus at that time, ie, the application's active
window.

Do you know some way of doing that ??

Thanks a lot for your answer,

objectref
"Stoitcho Goutsev (100) [C# MVP]" <10*@100.com> wrote in message
news:eU*************@TK2MSFTNGP11.phx.gbl...
Hi objectref,

.NET doesn't provide enumaration of windwos. However you can always use
PInvoke and call one of the windows enumeration APIs.

The only thing that you can do with .NET classes is to call
Process.GetProcesses() method to get array of all processes currently
started on the machine. Then for each process you can read the
MainWindowHandle property.

However, in this way you can read one of the process' toplevel windows.
Process may have more then one main windows. For example look at Windows and Internet Explorer application. They have only one process and a separate
thread for each GUI. For these application you'll get only one of the main
windows. Another example could be .NET application that runs more then one
application domains, which of which has its own GUI.

--

Stoitcho Goutsev (100) [C# MVP]
"objectref" <ob*******@mediatrel.com> wrote in message
news:%2***************@TK2MSFTNGP11.phx.gbl...
Hi to all,

is there a way to get the window handle of the main window of an

application
or process ?
For example, if someone opens Microsoft Word, he gets a window so he/she

can
write text.
Spy++ gives that this window is called _Wwg.

How we can get a handle to this window assuming that we do not know
beforehand the name of the process,
we just want to find the handle of every application's main window ?
(or the window that currently has the focus/caret).

Is this possible ??

Thanks a lot for any help,

objectref


Nov 16 '05 #3
You will have to make calls to the Win32 API using PInvoke to call PostMessage. See:
http://www.pinvoke.net/

--
Adam Clauss
ca*****@tamu.edu

"objectref" <ob*******@mediatrel.com> wrote in message news:%2****************@TK2MSFTNGP11.phx.gbl...
Hi Stoitcho,

i know all this and that's why i am calling EnumChildWindows based on the
MainWindowHandle of each process.
The problem is that i want to call PostMessage and send some characters in
the window that have the focus at that time, ie, the application's active
window.

Do you know some way of doing that ??

Thanks a lot for your answer,

objectref
"Stoitcho Goutsev (100) [C# MVP]" <10*@100.com> wrote in message
news:eU*************@TK2MSFTNGP11.phx.gbl...
Hi objectref,

.NET doesn't provide enumaration of windwos. However you can always use
PInvoke and call one of the windows enumeration APIs.

The only thing that you can do with .NET classes is to call
Process.GetProcesses() method to get array of all processes currently
started on the machine. Then for each process you can read the
MainWindowHandle property.

However, in this way you can read one of the process' toplevel windows.
Process may have more then one main windows. For example look at Windows

and
Internet Explorer application. They have only one process and a separate
thread for each GUI. For these application you'll get only one of the main
windows. Another example could be .NET application that runs more then one
application domains, which of which has its own GUI.

--

Stoitcho Goutsev (100) [C# MVP]
"objectref" <ob*******@mediatrel.com> wrote in message
news:%2***************@TK2MSFTNGP11.phx.gbl...
Hi to all,

is there a way to get the window handle of the main window of an

application
or process ?
For example, if someone opens Microsoft Word, he gets a window so he/she

can
write text.
Spy++ gives that this window is called _Wwg.

How we can get a handle to this window assuming that we do not know
beforehand the name of the process,
we just want to find the handle of every application's main window ?
(or the window that currently has the focus/caret).

Is this possible ??

Thanks a lot for any help,

objectref



Nov 16 '05 #4
Adam,

i am already making the calls using PostMessage... These calls succeds if
the process that
i use PostMessage to, has only one window, for example, a dos box.
I then find the process MainWindowHandle, and then using PostMessage i am
able to send
it some charcters, like the Environment.NewLine key.

But, if the app that is running is, for example, Notepad, then the above
solution does not work.
I have, somehow, to find the Active Window of the process and THEN i use
PostMessage
to send some the characters to.
Notepad is simplel, but MS Word, for example, is more complicated, it starts
with over that 10 windows with it
so i cannot tell for sure which is the active window that user is typing
textand so i will be
able to use PostMessage...
And of course, the processes that are running, are not known to me so i can
hardcode the handles
using Spy++, all that has to be dynamically determined.

Any other ideas ??...
objectref


"Adam Clauss" <ca*****@tamu.edu> wrote in message
news:OO*************@tk2msftngp13.phx.gbl...
You will have to make calls to the Win32 API using PInvoke to call PostMessage. See: http://www.pinvoke.net/

--
Adam Clauss
ca*****@tamu.edu

"objectref" <ob*******@mediatrel.com> wrote in message

news:%2****************@TK2MSFTNGP11.phx.gbl...
Hi Stoitcho,

i know all this and that's why i am calling EnumChildWindows based on the MainWindowHandle of each process.
The problem is that i want to call PostMessage and send some characters in the window that have the focus at that time, ie, the application's active window.

Do you know some way of doing that ??

Thanks a lot for your answer,

objectref
"Stoitcho Goutsev (100) [C# MVP]" <10*@100.com> wrote in message
news:eU*************@TK2MSFTNGP11.phx.gbl...
Hi objectref,

.NET doesn't provide enumaration of windwos. However you can always use PInvoke and call one of the windows enumeration APIs.

The only thing that you can do with .NET classes is to call
Process.GetProcesses() method to get array of all processes currently
started on the machine. Then for each process you can read the
MainWindowHandle property.

However, in this way you can read one of the process' toplevel windows. Process may have more then one main windows. For example look at Windows
and
Internet Explorer application. They have only one process and a

separate thread for each GUI. For these application you'll get only one of the main windows. Another example could be .NET application that runs more then one application domains, which of which has its own GUI.

--

Stoitcho Goutsev (100) [C# MVP]
"objectref" <ob*******@mediatrel.com> wrote in message
news:%2***************@TK2MSFTNGP11.phx.gbl...
> Hi to all,
>
> is there a way to get the window handle of the main window of an
application
> or process ?
> For example, if someone opens Microsoft Word, he gets a window so he/she can
> write text.
> Spy++ gives that this window is called _Wwg.
>
> How we can get a handle to this window assuming that we do not know
> beforehand the name of the process,
> we just want to find the handle of every application's main window ?
> (or the window that currently has the focus/caret).
>
> Is this possible ??
>
> Thanks a lot for any help,
>
> objectref
>
>



Nov 16 '05 #5
Hmm... I'm not sure about getting the "focused" window.

Is it only certain apps that this will occur in? You mention liked notepad and MSWord, are these part of a set list of applications
you need to interact with, or could it conceivable be ANY application? If it is a set list, you could come up with a routine that
locates the currently in use Window for a given program. Each program in your list would have its own method for doing this. Look
at the title of the main window to determine what program it is, then could use Spy++ to determine what pattern of windows to search
for - ex: Search for a window of class "....", then a child of class "..." and so forth using the FindWindow/FindWindowEx APIs.

Of course, if it is ANY application the above will not work. Would have to do some research to see if there is a
GetWindowWithCurrentFocus type API.

--
Adam Clauss
ca*****@tamu.edu

"objectref" <ob*******@mediatrel.com> wrote in message news:uQ**************@TK2MSFTNGP11.phx.gbl...
Adam,

i am already making the calls using PostMessage... These calls succeds if
the process that
i use PostMessage to, has only one window, for example, a dos box.
I then find the process MainWindowHandle, and then using PostMessage i am
able to send
it some charcters, like the Environment.NewLine key.

But, if the app that is running is, for example, Notepad, then the above
solution does not work.
I have, somehow, to find the Active Window of the process and THEN i use
PostMessage
to send some the characters to.
Notepad is simplel, but MS Word, for example, is more complicated, it starts
with over that 10 windows with it
so i cannot tell for sure which is the active window that user is typing
textand so i will be
able to use PostMessage...
And of course, the processes that are running, are not known to me so i can
hardcode the handles
using Spy++, all that has to be dynamically determined.

Any other ideas ??...
objectref


"Adam Clauss" <ca*****@tamu.edu> wrote in message
news:OO*************@tk2msftngp13.phx.gbl...
You will have to make calls to the Win32 API using PInvoke to call

PostMessage. See:
http://www.pinvoke.net/

--
Adam Clauss
ca*****@tamu.edu

"objectref" <ob*******@mediatrel.com> wrote in message

news:%2****************@TK2MSFTNGP11.phx.gbl...
Hi Stoitcho,

i know all this and that's why i am calling EnumChildWindows based on the MainWindowHandle of each process.
The problem is that i want to call PostMessage and send some characters in the window that have the focus at that time, ie, the application's active window.

Do you know some way of doing that ??

Thanks a lot for your answer,

objectref
"Stoitcho Goutsev (100) [C# MVP]" <10*@100.com> wrote in message
news:eU*************@TK2MSFTNGP11.phx.gbl...
> Hi objectref,
>
> .NET doesn't provide enumaration of windwos. However you can always use > PInvoke and call one of the windows enumeration APIs.
>
> The only thing that you can do with .NET classes is to call
> Process.GetProcesses() method to get array of all processes currently
> started on the machine. Then for each process you can read the
> MainWindowHandle property.
>
> However, in this way you can read one of the process' toplevel windows. > Process may have more then one main windows. For example look at Windows and
> Internet Explorer application. They have only one process and a separate > thread for each GUI. For these application you'll get only one of the main > windows. Another example could be .NET application that runs more then one > application domains, which of which has its own GUI.
>
> --
>
> Stoitcho Goutsev (100) [C# MVP]
>
>
> "objectref" <ob*******@mediatrel.com> wrote in message
> news:%2***************@TK2MSFTNGP11.phx.gbl...
> > Hi to all,
> >
> > is there a way to get the window handle of the main window of an
> application
> > or process ?
> > For example, if someone opens Microsoft Word, he gets a window so he/she > can
> > write text.
> > Spy++ gives that this window is called _Wwg.
> >
> > How we can get a handle to this window assuming that we do not know
> > beforehand the name of the process,
> > we just want to find the handle of every application's main window ?
> > (or the window that currently has the focus/caret).
> >
> > Is this possible ??
> >
> > Thanks a lot for any help,
> >
> > objectref
> >
> >
>
>



Nov 16 '05 #6
This sounds like it might do it:
GetForegroundWindow()
MSDN:
The GetForegroundWindow function returns a handle to the foreground window (the window with which the user is currently working).

--
Adam Clauss
ca*****@tamu.edu

"objectref" <ob*******@mediatrel.com> wrote in message news:uQ**************@TK2MSFTNGP11.phx.gbl...
Adam,

i am already making the calls using PostMessage... These calls succeds if
the process that
i use PostMessage to, has only one window, for example, a dos box.
I then find the process MainWindowHandle, and then using PostMessage i am
able to send
it some charcters, like the Environment.NewLine key.

But, if the app that is running is, for example, Notepad, then the above
solution does not work.
I have, somehow, to find the Active Window of the process and THEN i use
PostMessage
to send some the characters to.
Notepad is simplel, but MS Word, for example, is more complicated, it starts
with over that 10 windows with it
so i cannot tell for sure which is the active window that user is typing
textand so i will be
able to use PostMessage...
And of course, the processes that are running, are not known to me so i can
hardcode the handles
using Spy++, all that has to be dynamically determined.

Any other ideas ??...
objectref


"Adam Clauss" <ca*****@tamu.edu> wrote in message
news:OO*************@tk2msftngp13.phx.gbl...
You will have to make calls to the Win32 API using PInvoke to call

PostMessage. See:
http://www.pinvoke.net/

--
Adam Clauss
ca*****@tamu.edu

"objectref" <ob*******@mediatrel.com> wrote in message

news:%2****************@TK2MSFTNGP11.phx.gbl...
Hi Stoitcho,

i know all this and that's why i am calling EnumChildWindows based on the MainWindowHandle of each process.
The problem is that i want to call PostMessage and send some characters in the window that have the focus at that time, ie, the application's active window.

Do you know some way of doing that ??

Thanks a lot for your answer,

objectref
"Stoitcho Goutsev (100) [C# MVP]" <10*@100.com> wrote in message
news:eU*************@TK2MSFTNGP11.phx.gbl...
> Hi objectref,
>
> .NET doesn't provide enumaration of windwos. However you can always use > PInvoke and call one of the windows enumeration APIs.
>
> The only thing that you can do with .NET classes is to call
> Process.GetProcesses() method to get array of all processes currently
> started on the machine. Then for each process you can read the
> MainWindowHandle property.
>
> However, in this way you can read one of the process' toplevel windows. > Process may have more then one main windows. For example look at Windows and
> Internet Explorer application. They have only one process and a separate > thread for each GUI. For these application you'll get only one of the main > windows. Another example could be .NET application that runs more then one > application domains, which of which has its own GUI.
>
> --
>
> Stoitcho Goutsev (100) [C# MVP]
>
>
> "objectref" <ob*******@mediatrel.com> wrote in message
> news:%2***************@TK2MSFTNGP11.phx.gbl...
> > Hi to all,
> >
> > is there a way to get the window handle of the main window of an
> application
> > or process ?
> > For example, if someone opens Microsoft Word, he gets a window so he/she > can
> > write text.
> > Spy++ gives that this window is called _Wwg.
> >
> > How we can get a handle to this window assuming that we do not know
> > beforehand the name of the process,
> > we just want to find the handle of every application's main window ?
> > (or the window that currently has the focus/caret).
> >
> > Is this possible ??
> >
> > Thanks a lot for any help,
> >
> > objectref
> >
> >
>
>



Nov 16 '05 #7
objectref,

If you want to send keystrokes to the active application use SendKeys class.
This is the easiest.
It might be pretty reasonable as long as there is only one focused window
and it belongs to the foreground (active) window.

Anyways if you want to get the handle to the focused window you need to do a
bit more.
[DllImport("user32.dll")]
private static extern IntPtr GetForegroundWindow();
[DllImport("user32.dll")]
private static extern IntPtr GetWindowThreadProcessId(IntPtr hwnd, IntPtr
lpdwProcessId);
[DllImport("user32.dll")]
private static extern bool AttachThreadInput(IntPtr idAttach , IntPtr
idAttachTo, bool fAttach);
[DllImport("user32.dll")]
private static extern IntPtr GetFocus();

private void button1_Click(object sender, System.EventArgs e)
{
IntPtr thisThreadID = new IntPtr(AppDomain.GetCurrentThreadId());
IntPtr activeHwnd = GetForegroundWindow();
if(activeHwnd != IntPtr.Zero)
{
IntPtr activeThreadID = GetWindowThreadProcessId(activeHwnd,
IntPtr.Zero);
if(AttachThreadInput(activeThreadID, thisThreadID, true))
{
IntPtr focusedHwnd = GetFocus();
if(focusedHwnd != IntPtr.Zero)
{
Console.WriteLine("FocusedHwnd: {0}\n====", focusedHwnd.ToString());
}
AttachThreadInput(activeThreadID, thisThreadID, false);
}
}
}

Bare in mind that the thread executing that code has to be a UI thread. and
that this code cannot detect its own focused window because it cannot attach
a thread to itself. If you want to get your own focused window it is quite
straightforward using Control class.

--
HTH
Stoitcho Goutsev (100) [C# MVP]
"objectref" <ob*******@mediatrel.com> wrote in message
news:%2****************@TK2MSFTNGP11.phx.gbl...
Hi Stoitcho,

i know all this and that's why i am calling EnumChildWindows based on the
MainWindowHandle of each process.
The problem is that i want to call PostMessage and send some characters in
the window that have the focus at that time, ie, the application's active
window.

Do you know some way of doing that ??

Thanks a lot for your answer,

objectref
"Stoitcho Goutsev (100) [C# MVP]" <10*@100.com> wrote in message
news:eU*************@TK2MSFTNGP11.phx.gbl...
Hi objectref,

.NET doesn't provide enumaration of windwos. However you can always use
PInvoke and call one of the windows enumeration APIs.

The only thing that you can do with .NET classes is to call
Process.GetProcesses() method to get array of all processes currently
started on the machine. Then for each process you can read the
MainWindowHandle property.

However, in this way you can read one of the process' toplevel windows.
Process may have more then one main windows. For example look at Windows

and
Internet Explorer application. They have only one process and a separate
thread for each GUI. For these application you'll get only one of the main windows. Another example could be .NET application that runs more then one application domains, which of which has its own GUI.

--

Stoitcho Goutsev (100) [C# MVP]
"objectref" <ob*******@mediatrel.com> wrote in message
news:%2***************@TK2MSFTNGP11.phx.gbl...
Hi to all,

is there a way to get the window handle of the main window of an

application
or process ?
For example, if someone opens Microsoft Word, he gets a window so
he/she can
write text.
Spy++ gives that this window is called _Wwg.

How we can get a handle to this window assuming that we do not know
beforehand the name of the process,
we just want to find the handle of every application's main window ?
(or the window that currently has the focus/caret).

Is this possible ??

Thanks a lot for any help,

objectref



Nov 16 '05 #8
Hmmmm....

at first, thanks for your solution, it seems than know i am close enough,
but:

Let's assume that, for simplicity reasons, i want to test your code with a
pre-known
window handle. I take this from enumerating processes, find, let's say an
open Notepad window
and then get it's MainWindowHandle, that is, 3475108.

But, the below code does not work. It reaches the "PostMessage"line but i do
not
get an Enter hit in notepad app.
Can you figure out why ?
private void button1_Click(object sender, System.EventArgs e)
{
IntPtr thisThreadID = new IntPtr(AppDomain.GetCurrentThreadId());
IntPtr activeHwnd = GetForegroundWindow();

if (activeHwnd != IntPtr.Zero)
{
IntPtr activeThreadID = GetWindowThreadProcessId((IntPtr) 3475108,
IntPtr.Zero);
if (AttachThreadInput(activeThreadID, thisThreadID, true))
{
IntPtr focusedHwnd = GetFocus();
if (focusedHwnd != IntPtr.Zero)
{
//Console.WriteLine("FocusedHwnd: {0}\n====",
focusedHwnd.ToString());
listBox1.Items.Add("FocusedHwnd: {0}\n====" +
focusedHwnd.ToString());
PostMessage((IntPtr) focusedHwnd, WM_CHAR, 13, 0);
}
AttachThreadInput(activeThreadID, thisThreadID, false);
}
}
}


Nov 16 '05 #9
Hi objecref,

The problem I believe is that the Notepad is not the active application. How
I said there is only one focused window at a time and it is or it is a child
to the foreground (active) window. If you try GetFocus for a thread which
hasn't created the foreground window it always return null.

Just a little reminder. Even though you can post key messages to a window it
is not guarantee that the target window will accept them correctly. Each
input (UI) thread has associated keystate buffers and so on that play some
role in the key processing. If you just post key messages it won't go thru
all the internal key processing and won't update those structures. Methods
like GetKeyState, GetAsyncKeyState, GetKeyboardState and so on which
application use won't work. Key messages are only notifications aftere all
the work of precessing the keyboard is done. The only way to simulate real
keyboard activity is to use either SendInput or keybd_event API or SendKeys
class in .NET.
However because they synthesizes keyboard strokes, mouse movements and mouse
button clicks it you cannot specify the target window. Keystorkes go to the
focused window, mouse events go to the window under the mouse cursor
position.
Anyways if you still want to use post messages you may consider using
AttachThreadInput and SetKeyboardState method to handle some of the
situations. However some keys such as CapsLock, NumLock, etc cannot be
simulated unless SendInput or keybd_event are used.

--
HTH
Stoitcho Goutsev (100) [C# MVP]
"objectref" <ob*******@mediatrel.com> wrote in message
news:uc**************@TK2MSFTNGP12.phx.gbl...
Hmmmm....

at first, thanks for your solution, it seems than know i am close enough,
but:

Let's assume that, for simplicity reasons, i want to test your code with a
pre-known
window handle. I take this from enumerating processes, find, let's say an
open Notepad window
and then get it's MainWindowHandle, that is, 3475108.

But, the below code does not work. It reaches the "PostMessage"line but i do not
get an Enter hit in notepad app.
Can you figure out why ?
private void button1_Click(object sender, System.EventArgs e)
{
IntPtr thisThreadID = new IntPtr(AppDomain.GetCurrentThreadId());
IntPtr activeHwnd = GetForegroundWindow();

if (activeHwnd != IntPtr.Zero)
{
IntPtr activeThreadID = GetWindowThreadProcessId((IntPtr) 3475108,
IntPtr.Zero);
if (AttachThreadInput(activeThreadID, thisThreadID, true))
{
IntPtr focusedHwnd = GetFocus();
if (focusedHwnd != IntPtr.Zero)
{
//Console.WriteLine("FocusedHwnd: {0}\n====",
focusedHwnd.ToString());
listBox1.Items.Add("FocusedHwnd: {0}\n====" +
focusedHwnd.ToString());
PostMessage((IntPtr) focusedHwnd, WM_CHAR, 13, 0);
}
AttachThreadInput(activeThreadID, thisThreadID, false);
}
}
}

Nov 16 '05 #10
Hi Stoitcho,

i finally did that:
I use EnumWindows and EnumChildWindows and i present the user
in a treeview their relation for all open processes in the machine.
Then, the user select the windows that he needs to send keystrokes
and an adjustable timer is responsible to send them at timer intervals.

I did that because, as you said, the GetFocus needs to have the windows
in the foreground. I need these windows to be minimized and the whole
process
need not have any interaction wit the user, no flashing windows, no
SetForeGroundWindow,
nothing of all these.
So, as you understand, SendKeys was not an option for me. Imagine for
example, to have a Notepad app,
a dos-box and another one Winword app, running minimized and all getting the
same characters
at some timer interval, without interrupt the use working on something else
in that machine...

Anyway, the solution as it is right now is working perfectly.

Thanks again, you and Adam for your help!

objectref

"Stoitcho Goutsev (100) [C# MVP]" <10*@100.com> wrote in message
news:OE**************@TK2MSFTNGP09.phx.gbl...
Hi objecref,

The problem I believe is that the Notepad is not the active application. How I said there is only one focused window at a time and it is or it is a child to the foreground (active) window. If you try GetFocus for a thread which
hasn't created the foreground window it always return null.

Just a little reminder. Even though you can post key messages to a window it is not guarantee that the target window will accept them correctly. Each
input (UI) thread has associated keystate buffers and so on that play some
role in the key processing. If you just post key messages it won't go thru
all the internal key processing and won't update those structures. Methods like GetKeyState, GetAsyncKeyState, GetKeyboardState and so on which
application use won't work. Key messages are only notifications aftere all
the work of precessing the keyboard is done. The only way to simulate real
keyboard activity is to use either SendInput or keybd_event API or SendKeys class in .NET.
However because they synthesizes keyboard strokes, mouse movements and mouse button clicks it you cannot specify the target window. Keystorkes go to the focused window, mouse events go to the window under the mouse cursor
position.
Anyways if you still want to use post messages you may consider using
AttachThreadInput and SetKeyboardState method to handle some of the
situations. However some keys such as CapsLock, NumLock, etc cannot be
simulated unless SendInput or keybd_event are used.

--
HTH
Stoitcho Goutsev (100) [C# MVP]
"objectref" <ob*******@mediatrel.com> wrote in message
news:uc**************@TK2MSFTNGP12.phx.gbl...
Hmmmm....

at first, thanks for your solution, it seems than know i am close enough, but:

Let's assume that, for simplicity reasons, i want to test your code with a pre-known
window handle. I take this from enumerating processes, find, let's say an open Notepad window
and then get it's MainWindowHandle, that is, 3475108.

But, the below code does not work. It reaches the "PostMessage"line but i
do
not
get an Enter hit in notepad app.
Can you figure out why ?
private void button1_Click(object sender, System.EventArgs e)
{
IntPtr thisThreadID = new IntPtr(AppDomain.GetCurrentThreadId());
IntPtr activeHwnd = GetForegroundWindow();

if (activeHwnd != IntPtr.Zero)
{
IntPtr activeThreadID = GetWindowThreadProcessId((IntPtr)

3475108, IntPtr.Zero);
if (AttachThreadInput(activeThreadID, thisThreadID, true))
{
IntPtr focusedHwnd = GetFocus();
if (focusedHwnd != IntPtr.Zero)
{
//Console.WriteLine("FocusedHwnd: {0}\n====",
focusedHwnd.ToString());
listBox1.Items.Add("FocusedHwnd: {0}\n====" +
focusedHwnd.ToString());
PostMessage((IntPtr) focusedHwnd, WM_CHAR, 13, 0);
}
AttachThreadInput(activeThreadID, thisThreadID, false);
}
}
}


Nov 16 '05 #11
hii
i have read the problem and i have the same problem;
but i cant fix it.
i am working on a MY Task Manager program in c#
i wana do exactly the same one in Windows xp

almost i don but i need your help :
How can i get the list curent Applications Running (windows)
i got the processes list but i need the Application list.

i have thinking that it`s the same problem but you looked for a windows
opens
please help to do that one
thx for all the informations

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 16 '05 #12

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Altramagnus | last post by:
I have 30 - 40 type of different window. For each type I need about 20 instances of the window. When I try to create them, I get "Error creating window handle" My guess is there is a maximum...
3
by: Fred Hebert | last post by:
I am trying to use a 3rd party DLL that requires the main window handle as a parameter. e.g. MyFunc(WHND MyHandle); The example looks something like this: Result = MyFunc(Handle); Where...
4
by: Brad Jones | last post by:
<Previously posted in microsoft.public.dotnet.framework.windowsforms> Hi all. Any suggestions here would be appreciated. Thanks for reading. I'm primarly a C++ developer but I've been trying to...
3
by: NeverLift | last post by:
But, if it's not open, I don't want to open it . . . using window.open will open it if it doesn't exist, even if the url in that open is null (the window is then empty -- but it's open). The...
3
by: TC | last post by:
Hey Folks, I am using the following 4 Win32 APIs with a C# AddIn: FindWindow SetWindowLong SetForegroundWindow EnableWindow Within the AddIn, there are some winforms. I use these APIs to...
7
by: lkr | last post by:
hi is there is anyway to capture the document events of a MSWord?eg:I have to handle the event awhen WM_KEYUP or anyother events will occur. give me a solution............ thanks in advance lkr
17
by: Fred Hebert | last post by:
I am trying to use a 3rd party DLL that requires the main window handle as a parameter. e.g. MyFunc(WHND MyHandle); The example looks something like this: Result = MyFunc(Handle); Where...
12
by: Daniel Walzenbach | last post by:
Hi, I have a Website which allows users to input data. After they finished entering data they can click a button to save their input. Problem now is, that I have no possibility to visualize that...
1
by: Sephi berry via .NET 247 | last post by:
Hi I'm having this problem of accessing the handle of a window. I retrive it by: Dim procs As Process() = Process.GetProcessesByName("ArcScene") dim intHWND1 IntPtr = procs(0).Handle and get a...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.