C#制作自定义窗体样式

  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

★搜★(),为专业技术文档网站。

包括开发技术文档·C#开发技术文档·Access/SQL Server数据库开发技术文档· 开发技术文档。

还包括·项目实战经验总结·开发经验技巧总结·项目开发心得。

C#制作自定义窗体样式

作不规则窗体涉及到API的调用和大量的编程,是件很复杂的事情。下面我们可以使用Borland C# Builder轻松的实现一个不规则窗体,以下面用一个示例来讲述其制作过程。

一.准备不规则窗体位图

二.窗体的设置

三.代码的完成

一.准备不规则窗体位图

为了方便起见,我们随便找几个别的软件用的Skin。

这里使用金山影霸2003的安装目录下的skins\ocean\KingDVD_Disable.BMP

当然完全可以使用画图工具,制作一个有形状的位图,背景使用一种特别的颜色,如白色。这个颜色会在后面用得上。

[ 相关贴图]

二.窗体的设置

1.新建C# Application

[ 相关贴图]

2.选中新建的窗体,设置其相应属性:

(1).将FormBorderStyle 属性设置为None。

(2).将窗体的BackgroundImage 属性设置为先前面的位图文件。

(3).将TransparencyKey 属性设置为位图文件的背景色,本例中为白色。(此属性告诉应用程序窗体中的哪些部分需要设置为透明。)

(4).加一个picturebox1,就是一关闭位图,点击时关闭应用程序。

(5).加一个contextMenu1,添加一菜单项“退出”,将winform的contextMenu设为contextMenu1。

按F9运行你的程序,就可以看到你的不规则窗体了。

[ 相关贴图]

三.实现代码

前面做的窗口还不能移动。加入下面的代码使其能移动起来,帖出完整的示例代码:

using System;

using System.Drawing;

using System.Collections;

using ponentModel;

using System.Windows.Forms;

using System.Data;

namespace SForm

{

///

/// Summary description for WinForm.

///

public class WinForm : System.Windows.Forms.Form

{

///

/// Required designer variable.

///

private ponentModel.Container components = null;

private System.Windows.Forms.ContextMenu contextMenu1;

private System.Windows.Forms.MenuItem menuItem1;

private Point mouseOffset; //记录鼠标指针的坐标

private bool isMouseDown = false;

private System.Windows.Forms.PictureBox pictureBox1; //记录鼠标按键是否按下

public WinForm()

{

//

// Required for Windows Form Designer support

//

InitializeComponent();

//

// TODO: Add any constructor code after InitializeComponent call

//

}

///

/// Clean up any resources being used.

///

protected override void Dispose (bool disposing)

{

if (disposing)

{

if (components != null)

{

components.Dispose();

}

}

base.Dispose(disposing);

}

#region Windows Form Designer generated code

///

/// Required method for Designer support - do not modify

/// the contents of this method with the code editor.

///

private void InitializeComponent()

{

System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(WinForm));

this.contextMenu1 = new System.Windows.Forms.ContextMenu();

this.menuItem1 = new System.Windows.Forms.MenuItem();

this.pictureBox1 = new System.Windows.Forms.PictureBox();

this.SuspendLayout();

//

// contextMenu1

//

this.contextMenu1.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {

this.menuItem1});

//

// menuItem1

//

this.menuItem1.Index = 0;

this.menuItem1.Text = "退出";

this.menuItem1.Click += new System.EventHandler(this.menuItem1_Click);

//

// pictureBox1

//

相关文档
最新文档