以下是我在网上找到的一段代码,但是有些地方不是很清楚,因此请各位弟兄多多帮忙?
1,public class CustomForm : Component中,Component从哪里来的,
2,我程序运行后什么东西都没有?提示添加Component,
3,代码中有public CustomForm()还有 private void parentForm_MouseUp(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
IsMouseDown = false;
}
}
他们是不是两个不同的窗体,如果是该怎么做,也就是思路是什么?
4,BackImage是窗体的背景图片还是窗体里控件的背景图片?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("外观"),
RefreshProperties(RefreshProperties.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
}
}各位弟兄,帮帮忙, 这个问题困惑了好久,谢谢!
1,public class CustomForm : Component中,Component从哪里来的,
2,我程序运行后什么东西都没有?提示添加Component,
3,代码中有public CustomForm()还有 private void parentForm_MouseUp(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
IsMouseDown = false;
}
}
他们是不是两个不同的窗体,如果是该怎么做,也就是思路是什么?
4,BackImage是窗体的背景图片还是窗体里控件的背景图片?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("外观"),
RefreshProperties(RefreshProperties.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
}
}各位弟兄,帮帮忙, 这个问题困惑了好久,谢谢!
http://msdn.microsoft.com/zh-cn/library/system.componentmodel.component(VS.80).aspx
3:BackImage是窗体背景,作为类的特性存在的。
2.这是一个组件库,而不是一个Winform的程序。
3.ParentForm是CustomForm的一个Property,用来设置CustomForm所定义的窗体,CustomForm将对此窗体的外观进行修改,使之成为BackImage中图像的形状。parentForm_MouseUp用来处理ParentForm的鼠标事件,因为异形窗体没有标题栏,不能通过标题栏进行拖动,这些事件可以实现通过点击窗体内部进行拖动。
4.BackImage定义了异形窗体ParentForm的形状。