zoukankan      html  css  js  c++  java
  • C# 模仿 IBOX 程序管理 (简易版)

      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 程序管理简易版

    对于API调用有兴趣的可以先看看:http://www.cnblogs.com/NatureSex/archive/2011/12/07/2278575.html

  • 相关阅读:
    js上移、下移排序 效果
    如何为平板打造完美的网站页面?
    [BUUOJ]刮开有奖reverse
    [0CTF 2016]piapiapia
    [TSCTFJ 2019]bypass
    [安洵杯 2019]easy_serialize_php
    [TSCTFJ] relax
    c#访问网页
    DNN 数据访问
    c#访问数据库
  • 原文地址:https://www.cnblogs.com/NatureSex/p/2280742.html
Copyright © 2011-2022 走看看