📄 gmenuitem.cs
字号:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;using System.Drawing.Drawing2D;
namespace gowk.controls
{
/// <summary>
/// GMenuItem 的摘要说明。
/// </summary>
public class GMenuItem : System.Windows.Forms.MenuItem
{
Image image;int itemheight=0;
public GMenuItem():base()
{
this.OwnerDraw=true;
}
public GMenuItem(string text,Image img,System.EventHandler onclick):base(text,onclick)
{
this.OwnerDraw=true;
this.image=img;
}
public Image Image
{
get{return this.image;}
set
{
this.image=value;
if(this.image!=null)
{
this.image=Image.GetThumbnailImage(ControlSetting.MenuItemImageWidth,ControlSetting.MenuItemImageHeight,null,IntPtr.Zero);
}
}
}
protected override void OnDrawItem(DrawItemEventArgs e)
{
// base.OnDrawItem(e);
Graphics g=e.Graphics;
Rectangle rect=e.Bounds;
Color foreColor=ControlSetting.ForeColor;
Color backColor=ControlSetting.BackColor;
Font font=e.Font;
g.SmoothingMode=SmoothingMode.AntiAlias;
if(this.Parent.GetType()==typeof(GContextMenu))
{
rect.X+=ControlSetting.ContextMenuBannerWidth;
rect.Width-=ControlSetting.ContextMenuBannerWidth;
if(e.Index==0)
{
int total=this.GetTotalHeight(g);
g.TranslateTransform(0,total);
g.RotateTransform(270);
Rectangle bannerRect=new Rectangle(0,0,total,ControlSetting.ContextMenuBannerWidth);
g.FillRectangle(new SolidBrush(Color.White),bannerRect);
GContextMenu gcm=(GContextMenu)this.Parent;
if(gcm.Image!=null)
{
g.DrawImage(gcm.Image,bannerRect);
}
else
{
Font f=new Font(font.Name,16,FontStyle.Bold);
g.DrawString("GOWK",f,new SolidBrush(Color.LightBlue),bannerRect);
}
g.ResetTransform();
}
}
if(this.Text=="-")
{
g.FillRectangle(new SolidBrush(backColor),rect);
Color c=Color.FromArgb((int)((backColor.R+foreColor.R)/2)+(int)((backColor.G+foreColor.G)/2)+(int)((backColor.B+foreColor.B)/2));
g.DrawLine(new Pen(c),rect.X+ControlSetting.ItemOffset,rect.Y+2,rect.Right-ControlSetting.ItemOffset,rect.Y+2);
return;
}
Rectangle imgRect=new Rectangle();
imgRect.X=rect.X+ControlSetting.ItemOffset;
imgRect.Y=rect.Y+(int)((rect.Height-ControlSetting.MenuItemImageHeight)/2);
imgRect.Width=ControlSetting.MenuItemImageWidth;
imgRect.Height=ControlSetting.MenuItemImageHeight;
PointF strPoint=new PointF();
strPoint.X=imgRect.Right+ControlSetting.ItemOffset;
strPoint.Y=rect.Y+(rect.Height-font.Height)/2;
Rectangle checkBtnRect=new Rectangle();
checkBtnRect.Width=checkBtnRect.Height=16;
checkBtnRect.Y=rect.Y+4;
checkBtnRect.X=rect.Right-checkBtnRect.Width-15;
if((e.State & DrawItemState.Selected)==DrawItemState.Selected)
{
backColor=ControlSetting.SelectedBackColor;
foreColor=ControlSetting.SelectedForeColor;
imgRect.Offset(-1,-1);
strPoint.X-=1;
strPoint.Y-=1;
}
//draw the background
g.FillRectangle(new SolidBrush(backColor),rect);
//draw the icon
if(this.image!=null)
{
g.DrawImage(this.image,imgRect);
}
//draw text
g.DrawString(this.Text,font,new SolidBrush(foreColor),strPoint);
//if this item has child ,draw .......
/* if(this.IsParent)
{
PointF[] ps=new PointF[3];
ps[0].X=rect.Right-4;
ps[0].Y=rect.Y+rect.Height/2;
ps[1].X=ps[2].X=ps[0].X-3;
ps[1].Y=ps[0].Y-3;
ps[2].Y=ps[0].Y+3;
g.FillPolygon(new SolidBrush(Color.DarkGreen),ps);
}*/
//draw the checkbox
if(this.Checked)
{
Pen pen=new Pen(Color.Black,1);
// g.DrawRectangle(pen,checkBtnRect);
g.DrawLine(pen,checkBtnRect.X+2,checkBtnRect.Y+9,checkBtnRect.X+6,checkBtnRect.Bottom-2);
g.DrawLine(pen,checkBtnRect.X+6,checkBtnRect.Bottom-2,checkBtnRect.X+12,checkBtnRect.Y);
}
}
protected override void OnMeasureItem(MeasureItemEventArgs e)
{
// base.OnMeasureItem (e);
if(this.Text!="-")
{
e.ItemHeight=20;
this.itemheight=20;
e.ItemWidth=2*ControlSetting.ItemOffset+ControlSetting.MenuItemImageWidth+(int)(e.Graphics.MeasureString(this.Text,System.Windows.Forms.SystemInformation.MenuFont).Width);
if(this.Parent.GetType()==typeof(GContextMenu))e.ItemWidth+=ControlSetting.ContextMenuBannerWidth;
e.ItemWidth+=35;
}
else
{
e.ItemHeight=3;
this.itemheight=3;
}
}
int GetTotalHeight(Graphics g)
{
int t=0;
/* for(int i=0;i<this.Parent.MenuItems.Count;i++)
{
MeasureItemEventArgs arg=new MeasureItemEventArgs(g,i);
this.OnMeasureItem(arg);
t+=arg.ItemHeight;
}*/
foreach(GMenuItem item in this.Parent.MenuItems)
{
t+=item.itemheight;
}
return t;
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -