×

再谈不规则窗体制作

Kalet Kalet 发表于2009-03-20 12:00:14 浏览166 评论0

抢沙发发表评论

因为要给师弟们讲课,我写了《异型窗体制作(两种方法) 》
http://www.chenjiliang.com/Article/View.aspx?ArticleID=374&TypeID=79
这篇博客。后来有个师弟说他把我的代码修改成了一个组件,想把这个组件拖拽到窗体里,经过简单设置就能实现不规则窗体,但是没有成功。经过实验,我这里重新把原来的代码整理了一下,下面这个组件CusomForm编译后就可以使用,拖拽到窗体后需要设置ParentForm、BackImage、TranTransparentColor三个属性,并且不用再写代码处理没有标题栏的窗体无法移动这个问题。 再谈不规则窗体制作
代码如下:


复制  保存

using System;
using System.ComponentModel;
using System.Drawing;
using System.Drawing.Design;
using System.Drawing.Drawing2D;
using System.Windows.Forms;

namespace Spider.Common
{
/// <summary>
/// CustomForm 的摘要说明。
/// </summary>
[DefaultProperty("BackImage")]
public class CustomForm : Component
{
private Form parentForm;
private Bitmap BmpBack;
private Color transparentColor = Color.Empty;
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null;

public CustomForm(System.ComponentModel.IContainer container)
{
///
/// Windows.Forms 类撰写设计器支持所必需的
///
container.Add(this);
InitializeComponent();
}

public CustomForm()
{
///
/// Windows.Forms 类撰写设计器支持所必需的
///
InitializeComponent();

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

/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose(bool disposing)
{
if (disposing)
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose(disposing);
}

#region 组件设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
components = new System.ComponentModel.Container();
}
#endregion

/// <summary>
/// 设置或读取不规则窗体
/// </summary>
[DefaultValue(null), Category("外观")]
public Form ParentForm
{
get { return parentForm; }
set
{
parentForm = value;
if (parentForm != null)
{
if (BmpBack != null)
{
SetWindowRegion();
}
parentForm.MouseDown += new MouseEventHandler(parentForm_MouseDown);
parentForm.MouseMove += new MouseEventHandler(parentForm_MouseMove);
parentForm.MouseUp += new MouseEventHandler(parentForm_MouseUp);
}
}
}

/// <summary>
/// 设置或读取背景图片,依据该图片生成不规则窗体
/// </summary>
[DefaultValue(null), Category("外观"),
***Properties(***Properties.Repaint)]
public Image BackImage
{
get { return BmpBack; }
set再谈不规则窗体制作
{
if (value != null)
{
BmpBack = new Bitmap(value);
if (parentForm != null)
{
SetWindowRegion();
}
}
else
{
BmpBack = null;
parentForm.BackgroundImage = null;
parentForm.Region = new Region(new Rectangle(0, 0,
parentForm.Width, parentForm.Height));
}
}
}

/// <summary>
/// 设置或读取背景图片的透明色,背景图片非透明色区域构成不规则窗体
/// </summary>
[DefaultValue(typeof(Color), "Color.Empty"),
Category("外观"),]
public Color TransparentColor
{
get
{
return transparentColor;
}
set
{
transparentColor = value;
if (BmpBack != null && parentForm != null)
{
SetWindowRegion(transparentColor);
}
}
}

private void SetWindowRegion()
{
TransparentColor = BmpBack.GetPixel(0, 0);
SetWindowRegion(transparentColor);
}

private void SetWindowRegion(Color transparentColor)
{
Color TempColor;
GraphicsPath gp;

gp = new GraphicsPath();
parentForm.FormBorderStyle = FormBorderStyle.None;
parentForm.Width = BmpBack.Width;
parentForm.Height = BmpBack.Height;
parentForm.BackgroundImage = BmpBack;

for (int nX = 0; nX < BmpBack.Width; nX++)
{
for (int nY = 0; nY < BmpBack.Height; nY++)
{
TempColor = BmpBack.GetPixel(nX, nY);
if (TempColor != transparentColor)
{
gp.AddRectangle(new Rectangle(nX, nY, 1, 1));
}
}
}

parentForm.Region = new Region(gp);
}

#region 鼠标事件操作

private bool IsMouseDown;
private Point MouseOffset;

private void parentForm_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
IsMouseDown = true;
MouseOffset = new Point(e.X, e.Y);
}
}

private void parentForm_MouseMove(object sender, MouseEventArgs e)
{
if (IsMouseDown && e.Button == MouseButtons.Left)
{
parentForm.Location = new Point(parentForm.Left + e.X - MouseOffset.X,
parentForm.Top + e.Y - MouseOffset.Y);
}
}

private void parentForm_MouseUp(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
IsMouseDown = false;
}
}

#endregion
}再谈不规则窗体制作
}


群贤毕至

访客