仿Office的程序载入窗体
初次接触启动界面记不清是在哪一年了,估计是小学四年级第一次打开Office Word的时候吧,更记不清楚当时的启动界面是长啥样了。后来随着使用的软件越来越多,也见到各式各样的启动界面。下面就列举了两个平常本人平常最常见的窗体,其实windows系统在启动的过程中,有Window字样并且有动画效果的那个节面也算是一个启动界面。


其目的很明显,就是程序启动之后,由于加载主界面的时间过长而导致用户体验不佳,于是往往在显示主界面之前多显示一个不带windows窗体元素的窗体,来显示应用程序加载的进度或者直接是一个静态的视图,作用在于就是跟用户反映程序是有响应的并且正在运行当中,从而提高用户体验。下面是我的载入窗

主要是仿照了Office 2013的风格

简约明了。其中窗体的底色,字体,文字颜色可以更改,左上角的制造商,中间的软件名称,左下角的进度信息都可以更改。不过暂时还没有把图标附加到左上角而已。
窗体的设计如下所示

载入窗的制造商,软件名称这些信息通过构造函数传参进行设置,此外默认的构造函数被屏蔽了
private LoadingForm()
{
InitializeComponent();
} public LoadingForm(string logoStr, string appNameStr, string iniMsgStr):this()
{
this.lbLogo.Text = logoStr;
AppName = appNameStr;
this.lbMsg.Text = iniMsgStr;
this.picLoading.Width = this.Width; this.MouseDown+=new MouseEventHandler(LoadingForm_MouseDown);
foreach (Control con in this.Controls)
{
if (con.Equals(this.btnClose)) continue;
con.MouseDown += new MouseEventHandler(LoadingForm_MouseDown);
}
}
对窗体的打开并非用单纯的Show或者ShowDialog,因为在主线程上Show的话,本身主线程要加载时就会阻塞,这样再阻塞的线程上show的话,窗体难以显示。如果用ShowDialog的也不行,虽然它可以让窗体显示出来,但是调用了ShowDialog之后直到窗体关闭了才返回。就是说载入窗关闭了之后才执行载入加载之类的操作,这样显得毫无意义。
这里只是用来了一个异步去ShowDialog。代码如下
public void ShowLoading()
{
Action callback = new Action(delegate()
{
if (!this.IsDisposed)
this.ShowDialog();
});
callback.BeginInvoke(null, null);
}
这里在ShowDialog之前还多作了一个判断,在于关闭窗体的方法时释放了资源,因此在几个地方都要注意,关闭窗体的处理如下
public void CloseLoading()
{
if (!this.IsDisposed && this.IsHandleCreated)
{
this.Invoke((Action)delegate
{
this.Close();
this.Dispose(); });
}
}
由于调用了Invoke,这个需要判断当前窗体是否已经是显示了出来,否则就会因为窗体的句柄不存在而抛出异常。
在窗体上显示的进度信息,只是通过了一个外放的属性实现,其判断的用意跟关闭窗体时的一样。
public string Message {
get { return this.lbMsg.Text; }
set
{
if (!this.IsDisposed&&this.IsHandleCreated)
{
this.Invoke((Action)delegate()
{
this.lbMsg.Text = value;
});
}
}
}
为了窗体能具备可拖拽的效果,还额外加了一下的代码
[DllImport("user32.dll")]
private static extern bool ReleaseCapture();
[DllImport("user32.dll")]
private static extern bool SendMessage(IntPtr hwnd, int wMsg, int wParam, int lParam);
private const int WM_SYSCOMMAND = 0x0112;
private const int SC_MOVE = 0xF010;
private const int HTCAPTION = 0x0002;
protected void LoadingForm_MouseDown(object sender, MouseEventArgs e)
{
ReleaseCapture();
SendMessage(this.Handle, WM_SYSCOMMAND, SC_MOVE + HTCAPTION, );
}
调用的形式如下
LoadingForm frm = new LoadingForm("HopeGi", "猴健工具集", "启动中...");
frm.ShowLoading();
//一系列操作
frm.Message = "加载界面...";
//一系列操作
frm.CloseLoading();
最近出了点状况,电脑很久也没碰了,前进的步伐缓了下来。能写出来的博客也不咋的,各位有什么好的建议和意见尽管提,谢谢!最后附上源码,可是用到的gif图片没附带上来
public partial class LoadingForm : Form
{ [DllImport("user32.dll")]
private static extern bool ReleaseCapture();
[DllImport("user32.dll")]
private static extern bool SendMessage(IntPtr hwnd, int wMsg, int wParam, int lParam);
private const int WM_SYSCOMMAND = 0x0112;
private const int SC_MOVE = 0xF010;
private const int HTCAPTION = 0x0002; private LoadingForm()
{
InitializeComponent();
} public LoadingForm(string logoStr, string appNameStr, string iniMsgStr):this()
{
this.lbLogo.Text = logoStr;
AppName = appNameStr;
this.lbMsg.Text = iniMsgStr;
this.picLoading.Width = this.Width; this.MouseDown+=new MouseEventHandler(LoadingForm_MouseDown);
foreach (Control con in this.Controls)
{
if (con.Equals(this.btnClose)) continue;
con.MouseDown += new MouseEventHandler(LoadingForm_MouseDown);
}
} public string Message {
get { return this.lbMsg.Text; }
set
{
if (!this.IsDisposed&&this.IsHandleCreated)
{
this.Invoke((Action)delegate()
{
this.lbMsg.Text = value;
});
}
}
} public void ShowLoading()
{
Action callback = new Action(delegate()
{
if (!this.IsDisposed)
this.ShowDialog();
});
callback.BeginInvoke(null, null);
} public void CloseLoading()
{
if (!this.IsDisposed && this.IsHandleCreated)
{
this.Invoke((Action)delegate
{
this.Close();
this.Dispose(); });
}
} private string AppName {
get { return this.lbAppName.Text; }
set {
this.lbAppName.Text = value;
this.lbAppName.Location = new Point((this.Width - this.lbAppName.Width) / , this.lbAppName.Location.Y);
}
} private void btnClose_Click(object sender, EventArgs e)
{
this.CloseLoading();
Environment.Exit();
} protected void LoadingForm_MouseDown(object sender, MouseEventArgs e)
{ ReleaseCapture();
SendMessage(this.Handle, WM_SYSCOMMAND, SC_MOVE + HTCAPTION, );
} #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()
{
this.lbLogo = new System.Windows.Forms.Label();
this.lbAppName = new System.Windows.Forms.Label();
this.lbMsg = new System.Windows.Forms.Label();
this.btnClose = new System.Windows.Forms.PictureBox();
this.picLoading = new System.Windows.Forms.PictureBox();
((System.ComponentModel.ISupportInitialize)(this.btnClose)).BeginInit();
((System.ComponentModel.ISupportInitialize)(this.picLoading)).BeginInit();
this.SuspendLayout();
//
// lbLogo
//
this.lbLogo.AutoSize = true;
this.lbLogo.Font = new System.Drawing.Font("楷体", 12F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)()));
this.lbLogo.ForeColor = System.Drawing.Color.White;
this.lbLogo.Location = new System.Drawing.Point(, );
this.lbLogo.Name = "lbLogo";
this.lbLogo.Size = new System.Drawing.Size(, );
this.lbLogo.TabIndex = ;
this.lbLogo.Text = "LOGO";
//
// lbAppName
//
this.lbAppName.AutoSize = true;
this.lbAppName.Font = new System.Drawing.Font("黑体", 26.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)()));
this.lbAppName.ForeColor = System.Drawing.Color.White;
this.lbAppName.Location = new System.Drawing.Point(, );
this.lbAppName.Name = "lbAppName";
this.lbAppName.Size = new System.Drawing.Size(, );
this.lbAppName.TabIndex = ;
this.lbAppName.Text = "AppName\r\n";
//
// lbMsg
//
this.lbMsg.AutoSize = true;
this.lbMsg.Font = new System.Drawing.Font("宋体", 10.5F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)()));
this.lbMsg.ForeColor = System.Drawing.Color.White;
this.lbMsg.Location = new System.Drawing.Point(, );
this.lbMsg.Name = "lbMsg";
this.lbMsg.Size = new System.Drawing.Size(, );
this.lbMsg.TabIndex = ;
this.lbMsg.Text = "label1";
//
// btnClose
//
this.btnClose.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right)));
this.btnClose.Cursor = System.Windows.Forms.Cursors.Hand;
this.btnClose.Image = global::AllTypeTest.Properties.Resources.Delete;
this.btnClose.Location = new System.Drawing.Point(, );
this.btnClose.Name = "btnClose";
this.btnClose.Size = new System.Drawing.Size(, );
this.btnClose.SizeMode = System.Windows.Forms.PictureBoxSizeMode.Zoom;
this.btnClose.TabIndex = ;
this.btnClose.TabStop = false;
this.btnClose.Click += new System.EventHandler(this.btnClose_Click);
//
// picLoading
//
this.picLoading.Image = global::AllTypeTest.Properties.Resources.download;
this.picLoading.Location = new System.Drawing.Point(, );
this.picLoading.Name = "picLoading";
this.picLoading.Size = new System.Drawing.Size(, );
this.picLoading.SizeMode = System.Windows.Forms.PictureBoxSizeMode.Zoom;
this.picLoading.TabIndex = ;
this.picLoading.TabStop = false;
//
// LoadingForm
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.BackColor = System.Drawing.Color.ForestGreen;
this.ClientSize = new System.Drawing.Size(, );
this.Controls.Add(this.btnClose);
this.Controls.Add(this.lbMsg);
this.Controls.Add(this.picLoading);
this.Controls.Add(this.lbAppName);
this.Controls.Add(this.lbLogo);
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
this.Name = "LoadingForm";
this.ShowInTaskbar = false;
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
this.Text = "LoadingForm";
this.TopMost = true;
((System.ComponentModel.ISupportInitialize)(this.btnClose)).EndInit();
((System.ComponentModel.ISupportInitialize)(this.picLoading)).EndInit();
this.ResumeLayout(false);
this.PerformLayout(); } #endregion private System.Windows.Forms.Label lbLogo;
private System.Windows.Forms.Label lbAppName;
private System.Windows.Forms.PictureBox picLoading;
private System.Windows.Forms.Label lbMsg;
private System.Windows.Forms.PictureBox btnClose; }
LoadingForm
仿Office的程序载入窗体的更多相关文章
- 仿QQ聊天程序(java)
仿QQ聊天程序 转载:牟尼的专栏 http://blog.csdn.net/u012027907 一.设计内容及要求 1.1综述 A.系统概述 我们要做的就是类似QQ这样的面向企业内部的聊天软件,基本 ...
- 小白学phoneGap《构建跨平台APP:phoneGap移动应用实战》连载四(使用程序载入事件)
在了解了PhoneGap中都有哪些事件之后,本节将開始对这些事件的使用方法进行具体地介绍.本节要介绍的是程序载入事件,也就是deviceready.pause和resume这3个事件. [范例4-2 ...
- 快速找到Office应用程序安装路径
p{ font-size: 15px; } .alexrootdiv>div{ background: #eeeeee; border: 1px solid #aaa; width: 99%; ...
- 高仿Readhub小程序 微信小程序项目【原】
# News #### 项目介绍微信小程序项目涉及功能 https://gitee.com/richard1015/News https://github.com/richard1015/News 高 ...
- C#WinForm窗体内Panel容器中嵌入子窗体、程序主窗体设计例子
C#WinForm父级窗体内Panel容器中嵌入子窗体.程序主窗体设计例子 在项目开发中经常遇到父级窗体嵌入子窗体所以写了一个例子程序,顺便大概划分了下界面模块和配色,不足之处还望指点 主窗体窗体采用 ...
- C#程序实现窗体的最大化/最小化
C#程序实现窗体的最大化/最小化 http://blog.csdn.net/jiangqin115/article/details/41251215 private void button1_Clic ...
- Office应用程序对照表
任何Office应用程序(包括excel)的类型库都作为Office安装的一部分安装.类型库是特定于版本的(即,安装了哪个版本的Office). 例如,Office 2007版本为12.0,Offic ...
- 微信小程序--仿微信小程序朋友圈Pro(内容发布、点赞、评论、回复评论)
微信小程序--仿微信小程序朋友圈Pro(内容发布.点赞.评论.回复评论) 项目开源地址M朋友圈Pro 求个Star 项目背景 基于原来的开源项目 微信小程序仿朋友圈功能开发(发布.点赞.评论等功能 ...
- winfrom 关闭别的应用程序的窗体或者弹出框(winform 关闭句柄)
在word转换成html的时候,由于系统版本不一样,office总是抛出异常,Microsoft Word停止工作,下面有三个按钮,关闭程序等等,但是我的转换工作需要自动的,每当抛出异常的时候我的程序 ...
随机推荐
- http学习笔记(三)
几乎所有的http通信都是由TCP/IP承载的.http好比一辆汽车,而TCP是一条公路,所有的汽车都要在公路上跑,看看http是如何在tcp这条公路上往返的. 首先简单地看看tcp,TCP连接是通过 ...
- svn import-纳入版本控制
转svn import-纳入版本控制 import: 将未纳入版本控制的文件或目录树提交到版本库.用法: import [PATH] URL 递归地提交 PATH 的副本至 URL. 如果省略 PA ...
- Java-集合练习5
第五题 (Map)设计Account 对象如下: private long id; private double balance; private String password; 要求完善设计,使得 ...
- Time33算法
Time33是字符串哈希函数,现在几乎所有流行的HashMap都采用了DJB Hash Function,俗称"Times33"算法.Times33的算法很简单,就是不断的乘33. ...
- 锋利的JQuery —— 选择器
图片猛戳链接
- Node.js入门:事件机制
Evented I/O for V8 JavaScript 基于V8引擎实现的事件驱动IO. 事件机制的实现 Node.js中大部分的模块,都继承自Event模块(http://n ...
- Atitit Server Side Include ssi服务端包含规范 csi esi
Atitit Server Side Include ssi服务端包含规范 csi esi 一.CSI (Client Side Includes) 1 1.1. 客户端包含1 1.2. Ang ...
- PHP 基础
var_dump(empty($a)); 判断变量是否为空 var_dump(isset($a)); 判断变量是否定义 $a=10;unset($a); 删除变量 var_d ...
- 多线程 ThreadPool线程池
简单说明一下: 线程池可以看做容纳线程的容器:一个应用程序最多只能有一个线程池:ThreadPool静态类通过QueueUserWorkItem()方法将工作函数排入线程池: 每排入一个工作函数,就相 ...
- Python的datetime
Python的datetime 总会用到日期格式化和字符串转成日期,贴点代码以供参考,其实API真的是很全的,可是又不知道具体的method... datetime.datetime.strftime ...