
Windows.Forms.Message - processing a user defined Windows-message - 2nd try
Hello Tristan,
I have tried to capture the User-Define messages and it worked fine. You
may try the steps below:
1. Create a C# Windows Application project
2. Use the InteropServices name space.
using System.Runtime.InteropServices;
3. Add a button to the form
4. Double click the button and copy paste the following code:
[DllImport("user32.dll")]
private static extern bool SendMessage(
IntPtr hWnd, // handle to destination window
UInt32 Msg, // message
Int32 wParam, // first message parameter
Int32 lParam // second message parameter
);
private const int WM_USER = 0x0400;
private const int MSG_COMMAND_TERMINATE = WM_USER + 200 + 4;
protected override void WndProc(ref Message m)
{
if(m.Msg == MSG_COMMAND_TERMINATE)
MessageBox.Show("Hello");
base.WndProc(ref m);
Quote:
}
private void button1_Click(object sender, System.EventArgs e)
{
SendMessage (this.Handle, MSG_COMMAND_TERMINATE, 0, 0);
Quote:
}
I hope it helps.
Best regards,
Lion Shi, MCSE, MCSD
Microsoft Support Engineer
This posting is provided "AS IS" with no warranties, and confers no rights.
You assume all risk for your use. 2001 Microsoft Corporation. All rights
reserved.
--------------------
Subject: Windows.Forms.Message - processing a user defined
Windows-message - 2nd try
Date: Mon, 29 Apr 2002 16:38:13 +0200
Lines: 31
X-Priority: 3
X-MSMail-Priority: Normal
X-Newsreader: Microsoft Outlook Express 6.00.2600.0000
X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2600.0000
Newsgroups: microsoft.public.dotnet.languages.csharp
NNTP-Posting-Host: pd9e7c7ee.dip0.t-ipconnect.de 217.231.199.238
Path: cpmsftngxa08!cpmsftngxa07!tkmsftngxs01!tkmsftngp01!tkmsftngp04
Xref: cpmsftngxa08 microsoft.public.dotnet.languages.csharp:57349
X-Tomcat-NG: microsoft.public.dotnet.languages.csharp
Hello,
I posted my problem a long time ago in the newsgroup and you (Lion Shi)
replied me giving an example how to trap the
WM_KEYDOWN-message within a C#-program ... My problem now is, that I
want to be able to trap a self-defined Windows-message from an
elder program written in VC++ 6.
Unfortunately neither PreProcessMessage nor WndProc ever receives the
message - WndProc gets thousands of messages but never ever my
defined message ...
To verify that the message is sent I use the event of a button and make
a PostThreadMessage-call with the approbriate parameters...
private const int WM_USER = 0x0400;
private const int MSG_COMMAND_TERMINATE = WM_USER + 200 + 4;
IntPtr wParamPtr = new IntPtr ((int) 0);
IntPtr lParamPtr = new IntPtr ((int) 0);
Win32.PostThreadMessage (AppDomain.GetCurrentThreadId (),
MSG_COMMAND_TERMINATE, wParamPtr,lParamPtr);
(Win32 is a wrapper-class which imports dll-functions from the
user32.dll - I verified that PostThreadMessage() is working....)
I furtheron verified that AppDomain.GetCurrentThreadId() returns the
same value (= the current threadID of my application) ... I
even used PostMessage() without any success...
I even tried using a higher "message"-number - which had no effect - of
course...
So how in gods name do I receive user-defined messages from elder
programs - the same way it worked in VC6...??
Sincerely
Tristan McLure