一个Xp风格的表单类(一)

其实是一个 无边框窗体,写成的表单类….但是外表看起来和普通的FORM完全一样.

呵呵. 最可惜的是,现在还没找到,在工具栏上放菜单的办法……哭….

下面是运行时候的图片..

我喜欢程序,更喜欢程序有个PL的界面….

但是网上似乎找不到详细的,关于此方面的教程或源码.

重绘时改用了双缓冲,在窗体上有动画的时候,重绘不会出现闪烁的现象了.

献丑一下,把源码贴出来,大家一起交流….

下面是引用,以及,声明的部分##########################################

using System;

using System.ComponentModel;

using System.Windows.Forms;

using System.Drawing;

using System.Drawing.Drawing2D;

using System.IO;

using System.Runtime.InteropServices;

namespace XpFormOfApc

{

/// <summary>

/// Form1 的摘要说明。

/// </summary>

public class XpForm : System.Windows.Forms.Form

{

public delegate void SysHandler(object sender,System.EventArgs e);

public event SysHandler SysDoubleClick;

//############API设置窗口STYLE#############

private const int GWL_STYLE = (-16);

private const int WS_SYSMENU = 0x80000;

private const int WS_SIZEBOX = 0x40000;

private const int WS_MAXIMIZEBOX = 0x10000;

private const int WS_MINIMIZEBOX=0x20000;

[System.Runtime.InteropServices.DllImport("user32", EntryPoint="SetWindowLong")]

private static extern int SetWindowLongA(int hwnd, int nIndex, int dwNewLong);

//###########普通组件定义#################

private System.ComponentModel.IContainer components;

private System.Windows.Forms.ImageList imglist;

private System.Windows.Forms.ImageList syslist;

//###############组件属性##################

//—系统按钮—

private bool Sys_Close_Visible=true;

private bool Sys_Max_Visible=true;

private bool Sys_Res_Visible=false;

private bool Sys_Min_Visible=true;

private bool Sys_Down_Visible=false;

private bool Sys_Up_Visible=true;

private bool Sys_Tray_Visible=true;

private bool IsShowInTray=false;

private int FormIconWidth=16;

private int FormIconHeight=16;

private const int ONE=23; //按钮宽21+2间隔

private int[,] SysCmd=new int[9,7]{

{1,0,7,0,6,21,21},

{1,1,8,0,6,21,21},

{0,2,9,0,6,21,21},

{1,3,10,0,6,21,21},

{0,4,11,0,6,21,21},

{1,5,12,0,6,21,21},

{1,6,13,0,6,21,21},

{10,5,12,0,0,21,21},

{11,5,12,32,0,0,30}

}; //10-FORM_ICON 11-FORM_TITLE

//—表单图标–托盘图标—

private NotifyIcon ni=new NotifyIcon();

private Icon TrayIcon;

private ContextMenu TrayContextMenu;

private string[] TrayToolTip;

private string TempTrayToolTip;

//public event SysOnDoubleClick (System.EventArgs e); //托盘图标双击事件

private Rectangle FormRect;//表单上一状态的RECT 用于Down还原

//允许拖拽标志 – 坐标

private bool CanMove=false;

private Point CurPoint=new Point(0,0);

private Point CurPoint2=new Point(0,0);

//

private int CanUp=0;

//====================================================

[DllImport("user32.dll")]

public static extern bool ReleaseCapture();

[DllImport("user32.dll")]

public static extern bool SendMessage(IntPtr hwnd,int wMsg,int wParam,int lParam);

private const int WM_SYSCOMMAND=0x0112;

private const int SC_MOVE=0xF010;

private const int HTCAPTION=0x0002;

private const int HTRIGHT=0x0011;

################ 下面是代码 ###############

public XpForm()

{

//

// Windows 窗体设计器支持所必需的

//

InitializeComponent();

//this.SetBounds(4,30,this.Width-8,this.Height-34);

//

// TOD 在 InitializeComponent 调用后添加任何构造函数代码

//

}

/// <summary>

/// 清理所有正在使用的资源。

/// </summary>

protected override void Dispose( bool disposing )

{

if( disposing )

{

if (components != null)

{

components.Dispose();

}

}

base.Dispose( disposing );

}

#region Windows 窗体设计器生成的代码

/// <summary>

/// 设计器支持所需的方法 – 不要使用代码编辑器修改

/// 此方法的内容。

/// </summary>

private void InitializeComponent()

{

this.components = new System.ComponentModel.Container();

System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(XpForm));

this.imglist = new System.Windows.Forms.ImageList(this.components);

this.syslist = new System.Windows.Forms.ImageList(this.components);

//

// imglist

//

this.imglist.ColorDepth = System.Windows.Forms.ColorDepth.Depth32Bit;

this.imglist.ImageSize = new System.Drawing.Size(50, 50);

this.imglist.ImageStream = ((System.Windows.Forms.ImageListStreamer)(resources.GetObject("imglist.ImageStream")));

this.imglist.TransparentColor = System.Drawing.Color.Black;

//

// syslist

//

this.syslist.ColorDepth = System.Windows.Forms.ColorDepth.Depth32Bit;

this.syslist.ImageSize = new System.Drawing.Size(21, 21);

this.syslist.ImageStream = ((System.Windows.Forms.ImageListStreamer)(resources.GetObject("syslist.ImageStream")));

this.syslist.TransparentColor = System.Drawing.Color.Transparent;

//

// XpForm

//

this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);

this.ClientSize = new System.Drawing.Size(280, 136);

this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;

this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));

this.Name = "XpForm";

this.Text = "APC心灵空间";

}

#endregion

/// <summary>

/// 应用程序的主入口点。

/// </summary>

[STAThread]

static void Main()

{

//XpForm xf=new XpForm();

//xf.SysDoubleClick+=new SysHandler(xf.ni_DoubleClick);

Application.Run(new XpForm());

}

//######################### 自 定 函 数 ##############################

private void RePaint(bool ClearAll)

{

Graphics g=this.CreateGraphics();

if(ClearAll==true){g.Clear(Color.FromArgb(242,242,242));}

//画XP边框

g.DrawImage(imglist.Images[1],0,0,this.Width+50,30); //横幅

g.DrawImage(imglist.Images[0],0,0,6,30); //左角

g.DrawImage(imglist.Images[2],this.Width-6,0,6,30); //右角

g.DrawImage(imglist.Images[5],0,this.Height-4,this.Width+50,4);//下边

g.DrawImage(imglist.Images[3],0,7,4,this.Height); //左边

g.DrawImage(imglist.Images[4],this.Width-4,7,4,this.Height);//右边

//画图标

g.DrawImage((Image)this.Icon.ToBitmap(),new Rectangle(8,6,FormIconWidth,FormIconHeight));

//画标题

g.DrawString(this.Text,new Font("宋体",9,FontStyle.Bold),new SolidBrush(Color.White),32,10);

//计算..画按钮

InitSysCmd();

int SysIndex=1;

for(int i=0;i<=6;i++)

{

//if(SysVisible[i]==1)

if(SysCmd[i,0]==1)

{

g.DrawImage(syslist.Images[i],this.Width-ONE*SysIndex-6,6,21,21);

SysCmd[i,3]=this.Width-ONE*SysIndex-6; //记录各SysCmd的Rect.X

SysCmd[8,5]=this.Width-ONE*SysIndex-6; //标题栏拖拽的长度

SysIndex+=1;

}

}

g.Dispose();

}

private void RePaint()

{

Bitmap b=new Bitmap(this.Width,this.Height);

Graphics bmp=Graphics.FromImage((Image)b);

//Bitmap c=new Bitmap(100,100,this.CreateGraphics());

//g.Clear(this.BackColor);

//画XP边框

bmp.DrawImage(imglist.Images[1],0,0,this.Width+50,30); //横幅

bmp.DrawImage(imglist.Images[0],0,0,6,30); //左角

bmp.DrawImage(imglist.Images[2],this.Width-6,0,6,30); //右角

bmp.DrawImage(imglist.Images[5],0,this.Height-4,this.Width+50,4);//下边

bmp.DrawImage(imglist.Images[3],0,7,4,this.Height); //左边

bmp.DrawImage(imglist.Images[4],this.Width-4,7,4,this.Height);//右边

//画图标

bmp.DrawImage((Image)this.Icon.ToBitmap(),new Rectangle(8,6,FormIconWidth,FormIconHeight));

//画标题

bmp.DrawString(this.Text,new Font("宋体",9,FontStyle.Bold),new SolidBrush(Color.White),32,10);

//计算..画按钮

InitSysCmd();

int SysIndex=1;

for(int i=0;i<=6;i++)

{

//if(SysVisible[i]==1)

if(SysCmd[i,0]==1)

{

bmp.DrawImage(syslist.Images[i],this.Width-ONE*SysIndex-6,6,21,21);

SysCmd[i,3]=this.Width-ONE*SysIndex-6; //记录各SysCmd的Rect.X

SysCmd[8,5]=this.Width-ONE*SysIndex-6; //标题栏拖拽的长度

SysIndex+=1;

}

}

Graphics g=this.CreateGraphics();

g.DrawImage(b,0,0);

bmp.Dispose();

g.Dispose();

}

private void ReCreateRegion()

{

Rectangle r=Screen.GetWorkingArea(this);

Point[] p=new Point[]{

new Point(0,6),

new Point(1,4),

new Point(4,1),

new Point(6,0),

new Point(this.Width-6,0),

new Point(this.Width-4,1),

new Point(this.Width-1,4),

new Point(this.Width-1,5),

new Point(this.Width,this.Height),

new Point(0,this.Height)};

byte[] b=new byte[]{

(byte)PathPointType.Line,

(byte)PathPointType.Line,

(byte)PathPointType.Line,

(byte)PathPointType.Line,

(byte)PathPointType.Line,

(byte)PathPointType.Line,

(byte)PathPointType.Line,

(byte)PathPointType.Line,

(byte)PathPointType.Line,

(byte)PathPointType.Line};

System.Drawing.Drawing2D.GraphicsPath path=new GraphicsPath(p,b);

this.Region=new Region(path);

}

private void InitSysCmd()

{

if(Sys_Close_Visible==true){SysCmd[0,0]=1;}

else{SysCmd[0,0]=0;}

if(Sys_Max_Visible==true){SysCmd[1,0]=1;}

else{SysCmd[1,0]=0;}

if(Sys_Res_Visible==true){SysCmd[2,0]=1;}

else{SysCmd[2,0]=0;}

if(Sys_Min_Visible==true){SysCmd[3,0]=1;}

else{SysCmd[3,0]=0;}

if(Sys_Up_Visible==true){SysCmd[5,0]=1;}

else{SysCmd[5,0]=0;}

if(Sys_Down_Visible==true){SysCmd[4,0]=1;}

else{SysCmd[4,0]=0;}

if(Sys_Tray_Visible==true){SysCmd[6,0]=1;}

else{SysCmd[6,0]=0;}

}

private void DoClick(int i)

{

switch(i)

{

case 0://关闭

this.Close();

break;

case 1://最大化

this.WindowState=FormWindowState.Maximized;

break;

case 2://还原

this.WindowState=FormWindowState.Normal;

break;

case 3://最小化

this.WindowState=FormWindowState.Minimized;

break;

case 4://放下

this.Top=FormRect.Y;

this.Left=FormRect.X;

this.Width=FormRect.Width;

this.Height=FormRect.Height;

Sys_Down_Visible=false;

Sys_Up_Visible=true;

//this.RePaint();

break;

case 5://收起

FormRect=new Rectangle(this.Left,this.Top,this.Width,this.Height);

this.Height=30;

this.Top=0;

Sys_Up_Visible=false;

Sys_Down_Visible=true;

this.RePaint();

break;

case 6://缩入托盘

ni.Text=TempTrayToolTip;

ni.Icon=TrayIcon;

ni.ContextMenu=TrayContextMenu;

this.WindowState=FormWindowState.Minimized;

this.ShowInTaskbar=false;

this.Visible=false;

ni.Visible=true;

break;

}

}

private void ni_DoubleClick(object sender,System.EventArgs e)

{

OnSysDoubleClick(e);

if(ShowInTray==true)

{

ni.Visible=true;

}

}

void OnSysDoubleClick(System.EventArgs e)

{

if(this.Visible==true)

{

this.WindowState=FormWindowState.Minimized;

this.ShowInTaskbar=false;

this.Visible=false;

ni.Visible=true;

}

else

{

int mStyle=WS_SYSMENU;

if(Sys_Max_Visible==true){mStyle=mStyle|WS_MAXIMIZEBOX;}

if(Sys_Min_Visible==true){mStyle=mStyle|WS_MINIMIZEBOX;}

if(ShowInTray==false){ni.Visible=false;}

this.ShowInTaskbar=true;

this.WindowState=FormWindowState.Minimized;

int a=SetWindowLongA(this.Handle.ToInt32(),GWL_STYLE,mStyle);

this.Visible=true;

this.WindowState=FormWindowState.Normal;

}

}

#region __重 载 事 件___

//######################### 重 载 事 件 ###############################

protected override void OnLoad(System.EventArgs e)

{

int mStyle=WS_SYSMENU;

if(Sys_Max_Visible==true){mStyle=mStyle|WS_MAXIMIZEBOX;}

if(Sys_Min_Visible==true){mStyle=mStyle|WS_MINIMIZEBOX;}

int a=SetWindowLongA(this.Handle.ToInt32(),GWL_STYLE,mStyle);

if(IsShowInTray==true){ni.Icon=TrayIcon;ni.ContextMenu=TrayContextMenu;ni.Text=TempTrayToolTip;ni.Visible=true;}

this.ni.DoubleClick+=new System.EventHandler(this.ni_DoubleClick);

this.SetStyle(ControlStyles.DoubleBuffer|ControlStyles.ResizeRedraw|ControlStyles.ContainerControl,true);

}

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

{

//base.OnPaint(e);

this.RePaint();

base.OnPaint(e);

//ee=e;

}

protected override void OnResize(System.EventArgs e)

{

Rectangle r=Screen.GetWorkingArea(this);

this.MaximumSize=new Size(r.Width,r.Height);

if(this.WindowState==FormWindowState.Maximized)

{

if(Sys_Max_Visible==true)

{

Sys_Max_Visible=false;

Sys_Res_Visible=true;

}

if(Sys_Up_Visible==true)

{

Sys_Up_Visible=false;

CanUp=1;

}

}

if(this.WindowState==FormWindowState.Normal)

{

if(Sys_Res_Visible==true)

{

Sys_Max_Visible=true;

Sys_Res_Visible=false;

}

if(CanUp==1)

{

Sys_Up_Visible=true;

CanUp=0;

}

}

this.RePaint();

//this.OnPaint(ee);

ReCreateRegion();

this.Refresh();

}

protected override void OnMouseMove(System.Windows.Forms.MouseEventArgs e)

{

if(CanMove==true)

{

//this.Cursor=Cursors.SizeAll;

//this.Top=Cursor.Position.Y-CurPoint.Y;

//this.Left=Cursor.Position.X-CurPoint.X;

if(CurPoint.X>SysCmd[8,3] && CurPoint.X<SysCmd[8,5] && CurPoint.Y<30 && (Sys_Res_Visible!=true ))

{

ReleaseCapture();

SendMessage(this.Handle,WM_SYSCOMMAND,SC_MOVE+HTCAPTION, 0);

}

}

Graphics g=this.CreateGraphics();

for(int i=0;i<=6;i++)

{

if(SysCmd[i,0]==1)

{

if(e.X>SysCmd[i,3] && e.X<SysCmd[i,3]+21 && e.Y>6 && e.Y<27)

{g.DrawImage(syslist.Images[SysCmd[i,2]],SysCmd[i,3],6,21,21);}

else{g.DrawImage(syslist.Images[SysCmd[i,1]],SysCmd[i,3],6,21,21);}

}

}

g.Dispose();

CurPoint2.X=Cursor.Position.X-this.Left;

CurPoint2.Y=Cursor.Position.Y-this.Top;

}

protected override void OnMouseDown(System.Windows.Forms.MouseEventArgs e)

{

if(e.Button==MouseButtons.Left && e.X>SysCmd[8,3] && e.X<SysCmd[8,5] && e.Y<30)

{

CanMove=true;

}

CurPoint.X=e.X;

CurPoint.Y=e.Y;

}

protected override void OnMouseUp(System.Windows.Forms.MouseEventArgs e)

{

//if(e.Button==MouseButtons.Left && e.X>SysCmd[8,3] && e.X<SysCmd[8,5] && e.Y<30)

//{

CanMove=false;

this.Cursor=Cursors.Default;

/