程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> C# >> C#入門知識 >> C# 模仿 IBOX 程序管理 (簡易版)

C# 模仿 IBOX 程序管理 (簡易版)

編輯:C#入門知識

 

C# IBOX 程序管理簡易版,該程序是2009年閒來無事做的程序,本來是想做成網絡程序下載器的,結果做成程序管理器了。

今天整理以前代碼的時候發現了這個東東,發給剛入門不久的.net 童鞋學習用,希望笑納。

  裡面會涉及一些API調用,圖標布局算法問題,系統消息機制的攔截,動態添加事件處理。可能對於剛學習不久的有點困難。

先上一個主界面截圖:(圖像被我壓縮了不然的有1.4MB,所以有點模糊

\

拖入增加文件或程序。。。

\
手動添加文件或程序

\
選擇刪除程序快捷方式

\
隱藏起來的快捷方式

\
顯示隱藏的程序

\
程序管理界面

\

以上為程序的部分截圖,下面貼出核心代碼含注釋。

 

 

#region [獲取圖標和可以拖動窗體]

 

        //調用系統API獲取程序的圖標信息

        [System.Runtime.InteropServices.DllImport("shell32.dll", EntryPoint = "ExtractAssociatedIcon")]

        private static extern IntPtr ExtractAssociatedIconA(

            IntPtr hInst,

        [System.Runtime.InteropServices.MarshalAs(

                 System.Runtime.InteropServices.UnmanagedType.LPStr)] string lpIconPath,

            ref int lpiIcon);

        //private static IntPtr hInst;

 

       

 

        private const int WM_NCHITTEST = 0x84;//客戶區消息

         private const int HTCLIENT = 0x1;

        private const int HTCAPTION = 0x2;

        private const int WM_NCLBUTTONDBLCLK = 0xA3;//雙擊消息

 

 

        //窗體消息,攔截標題區域雙擊消息和窗體移動區域消息

        protected override void WndProc(ref Message m)

        {

 

            switch (m.Msg)

            {

                case WM_NCHITTEST:

                    base.WndProc(ref m);

                    if ((int)m.Result == HTCLIENT)

                        m.Result = (IntPtr)HTCAPTION;

                    return;

                //  break;

                case WM_NCLBUTTONDBLCLK:

                    if ((int)m.Result == WM_NCLBUTTONDBLCLK)

                        m.Result = (IntPtr)0xa1;

                    return;

            }

            base.WndProc(ref m);

        }

 

        /// <summary>

        /// 獲取圖標

        /// </summary>

        public static Icon ExtractAssociatedIcon(string fileName)

        {

 

                if (File.Exists(fileName) || Directory.Exists(fileName))

                {

                    int i = 0;

                    //根據文件路徑獲取圖標指針

                    IntPtr hIcon = ExtractAssociatedIconA(new IntPtr(), fileName, ref i);

 

                    if (!hIcon.Equals(IntPtr.Zero))

                    {

                        //圖標指針轉換為ICO圖標

                        return Icon.FromHandle(hIcon);

                    }

                    else

                    {

                        return null;

                    }

                }

                else

                {

                    throw new Exception("加載某些文件失敗!可能有些文件已不存在!\n【請在[-管理界面-]刪除或者重新指定】");

                }

 

 

        }

        #endregion

 

 

/// <summary>

        /// 只能打開一個這個程序,這個是有BUG的,改了名稱就可以繼續打開

        /// </summary>

        private void IsOpen()

        {

            int isOpen = 0;

            //得到所有的進程

            Process[] computer = Process.GetProcesses();

            foreach (Process p in computer)

            {

                if (p.ProcessName.Equals("MyBox"))

                {

                    isOpen = isOpen + 1;

                }

            }

            if (isOpen == 1)

            {

                //清空

                pnlMain.Controls.Clear();

                //重新加載

                pnlMain.Controls.Add(this.msMenu);

                pnlMain.Controls.Add(this.grpAddMain);

                pnlMain.Controls.Add(this.pnlPwd);

                //讀取設置

                if (File.Exists("FileConfig.ini"))

                {

 

                    try

                    {

                        gm.loadSet();

                    }

                    catch (Exception e)

                    {

                        MessageBox.Show("配置文件讀取失敗!文件被篡改!所有文件列表已重置!");

                        MessageBox.Show("請手動刪除被更改FileConfig.ini文件!");

                        this.Close();

                        Console.WriteLine(e.Message);

                        this.Dispose();

                        return;

                    }

                }

                else

                {

                    MessageBox.Show("配置文件不存在!所有信息已重置!");

                    //重置配置

                    gm.saveSet(Application.StartupPath.ToString());

                    //清空文件

                    gm.save(Application.StartupPath.ToString());

                }

                //配置設置

                MainSet();

                //加載文件

                CreateGame(false);

            }

            if (isOpen == 2)

            {

                MessageBox.Show("此程序已經打開!");

                this.Close();

                this.Dispose();

            }

        }

 

 

/// <summary>

        /// 創建所有文件

        /// </summary>

        /// <param name="ispwd">true為加載加密文件</param>

        public void CreateGame(bool ispwd)

        {

            int errFileNum = 0;//錯誤文件數量

            gm.load();

            PictureBox pb;

            Label lb;

            int x=0;

            int y=0;

            int width = this.Size.Width;

            bool iscreate=true;//是否創建

            foreach (string key in gm.Info.Keys)

            {

                iscreate = true;

                pb= new PictureBox();

                lb = new Label();

                Icon icon=null;

                Bitmap appicon;

                //如果找不到指定文件!彈出警告信息!

                try

                {

                  icon = ExtractAssociatedIcon(@gm.Info[key].Path);

                }

                catch (Exception e)

                {

                    errFileNum++;

                    MessageBox.Show(e.Message+"\n\t加載錯誤文件數:["+errFileNum+"]","提示:出錯啦!(MyBox作者提醒您!)",MessageBoxButtons.OK,MessageBoxIcon.Information);

                    iscreate = false;//出錯文件不加載

                }

                if (iscreate)

                {

                    appicon = icon.ToBitmap();

                    //定義圖片

                    pb.Cursor = Cursors.Hand;

                    pb.BackgroundImage = appicon;

                    if (ispwd == true)

                    {

                        pb.Location = new Point((x + 1) + 25, (y + 1) + 15);

                    }

                    else

                    {

                        pb.Location = new Point((x + 1) + 25, (y + 1) + 50);

                    }

                    pb.Size = new Size(50, 50);

                    pb.DoubleClick += new EventHandler(picIco_DoubleClick);

                    pb.MouseEnter += new EventHandler(picGame_MouseEnter);

                    pb.MouseLeave += new EventHandler(picGame_MouseLeave);

                    pb.BackColor = Color.Transparent;

                    pb.Tag = gm.Info[key].Name.ToString();

                    pb.BackgroundImageLayout = ImageLayout.Center;

                    pb.ContextMenuStrip = cmsGame;

                    //定義文本

                    lb.Text = gm.Info[key].Name.ToString();

                    lb.Location = new Point(pb.Location.X - 13, pb.Location.Y + 50);

                    lb.BackColor = Color.Transparent;

                    lb.Size = new Size(pb.Size.Width + 30, lb.Size.Height);

                    lb.TextAlign = ContentAlignment.MiddleCenter;

                    lb.ForeColor = Color.Aqua;

                    if (ispwd == true&&gm.Info[key].Type==true)

                    {

                        pnlPwd.Controls.Add(pb);

                        pnlPwd.Controls.Add(lb);

                        x = x + 100;

                        if (x > width - 25)

                        {

                            x = 0;

                            y = y + 100;

                        }

                    }

                    else if (gm.Info[key].Type == false && ispwd == false)

                    {

                        pnlMain.Controls.Add(pb);

                        pnlMain.Controls.Add(lb);

                        x = x + 100;

                        if (x > width - 25)

                        {

                            x = 0;

                            y = y + 100;

                        }

                    } 

                }

            }

        }

 

代碼太多!這裡就不一一貼出!

直接放源碼:C# IBOX 程序管理簡易版 http://www.BkJia.com/uploadfile/2011/1210/20111210024951448.rar  

作者: NatureSexy

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