怎么样使用Borland C# Builder制作不规则窗体?
怎么样使用Borland C# Builder制作不规则窗体?
徐长友悠游在线作不规则窗体涉及到API的调用和大量的编程,是件很复杂的事情。下面我们可以使用BorlandC#Builder轻松的实现一个不规则窗体,以下面用一个示例来讲述其制作过程。
一.准备不规则窗体位图
二.窗体的设置
三.代码的完成
一.准备不规则窗体位图
为了方便起见,我们随便找几个别的软件用的Skin。
这里使用金山影霸2003的安装目录下的skins/ocean/KingDVD_Disable.BMP
当然完全可以使用画图工具,制作一个有形状的位图,背景使用一种特别的颜色,如白色。这个颜色会在后面用得上。
[ 相关贴图 ]
screen.width-430)this.width=screen.width-430" align=center border=0>
二.窗体的设置
1.新建C#Application
[ 相关贴图 ]
screen.width-430)this.width=screen.width-430" align=center border=0>
2.选中新建的窗体,设置其相应属性:
(1).将FormBorderStyle属性设置为None。
(2).将窗体的BackgroundImage属性设置为先前面的位图文件。
(3).将TransparencyKey属性设置为位图文件的背景色,本例中为白色。(此属性告诉应用程序窗体中的哪些部分需要设置为透明。)
(4).加一个picturebox1,就是一关闭位图,点击时关闭应用程序。
(5).加一个contextMenu1,添加一菜单项“退出”,将winform的contextMenu设为contextMenu1。
按F9运行你的程序,就可以看到你的不规则窗体了。
[ 相关贴图 ]
screen.width-430)this.width=screen.width-430" align=center border=0>
三.代码的完成
前面做的窗口还不能移动。加入下面的代码使其能移动起来,帖出完整的示例代码:
usingSystem;
usingSystem.Drawing;
usingSystem.Collections;
usingSystem.ComponentModel;
usingSystem.Windows.Forms;
usingSystem.Data;
namespaceSForm
{
///<summary>
///SummarydescriptionforWinForm.
///</summary>
publicclassWinForm:System.Windows.Forms.Form
{
///<summary>
///Requireddesignervariable.
///</summary>
privateSystem.ComponentModel.Containercomponents=null;
privateSystem.Windows.Forms.ContextMenucontextMenu1;
privateSystem.Windows.Forms.MenuItemmenuItem1;
privatePointmouseOffset;//记录鼠标指针的坐标
privateboolisMouseDown=false;
privateSystem.Windows.Forms.PictureBoxpictureBox1;//记录鼠标按键是否按下
publicWinForm()
{
//
//RequiredforWindowsFormDesignersupport
//
InitializeComponent();
//
//TODO:AddanyconstructorcodeafterInitializeComponentcall
//
}
///<summary>
///Cleanupanyresourcesbeingused.
///</summary>
protectedoverridevoidDispose(booldisposing)
{
if(disposing)
{
if(components!=null)
{
components.Dispose();
}
}
base.Dispose(disposing);
}
#regionWindowsFormDesignergeneratedcode
///<summary>
///RequiredmethodforDesignersupport-donotmodify
///thecontentsofthismethodwiththecodeeditor.
///</summary>
privatevoidInitializeComponent()
{
System.Resources.ResourceManagerresources=newSystem.Resources.ResourceManager(typeof(WinForm));
this.contextMenu1=newSystem.Windows.Forms.ContextMenu();
this.menuItem1=newSystem.Windows.Forms.MenuItem();
this.pictureBox1=newSystem.Windows.Forms.PictureBox();
this.SuspendLayout();
//
//contextMenu1
//
this.contextMenu1.MenuItems.AddRange(newSystem.Windows.Forms.MenuItem[]{
this.menuItem1});
//
//menuItem1
//
this.menuItem1.Index=0;
this.menuItem1.Text="退出";
this.menuItem1.Click+=newSystem.EventHandler(this.menuItem1_Click);
//
//pictureBox1
//
this.pictureBox1.Image=((System.Drawing.Image)(resources.GetObject("pictureBox1.Image")));
this.pictureBox1.Location=newSystem.Drawing.Point(290,8);
this.pictureBox1.Name="pictureBox1";
this.pictureBox1.Size=newSystem.Drawing.Size(14,13);
this.pictureBox1.SizeMode=System.Windows.Forms.PictureBoxSizeMode.AutoSize;
this.pictureBox1.TabIndex=0;
this.pictureBox1.TabStop=false;
this.pictureBox1.Click+=newSystem.EventHandler(this.pictureBox1_Click);
//
//WinForm
//
this.AutoScaleBaseSize=newSystem.Drawing.Size(6,14);
this.BackgroundImage=((System.Drawing.Image)(resources.GetObject("$this.BackgroundImage")));
this.ClientSize=newSystem.Drawing.Size(365,235);
this.ContextMenu=this.contextMenu1;
this.Controls.Add(this.pictureBox1);
this.FormBorderStyle=System.Windows.Forms.FormBorderStyle.None;
this.MaximizeBox=false;
this.MinimizeBox=false;
this.Name="WinForm";
this.Text="WinForm";
this.TransparencyKey=System.Drawing.Color.White;
this.MouseDown+=newSystem.Windows.Forms.MouseEventHandler(this.WinForm_MouseDown);
this.MouseUp+=newSystem.Windows.Forms.MouseEventHandler(this.WinForm_MouseUp);
this.MouseMove+=newSystem.Windows.Forms.MouseEventHandler(this.WinForm_MouseMove);
this.ResumeLayout(false);
}
#endregion
///<summary>
///Themainentrypointfortheapplication.
///</summary>
[STAThread]
staticvoidMain()
{
Application.Run(newWinForm());
}
privatevoidmenuItem1_Click(objectsender,System.EventArgse)
{
this.Close();
}
privatevoidWinForm_MouseMove(objectsender,System.Windows.Forms.MouseEventArgse)
{
if(isMouseDown)
{
PointmousePos=Control.MousePosition;
mousePos.Offset(mouseOffset.X,mouseOffset.Y);
Location=mousePos;
}
}
privatevoidWinForm_MouseDown(objectsender,System.Windows.Forms.MouseEventArgse)
{
intxOffset;
intyOffset;
if(e.Button==MouseButtons.Left)
{
xOffset=-e.X-SystemInformation.FrameBorderSize.Width;
yOffset=-e.Y-SystemInformation.CaptionHeight-
SystemInformation.FrameBorderSize.Height;
mouseOffset=newPoint(xOffset,yOffset);
isMouseDown=true;
}
}
privatevoidWinForm_MouseUp(objectsender,System.Windows.Forms.MouseEventArgse)
{
//修改鼠标状态isMouseDown的值
//确保只有鼠标左键按下并移动时,才移动窗体
if(e.Button==MouseButtons.Left)
{
isMouseDown=false;
}
}
privatevoidpictureBox1_Click(objectsender,System.EventArgse)
{
this.Close();
}
}
}
注:如果监视器的颜色深度设置大于24位,TransparencyKey设置成白色,窗体的非透明部分还是会显示出来,不知道为什么。
有兴趣的朋友交流一下,主页:http://yousoft.hi.com.cn