Message box breaks owner draw controls in .NET controls used as ActiveX controls (again)

Message box breaks owner draw controls in .NET controls used as ActiveX controls (again)

Post by Clive Dixo » Thu, 27 May 2004 18:15:54


urther to my previous posting on this, here is a very simple way of
reproducing the problem. Has anyone else come across this and found a
workaround (other than either saying "don't create the owner draw control in
InitializeComponent" or creating the parent before the control)?

Create a windows control library project.

Add an owner draw combo (or other owner draw control) and a button to the
user control.

Add a derived control class to handle the owner draw, modify the IDE
generated code to use this class.

Explicitly create the owner draw combo in InitializeComponent using
CreateControl. (This causes the control to be created with a parent window
of class System.Windows.Forms.Application.ParkingWindow. Even after the
parent control has been created and the owner draw control's parent is
correctly reassigned, owner draw messages continue to be routed through
System.Windows.Forms.Application.ParkingWindow.WndProc.)

Add button click handler that shows a message box.

Add COM attributes to user control class.

Add ComRegister function to register component as ActiveX control.

Set project to register for interop.

Compile.

Add control to tstcon32.exe (or add to MFC app dialog).

Press button on user control to show message box.

Owner draw control is now broken. (MessageBox has destroyed the instance of
System.Windows.Forms.Application.ParkingWindow.)



The owner draw message routing through
System.Windows.Forms.Application.ParkingWindow.WndProc occurs whenever an
owner draw control is created before its parent window.

The message routing only breaks if the control is in a component enabled for
use and used as an ActiveX control.



Here is the C# code to reproduce this:

using System;

using System.Collections;

using System.ComponentModel;

using System.Drawing;

using System.Data;

using System.Runtime.InteropServices;

using System.Windows.Forms;

using Microsoft.Win32;

namespace WindowsControlLibrary2

{

public class OwnerDrawCombo : System.Windows.Forms.ComboBox

{

protected override void OnDrawItem(DrawItemEventArgs e)

{

e.DrawBackground();

if ( e.Index >= 0 && e.Index < this.Items.Count )

{

e.Graphics.DrawString(this.Items[e.Index].ToString(),

e.Font,Brushes.Black,

e.Bounds.Left,e.Bounds.Top);

}

e.DrawFocusRectangle();

}

}

[Guid("BDD17877-B805-4970-B3BB-2F1A2C1E5A38")]

[ProgId("WindowsControlLibrary2.UserControl1")]

[ClassInterface(ClassInterfaceType.AutoDual)]

public class UserControl1 : System.Windows.Forms.UserControl

{

private OwnerDrawCombo comboBox1;

private System.Windows.Forms.Button button1;

private System.ComponentModel.Container components = null;

[ComRegisterFunction]

static void ComRegister(Type t)

{

string keyName = @"CLSID\" + t.GUID.ToString("B");

using (RegistryKey key =
Registry.ClassesRoot.OpenSubKey(keyName,true) )

{

key.CreateSubKey("Control").Close();

}

}

[ComUnregisterFunction]

static void ComUnregister(Type t)

{

string keyName = @"CLSID\" + t.GUID.ToString("B");

Registry.ClassesRoot.DeleteSubKey
 
 
 

1. Handling Owner Draw messages for controls inside controls?

2. Custom Control vs. Owner Draw Control?

Hi

Custom Draw, Owner Draw, Custom Control, Owner Draw Control...

Overriding DrawItem() Method, Overriding OnPaint() Method, OnCustomDraw()
Method, WM_CTLCOLOR Handler, adding custom control which is in toolbox and
"Custom Control DLL"...

I started to search control development in MFC to learn how to write
(stylish)controls but it seems that it will be difficult and confused...
I have basic 2 questions, if you help me to find answers i will be very glad.

1-) In Terminology, are "custom control" and "owner draw control" same
things? If not, i understand that custom control means that writing "custom
control dll" and overriding OnPaint() method and this control will be
reusable in other projects. But owner draw control means changing controls
color for that solution and we only override DrawItem() method. Am i right?

2-) As seen and i am correct, there are many ways to draw a control with
different style and colors. If i want to make a button with gradient color
and want to add it in Toolbox to use in different projects and there will be
2 color options that developer will adjust for drawing button in properties
window when add it to form and select it. Which way must i choose? What must
be the roadmap?

Thanks..

3. My ActiveX control, drawing its child controls etc.

4. How to export a .net controls into ActiveX control?

5. Recieving events from an activeX control hosted in a composite control in Visual C++ (.Net 2003)

6. ActiveX control "Click to activate and use this control" message

7. Activex control events not firing or ActiveX composite control not listening

8. WM_COPY message not being passed to controls deriving from CEdit within an ActiveX control

9. Writing an activex control to proxy calls to an unregistered activex control

10. ActiveX control in another activex control

11. Creating ActiveX control Within activeX Control

12. ActiveX Control prompt "An ActiveX control on this page might be u

13. Getting activex control event from ActiveX control.

14. WebBrowser control : need help controlling browser control from within VC++ .NET 2003 program