日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

Winform开发几个常用的开发经验及知识积累(一)

發(fā)布時(shí)間:2025/4/5 编程问答 35 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Winform开发几个常用的开发经验及知识积累(一) 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

本人做Winform開發(fā)多年,孜孜不倦,略有小成,其中收集或者自己開發(fā)一些常用的東西,基本上在各個(gè)項(xiàng)目都能用到的一些開發(fā)經(jīng)驗(yàn)及知識(shí)積累,現(xiàn)逐步介紹一些,以饗讀者,共同進(jìn)步。

?1、窗口【×】關(guān)閉按鈕變?yōu)樽钚』?#xff0c;并在托盤提示信息

一般有些管理系統(tǒng),為了防止客戶隨意關(guān)閉程序或者基于其他原因,一般會(huì)把?窗口【×】關(guān)閉按鈕變?yōu)樽钚』?#xff0c;如大家熟悉的飛信、MSN等等,但是有些不是很熟悉的客戶,最小化到托盤的時(shí)候,卻不知道程序到了那里去了,因此,最小化的時(shí)候,伴隨一個(gè)氣泡提示信息,顯得有一定的必要,如下截圖所示。

?

首先在主窗體的設(shè)計(jì)界面中添加一個(gè)NotifyIcon控件,然后實(shí)現(xiàn)相關(guān)的代碼即可。?

下面列出一些關(guān)鍵的代碼出來,大家看了應(yīng)該就知道如何實(shí)現(xiàn)了

?????????private?void?notifyMenu_Show_Click(object?sender,?EventArgs?e)
?? ? ? ?{
????????????
if?(this.WindowState?==?FormWindowState.Minimized)
????????????{
????????????????
this.WindowState?=?FormWindowState.Maximized;
????????????????
this.Show();
????????????????
this.BringToFront();
????????????????
this.Activate();
????????????????
this.Focus();
????????????}
????????????
else
????????????{
????????????????
this.WindowState?=?FormWindowState.Minimized;
????????????????
this.Hide();
????????????}
????????}

???????
private?void?notifyMenu_Exit_Click(object?sender,?EventArgs?e)
????????{
????????????
try
????????????{
????????????????
this.ShowInTaskbar?=?false;
????????????????Portal.gc.Quit();
????????????}
????????????
catch
????????????{
????????????????
//?Nothing?to?do.
????????????}
????????}

????????
private?void?notifyIcon1_MouseDoubleClick(object?sender,?MouseEventArgs?e)
????????{
????????????notifyMenu_Show_Click(sender,?e);
????????}

????????
private?void?MainForm_MaximizedBoundsChanged(object?sender,?EventArgs?e)
????????{
????????????
this.Hide();
????????}

????????
///?<summary>
????????
///?縮小到托盤中,不退出
????????
///?</summary>
????????private?void?MainForm_FormClosing(object?sender,?FormClosingEventArgs?e)
????????{

????????????
//如果我們操作【×】按鈕,那么不關(guān)閉程序而是縮小化到托盤,并提示用戶.
????????????if?(this.WindowState?!=?FormWindowState.Minimized)
????????????{
????????????????e.Cancel?
=?true;//不關(guān)閉程序

????????????????
//最小化到托盤的時(shí)候顯示圖標(biāo)提示信息,提示用戶并未關(guān)閉程序
????????????????this.WindowState?=?FormWindowState.Minimized;
????????????????notifyIcon1.ShowBalloonTip(
3000,?"程序最小化提示",
?????????????????????
"圖標(biāo)已經(jīng)縮小到托盤,打開窗口請雙擊圖標(biāo)即可。",
?????????????????????ToolTipIcon.Info);
????????????}
????????}

????????
private?void?MainForm_Move(object?sender,?EventArgs?e)
????????{
????????????
if?(this?==?null)
????????????{
????????????????
return;
????????????}

????????????
//最小化到托盤的時(shí)候顯示圖標(biāo)提示信息
????????????if?(this.WindowState?==?FormWindowState.Minimized)
????????????{
????????????????
this.Hide();
????????????????notifyIcon1.ShowBalloonTip(
3000,?"程序最小化提示",
????????????????????
"圖標(biāo)已經(jīng)縮小到托盤,打開窗口請雙擊圖標(biāo)即可。",
????????????????????ToolTipIcon.Info);
????????????}
????????}
?2、只允許允許一個(gè)程序?qū)嵗?#xff0c;即使是通過虛擬桌面方式連接過來的,也是只允許一個(gè)人運(yùn)行。

?這個(gè)已經(jīng)封裝好代碼了,只需要在Main函數(shù)里面調(diào)用一下函數(shù)即可,允許多個(gè)實(shí)例會(huì)出現(xiàn)下面的對(duì)話框提示信息,提示不允許多實(shí)例運(yùn)行,如下所示:

?

代碼如下所示。

?? ? ? ?///?<summary>
????????
///?應(yīng)用程序的主入口點(diǎn)。
????????
///?</summary>
????????[STAThread]
????????
private?static?void?Main()
????????{
????????????GlobalMutex();

????????????Application.EnableVisualStyles();
????????????Application.SetCompatibleTextRenderingDefault(
false);

????????????
//******啟動(dòng)代碼**********
????????}

????????
private?static?Mutex?mutex?=?null;
????????
private?static?void?GlobalMutex()
????????{
????????????
//?是否第一次創(chuàng)建mutex
????????????bool?newMutexCreated?=?false;
????????????
string?mutexName?=?"Global\\"?+?"WareHouseMis";//系統(tǒng)名稱,Global為全局,表示即使通過通過虛擬桌面連接過來,也只是允許運(yùn)行一次
????????????
try
????????????{
????????????????mutex?
=?new?Mutex(false,?mutexName,?out?newMutexCreated);
????????????}
????????????
catch?(Exception?ex)
????????????{
????????????????Console.Write(ex.Message);
????????????????System.Threading.Thread.Sleep(
1000);
????????????????Environment.Exit(
1);
????????????}

????????????
//?第一次創(chuàng)建mutex
????????????if?(newMutexCreated)
????????????{
????????????????Console.WriteLine(
"程序已啟動(dòng)");
????????????????
//todo:此處為要執(zhí)行的任務(wù)
????????????}
????????????
else
????????????{
????????????????MessageUtil.ShowTips(
"另一個(gè)窗口已在運(yùn)行,不能重復(fù)運(yùn)行。");
????????????????System.Threading.Thread.Sleep(
1000);
????????????????Environment.Exit(
1);//退出程序
????????????}

?? ? ? ?}?

3、使用NotifyWindow給用戶提示信息

?可以通過NotifyWindow類(最后附件中有),做一些信息的提示,方便用戶了解一些重要信息的提示,界面較為友好,如下所示:

?

?提示信息的代碼使用如下:

?? ? ? ?///?<summary>
????????
///?彈出提示消息窗口
????????
///?</summary>
????????public?void?Notify(string?caption,?string?content)
????????{
????????????Notify(caption,?content,?
400,?200,?5000);
????????}

????????
///?<summary>
????????
///?彈出提示消息窗口
????????
///?</summary>
????????public?void?Notify(string?caption,?string?content,?int?width,?int?height,?int?waitTime)
????????{
????????????NotifyWindow?notifyWindow?
=?new?NotifyWindow(caption,?content);
????????????notifyWindow.TitleClicked?
+=?new?System.EventHandler(notifyWindowClick);
????????????notifyWindow.TextClicked?
+=?new?EventHandler(notifyWindowClick);
????????????notifyWindow.SetDimensions(width,?height);
????????????notifyWindow.WaitTime?
=?waitTime;
????????????notifyWindow.Notify();
????????}

????????
private?void?notifyWindowClick(object?sender,?EventArgs?e)
????????{
????????????
//SystemMessageInfo?info?=?BLLFactory<SystemMessage>.Instance.FindLast();
????????????
//if?(info?!=?null)
????????????
//{
????????????
//????//FrmEditMessage?dlg?=?new?FrmEditMessage();
????????????
//????//dlg.ID?=?info.ID;
????????????
//????//dlg.ShowDialog();
????????????
//}
?? ? ? ?}?

4、使用SearchCondion控件,簡化查詢條件的轉(zhuǎn)化

不管在Winform或者在WebForm中,查詢構(gòu)造條件總是非常繁瑣的事情,使用該控件能有效簡化代碼,提高操作的準(zhǔn)確及方便行,這個(gè)控件我完成了幾年了,一直伴隨我處理各種查詢操作。

?? ? ? ?private?string?GetConditionSql()
????????{
????????????SearchCondition?condition?
=?new?SearchCondition();
????????????condition.AddCondition(
"ItemName",?this.txtName.Text,?SqlOperator.Like)
????????????????.AddCondition(
"ItemBigType",?this.txtBigType.Text,?SqlOperator.Like)
????????????????.AddCondition(
"ItemType",?this.txtItemType.Text,?SqlOperator.Like)
????????????????.AddCondition(
"Specification",?this.cmbSpecNumber.Text,?SqlOperator.Like)
????????????????.AddCondition(
"MapNo",?this.txtMapNo.Text,?SqlOperator.Like)
????????????????.AddCondition(
"Material",?this.txtMaterial.Text,?SqlOperator.Like)
????????????????.AddCondition(
"Source",?this.txtSource.Text,?SqlOperator.Like)
????????????????.AddCondition(
"Note",?this.txtNote.Text,?SqlOperator.Like)
????????????????.AddCondition(
"Manufacture",?this.txtManufacture.Text,?SqlOperator.Like)
????????????????.AddCondition(
"ItemNo",?this.txtItemNo.Text,?SqlOperator.LikeStartAt);
????????????
string?where?=?condition.BuildConditionSql().Replace("Where",?"");

????????????
return?where;
?? ? ? ?}?

可以構(gòu)造條件后,傳入查詢函數(shù),實(shí)現(xiàn)數(shù)據(jù)的查詢。

?? ? ? ? ? ?string?where?=?GetConditionSql();
????????????List
<ItemDetailInfo>?list?=?BLLFactory<ItemDetail>.Instance.Find(where,?this.winGridViewPager1.PagerInfo);
????????????
this.winGridViewPager1.DataSource?=?new?WHC.Pager.WinControl.SortableBindingList<ItemDetailInfo>(list);
?? ? ? ? ? ?this.winGridViewPager1.PrintTitle?=?Portal.gc.gAppUnit?+?"?--?"?+?"備件信息報(bào)表";

最后呈上代碼用到的一些類庫及控件:http://files.cnblogs.com/wuhuacong/WinformTips.rar???

本文轉(zhuǎn)自博客園伍華聰?shù)牟┛?#xff0c;原文鏈接:Winform開發(fā)幾個(gè)常用的開發(fā)經(jīng)驗(yàn)及知識(shí)積累(一),如需轉(zhuǎn)載請自行聯(lián)系原博主。



總結(jié)

以上是生活随笔為你收集整理的Winform开发几个常用的开发经验及知识积累(一)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。