程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> C# >> C#入門知識 >> Winform開發幾個常用的開發經驗及知識積累(一),winform開發

Winform開發幾個常用的開發經驗及知識積累(一),winform開發

編輯:C#入門知識

Winform開發幾個常用的開發經驗及知識積累(一),winform開發


本人做Winform開發多年,孜孜不倦,略有小成,其中收集或者自己開發一些常用的東西,基本上在各個項目都能用到的一些開發經驗及知識積累,現逐步介紹一些,以飨讀者,共同進步。

1、窗口【×】關閉按鈕變為最小化,並在托盤提示信息

一般有些管理系統,為了防止客戶隨意關閉程序或者基於其他原因,一般會把 窗口【×】關閉按鈕變為最小化,如大家熟悉的飛信、MSN等等,但是有些不是很熟悉的客戶,最小化到托盤的時候,卻不知道程序到了那裡去了,因此,最小化的時候,伴隨一個氣泡提示信息,顯得有一定的必要,如下截圖所示。

首先在主窗體的設計界面中添加一個NotifyIcon控件,然後實現相關的代碼即可。 

下面列出一些關鍵的代碼出來,大家看了應該就知道如何實現了

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)

        {

            //如果我們操作【×】按鈕,那麼不關閉程序而是縮小化到托盤,並提示用戶.

            if (this.WindowState != FormWindowState.Minimized)

            {

                e.Cancel = true;//不關閉程序



                //最小化到托盤的時候顯示圖標提示信息,提示用戶並未關閉程序

                this.WindowState = FormWindowState.Minimized;

                notifyIcon1.ShowBalloonTip(3000, "程序最小化提示",

                     "圖標已經縮小到托盤,打開窗口請雙擊圖標即可。",

                     ToolTipIcon.Info);

            }

        }



        private void MainForm_Move(object sender, EventArgs e)

        {

            if (this == null)

            {

                return;

            }



            //最小化到托盤的時候顯示圖標提示信息

            if (this.WindowState == FormWindowState.Minimized)

            {

                this.Hide();

                notifyIcon1.ShowBalloonTip(3000, "程序最小化提示",

                    "圖標已經縮小到托盤,打開窗口請雙擊圖標即可。",

                    ToolTipIcon.Info);

            }

        }

2、只允許允許一個程序實例,即使是通過虛擬桌面方式連接過來的,也是只允許一個人運行。

這個已經封裝好代碼了,只需要在Main函數裡面調用一下函數即可,允許多個實例會出現下面的對話框提示信息,提示不允許多實例運行,如下所示:

代碼如下所示。

/// <summary>
        /// 應用程序的主入口點。
        /// </summary>
        [STAThread]
        private static void Main()
        {
            GlobalMutex();

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

            //******啟動代碼**********
        }

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

            // 第一次創建mutex
            if (newMutexCreated)
            {
                Console.WriteLine("程序已啟動");
                //todo:此處為要執行的任務
            }
            else
            {
                MessageUtil.ShowTips("另一個窗口已在運行,不能重復運行。");
                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控件,簡化查詢條件的轉化

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

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;
        }

可以構造條件後,傳入查詢函數,實現數據的查詢。

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 + " -- " + "備件信息報表";
最後呈上代碼用到的一些類庫及控件:http://files.cnblogs.com/wuhuacong/WinformTips.rar

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved