一、在说我用的方法前,我不得不说一下另一种方法,这种方法在实现不规则窗体自身显示效果(即除开窗体的移动、最大最小话、关闭等)时是不用编代码的。非常简便,但它的致命缺点就是要要求程序运行环境在24位色以下,否则不规则窗体的透明部分就会显示出来,窗体会非常难看。
方法1:
步骤1:先用图象处理软件制作您的不规则窗体的位图BMP(最好是位图,其它的我没有试过:))。制作时请注意将背景色(即需要设置成透明的颜色部分)设置成与非背景图片颜色反差较大的颜色,并且使用一种容易记忆的颜色。如下图:
图中黄颜色背景将要设置成透明部分
步骤2:新建windows应用程序。创建windows窗体并设置窗体基本属性。
(1)将 FormBorderStyle 属性设置为 None。
(2)将窗体的 BackgroundImage 属性设置为先前创建的位图文件。不必将文件添加到项目系统中;这将在指定该文件作为背景图像时自动完成。
(3)将 TransparencyKey 属性设置为位图文件的背景色,本例中为黄色。(此属性告诉应用程序窗体中的哪些部分需要设置为透明。 )
上面两个步骤已经完成了不规则窗体自身显示效果的制作,此刻您要做的就是为窗体添加移动、关闭、最大最小化的事件。这个将在方法2中详细介绍。
方法1在24位色以下的环境中可以显示正常,但在24位色以上时黄色背景不能消失,所以方法1不能胜任24位色以上环境。
为了解决这个问题,我们可以用到方法2。
方法2
步骤1:同方法1,先用图象处理软件制作您的不规则窗体的位图BMP
步骤2:创建windows应用程序。创建windows窗体。
由于方法2是调用类来实现制作不规则窗体,所以您只需要在窗体的LOAD事件中加入以下代码:
private void login_Load(object sender, System.EventArgs e)
{
BitmapRegion BitmapRegion = new BitmapRegion(); BitmapRegion.CreateControlRegion(this, new Bitmap("HMlogin.bmp"));
}
其中"HMlogin.bmp"为您制作的位图。
下面就是文件BitmapRegion.cs 我在网上找到的是英文的,自己翻译了一下,英语水平有限,有错的地方还请大家指出。
using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Windows.Forms;
namespace MsgClassLibrary
{
public class BitmapRegion
{
public BitmapRegion()
{ }
public static void CreateControlRegion(Control control, Bitmap bitmap)
{
if (control == null || bitmap == null)
return;
control.Width = bitmap.Width;
control.Height = bitmap.Height;
if (control is System.Windows.Forms.Form)
{
Form form = (Form) control;
form.Width = control.Width;
form.Height = control.Height;
form.FormBorderStyle = FormBorderStyle.None;
form.BackgroundImage = bitmap;
GraphicsPath graphicsPath = CalculateControlGraphicsPath(bitmap);
form.Region = new Region(graphicsPath);
}
else if (control is System.Windows.Forms.Button)
{
Button button = (Button) control;
button.Text = "";
button.Cursor = Cursors.Hand;
button.BackgroundImage = bitmap;
GraphicsPath graphicsPath = CalculateControlGraphicsPath(bitmap);
button.Region = new Region(graphicsPath);
}
}
private static GraphicsPath CalculateControlGraphicsPath(Bitmap bitmap)
{
GraphicsPath graphicsPath = new GraphicsPath();
Color colorTransparent = bitmap.GetPixel(0, 0);
int colOpaquePixel = 0;
for (int row = 0; row < bitmap.Height; row++)
{
colOpaquePixel = 0;
for (int col = 0; col < bitmap.Width; col++)
{
if (bitmap.GetPixel(col, row) != colorTransparent)
{
colOpaquePixel = col;
int colNext = col;
for (colNext = colOpaquePixel; colNext < bitmap.Width; colNext++)
if (bitmap.GetPixel(colNext, row) == colorTransparent)
break;
graphicsPath.AddRectangle(new Rectangle(colOpaquePixel, row, colNext - colOpaquePixel, 1));
col = colNext;
}
}
}
return graphicsPath;
}
}
}
完成窗口自身效果后此刻您要做的就是为窗体添加移动、关闭、最大最小化的事件代码了。
1、首先,关闭很简单,只需要在您的事件中加入
this.Close();Application.Exit();
2、最大最小化事件也很简单
this.WindowState=FormWindowState.Minimized;this.WindowState=FormWindowState.Maximized;
3、移动相对比较麻烦
你先需要建立两个全局变量:
private Point mouseOffset; private bool isMouseDown = false;
然后为您的事件加入相应的代码:
private void form1_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
{
int xOffset;
int yOffset;
if (e.Button == MouseButtons.Left)
{
xOffset = -e.X - SystemInformation.FrameBorderSize.Width;
yOffset = -e.Y - SystemInformation.CaptionHeight -
SystemInformation.FrameBorderSize.Height;
mouseOffset = new Point(xOffset, yOffset);
isMouseDown = true;
}
}
private void form1_MouseMove(object sender, System.Windows.Forms.MouseEventArgs e)
{
if (isMouseDown)
{
Point mousePos = Control.MousePosition;
mousePos.Offset(mouseOffset.X, mouseOffset.Y);
Location = mousePos;
}
}
private void form1_MouseUp(object sender, System.Windows.Forms.MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
isMouseDown = false;
}
}
程序的执行情况如下图:
总结:
我在了解到方法2前也是一直用的方法1,在24位色下不能正常显示的问题我也头疼了很久,当时在MSDN上找的文章居然都说是------------“请确保您的用户是在24位色以下环境中使用本程序”!!!晕死了,这个怎么行?!现在有了方法2就好了,自己解决了问题不敢独享,那出来给大家看看。
另外,方法2中的类也支持制作不规则的button控件,有兴趣的朋友可以试试。
我将此文发到首页合乎博客园规范吗?如果不,请斑竹提醒,我想这篇文章对大家还是有用的。