一个Xp风格的表单类(一)-.NET教程,报表/图形/Office

其实是一个 无边框窗体,写成的表单类....但是外表看起来和普通的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;
//}
}

protected override void onclick(system.eventargs e)
{
for(int i=0;i<=6;i++)
{
if(syscmd[i,0]==1)
{
if(curpoint.x>syscmd[i,3] && curpoint.x<syscmd[i,3]+21 && curpoint.y>6 && curpoint.y<27)
{doclick(i);}
else{}
}
}
}
protected override void ondoubleclick(system.eventargs e)
{
if(curpoint.x>syscmd[8,3] && curpoint.x<syscmd[8,5] && curpoint.y<30 && (sys_max_visible==true||sys_res_visible==true ))
{
if(this.windowstate==formwindowstate.maximized)
{
this.windowstate=formwindowstate.normal;
}
else
{
this.windowstate=formwindowstate.maximized;
}
this.cursor=cursors.default;
}
}

protected override void onclosing(system.componentmodel.canceleventargs e)
{
ni.visible=false;
ni.dispose();
}

protected override void ontextchanged(system.eventargs e)
{this.repaint(false);}

//protected override void onlayout(system.windows.forms.layouteventargs e)
//{
//mmd,在这里一加事件,居然从tray里恢复窗口的时候,不从画子控件了..郁闷
//this.xppanel.location=new point(4,30);
//this.xppanel.size=new size(this.width-8,this.height-34);
//this.xppanel.sendtoback();
//foreach(control c in this.controls)
//{
//c.parent=this.xppanel;
//}
//}

//protected override void oncontroladded(system.windows.forms.controleventargs ce)
//{
/* 这也不是什么根本的办法,,,,,自己用还中....
try
{
if(ce.control.parent==this && ce.control.name!=this.name && ce.control.name!=this.xppanel.name)
{
ce.control.parent=this.xppanel;
}
}
catch{}
*/
//}

/*
protected override void wndproc(ref message m)
{
base.wndproc(ref m);
if (m.msg == 0x0084 && curpoint2.y<=30 ) //wm_nchittest
{
m.result= (intptr)2; // htclient
this.text=this.curpoint2.y.tostring();
return;
}

}*/
//protected override void onactivated(system.eventargs e)
//{

//}
//####################################################################
#endregion









#region 自 定 义 属 性
//####################### 自 定 义 属 性 #############################
[
category("xpform属性"),
description("获取或设置窗体的关闭按钮是否可见.")
]
public bool sys_close_isvisible
{
get{return sys_close_visible;}
set
{
sys_close_visible=value;
initsyscmd();
int sysindex=0;
for(int i=0;i<=6;i++)
{
if(syscmd[i,0]==1)
{
sysindex+=1;
}
}
this.invalidate(new rectangle(this.width-6-one*sysindex,6,6+one*sysindex,21));
}
}
[
category("xpform属性"),
description("获取或设置窗体的最大化按钮是否可见.")
]
public bool sys_max_isvisible
{
get{return sys_max_visible;}
set
{
sys_max_visible=value;
initsyscmd();
int sysindex=0;
for(int i=0;i<=6;i++)
{
if(syscmd[i,0]==1)
{
sysindex+=1;
}
}
this.invalidate(new rectangle(this.width-6-one*sysindex,6,6+one*sysindex,21));
}
}
[
category("xpform属性"),
description("获取或设置窗体的最小化按钮是否可见.")
]
public bool sys_min_isvisible
{
get{return sys_min_visible;}
set
{
sys_min_visible=value;
initsyscmd();
int sysindex=0;
for(int i=0;i<=6;i++)
{
if(syscmd[i,0]==1)
{
sysindex+=1;
}
}
this.invalidate(new rectangle(this.width-6-one*sysindex,6,6+one*sysindex,21));
}
}
[
category("xpform属性"),
description("获取或设置窗体的收起(向上)按钮是否可见.")
]
public bool sys_up_isvisible
{
get{return sys_up_visible;}
set
{
sys_up_visible=value;
initsyscmd();
int sysindex=0;
for(int i=0;i<=6;i++)
{
if(syscmd[i,0]==1)
{
sysindex+=1;
}
}
this.invalidate(new rectangle(this.width-6-one*sysindex,6,6+one*sysindex,21));
}
}
[
category("xpform属性"),
description("获取或设置窗体的缩入托盘按钮是否可见.")
]
public bool sys_tray_isvisible
{
get{return sys_tray_visible;}
set
{
sys_tray_visible=value;
initsyscmd();
int sysindex=0;
for(int i=0;i<=6;i++)
{
if(syscmd[i,0]==1)
{
sysindex+=1;
}
}
this.invalidate(new rectangle(this.width-6-one*sysindex,6,6+one*sysindex,21));
}
}
[
category("xpform属性"),
description("获取或设置窗体在托盘区显示的图标.")
]
public icon sys_icon
{
get{return trayicon;}
set{trayicon=value;ni.icon=trayicon;}
}
[
category("xpform属性"),
description("获取或设置窗体在托盘区的上下文菜单.")
]
public contextmenu sys_contextmenu
{
get{return traycontextmenu;}
set{traycontextmenu=value;ni.contextmenu=traycontextmenu;}
}
[
category("xpform属性"),
description("获取或设置当鼠标停留在托盘区图标上时的提示文字.")
]
public string sys_tooltip
{
get{return temptraytooltip;}
set{temptraytooltip=value;traytooltip=temptraytooltip.split("/n".tochararray(),0);ni.text=value;}
}
[
category("xpform属性"),
description("多行编辑控件中的文本行,作为字符串值的数组.(用于设置多行tooltip)")
]
public string[] sys_tooltip_lines
{
get{return traytooltip;}
set
{
string[] a=value;
//traytooltip=value[0];
temptraytooltip="";
for(int i=0;i<=a.length-1;i++)
{
temptraytooltip=temptraytooltip+a.getvalue(i).tostring();
if(i<a.length-1)
{
temptraytooltip=temptraytooltip+"/n";
}
}
ni.text=temptraytooltip;
traytooltip=value;
}
}
[
category("xpform属性"),
description("获取或设置窗体刚启动时是否在托盘区显示.(需要先设置sys_icon属性.)")
]
public bool showintray
{
get{return isshowintray;}
set
{
isshowintray=value;
if(!this.designmode)
{
ni.icon=trayicon;
ni.contextmenu=traycontextmenu;
ni.text=temptraytooltip;
ni.visible=value;
}
}
}
[
category("xpform属性"),
description("窗体图标的宽.)")
]
public int formicon_width
{
get{return formiconwidth;}
set
{
if(value>=formiconwidth)
{
formiconwidth=value;
this.invalidate(new rectangle(8,6,value,formiconheight));
}
else
{
int temp=formiconwidth;
formiconwidth=value;
this.invalidate(new rectangle(8,6,value,temp));
}
}
}
[
category("xpform属性"),
description("窗体图标的高.)")
]
public int formicon_height
{
get{return formiconheight;}
set
{
if(value>=formiconheight)
{
formiconheight=value;
this.invalidate(new rectangle(8,6,formiconwidth,value));
}
else
{
int temp=formiconheight;
formiconheight=value;
this.invalidate(new rectangle(8,6,temp,value));
}
}
}
//####################################################################
#endregion
}

 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值