///<summary>Runs any time the control is invalidated.</summary> protected override void OnPaint(System.Windows.Forms.PaintEventArgs e) { ODPaintTools paintToolboxCur = _paintToolboxDefault; if (DesignMode) { e.Graphics.DrawRectangle(paintToolboxCur.PenOutline, 0, 0, Width - 1, Height - 1); //draw toolbar StringFormat format = new StringFormat(); format.Alignment = StringAlignment.Center; format.LineAlignment = StringAlignment.Center; e.Graphics.DrawString(this.Name, Font, paintToolboxCur.BrushText, new Rectangle(0, 0, Width, Height), format); return; } //OnPaint gets called a lot and the button collection can change while it is in the middle of drawing which can crash the program. //It's easier to just swallow any exception random exception that occurs because OnPaint will most likely get fired again real soon. //A better solution might be to surround Buttons with a read / write lock but that would be much more complicated than simply repainting. ODException.SwallowAnyException(() => { RecalculateButtonsizes(e.Graphics); foreach (ODToolBarButton button in Buttons) { //check to see if bound are in the clipping rectangle DrawButton(e.Graphics, button); } e.Graphics.DrawLine(paintToolboxCur.PenOutline, 0, Height - 1, Width - 1, Height - 1); }); }
///<summary>Reloads brushes based on current theme.</summary> private static void ReloadBrushes() { _paintToolboxDefault=new ODPaintTools(DefaultForeColor);//(Usually Grey) if(UseBlueTheme) { _paintToolboxDefault=new ODPaintTools(DefaultForeColor,Color.FromArgb(255,255,255),Color.FromArgb(171,181,209));//(White and Blue) } _paintToolboxError=new ODPaintTools(DefaultForeColor,Color.FromArgb(255,192,192),Color.FromArgb(255,98,98));//Color.FromArgb(255,255,255),Color.FromArgb(255,98,98));//Color.FromArgb(255,192,192));//(Red) }
///<summary>Reloads brushes based on current theme.</summary> private static void ReloadBrushes() { _paintToolboxDefault = new ODPaintTools(DefaultForeColor); //(Usually Grey) if (UseBlueTheme) { _paintToolboxDefault = new ODPaintTools(DefaultForeColor, Color.FromArgb(255, 255, 255), Color.FromArgb(171, 181, 209)); //(White and Blue) } _paintToolboxError = new ODPaintTools(DefaultForeColor, Color.FromArgb(255, 192, 192), Color.FromArgb(255, 98, 98)); //Color.FromArgb(255,255,255),Color.FromArgb(255,98,98));//Color.FromArgb(255,192,192));//(Red) }
private void DrawButton(Graphics g, ODToolBarButton button) { const int dropDownWidth = 15; //The width of the dropdown rectangle where the triangle shows. bool isNotify = (button.Style == ODToolBarButtonStyle.DropDownButton && !String.IsNullOrWhiteSpace(button.NotificationText)); //Notifies even when disabled. ODPaintTools paintToolboxCur = _paintToolboxDefault; if (button.IsRed) { paintToolboxCur = _paintToolboxError; } #region Separator if (button.Style == ODToolBarButtonStyle.Separator) { //was 112,128,144 //medium stripe g.DrawLine(new Pen(Color.FromArgb(190, 200, 210)), button.Bounds.Left, button.Bounds.Top + 1, button.Bounds.Left, button.Bounds.Bottom - 2); //dark stripe g.DrawLine(new Pen(Color.FromArgb(130, 140, 160)), button.Bounds.Left + 1, button.Bounds.Top, button.Bounds.Left + 1, button.Bounds.Bottom - 1); //white stripe g.DrawLine(new Pen(Color.FromArgb(255, 255, 255)), button.Bounds.Left + 2, button.Bounds.Top + 1, button.Bounds.Left + 2, button.Bounds.Bottom - 2); return; } #endregion //draw background if (!button.Enabled) { g.FillRectangle(paintToolboxCur.BrushMedium, button.Bounds); } else if (button.Style == ODToolBarButtonStyle.ToggleButton && button.Pushed) { g.FillRectangle(paintToolboxCur.BrushPushed, button.Bounds); } else if (button.Style == ODToolBarButtonStyle.Label || button.Style == ODToolBarButtonStyle.PageNav) { g.FillRectangle(paintToolboxCur.BrushMedium, button.Bounds); } else { switch (button.State) { case ToolBarButtonState.Normal: //Control is 224,223,227 (==Ryan. This is not always true. Control is 240,240,240 for me.) g.FillRectangle(paintToolboxCur.BrushMedium, button.Bounds); break; case ToolBarButtonState.Hover: //this is lighter than control g.FillRectangle(paintToolboxCur.BrushHover, button.Bounds); break; case ToolBarButtonState.Pressed: //slightly darker than control g.FillRectangle(paintToolboxCur.BrushDark, button.Bounds); break; case ToolBarButtonState.DropPressed: //left half looks like hover: g.FillRectangle(paintToolboxCur.BrushHover , new Rectangle(button.Bounds.X, button.Bounds.Y , button.Bounds.Width - 15, button.Bounds.Height)); //right section looks like Pressed: g.FillRectangle(paintToolboxCur.BrushDark , new Rectangle(button.Bounds.X + button.Bounds.Width - 15, button.Bounds.Y , 15, button.Bounds.Height)); break; } } if (isNotify) //Override dropdown background to show notification color. { Rectangle rectDropDown = new Rectangle(button.Bounds.X + button.Bounds.Width - dropDownWidth, button.Bounds.Y, dropDownWidth, button.Bounds.Height); g.FillRectangle(paintToolboxCur.BrushNotify, rectDropDown); //Fill the dropdown background area with the notification color. } //draw image and/or text //Color textColor=ForeColor; Rectangle textRect; int textWidth = button.Bounds.Width; if (button.Style == ODToolBarButtonStyle.DropDownButton) { textWidth -= 15; } //if(!button.Enabled){ // textColor=SystemColors.GrayText; //} if (imageList != null && button.ImageIndex != -1 && button.ImageIndex < imageList.Images.Count) //draw image and text { if (!button.Enabled) { System.Windows.Forms.ControlPaint.DrawImageDisabled(g, imageList.Images[button.ImageIndex] , button.Bounds.X + 3, button.Bounds.Y + 1, SystemColors.Control); textRect = new Rectangle(button.Bounds.X + imageList.ImageSize.Width + 3 , button.Bounds.Y, textWidth - imageList.ImageSize.Width - 3, button.Bounds.Height); } else if (button.State == ToolBarButtonState.Pressed) //draw slightly down and right { g.DrawImage(imageList.Images[button.ImageIndex], button.Bounds.X + 4, button.Bounds.Y + 2); textRect = new Rectangle(button.Bounds.X + 1 + imageList.ImageSize.Width + 3 , button.Bounds.Y + 1, textWidth - imageList.ImageSize.Width - 3, button.Bounds.Height); } else { g.DrawImage(imageList.Images[button.ImageIndex], button.Bounds.X + 3, button.Bounds.Y + 1); textRect = new Rectangle(button.Bounds.X + imageList.ImageSize.Width + 3 , button.Bounds.Y, textWidth - imageList.ImageSize.Width - 3, button.Bounds.Height); } } else //only draw text { if (button.Style == ODToolBarButtonStyle.Label || button.Style == ODToolBarButtonStyle.PageNav) { textRect = new Rectangle(button.Bounds.X, button.Bounds.Y , textWidth, button.Bounds.Height); } else if (button.State == ToolBarButtonState.Pressed) //draw slightly down and right { textRect = new Rectangle(button.Bounds.X + 1, button.Bounds.Y + 1 , textWidth, button.Bounds.Height); } else { textRect = new Rectangle(button.Bounds.X, button.Bounds.Y , textWidth, button.Bounds.Height); } } StringFormat format; if (imageList != null && button.ImageIndex != -1) //if there is an image //draw text very close to image { format = new StringFormat(); format.Alignment = StringAlignment.Near; format.LineAlignment = StringAlignment.Center; if (button.Enabled) { g.DrawString(button.Text, Font, paintToolboxCur.BrushTextFore, textRect, format); } else { g.DrawString(button.Text, Font, paintToolboxCur.BrushTextDisabled, textRect, format); } } else { format = new StringFormat(); format.Alignment = StringAlignment.Center; format.LineAlignment = StringAlignment.Center; if (button.Enabled) { g.DrawString(button.Text, Font, paintToolboxCur.BrushTextFore, textRect, format); //For page navigation buttons we ALWAYS show the text box so that users know they can type in the box to change pages. if (button.Style == ODToolBarButtonStyle.PageNav) { //Because we do not display zeros to users (e.g. 001 / 135) and instead leave them off (e.g. 1 / 135) the width will be lopsided //We need to draw each side of the page navigation (and pad a little) individually. SizeF size = g.MeasureString("/", Font); g.DrawString(button.PageValue.ToString(), Font, paintToolboxCur.BrushTextFore , new Rectangle(textRect.Location, new Size((textRect.Width / 2) - (int)(size.Width / 2) - 2, textRect.Height)) , new StringFormat() { Alignment = StringAlignment.Far, LineAlignment = StringAlignment.Center }); g.DrawString(button.PageMax.ToString(), Font, paintToolboxCur.BrushTextFore , new Rectangle(new Point(textRect.X + (textRect.Width / 2) + (int)(size.Width / 2) + 2, textRect.Y) , new Size((textRect.Width / 2) - (int)(size.Width / 2), textRect.Height)) , new StringFormat() { Alignment = StringAlignment.Near, LineAlignment = StringAlignment.Center }); //Only add the text box for page navigation once. //However, we need to add the text box here in the paint otherwise we don't know how large to make the text box. if (textPageNav == null) { textPageNav = new ValidNum(); textPageNav.Size = new Size((button.Bounds.Width - 10) / 2, button.Bounds.Height); textPageNav.KeyDown += TextPageNav_KeyDown; textPageNav.MinVal = 1; //There is no such thing as 0 pages in a preview, always set min to 1. textPageNav.RightToLeft = RightToLeft.Yes; this.Controls.Add(textPageNav); } //Always recalculate the location of the button just in case someone changes the size of another button on the toolbar. textPageNav.Location = new Point(button.Bounds.X + 1, button.Bounds.Y + 2); //Nav text changes constantly so we need to update it when redrawing. textPageNav.Text = button.PageValue.ToString(); if (button.PageMax != 0) { textPageNav.MaxVal = button.PageMax; } } } else { g.DrawString(button.Text, Font, paintToolboxCur.BrushTextDisabled, textRect, format); } } //draw outline //Pen penR=penMedium;//new Pen(Color.FromArgb(180,180,180)); if (!button.Enabled) { //no outline g.DrawLine(paintToolboxCur.PenDivider, button.Bounds.Right - 1, button.Bounds.Top, button.Bounds.Right - 1, button.Bounds.Bottom - 1); //vertical line on the right side } else if (button.Style == ODToolBarButtonStyle.ToggleButton && button.Pushed) { g.DrawRectangle(paintToolboxCur.PenOutline, new Rectangle(button.Bounds.X, button.Bounds.Y , button.Bounds.Width - 1, button.Bounds.Height - 1)); } else if (button.Style == ODToolBarButtonStyle.Label || button.Style == ODToolBarButtonStyle.PageNav) { //no outline g.DrawLine(paintToolboxCur.PenDivider, button.Bounds.Right - 1, button.Bounds.Top, button.Bounds.Right - 1, button.Bounds.Bottom - 1); //vertical line on the right side } else { switch (button.State) { case ToolBarButtonState.Normal: //no outline g.DrawLine(paintToolboxCur.PenDivider, button.Bounds.Right - 1, button.Bounds.Top, button.Bounds.Right - 1, button.Bounds.Bottom - 1); break; case ToolBarButtonState.Hover: g.DrawRectangle(paintToolboxCur.PenOutline, new Rectangle(button.Bounds.X, button.Bounds.Y , button.Bounds.Width - 1, button.Bounds.Height - 1)); break; case ToolBarButtonState.Pressed: g.DrawRectangle(paintToolboxCur.PenOutline, new Rectangle(button.Bounds.X, button.Bounds.Y , button.Bounds.Width - 1, button.Bounds.Height - 1)); break; case ToolBarButtonState.DropPressed: g.DrawRectangle(paintToolboxCur.PenOutline, new Rectangle(button.Bounds.X, button.Bounds.Y , button.Bounds.Width - 1, button.Bounds.Height - 1)); break; } } if (button.Style == ODToolBarButtonStyle.DropDownButton) { int adjDown = 0; //The distance to push the triangle down to show the notification text. if (isNotify) { adjDown = 6; //Draw the notification text. Size sizeText = TextRenderer.MeasureText(button.NotificationText, Font); g.DrawString(button.NotificationText, Font, (button.Enabled?paintToolboxCur.BrushTextFore:paintToolboxCur.BrushTextDisabled), button.Bounds.X + button.Bounds.Width + 1 - (dropDownWidth + sizeText.Width) / 2f, button.Bounds.Y + 2 + sizeText.Height / 2f, format); } Point[] triangle = new Point[3]; triangle[0] = new Point(button.Bounds.X + button.Bounds.Width - 11 , button.Bounds.Y + button.Bounds.Height / 2 - 2 + adjDown); triangle[1] = new Point(button.Bounds.X + button.Bounds.Width - 4 , button.Bounds.Y + button.Bounds.Height / 2 - 2 + adjDown); triangle[2] = new Point(button.Bounds.X + button.Bounds.Width - 8 , button.Bounds.Y + button.Bounds.Height / 2 + 2 + adjDown); if (button.Enabled) { g.FillPolygon(paintToolboxCur.BrushTextFore, triangle); } else { g.FillPolygon(paintToolboxCur.BrushTextDisabled, triangle); } if (button.State != ToolBarButtonState.Normal && button.Enabled) { g.DrawLine(paintToolboxCur.PenOutline, button.Bounds.X + button.Bounds.Width - 15, button.Bounds.Y , button.Bounds.X + button.Bounds.Width - 15, button.Bounds.Y + button.Bounds.Height); } } }