Link to home
Start Free TrialLog in
Avatar of pgloor
pgloor

asked on

Getting the rectangle to draw a reversible frame around a panel

I have a user control with 4 panels around a panel in the center.

See http://img345.imageshack.us/img345/4454/dockingstation1lt.png how it looks like.

Each of the 4 surrounding panels has a "drag handle" on the side towards the center panel. The drag handle works like a splitter and allows to resize the panel. It appears whenever the mouse is over the border between two panels.

To indicate the selected area I wanted to draw a dashed reversible rectangle around the whole panel on mouse down over the drag handle. I tried to do this using ControlPaint.DrawReversibleFrame(rect, Color.DimGray, FrameStyle.Dashed);

I have no problems using this in simple constructs on a form following the examples on:
http://msdn.microsoft.com/library/en-us/cpref/html/frlrfsystemwindowsformscontrolclasspointtoscreentopic.asp

However, I have problems in getting the correct absolute positions to draw the reversible frame for each of my four rectangles if I use my control on a form with a menue.

What ever I tried to do, it looks to me that something like reversibleFrame.Location = control.PointToScreen(control.Parent.Location) does not calculate the correct location. My reversibleFrame appears somewhere on the screen but never exactly on my panels.

What are your suggestions?

Peter
Avatar of vo1d
vo1d
Flag of Germany image

could you post a screenshot of that please?
You need to use the container of the Panel...if this is the Form itself then:

        private void panel1_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                // draw the reversible frame
                Rectangle panelRect = this.RectangleToScreen(new Rectangle(panel1.Location, panel1.Size));
                ControlPaint.DrawReversibleFrame(panelRect, Color.DimGray, FrameStyle.Dashed);
            }
        }

        private void panel1_MouseUp(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                // erase the reversible frame
                Rectangle panelRect = this.RectangleToScreen(new Rectangle(panel1.Location, panel1.Size));
                ControlPaint.DrawReversibleFrame(panelRect, Color.DimGray, FrameStyle.Dashed);
            }
        }

If the Form is not the container, then replace "this" with the name of the Panels container.
Avatar of pgloor
pgloor

ASKER

Sorry, I still don't get it.

Please note:
1) I have 4 panels, called topPanel, leftPanel, bottomPanel, rightPanel.
2) The 4 panels are arranged around a fifth panel, the centerPanel (not interesting for the problem).
3) Inside each panel, at the border to the centerPanel, there is a small invisible component. I call this DragHandle.
4) The DragHandle appears as a gray line with a HSplit or VSplit cursor whenever the mouse is moved over it.
5) If the mouse button is pressed over the drag handle a reversible frame should be drawn around the enclosing panel.

To make the problem better visible I slightly changed the code to draw the reversible frame with a thick black line.

The following screenshot shows what happens if the mouse is down over the drag handle on the topPanel.
http://img172.imageshack.us/img172/1395/dockingtop6vc.png

The following screenshot shows what happens if the mouse is down over the drag handle on the leftPanel.
http://img475.imageshack.us/img475/5873/dockingleft4my.png

I hope this helps to better understand the problem.

Peter
Avatar of pgloor

ASKER

Sorry I forgot to mention: on the screenshots mentioned above you can also see the code I used to get the rectangle and to draw the reversible frame.

Peter
ASKER CERTIFIED SOLUTION
Avatar of Mike Tomlinson
Mike Tomlinson
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of pgloor

ASKER

You are great!

ctlGrandParent.RectangleToScreen(new Rectangle(ctlParent.Location, ctlParent.Size)); did the trick!

So far I only tried:
  ctlParent.RectangleToScreen(new Rectangle(ctlParent.Location, ctlParent.Size))
  ctl.RectangleToScreen(new Rectangle(ctlParent.Location, ctlParent.Size))
  this.RectangleToScreen(new Rectangle(ctlParent.Location, ctlParent.Size))
and variations of them with different parameters.

I didn't realize the relation between the object where I'm calling the method from and the rectangle passed as the parameter. Now it's clear.
Glad I was able to write the code in a more understandable way.  =)