How to receive message in CSharp UserControl

P

Priyko

I have a C# class that derives from UserControl. Its a container control
object/window that is embedded in a c sharp applicatiion. I want to be able
to receive custom messages sent to the UserControl from a c++/mfc
application. So I overrided the WndProc as below, but I am not able to get my
custom message in wndproc. Any idea what I am missing? Is this possible at
all?

[DllImport("user32.dll", CallingConvention = CallingConvention.Cdecl)]
public static extern uint RegisterWindowMessage(string lpString);

//Custom Registered Windows Message
public uint WM_CUSTOM_MESSAGE = RegisterWindowMessage("CUSTOM_MSG_GUID");

protected override void WndProc(ref Message msg)
{
if (msg.Msg == WM_CUSTOM_MESSAGE)
{
//NEVER GET HERE!
}
}

In the C++ app side:

uint WM_CUSTOM_MESSAGE = RegisterWindowMessage("CUSTOM_MSG_GUID");
::postMessage(HWND_BROADCAST, WM_CUSTOM_MESSAGE, wParam, lParam);
 
P

Peter Duniho

[...]
In the C++ app side:

uint WM_CUSTOM_MESSAGE = RegisterWindowMessage("CUSTOM_MSG_GUID");
::postMessage(HWND_BROADCAST, WM_CUSTOM_MESSAGE, wParam, lParam);

If I recall correctly, HWND_BROADCAST only sends messages to top-level
windows. As your UserControl is not a top-level window, it won't receive
the message.

In other words, what you're trying to do wouldn't have worked in unmanaged
code either. :)

You'll need to override the WndProc for the containing form if you want to
receive that message. The containing form can then do whatever's
appropriate, including calling some method on the UserControl-derived
class or simply delegating the message to the UserControl-derived control
instance.

Pete
 
P

Priyko

Thanks for the quick reply Pete. I do not have control over the containing
form- would it be possible for my c++ app to do a FindWindow and get a handle
to the UserControl window and send message to that directly? How do I specify
the classname/window name (CreateWindow in C#)? I will investigate as well.
Thanks again.

Priya

Peter Duniho said:
[...]
In the C++ app side:

uint WM_CUSTOM_MESSAGE = RegisterWindowMessage("CUSTOM_MSG_GUID");
::postMessage(HWND_BROADCAST, WM_CUSTOM_MESSAGE, wParam, lParam);

If I recall correctly, HWND_BROADCAST only sends messages to top-level
windows. As your UserControl is not a top-level window, it won't receive
the message.

In other words, what you're trying to do wouldn't have worked in unmanaged
code either. :)

You'll need to override the WndProc for the containing form if you want to
receive that message. The containing form can then do whatever's
appropriate, including calling some method on the UserControl-derived
class or simply delegating the message to the UserControl-derived control
instance.

Pete
 
P

Peter Duniho

Thanks for the quick reply Pete. I do not have control over the
containing
form- would it be possible for my c++ app to do a FindWindow and get a
handle
to the UserControl window and send message to that directly? How do I
specify
the classname/window name (CreateWindow in C#)? I will investigate as
well.
Thanks again.

I don't have the specifics regarding using FindWindow off the top of my
head. I don't even know what window class name .NET uses for things like
the UserControl, though I think it would be easy enough to write a test
program that would tell you. You may not be able to provide your own
class name and/or window name, but if it's not dynamically generated, then
at least you can find out what the right name to use would be.

Another alternative is to install a message filter for the window
messages. Then your UserControl could see the messages sent to the parent
window. That should be able to be done entirely in managed code, so it
might be a preferable solution to having your C++ program enumerate all
the windows on the system looking for your UserControl.

Pete
 
L

Linda Liu[MSFT]

Hi Priya,
would it be possible for my c++ app to do a FindWindow and get a handle
to the UserControl window and send message to that directly?

Yes. You can use the Win32 API FindWindow to find the containing form first
and then use the EnumChildWindows function to find the UserControl you want.
How do I specify the classname/window name (CreateWindow in C#)?

The window name is determined by the Text property of this window. As for
UserControl, its Text property is hidden from the IntelliSense because this
property is meaningless to UserControl, but you can use this property
without any problem.

The following is a sample written in C# to find the handle to a UserControl
contained in a form. It assumes that the form's Text property is "Form1"
and the UserControl's Text property is set to "UserControlText".

using System.Runtime.InteropServices;
public partial class Form1 : Form
{
[DllImport("user32.dll")]
static extern private IntPtr FindWindow(string lpClassName, string
lpWindowName);
[DllImport("user32.dll")]
static extern bool EnumChildWindows(IntPtr hWndParent , MyDelegate
lpEnumFunc , WindowParam lParam);
[DllImport("user32.dll")]
static extern int GetClassName (IntPtr hWnd , StringBuilder
lpClassName, int nMaxCount);
[DllImport("user32.dll")]
static extern int GetWindowText (IntPtr hWnd,StringBuilder
lpString,int nMaxCount);

delegate bool MyDelegate(IntPtr hwndChild,WindowParam lParam);

private bool EnumChildProc(IntPtr hwndChild, WindowParam lParam)
{
StringBuilder className = new StringBuilder(255);
GetClassName(hwndChild, className, 255);

StringBuilder windowName = new StringBuilder(255);
GetWindowText(hwndChild, windowName, 255);

if (lParam.ClassName != "")
{
if (className.ToString().Equals(lParam.ClassName) &&
windowName.ToString().Equals(lParam.WindowName))
{
expectedChild = hwndChild;
return false;
}
else
{
return true;
}
}
else
{
if (windowName.ToString().Equals(lParam.WindowName))
{
expectedChild = hwndChild;
return false;
}
else
{
return true;
}
}
}

class WindowParam
{
private string _className;
private string _windowName;
public string ClassName
{
get
{
return _className;
}
set
{
_className = value;
}
}
public string WindowName
{
get
{
return _windowName;
}
set
{
_windowName = value;
}

}
public WindowParam(string cn, string wn)
{
_className = cn;
_windowName = wn;
}
}

IntPtr expectedChild;
private void button1_Click(object sender, EventArgs e)
{
IntPtr hwndParen = FindWindow(null, "Form1");
bool result = EnumChildWindows(hwndParen, new
MyDelegate(EnumChildProc), new WindowParam("","UserControlText"));
if (!result)
{
MessageBox.Show(Convert.ToString(expectedChild.ToInt32(),
16));
}
}
}

Although this sample is written in C#, I believe you can translate it in
C++ without difficulty.
If you have any question, please feel free to let me know.

Sincerely,
Linda Liu
Microsoft Online Community Support

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.
 

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