locked
How do I pass Mouse Events to selected areas of underlying Child Controls RRS feed

  • Question

  • I am using Visual Studio 2005 and .Net 2.0 to write a simple C# application to try to find a way to solve a problem in a larger application. Here is a description of the test application and what I am trying to accomplish with it:

    I have a class derived from UserControl. Its only code is Trace.Writeline() statements in OnMouse Enter, Leave, Down, Move, and Up.

    II also have a class derived from Panel. It is made to be Transparent (override CreateParms, ExStyle = 0x0020 // WS_EX_TRANSPARENT, ...) It too has Trace.WriteLine() statements in OnMouse Enter, Leave, Down, Move, and Up. Its OnPaint() displays a text string in the middle of the client area.

    The test application consists of a simple form with a menu bar. It contains nothing at design time except the Main menu. I create three instances of the UserControl-derived class in the constructor and assign each a unique BackColor. I then add them to the form in the OnLoad() using this.Controls.Add(). To one, I add a TextBox control that contains some text (the Gettysburg address). It is small enough to require a vertical scroll bar.

    Also, in the constructor of the test application, I add an instance of the Transparent Panel to the other three controls (this.Controls.Add(...)). I use this.Controls.SetChildIndex(TransparentPanel, 0) to insure that the Transparent Panel is the top-most of the 4 contained controls in the Form.

    Now, the problem:

    The purpose of the Transparent Panel is to intercept all mouse activity so that the three UserControl controls do not see them. The Transparent Panel is completely see-through so that they can be seen completely. This all works just fine.

    However, I would like to be able to allow Mouse Events detected by the Transparent Panel that occur over the vertical scroll bar of the UserControl that contains the Gettysburg Address to be passed down to that TextBox in the one UserControl.

    I have tried various ways using PostMessage() and SendMessage() where the Mouse Events detected by the Transparent Panel are forwarded to the underlying control. I have gotten this to work in that the TextBox can see and respond to the mouse messages - text can be selected. However, I cannot make the vertical scroll bar to work - it acts as though the vertical scroll bar is not there at all.

    I tried converting the Transparent Panel to be derived from Form. I then added TopLevel = false; and Visible = true; in the constructor. I added a text display to the otherwise transparent form so that I could be convinced that it was there, on top of the other 3 controls - it was. I tried TransparencyKey using the color of the text but all Mouse Events still went to the Transparent Form - nothing to the underlying controls. I then discovered that TransparencyKey only works for TopLevel Forms and I cannot add a TopLevel Form as a child control to another Form. So much for that.

    I am looking for the "proper .Net" way to do this: Capture and keep all Mouse event messages in the Transparent Panel except those that occur over the scroll bar of the underlying TextBox.

    In the real application that I am trying to simulate, the TextBox will be a Browser control and the others will be various large Active X controls.

    I will appreciate any help anyone can give. I can also furnish more details as appropriate.

    Thanks



     

    • Moved by Zhi-Xin Ye Tuesday, March 31, 2009 11:18 AM not a BCL question
    Wednesday, March 25, 2009 3:14 PM

Answers