Whats the use of postmessage??

  • Thread starter Thread starter Chacko
  • Start date Start date
C

Chacko

Hi anyone and everyone,
Could anybody tell me clearly wats the use of PostMessage and sort
explain to me in simple english...Its just that i searched lot of
places and i am still not getting it right...

Regards,
Jacob
 
Hi Jacob,

PostMessage is handy for sending messages to other controls or windows
outside your program. Native windows programs uses message queues
(.net-programs hides this message queue with the event system). If you
get the address to any window you can manipulate it by posting messages to
it, hence PostMessage. PostMessage will 'post' a message to the window
and returns without waiting for a reply, while SendMessage does the same,
but waits for a reply before returning.

The sample code below will close all Notepad windows on your desktop by
posting a WM_Close message to each window

class Program
{
static void Main(string[] args)
{
Process[] processes =
System.Diagnostics.Process.GetProcessesByName("Notepad");
foreach(Process p in processes)
{
IntPtr hWnd = p.MainWindowHandle; // handle to the notepad
window
UInt32 msg = 0x0010; // WM_Close, a message for closing a
window
IntPtr wParam = IntPtr.Zero; // no extra parameters needed
IntPtr lParam = IntPtr.Zero;

PostMessage(hWnd, msg, wParam, lParam);
}
}

[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError=
false)]
static extern void PostMessage(IntPtr hWnd, uint Msg, IntPtr
wParam, IntPtr lParam);
}
 
Thanx morton for explaining PostMessage to me...
Now morton i have a problem with the first parameter of PostMessage...
As in i want to be able to post my messages to disabled windows or
invisible windows...
How do i do that???
I came across some special value thing but then i didn;t understand
it...
Jacob



Morten said:
Hi Jacob,

PostMessage is handy for sending messages to other controls or windows
outside your program. Native windows programs uses message queues
(.net-programs hides this message queue with the event system). If you
get the address to any window you can manipulate it by posting messages to
it, hence PostMessage. PostMessage will 'post' a message to the window
and returns without waiting for a reply, while SendMessage does the same,
but waits for a reply before returning.

The sample code below will close all Notepad windows on your desktop by
posting a WM_Close message to each window

class Program
{
static void Main(string[] args)
{
Process[] processes =
System.Diagnostics.Process.GetProcessesByName("Notepad");
foreach(Process p in processes)
{
IntPtr hWnd = p.MainWindowHandle; // handle to the notepad
window
UInt32 msg = 0x0010; // WM_Close, a message for closing a
window
IntPtr wParam = IntPtr.Zero; // no extra parameters needed
IntPtr lParam = IntPtr.Zero;

PostMessage(hWnd, msg, wParam, lParam);
}
}

[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError =
false)]
static extern void PostMessage(IntPtr hWnd, uint Msg, IntPtr
wParam, IntPtr lParam);
}


Hi anyone and everyone,
Could anybody tell me clearly wats the use of PostMessage and sort
explain to me in simple english...Its just that i searched lot of
places and i am still not getting it right...

Regards,
Jacob
 
To get the window handle to a hidden window you need to use FindWindowEx
in the Win32 API using pinvoke.

http://www.pinvoke.net/search.aspx?search=FindWindowEx&namespace=[All]

The handle to the parent window is 'null' for a main window.



Thanx morton for explaining PostMessage to me...
Now morton i have a problem with the first parameter of PostMessage...
As in i want to be able to post my messages to disabled windows or
invisible windows...
How do i do that???
I came across some special value thing but then i didn;t understand
it...
Jacob



Morten said:
Hi Jacob,

PostMessage is handy for sending messages to other controls or windows
outside your program. Native windows programs uses message queues
(.net-programs hides this message queue with the event system). If you
get the address to any window you can manipulate it by posting messages
to
it, hence PostMessage. PostMessage will 'post' a message to the window
and returns without waiting for a reply, while SendMessage does the
same,
but waits for a reply before returning.

The sample code below will close all Notepad windows on your desktop by
posting a WM_Close message to each window

class Program
{
static void Main(string[] args)
{
Process[] processes =
System.Diagnostics.Process.GetProcessesByName("Notepad");
foreach(Process p in processes)
{
IntPtr hWnd = p.MainWindowHandle; // handle to the
notepad
window
UInt32 msg = 0x0010; // WM_Close, a message for
closing a
window
IntPtr wParam = IntPtr.Zero; // no extra parameters
needed
IntPtr lParam = IntPtr.Zero;

PostMessage(hWnd, msg, wParam, lParam);
}
}

[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError =
false)]
static extern void PostMessage(IntPtr hWnd, uint Msg, IntPtr
wParam, IntPtr lParam);
}


Hi anyone and everyone,
Could anybody tell me clearly wats the use of PostMessage and sort
explain to me in simple english...Its just that i searched lot of
places and i am still not getting it right...

Regards,
Jacob
 
Morton i guess i gave u the wrong picture...
Here is the explaination in brief abt the problem
When i write
this.ShowInTaskbar=false;
i am not able to get an another window(instance of the program) or an
thread to be created...
but when the ShowInTaskBar= true
then it works perfectly...
Could u help me out here
Thankx

another
Morten said:
To get the window handle to a hidden window you need to use FindWindowEx
in the Win32 API using pinvoke.

http://www.pinvoke.net/search.aspx?search=FindWindowEx&namespace=[All]

The handle to the parent window is 'null' for a main window.



Thanx morton for explaining PostMessage to me...
Now morton i have a problem with the first parameter of PostMessage...
As in i want to be able to post my messages to disabled windows or
invisible windows...
How do i do that???
I came across some special value thing but then i didn;t understand
it...
Jacob



Morten said:
Hi Jacob,

PostMessage is handy for sending messages to other controls or windows
outside your program. Native windows programs uses message queues
(.net-programs hides this message queue with the event system). If you
get the address to any window you can manipulate it by posting messages
to
it, hence PostMessage. PostMessage will 'post' a message to the window
and returns without waiting for a reply, while SendMessage does the
same,
but waits for a reply before returning.

The sample code below will close all Notepad windows on your desktop by
posting a WM_Close message to each window

class Program
{
static void Main(string[] args)
{
Process[] processes =
System.Diagnostics.Process.GetProcessesByName("Notepad");
foreach(Process p in processes)
{
IntPtr hWnd = p.MainWindowHandle; // handle to the
notepad
window
UInt32 msg = 0x0010; // WM_Close, a message for
closing a
window
IntPtr wParam = IntPtr.Zero; // no extra parameters
needed
IntPtr lParam = IntPtr.Zero;

PostMessage(hWnd, msg, wParam, lParam);
}
}

[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError =
false)]
static extern void PostMessage(IntPtr hWnd, uint Msg, IntPtr
wParam, IntPtr lParam);
}


Hi anyone and everyone,
Could anybody tell me clearly wats the use of PostMessage and sort
explain to me in simple english...Its just that i searched lot of
places and i am still not getting it right...

Regards,
Jacob
 
Back
Top