当前位置:
首页 > 编程开发 > Objective-C编程 >
-
利用c# Builder制作不规则窗体
制作者:剑锋冷月 单位:无忧统计网,www.51stat.net
作不规则窗体涉及到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 System.ComponentModel;
using System.Windows.Forms;
using System.Data;
namespace SForm
{
/// <summary>
/// Summary description for WinForm.
/// </summary>
public class WinForm : System.Windows.Forms.Form
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.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
//
}
/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose (bool disposing)
{
if (disposing)
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
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
//
this.pictureBox1.Image = ((System.Drawing.Image)(resources.GetObject("pictureBox1.Image")));
this.pictureBox1.Location = new System.Drawing.Point(290, 8);
this.pictureBox1.Name = "pictureBox1";
this.pictureBox1.Size = new System.Drawing.Size(14, 13);
this.pictureBox1.SizeMode = System.Windows.Forms.PictureBoxSizeMode.AutoSize;
this.pictureBox1.TabIndex = 0;
this.pictureBox1.TabStop = false;
this.pictureBox1.Click += new System.EventHandler(this.pictureBox1_Click);
//
// WinForm
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.BackgroundImage = ((System.Drawing.Image)(resources.GetObject("$this.BackgroundImage")));
this.ClientSize = new System.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 += new System.Windows.Forms.MouseEventHandler(this.WinForm_MouseDown);
this.MouseUp += new System.Windows.Forms.MouseEventHandler(this.WinForm_MouseUp);
this.MouseMove += new System.Windows.Forms.MouseEventHandler(this.WinForm_MouseMove);
this.ResumeLayout(false);
}
#endregion
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new WinForm());
}
private void menuItem1_Click(object sender, System.EventArgs e)
{
this.Close();
}
private void WinForm_MouseMove(object sender, System.Windows.Forms.MouseEventArgs e)
{
if (isMouseDown)
{
Point mousePos = Control.MousePosition;
mousePos.Offset(mouseOffset.X, mouseOffset.Y);
Location = mousePos;
}
}
private void WinForm_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 WinForm_MouseUp(object sender, System.Windows.Forms.MouseEventArgs e)
{
// 修改鼠标状态isMouseDown的值
// 确保只有鼠标左键按下并移动时,才移动窗体
if (e.Button == MouseButtons.Left)
{
isMouseDown = false;
}
}
private void pictureBox1_Click(object sender, System.EventArgs e)
{
this.Close();
}
}
}
注:如果监视器的颜色深度设置大于 24 位,TransparencyKey 设置成白色,窗体的非透明部分还是会显示出来,不知道为什么。
前面做的窗口还不能移动。加入下面的代码使其能移动起来,帖出完整的示例代码:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
namespace SForm
{
/// <summary>
/// Summary description for WinForm.
/// </summary>
public class WinForm : System.Windows.Forms.Form
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.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
//
}
/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose (bool disposing)
{
if (disposing)
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
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
//
this.pictureBox1.Image = ((System.Drawing.Image)(resources.GetObject("pictureBox1.Image")));
this.pictureBox1.Location = new System.Drawing.Point(290, 8);
this.pictureBox1.Name = "pictureBox1";
this.pictureBox1.Size = new System.Drawing.Size(14, 13);
this.pictureBox1.SizeMode = System.Windows.Forms.PictureBoxSizeMode.AutoSize;
this.pictureBox1.TabIndex = 0;
this.pictureBox1.TabStop = false;
this.pictureBox1.Click += new System.EventHandler(this.pictureBox1_Click);
//
// WinForm
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.BackgroundImage = ((System.Drawing.Image)(resources.GetObject("$this.BackgroundImage")));
this.ClientSize = new System.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 += new System.Windows.Forms.MouseEventHandler(this.WinForm_MouseDown);
this.MouseUp += new System.Windows.Forms.MouseEventHandler(this.WinForm_MouseUp);
this.MouseMove += new System.Windows.Forms.MouseEventHandler(this.WinForm_MouseMove);
this.ResumeLayout(false);
}
#endregion
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new WinForm());
}
private void menuItem1_Click(object sender, System.EventArgs e)
{
this.Close();
}
private void WinForm_MouseMove(object sender, System.Windows.Forms.MouseEventArgs e)
{
if (isMouseDown)
{
Point mousePos = Control.MousePosition;
mousePos.Offset(mouseOffset.X, mouseOffset.Y);
Location = mousePos;
}
}
private void WinForm_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 WinForm_MouseUp(object sender, System.Windows.Forms.MouseEventArgs e)
{
// 修改鼠标状态isMouseDown的值
// 确保只有鼠标左键按下并移动时,才移动窗体
if (e.Button == MouseButtons.Left)
{
isMouseDown = false;
}
}
private void pictureBox1_Click(object sender, System.EventArgs e)
{
this.Close();
}
}
}
注:如果监视器的颜色深度设置大于 24 位,TransparencyKey 设置成白色,窗体的非透明部分还是会显示出来,不知道为什么。
作不规则窗体涉及到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 System.ComponentModel;
using System.Windows.Forms;
using System.Data;
namespace SForm
{
/// <summary>
/// Summary description for WinForm.
/// </summary>
public class WinForm : System.Windows.Forms.Form
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.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
//
}
/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose (bool disposing)
{
if (disposing)
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
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
//
this.pictureBox1.Image = ((System.Drawing.Image)(resources.GetObject("pictureBox1.Image")));
this.pictureBox1.Location = new System.Drawing.Point(290, 8);
this.pictureBox1.Name = "pictureBox1";
this.pictureBox1.Size = new System.Drawing.Size(14, 13);
this.pictureBox1.SizeMode = System.Windows.Forms.PictureBoxSizeMode.AutoSize;
this.pictureBox1.TabIndex = 0;
this.pictureBox1.TabStop = false;
this.pictureBox1.Click += new System.EventHandler(this.pictureBox1_Click);
//
// WinForm
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.BackgroundImage = ((System.Drawing.Image)(resources.GetObject("$this.BackgroundImage")));
this.ClientSize = new System.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 += new System.Windows.Forms.MouseEventHandler(this.WinForm_MouseDown);
this.MouseUp += new System.Windows.Forms.MouseEventHandler(this.WinForm_MouseUp);
this.MouseMove += new System.Windows.Forms.MouseEventHandler(this.WinForm_MouseMove);
this.ResumeLayout(false);
}
#endregion
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new WinForm());
}
private void menuItem1_Click(object sender, System.EventArgs e)
{
this.Close();
}
private void WinForm_MouseMove(object sender, System.Windows.Forms.MouseEventArgs e)
{
if (isMouseDown)
{
Point mousePos = Control.MousePosition;
mousePos.Offset(mouseOffset.X, mouseOffset.Y);
Location = mousePos;
}
}
private void WinForm_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 WinForm_MouseUp(object sender, System.Windows.Forms.MouseEventArgs e)
{
// 修改鼠标状态isMouseDown的值
// 确保只有鼠标左键按下并移动时,才移动窗体
if (e.Button == MouseButtons.Left)
{
isMouseDown = false;
}
}
private void pictureBox1_Click(object sender, System.EventArgs e)
{
this.Close();
}
}
}
注:如果监视器的颜色深度设置大于 24 位,TransparencyKey 设置成白色,窗体的非透明部分还是会显示出来,不知道为什么。
前面做的窗口还不能移动。加入下面的代码使其能移动起来,帖出完整的示例代码:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
namespace SForm
{
/// <summary>
/// Summary description for WinForm.
/// </summary>
public class WinForm : System.Windows.Forms.Form
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.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
//
}
/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose (bool disposing)
{
if (disposing)
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
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
//
this.pictureBox1.Image = ((System.Drawing.Image)(resources.GetObject("pictureBox1.Image")));
this.pictureBox1.Location = new System.Drawing.Point(290, 8);
this.pictureBox1.Name = "pictureBox1";
this.pictureBox1.Size = new System.Drawing.Size(14, 13);
this.pictureBox1.SizeMode = System.Windows.Forms.PictureBoxSizeMode.AutoSize;
this.pictureBox1.TabIndex = 0;
this.pictureBox1.TabStop = false;
this.pictureBox1.Click += new System.EventHandler(this.pictureBox1_Click);
//
// WinForm
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.BackgroundImage = ((System.Drawing.Image)(resources.GetObject("$this.BackgroundImage")));
this.ClientSize = new System.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 += new System.Windows.Forms.MouseEventHandler(this.WinForm_MouseDown);
this.MouseUp += new System.Windows.Forms.MouseEventHandler(this.WinForm_MouseUp);
this.MouseMove += new System.Windows.Forms.MouseEventHandler(this.WinForm_MouseMove);
this.ResumeLayout(false);
}
#endregion
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new WinForm());
}
private void menuItem1_Click(object sender, System.EventArgs e)
{
this.Close();
}
private void WinForm_MouseMove(object sender, System.Windows.Forms.MouseEventArgs e)
{
if (isMouseDown)
{
Point mousePos = Control.MousePosition;
mousePos.Offset(mouseOffset.X, mouseOffset.Y);
Location = mousePos;
}
}
private void WinForm_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 WinForm_MouseUp(object sender, System.Windows.Forms.MouseEventArgs e)
{
// 修改鼠标状态isMouseDown的值
// 确保只有鼠标左键按下并移动时,才移动窗体
if (e.Button == MouseButtons.Left)
{
isMouseDown = false;
}
}
private void pictureBox1_Click(object sender, System.EventArgs e)
{
this.Close();
}
}
}
注:如果监视器的颜色深度设置大于 24 位,TransparencyKey 设置成白色,窗体的非透明部分还是会显示出来,不知道为什么。
最新更新
nodejs爬虫
Python正则表达式完全指南
爬取豆瓣Top250图书数据
shp 地图文件批量添加字段
爬虫小试牛刀(爬取学校通知公告)
【python基础】函数-初识函数
【python基础】函数-返回值
HTTP请求:requests模块基础使用必知必会
Python初学者友好丨详解参数传递类型
如何有效管理爬虫流量?
2个场景实例讲解GaussDB(DWS)基表统计信息估
常用的 SQL Server 关键字及其含义
动手分析SQL Server中的事务中使用的锁
openGauss内核分析:SQL by pass & 经典执行
一招教你如何高效批量导入与更新数据
天天写SQL,这些神奇的特性你知道吗?
openGauss内核分析:执行计划生成
[IM002]Navicat ODBC驱动器管理器 未发现数据
初入Sql Server 之 存储过程的简单使用
SQL Server -- 解决存储过程传入参数作为s
关于JS定时器的整理
JS中使用Promise.all控制所有的异步请求都完
js中字符串的方法
import-local执行流程与node模块路径解析流程
检测数据类型的四种方法
js中数组的方法,32种方法
前端操作方法
数据类型
window.localStorage.setItem 和 localStorage.setIte
如何完美解决前端数字计算精度丢失与数