【控件重绘】谁有最简单的控件重绘的例子,我现在想重绘两个控件toolbar和statusbar

bearbaba 2004-01-10 10:20:09
谁有例子可以给我看看,谢谢
...全文
73 点赞 收藏 6
写回复
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
ffb 2004-09-06
C:\Documents and Settings\XXX\My Documents\Visual Studio Projects\ChartOcxTest\Class1.cs(125): 类型或命名空间名称“Drawing”在类或命名空间“Iridescent”中不存在(是否缺少程序集引用?)
回复
haiwangstar 2004-01-11
private void DrawBackground(Graphics g, Color color, bool border, Rectangle rect)
{
g.FillRectangle(new SolidBrush(color), rect);
if (border)
g.DrawRectangle(new Pen(Iridescent.Drawing.SystemColors.SelectionBorderColor), rect);
}

private void DrawImage(Graphics g, int index, bool pressed, bool enabled, Rectangle rect)
{
if(index > -1 && this.ImageList != null && this.ImageList.Images.Count > index)
{
Image img = this.ImageList.Images[index];

if(pressed)
{
rect = new Rectangle(rect.X+1, rect.Y+1, rect.Width, rect.Height);
}

// Draw image
if(enabled)
{
g.DrawImageUnscaled(img, rect);
}
else
{
ControlPaint.DrawImageDisabled(g, img, rect.X, rect.Y, Color.Transparent);
}
}
}

private void DrawText(Graphics g, string text, bool pressed, bool enabled, Rectangle rect)
{
if(pressed)
{
rect = new Rectangle(rect.X+1, rect.Y+1, rect.Width, rect.Height);
}

StringFormat sf = new StringFormat();
if(this.TextAlign == ToolBarTextAlign.Right)
{
sf.Alignment = StringAlignment.Near;
sf.LineAlignment = StringAlignment.Center;
}
else
{
sf.Alignment = StringAlignment.Center;
sf.LineAlignment = StringAlignment.Far;
}
if (rtl) sf.FormatFlags = StringFormatFlags.DirectionRightToLeft;

// Draw text
if(enabled)
{
g.DrawString(text, this.Font, SystemBrushes.ControlText, rect, sf);
}
else
{
ControlPaint.DrawStringDisabled(g, text, this.Font, SystemColors.GrayText, rect, sf);
}
}

private void DrawArrow(Graphics g, ToolBarButton btn, Rectangle rect)
{
Point p1 = new Point(rect.X+3, rect.Y+rect.Height/2-2);
Point p2 = new Point(rect.X+rect.Width-3, rect.Y+rect.Height/2-2);
Point p3 = new Point(rect.X+rect.Width/2, rect.Y+rect.Height/2+2);

// Draw triangle
if(btn.Enabled)
{
g.FillPolygon(new SolidBrush(System.Drawing.SystemColors.ControlText),new Point[]{p1, p2, p3});
}
else
{
g.FillPolygon(new SolidBrush(Iridescent.Drawing.SystemColors.ControlColor),new Point[]{p1, p2, p3});
}
}

private void DrawSeparator(Graphics g,ToolBarButton btn)
{
g.DrawLine(new Pen(System.Drawing.SystemColors.ControlDarkDark),btn.Rectangle.X+1,btn.Rectangle.Y+2,btn.Rectangle.X+1,btn.Rectangle.Bottom-4);
}
#endregion

private bool getrtl()
{
//used SystemInformation.RightAlignedMenus because couldn't
//find any other system property that may be close to RTL
bool isrtl = SystemInformation.RightAlignedMenus;
if (this.RightToLeft == RightToLeft.Yes)
isrtl = true;
if (this.RightToLeft == RightToLeft.No)
isrtl = false;
return isrtl;
}
}
}
回复
haiwangstar 2004-01-11
using System;
using System.Collections;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Windows.Forms;

namespace Iridescent.Windows.Forms
{ /// <summary>
/// Summary description for WToolBar.
/// </summary>
public class RichToolBar : System.Windows.Forms.ToolBar
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;
private bool rtl = false; //right to left

#region Constructor, initializer, destructor
public RichToolBar()
{
// This call is required by the Windows.Forms Form Designer.
InitializeComponent();

// TODO: Add any initialization after the InitForm call

rtl = getrtl();

SetStyle(ControlStyles.UserPaint,true);
SetStyle(ControlStyles.DoubleBuffer,true);
SetStyle(ControlStyles.AllPaintingInWmPaint,true);

base.Appearance = ToolBarAppearance.Flat;
base.BorderStyle = BorderStyle.None;
base.Divider = false;
}

/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if(components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}

/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
components = new System.ComponentModel.Container();
}
#endregion

#region overrides

protected override void OnButtonClick(ToolBarButtonClickEventArgs e)
{
base.OnButtonClick(e);
this.Invalidate();
}

protected override void OnMouseUp(MouseEventArgs e)
{
base.OnMouseUp(e);
this.Invalidate();
}

protected override void OnPaint(System.Windows.Forms.PaintEventArgs e)
{
base.OnPaint(e);

Point mPt = this.PointToClient(Control.MousePosition);
foreach(ToolBarButton btn in this.Buttons)
{
bool hot = btn.Rectangle.Contains(mPt);

if(btn.Style == ToolBarButtonStyle.Separator)
{
DrawSeparator(e.Graphics,btn);
}
else
{
DrawButton(e.Graphics,hot,btn.Rectangle,btn);
}
}
}

protected override void OnRightToLeftChanged(System.EventArgs e)
{
base.OnRightToLeftChanged(e);
rtl = getrtl();
this.Invalidate();
}
#endregion

#region Drawing Functions

private void DrawButton(Graphics g,bool hot,Rectangle buttonRect,ToolBarButton btn)
{
buttonRect = new Rectangle(buttonRect.X,buttonRect.Y,buttonRect.Width-1,buttonRect.Height-1);

Point mPt = this.PointToClient(Control.MousePosition);
bool mouseInDropDown = false;
bool pressed = (Control.MouseButtons == MouseButtons.Left && btn.Rectangle.Contains(mPt));

// Get if mouse is in DropDownButton.
if(btn.Style == ToolBarButtonStyle.DropDownButton)
{
Rectangle dropDownRect = new Rectangle(buttonRect.X+buttonRect.Width-12,buttonRect.Y,12,buttonRect.Height);
mouseInDropDown = dropDownRect.Contains(mPt);
}

if(hot && btn.Enabled)
{
DrawBackground(g, Iridescent.Drawing.SystemColors.SelectionColor, true, buttonRect);

// If button is pressed, replace hot color with pressed color in button rect.
// For DropDownButton, don't draw pressed color for right dropDown section.
if(pressed)
{
if (mouseInDropDown)
DrawBackground(g, System.Drawing.SystemColors.Control, true, buttonRect);
else
DrawBackground(g, Iridescent.Drawing.SystemColors.PressedColor, true, buttonRect);
}

// Draw vertical(|) separator for DropDownButton
if(btn.Style == ToolBarButtonStyle.DropDownButton)
{
if(!(mouseInDropDown && pressed))
{
g.DrawLine(new Pen(Iridescent.Drawing.SystemColors.SelectionBorderColor),buttonRect.Right-13,buttonRect.Top,buttonRect.Right- 13,buttonRect.Bottom);
}
}
}
else
{
DrawBackground(g, System.Drawing.SystemColors.Control, false, buttonRect);

//Handle the special case of Toggle Button
if (btn.Style == ToolBarButtonStyle.ToggleButton)
{
if (btn.Pushed)
DrawBackground(g, Iridescent.Drawing.SystemColors.SelectionColor, true, buttonRect);
if (btn.PartialPush)
DrawBackground(g, Iridescent.Drawing.SystemColors.CheckedColor, true, buttonRect);
}
}

//Split the current rectangle into two
Rectangle imgRect;
Rectangle textRect;

if (this.TextAlign == ToolBarTextAlign.Right)
{
imgRect = new Rectangle(buttonRect.X+3, buttonRect.Y+(buttonRect.Height-this.ImageSize.Height)/2, this.ImageSize.Width, this.ImageSize.Height);
textRect = new Rectangle(buttonRect.X+this.ImageSize.Width+6, buttonRect.Y, buttonRect.Width-this.ImageSize.Width-6, buttonRect.Height);
}
else //if (this.TextAlign == ToolBarTextAlign.Underneath)
{
imgRect = new Rectangle(buttonRect.X+(buttonRect.Width-this.ImageSize.Width)/2, buttonRect.Y+3, this.ImageSize.Width, this.ImageSize.Height);
textRect = new Rectangle(buttonRect.X, buttonRect.Y+this.ImageSize.Height+6, buttonRect.Width, buttonRect.Height-this.ImageSize.Height-6);
}

//if we need to draw the drop down glyph as well
if(btn.Style == ToolBarButtonStyle.DropDownButton)
{
if (this.TextAlign == ToolBarTextAlign.Right)
{
textRect.Width -= 12;
}
else
{
imgRect.X -= 6;
textRect.X -= 6;
}
Rectangle glyphRect = new Rectangle(buttonRect.X+buttonRect.Width-12, buttonRect.Y, 12, buttonRect.Height);
DrawArrow(g,btn, glyphRect);
}

// Draw image for button
DrawImage(g, btn.ImageIndex, pressed, btn.Enabled, imgRect);

// Draw Text
if (btn.Text.Length != 0)
DrawText(g, btn.Text, pressed, btn.Enabled, textRect);
}

回复
haiwangstar 2004-01-11
StatusBar

using System;
using System.Collections;
using System.Drawing;
using System.Windows.Forms;

namespace Iridescent.Windows.Forms
{
/// <summary>
/// Provides StatusBar
/// </summary>
public class RichStatusBar : System.Windows.Forms.StatusBar
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;
private bool rtl = false; //right to left

#region Constructor, initializer, destructor
public RichStatusBar()
{
// This call is required by the Windows.Forms Form Designer.
InitializeComponent();

// TODO: Add any initialization after the InitForm call

SetStyle(ControlStyles.UserPaint,true);
SetStyle(ControlStyles.DoubleBuffer,true);
SetStyle(ControlStyles.AllPaintingInWmPaint,true);

rtl = getrtl();
this.SizingGrip = false;
this.Dock = DockStyle.Bottom;
}

/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if(components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}

/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
components = new System.ComponentModel.Container();
}
#endregion

#region Override
protected override void OnDrawItem(StatusBarDrawItemEventArgs e)
{
}

protected override void OnRightToLeftChanged(System.EventArgs e)
{
base.OnRightToLeftChanged(e);
rtl = getrtl();
this.Invalidate();
}

protected override void OnPaint(System.Windows.Forms.PaintEventArgs e)
{
//Fill the area with control color
e.Graphics.FillRectangle(SystemBrushes.Control, e.ClipRectangle);

//set anti-alias mode. It looks better that way
//this is called after fill rectangle, because otherwise it leaves artifacts
e.Graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;

int x = e.ClipRectangle.X;
int y = e.ClipRectangle.Y;
int h = e.ClipRectangle.Height;
int start = 0;
foreach (StatusBarPanel panel in this.Panels)
{
int width = panel.Width;
if(start < e.ClipRectangle.Width)
{
if ((start+width) > e.ClipRectangle.Width)
{
width = e.ClipRectangle.Width - start;
}
Rectangle rect = new Rectangle(start, e.ClipRectangle.Y, width, e.ClipRectangle.Height);
DrawPanel(e.Graphics, rect, panel);
start+= panel.Width;
}
}
}
#endregion

private bool getrtl()
{
//used SystemInformation.RightAlignedMenus because couldn't
//find any other system property that may be close to RTL
bool isrtl = SystemInformation.RightAlignedMenus;
if (this.RightToLeft == RightToLeft.Yes)
isrtl = true;
if (this.RightToLeft == RightToLeft.No)
isrtl = false;
return isrtl;
}

private void DrawPanel (Graphics g, Rectangle rect, StatusBarPanel panel)
{
if (panel.Icon != null)
{
int ih = panel.Icon.Height;
int iw = panel.Icon.Width;
Size ts = g.MeasureString(panel.Text, this.Font).ToSize();
int th = ts.Height;
int tw = ts.Width+2;//just to avoid ellipsis

Rectangle iconRect = new Rectangle(rect.X+4, rect.Y+(rect.Height-panel.Icon.Height)/2, panel.Icon.Width, panel.Icon.Height);
Rectangle textRect = new Rectangle(rect.X+8+iw, rect.Y+(rect.Height-th)/2, tw, th);
int contentWidth = iw+tw+12; //12 = 2(border left) +2(icon margin left) +2(icon margin right) +2 (text margin left) + 2(text margin right) + 2(border right)
if(contentWidth < panel.Width)
{
//is it to be drawn to left?
//then we don't need to shift anything
int margin = 0;
//is it to be drawn to the right
if(((panel.Alignment == HorizontalAlignment.Right)&&(!rtl)) || ((panel.Alignment == HorizontalAlignment.Left)&&(rtl)))
{
margin = rect.Width-contentWidth;
}
//it is to be drawn to the center
if(panel.Alignment == HorizontalAlignment.Center)
{
margin = (rect.Width-contentWidth)/2;
}

iconRect.X += margin;
textRect.X += margin;
}
iconRect.Intersect(rect);
textRect.Intersect(rect);

//Draw the icon
g.DrawIcon(panel.Icon, iconRect);
DrawText(g, textRect, panel);
}
else
{
DrawText(g, rect, panel);
}

//Draw the border
g.DrawRectangle(SystemPens.ControlDark, new Rectangle(rect.X+2, rect.Y+2, rect.Width - 4, rect.Height - 4));
}

private void DrawText(Graphics g, Rectangle rect, StatusBarPanel panel)
{
StringFormat sf = new StringFormat();
sf.Trimming = StringTrimming.EllipsisWord;
switch (panel.Alignment)
{
case HorizontalAlignment.Left:
sf.Alignment = StringAlignment.Near;
break;
case HorizontalAlignment.Center:
sf.Alignment = StringAlignment.Center;
break;
case HorizontalAlignment.Right:
sf.Alignment = StringAlignment.Far;
break;
}
if(rtl) sf.FormatFlags = StringFormatFlags.DirectionRightToLeft;
Rectangle textRect = new Rectangle(rect.X, rect.Y + (rect.Height - this.Font.Height)/2, rect.Width, rect.Height - (rect.Height - this.Font.Height));
g.DrawString(panel.Text, this.Font, SystemBrushes.ControlText, textRect, sf);
}
}
}
回复
lyhold 2004-01-11
帮你顶一下!
回复
wd_318 2004-01-10
using System;
using System.Drawing;
using System.Windows;
using System.Windows.Forms;

namespace MyCtrl
{
/// <summary>
/// 自绘XP风格按钮
/// </summary>
public class MyButton : System.Windows.Forms.Button
{
private bool mouseDown=false;
private bool mouseHover=false;
public MyButton()
{
SetStyle(ControlStyles.UserPaint,true);
MouseDown+=new MouseEventHandler(OnMouseDown);
MouseUp+=new MouseEventHandler(OnMouseUp);
MouseEnter+=new EventHandler(OnMouseEnter);
MouseLeave+=new EventHandler(OnMouseLeave);
Height=23;
Width=75;
}

protected override void OnPaint(PaintEventArgs pe)
{
pe.Graphics.FillRectangle(new SolidBrush(Parent.BackColor),
pe.ClipRectangle);
if (Enabled == false)
{
DrawDisableButton(pe.Graphics);
}
else if (mouseDown)
{
DrawMouseDownButton(pe.Graphics);
}
else if (mouseHover)
{
DrawMouseHoverButton(pe.Graphics);
}
else if (Focused)
{
DrawContainFocusButton(pe.Graphics);
}
else
{
DrawNormalButton(pe.Graphics);
}
WriteText(pe.Graphics);
}
private void OnMouseDown(object sender,MouseEventArgs e)
{
mouseDown=true;
}

private void OnMouseUp(object sender,MouseEventArgs e)
{
mouseDown=false;
PaintEventArgs pe =
new PaintEventArgs(CreateGraphics(),ClientRectangle);
OnPaint(pe);
}

private void OnMouseEnter(object sender,EventArgs e)
{
mouseHover=true;
PaintEventArgs pe =
new PaintEventArgs(CreateGraphics(),ClientRectangle);
OnPaint(pe);
}

private void OnMouseLeave(object sender,EventArgs e)
{
mouseHover=false;
PaintEventArgs pe =
new PaintEventArgs(CreateGraphics(),ClientRectangle);
OnPaint(pe);
}

private void DrawBorder(Graphics g,int state)
{
if (state==1)
{
Pen p = new Pen(SystemColors.ControlLightLight,2);
g.DrawLine(p,1,1,1,Height-2);
g.DrawLine(p,1,1,Width-2,1);
g.DrawLine(p,Width-1,2,Width-1,Height-2);
g.DrawLine(p,2,Height-1,Width-2,Height-1);
}
else if (state==2)
{
Pen p = new Pen(Color.Yellow,2);
g.DrawLine(p,1,1,1,Height-2);
g.DrawLine(p,1,1,Width-2,1);
g.DrawLine(p,Width-1,2,Width-1,Height-2);
g.DrawLine(p,2,Height-1,Width-2,Height-1);
}
else if (state==3)
{
Pen p = new Pen(SystemColors.ControlDark,2);
g.DrawLine(p,1,1,1,Height-2);
g.DrawLine(p,1,1,Width-2,1);
g.DrawLine(p,Width-1,2,Width-1,Height-2);
g.DrawLine(p,2,Height-1,Width-2,Height-1);
}
else if (state==4)
{
Pen p = new Pen(SystemColors.ControlLight,2);
g.DrawLine(p,1,1,1,Height-2);
g.DrawLine(p,1,1,Width-2,1);
g.DrawLine(p,Width-1,2,Width-1,Height-2);
g.DrawLine(p,2,Height-1,Width-2,Height-1);
}
else if (state==5)
{
Pen p = new Pen(Color.SkyBlue,2);
g.DrawLine(p,1,1,1,Height-2);
g.DrawLine(p,1,1,Width-2,1);
g.DrawLine(p,Width-1,2,Width-1,Height-2);
g.DrawLine(p,2,Height-1,Width-2,Height-1);
}
if (state==4)
{
Pen p = new Pen(Color.FromArgb(161,161,146),1);
g.DrawLine(p,0,2,0,Height-3);
g.DrawLine(p,2,0,Width-3,0);
g.DrawLine(p,Width-1,2,Width-1,Height-3);
g.DrawLine(p,2,Height-1,Width-3,Height-1);
g.DrawLine(p,0,2,2,0);
g.DrawLine(p,0,Height-3,2,Height-1);
g.DrawLine(p,Width-3,0,Width-1,2);
g.DrawLine(p,Width-3,Height-1,Width-1,Height-3);
}
else
{
g.DrawLine(Pens.Black,0,2,0,Height-3);
g.DrawLine(Pens.Black,2,0,Width-3,0);
g.DrawLine(Pens.Black,Width-1,2,Width-1,Height-3);
g.DrawLine(Pens.Black,2,Height-1,Width-3,Height-1);
g.DrawLine(Pens.Black,0,2,2,0);
g.DrawLine(Pens.Black,0,Height-3,2,Height-1);
g.DrawLine(Pens.Black,Width-3,0,Width-1,2);
g.DrawLine(Pens.Black,Width-3,Height-1,
Width-1,Height-3);
}
}

private void DrawNormalButton(Graphics g)
{
DrawBorder(g,1);
PaintBack(g,SystemColors.ControlLightLight);
}

private void DrawMouseHoverButton(Graphics g)
{
DrawBorder(g,2);
PaintBack(g,SystemColors.ControlLightLight);
}

private void DrawMouseDownButton(Graphics g)
{
DrawBorder(g,3);
PaintBack(g,SystemColors.ControlLight);
}

private void DrawDisableButton(Graphics g)
{
DrawBorder(g,4);
PaintBack(g,SystemColors.ControlLight);
}

private void DrawContainFocusButton(Graphics g)
{
DrawBorder(g,5);
PaintBack(g,SystemColors.ControlLightLight);
}

private void PaintBack(Graphics g,Color c)
{
g.FillRectangle(new SolidBrush(c),3,3,
Width-6,Height-6);
}

private void WriteText(Graphics g)
{
int x=0,y=0;
Size s = g.MeasureString(Text,Font).ToSize();
x=(Width-s.Width)/2;
y=(Height-s.Height)/2;
if (Enabled)
g.DrawString(Text,Font,Brushes.Black,x,y);
else
g.DrawString(Text,Font,Brushes.Gray,x,y);
}
}
}
回复
发动态
发帖子
C#
创建于2007-09-28

10.4w+

社区成员

.NET技术 C#
申请成为版主
社区公告

全世界最好的语言,没有之一.