I have a panel that I'm trying to draw a blue border around. I'm using the following code to draw the blue rectangle. The problem is when I add a control to the panel and sets its dockstate to fill it hides the blue border.
With my example I derrive the graphics object from the control. And as a result the control hides the blue rectangle because its underneath the control.
I don’t want to draw a rectangle on the panels container as the panel fills the container.
I don’t want to draw a rectangle on the control. For other reasons…
The graphics object is derived from object where the graphic is to be drawn.
Questions:
- Is is possible to draw a graphic and control the Z order so that the graphic is drawn at the top and is visible?
- How can I draw the rectangle and make it visible?
Private Sub DrawRectanle
Dim myGraphics As Graphics
Dim myRectangle As Rectangle
Dim myPen As New Pen(Color.Red)
'return the current form as a drawing surface
myGraphics = Panel1.CreateGraphics()
myRectangle = New Rectangle(Panel1.Location.X, Panel1.Location.Y, Panel1.Size.Width, Panel1.Size.Height)
'draw rectangle from pen and rectangle objects
myGraphics.DrawRectangle(pen:=myPen, rect:=myRectangle)
End Sub
glennPosted Apr 10, 2008, 2:28 PM
Yes what you described works unti a control sits over your drawing surface then the line is hidden. Consider this. I have a form with button, textbox, label, treeview, basically a whole bunch of controls randomly arranged on the form. Now How would I draw a diagnol line from the lower left corner to the upper right corner. The line from its start to its end will intersect with the controls. The line is to appear visible and to be in front of all the controls.
Mike GoldPosted Apr 8, 2008, 11:35 PM
Hi Glenn,
I just tried the custom control I posted and added a button, then docked the button to full. I still see the blue border.
What you want to do in Windows is difficult to accomplish. (Difficult but not impossible). You basically want a bitmap to sit over all your controls. Is that what you are trying to do?
One technique is to create a bitmap with transparent colors (argb, where a is the alpha channel made transparent. You can overlay the bitmap over the controls. Is this what you are trying to do ?
I think it would help if you described what it is exactly you are trying to accomplish, and perhaps we can suggest something.
glennPosted Apr 8, 2008, 10:13 AM
Howdy Mike,
What you've suggested so far is good. I still have one delima. If I site a control on the panel and set the controls dock state to fill is hides the rectangle underneath. I run into this problem all the time and I'm wondering if their is some technique to controlling the display order or z order of the graphics object/rectangle so it will display AKA bring it to the front. So no matter what controls are on top of the panel the rectangle or graphic will display. This way I don't have to go to the trouble of creating a custom control everytime i want o display graphical items on a container.
Heres another example What if I wanted to draw a diagnol line accross the panel. Lets say the panel had a collection of controls, labels, textboxes, etc. sited on it. If i'd draw the line on the panel it would be hidden behind each of these controls. How could I draw it over top?
Thanks
Glenn
Mike GoldPosted Apr 8, 2008, 1:14 AM
The best place to create a graphics object in Windows is always the OnPaint event. This being the case, what you need to do is create a Custom Control (use the Custom Control Template in Add New Item) and derive from Panel. In Panel you override OnPaint to add your border. Here is the code in C# for the custom control you want.
using
System;using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using
System.Drawing;using
System.Text;using
System.Windows.Forms;namespace
BlueRectangleApp{
public partial class PanelWithBlueBorder : Panel{
public PanelWithBlueBorder(){
InitializeComponent();
}
Pen _borderPen = new Pen(Color.Blue, 2); protected override void OnPaint(PaintEventArgs pe){
// TODO: Add custom paint code here // Calling the base class OnPaint base.OnPaint(pe); Graphics g = pe.Graphics;g.DrawRectangle(_borderPen,
new Rectangle(2, 2, Width-4, Height-4));}
}
}