制作xp样式的窗体风格(仅在xp下可用)(主要是解决了按钮、文本框、下拉组合框等,工具栏没有解决)
新建一文本文件,改名为:应用程序名.exe.manifest;(注意应用程序名在\bin\debug\下,扩展名是manifest,不是txt),将其与你的应用程序放在一起,不行就放与解决方案文件放一起。
内容如下:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<assembly xmlns="urn:schemas-microsoft-com:asm.v1" manifestVersion="1.0">
<assemblyIdentity
version="1.0.0.0"
processorArchitecture="X86"
name="Microsoft.Windows.MyApp"
type="win32"
/>
<description>MyApp</description>
<dependency>
<dependentAssembly>
<assemblyIdentity
type="win32"
name="Microsoft.Windows.Common-Controls"
version="6.0.0.0"
processorArchitecture="X86"
publicKeyToken="6595b64144ccf1df"
language="*"
/>
</dependentAssembly>
</dependency>
</assembly>
然后把所有的控件的FlatStyle属性(没有这个属性的不用改)改为System;就可以显示xp风格了。
然后在主程序的main方法中(在RUN前)加入:Application.EnableStyle();
编写菜单:(仿officexp样式,最基本的,大家可以自己改,目前只能对子菜单项操作(如文件下的打开、保存子菜单),不能对主菜单(如打开菜单)进行操作。
命名空间:using System.Drawing;
先写一方法,放在有菜单的窗体的load事件中:
private void MyDrawMenuItems(string str, System.Windows.Forms.DrawItemEventArgs e,bool spliter)//, bool upline,bool downline
{
Graphics g = e.Graphics ;
Rectangle aRect = new Rectangle ();//绘制图标的矩形区域
aRect.X = e.Bounds .X+1 ;
aRect.Y = e.Bounds .Y+1 ;
aRect.Height = e.Bounds .Height-2 ;;//-2的目的是选中项要离边框一个象素距离,下面一行同理
aRect.Width = e.Bounds .Width -2;
//以下定义每个菜单项顺时针四个角的坐标
int x1 = e.Bounds.X;
int y1 = e.Bounds.Y;
int x2 = e.Bounds.X + e.Bounds.Width;
int y2 = e.Bounds.Y;
int x3 = x2;
int y3 = e.Bounds.Y+ e.Bounds.Height;
int x4 = x1;
int y4 = y3;
if (spliter == false)
{
if ((e.State & DrawItemState.Selected ) == DrawItemState.Selected )
{//判断菜单项是否被选中,填充不同颜色,下面的代码为被选中
g.FillRectangle (new SolidBrush (Color.FromArgb(60,0,0,255)),aRect );//填充60%透明的蓝
//下面四行语句是对每个选中菜单项画边框
g.DrawLine(Pens.Blue,x1+1,y1+1,x2-1,y2+1);
g.DrawLine(Pens.Blue,x2-1,y2+1,x3-1,y3-1);
g.DrawLine(Pens.Blue,x3-1,y3-1,x4+1,y4-1);
g.DrawLine(Pens.Blue,x4+1,y4-1,x1+1,y1+1);
}
else //没选中
{
g.FillRectangle (new SolidBrush (Color.FromArgb(252,252,249) ),e.Bounds );//重画菜单
g.FillRectangle (new SolidBrush (Color.FromArgb(226,223,216)),e.Bounds.X,e.Bounds.Y,15,e.Bounds.Height);//画菜单左侧的竖条
}
//写菜单文本
g.DrawString (str,this.Font ,new SolidBrush (Color.Black ),e.Bounds.X +20,e.Bounds .Y+9 );
}
else //如果是分隔条
{
g.FillRectangle (new SolidBrush (Color.FromArgb(252,252,249) ),e.Bounds );
g.FillRectangle (new SolidBrush (Color.FromArgb(226,223,216)),e.Bounds.X,e.Bounds.Y,15,e.Bounds.Height);
g.DrawLine (new Pen(SystemColors.GrayText),x1+20,y1+e.Bounds.Height/2,x3-5,y1+e.Bounds.Height/2);//画分隔线
}
}
将菜单项的ownerdraw属性改为true,在每个菜单项的MeasureItem事件中加入:
private void menuItem6_MeasureItem(object sender, System.Windows.Forms.MeasureItemEventArgs e)
{
e.ItemWidth = 120;
e.ItemHeight = 25;
}
DrawItem事件中加入:
private void menuItem2_DrawItem(object sender, System.Windows.Forms.DrawItemEventArgs e)
{
MyDrawMenuItems("1234" ,e,false);//要是分隔线的话就是true
}
ok了!
[此贴子已经被作者于2006-6-18 17:02:33编辑过]