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.