读者你好:
下面有一段关于控件翻转的代码,可以参考一下:
using system;
using system.collections;
using system.componentmodel;
using system.drawing;
using system.data;
using system.windows.forms;
namespace imagezoomer
{
/// <summary>
///
/// </summary>
//枚举类型定义,定义图象的四种翻转方式
public enum flipmodestyle
{
noflip=0,//不翻转
flipx=1,//水平翻转
flipy=2,//垂直翻转
flipxy=3//水平垂直翻转
}
//事件数据类定义,报告图象的显示尺寸
public class displaysizechangedeventargs:system.eventargs
{
public int width;
public int height;
public displaysizechangedeventargs()
{
}
}
//事件代表的声明
public delegate void displaysizechangedeventhandler(object sender,displaysizechangedeventargs e);
//用户自定义控件类
public class imagezoomercontrol : system.windows.forms.control
{
private int width;//控件宽度
private int height;//控件高度
private system.drawing.bitmap bitmap;//控件上的图象
private flipmodestyle flip;//图象的翻转方式
private event displaysizechangedeventhandler eventhandler;//事件
//构造方法,初始化数据成员
public imagezoomercontrol()
{
width=this.width;
height=this.height;
bitmap=null;
eventhandler=null;
}
//宽度属性
[
category("imagezoomer"),
description("the displayed image width.")
]
public int displaywidth
{
get
{
return width;
}
set
{
if(value>=0)
{
width=value;
invalidate(this.clientrectangle);
}
}
}
//高度属性
[
category("imagezoomer"),
description("the displayed image height.")
]
public int displayheight
{
get
{
return height;
}
set
{
if(value>=0)
{
height=value;
this.invalidate(this.clientrectangle);
}
}
}
//图象属性
[
category("imagezoomer"),
description("the image displayed by this control."),
defaultvalue(null)
]
public bitmap displayimage
{
get
{
return bitmap;
}
set
{
bitmap=value;
if(bitmap!=null)
{
width=bitmap.width;
height=bitmap.height;
}
else
{
width=this.width;
height=this.height;
}
this.invalidate(this.clientrectangle);
}
}
//翻转方式属性
[
category("imagezoomer"),
description("specify how the image will be flipped.")
]
public flipmodestyle flipmode
{
get
{
return flip;
}
set
{
flip=value;
this.invalidate(this.clientrectangle);
}
}
//事件属性
[
category("imagezoomer"),
description("occurs when the image size is changed.")
]
public event displaysizechangedeventhandler displaysizechanged
{
add
{
eventhandler+=value;
}
remove
{
eventhandler-=value;
}
}
protected override void onpaint(painteventargs pe)
{
if(bitmap==null)
{
return;
}
graphics g=pe.graphics;
//转换距阵
system.drawing.drawing2d.matrix transform;//距阵
if(flip.equals(flipmodestyle.flipx))
{
transform=new system.drawing.drawing2d.matrix(-1,0,0,1,width,0);
}
else if(flip.equals(flipmodestyle.flipy))
{
transform=new system.drawing.drawing2d.matrix(1,0,0,-1,0,height);
}
else if(flip.equals(flipmodestyle.flipxy))
{
transform=new system.drawing.drawing2d.matrix(-1,0,0,-1,width,height);
}
else
{
transform=new system.drawing.drawing2d.matrix(1,0,0,1,0,0);
}
//设置转换距阵
g.transform=transform;
g.drawimage(bitmap,new system.drawing.rectangle(0,0,width,height),
0,0,bitmap.width,bitmap.height,
graphicsunit.pixel);
//恢复绘图平面
g.resettransform();
}
//发出事件的方法
protected virtual void ondisplaysizechanged(displaysizechangedeventargs e)
{
//调用事件对象指向的方法
this.invoke(eventhandler,new object[]
{
this,e
}
);
}
//重载方法,调用ondisplaysizechanged()发出事件
protected override void onmousedown(mouseeventargs e)
{
width=e.x;
height=e.y;
this.invalidate(this.clientrectangle);
displaysizechangedeventargs eventdata=new displaysizechangedeventargs();
eventdata.width=width;
eventdata.height=height;
this.ondisplaysizechanged(eventdata);
base.onmousedown(e);
}
}
}