|
|||||||||||||||||||||
|
|||||||||||||||||||||
|
Announcements
Chapters
Services
Feature Zones
|
IntroductionI was needing a panel that was able to expand/collapse on both X and Y axis, but could not find one that would do it for the Y axis so decided to write my own. The main idea of this is inheriting the Panel control and having a "caption" control docked left, right, up or down. Using the codeIn order to use this control, while in design mode go to the toolbox window pickup your category to include the control, right click it and select "Choose Items..." and then locate the ExtendedPanel.dll file. Soon after, you should be able to see the Control propertiesThe control provides the following properties :
ArchitectureThe main classes defined in this assembly are present in the following list:
By default, whenever a Paint event is raised ( Once you have done this the drawing process is changed a little bit, instead of the So the idea behind the protected BufferPaintingCtrl()
{
///set up the control styles so that it support double buffering painting
this.SetStyle( ControlStyles.AllPaintingInWmPaint |
ControlStyles.UserPaint |
ControlStyles.OptimizedDoubleBuffer |
ControlStyles.DoubleBuffer,true);
UpdateStyles();
}
CornerCtrlCornerCtrl is supposed to offer support for drawing the borders either with normal corners or rounded ones. The control has a graphic path object that is used in the Paint handler. The next listing presents the method for instantiating the graphic path object; the cornerSquare is used to define the region that will contain the ellipse whose arc is being drawn, if rounded corners are being selected. This member has to be defined in the child classes in order to control the rounding, otherwise we won't get any. protected virtual void InitializeGraphicPath()
{
if (null != graphicPath)
{
graphicPath.Dispose();
graphicPath = null;
}
graphicPath = new GraphicsPath();
switch (cornerStyle)
{
case CornerStyle.Rounded:
graphicPath.AddArc(0, 0, cornerSquare, cornerSquare, 180, 90);
graphicPath.AddLine(cornerSquare - cornerSquare / 2, 0,
Width - cornerSquare + cornerSquare / 2 - 1, 0);
graphicPath.AddArc(Width - cornerSquare - 1, 0, cornerSquare,
cornerSquare, -90, 90);
graphicPath.AddLine(Width - 1, cornerSquare - cornerSquare / 2,
Width - 1, Height - cornerSquare + cornerSquare / 2);
graphicPath.AddArc(Width - cornerSquare - 1,
Height - 1 - cornerSquare, cornerSquare, cornerSquare, 0, 90);
graphicPath.AddLine(cornerSquare - cornerSquare / 2, Height - 1,
Width - cornerSquare + cornerSquare / 2, Height - 1);
graphicPath.AddArc(0, Height - cornerSquare - 1, cornerSquare,
cornerSquare, 90, 90);
graphicPath.AddLine(0, cornerSquare - cornerSquare / 2, 0,
Height - cornerSquare + cornerSquare / 2);
break;
case CornerStyle.Normal:
graphicPath.AddLine(0, 0, Width-1, 0);
graphicPath.AddLine(Width-1, 0, Width-1, Height-1);
graphicPath.AddLine(Width-1, Height-1, 0, Height-1);
graphicPath.AddLine(0, Height-1, 0, 0);
break;
default:
throw
new ApplicationException("Unrecognized style for rendering the corners");
break;
}
}
ExpandedPanelAs shown in the UML class diagram this class is embedding a //set handler for collapsing/expanding
captionCtrl.SetStyleChangedHandler(new
DirectionCtrlStyleChangedEvent(CollapsingHandler));
//set the handler for the dragging event
captionCtrl.Dragging += new CaptionDraggingEvent(CaptionDraggingEvent);
I won't talk about the dragging part as the code is self explanatory. I will focus though on the part with collapsing/expanding. As mentioned earlier the direction control present in the caption control is defining a handler for the click event. Once the event is captured, the control style is being changed (basically pointing to the opposite direction it was pointing before the click), and raises the event causing ExtendedPanel object to expand/collapse. The panel object in its handler will prepare all the context needed for the animation, will instantiate the CollapseAnimation object (if this would be the case), will set its properties and will start the background thread to perform behind the scenes steps required for the animation (I won't list this method but you can find it in the source code). We are getting now to the most challenging part in writing this code. Within the lines of this method you will find a call to ChangeCaptionParent. This will be the case when the caption has been set to be docked either on the bottom or on the right side. More attention is required, will explain why next. As you know any control has a location (starting point) from where is being rendered and a size (width and height). All containers have their child controls location defined relative to its upper left corner. By changing the width/height of the container all child controls having the location set to be greater than the new size will become invisible. So will be the case in the two scenarios i mentioned; we have to bring the caption control back where it should be, by updating its location. By doing so a WM_PAINT message will be raised. Changing panel size will cause the painting event to be raised for this one as well, and will be facing a situation where seeing-not seeing the caption will repeat very fast, causing the very annoying effect of flickering (and we don't want that).
So my solution for the problem was to take out the panel caption during animation, keeping it though at the "same" location from the user's point of view. So during the animation period the caption control parent would be the same as the panel parent. private void ChangeCaptionParent()
{
//take the caption out of the panel beacause of the flickering
this.captionCtrl.Parent = this.Parent;
this.captionCtrl.Location = new Point(this.Location.X + this.Width -
this.captionCtrl.Width, this.Location.Y + this.Height -
this.captionCtrl.Height);
Win32Wrapper.SetWindowPos(this.Handle, this.captionCtrl.Handle,
0, 0, 0, 0,
Win32Wrapper.FlagsSetWindowPos.SWP_NOMOVE |
Win32Wrapper.FlagsSetWindowPos.SWP_NOSIZE |
Win32Wrapper.FlagsSetWindowPos.SWP_NOREDRAW);
//disable moving
backupMoveable = moveable;
moveable = false;
}
The panel is notified at every step of the animation that it should update its size and in the aforementioned cases its location. The end of the animation needs special treatment as well as in the scenarios where the docking is at the bottom or right, the caption control has to be brought back into the panel control. private void OnNotifyAnimationFinished(object sender)
{
if (captionAlign == DirectionStyle.Down)
{
//set caption location (no redrawing) and hiding
Win32Wrapper.SetWindowPos(this.captionCtrl.Handle, IntPtr.Zero, 0,
this.Height - this.captionCtrl.Height, 0, 0,
Win32Wrapper.FlagsSetWindowPos.SWP_NOREDRAW |
Win32Wrapper.FlagsSetWindowPos.SWP_NOZORDER |
Win32Wrapper.FlagsSetWindowPos.SWP_NOSIZE |
Win32Wrapper.FlagsSetWindowPos.SWP_HIDEWINDOW );
//set back the parent
this.captionCtrl.Parent = this;
this.captionCtrl.Visible = true;
//set back the moveable property; during collapsing the movement
//is not allowed
moveable = backupMoveable;
}
else
{
if (captionAlign == DirectionStyle.Right)
{
//set caption location (no redrawing) and hiding
Win32Wrapper.SetWindowPos(this.captionCtrl.Handle, IntPtr.Zero,
this.Width - this.captionCtrl.Width, 0, 0, 0,
Win32Wrapper.FlagsSetWindowPos.SWP_NOREDRAW |
Win32Wrapper.FlagsSetWindowPos.SWP_NOZORDER |
Win32Wrapper.FlagsSetWindowPos.SWP_NOSIZE |
Win32Wrapper.FlagsSetWindowPos.SWP_HIDEWINDOW);
//set back the parent
this.captionCtrl.Parent = this;
this.captionCtrl.Visible = true;
//set back the moveable property; during collapsing the movement
//is not allowed
moveable = backupMoveable;
}
}
//set the state of the object expanded/collapsed
SetState();
}
Adding the caption back where it should be it is a bit tricky as well. I have to perform two actions, set the panel as its parent and update its location (there is a great chance the current location within the panel parent control to be different than what we need). Either way it is done, parent set first and then location updated or vice versa, is not a reliable solution. If I set the parent first due to the actual location coordinates the caption will be moved (probably where it will become invisible) into the panel control and only setting the location would bring it back where it should be. We would end up having the unwanted flickering effect. If I set the location first related to the panel control (we would want the caption either at the bottom or on the right side) a paint event is raised and will cause the control to be drawn somewhere else on the screen. Only setting the caption parent to be the panel again we would have the things back to normal. But this is not a solution an user would accept. So Win32 to the rescue. The Windows API SetWindowsPos method is giving us the chance to set the new location without repainting message raised. Having used this method would save me having the caption appearing somewhere else on the screen. I can now safely set the parent to be the panel again, and because I was hiding it set it back to visible.
I know I can't always explain very well (good thing i am not a teacher), but i hope you have got the idea. ConclusionHopefully, someone out there would find this control useful. I am pretty sure there is room for improvement, so any feedback would be appreciated. History
| ||||||||||||||||||||