欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

asp.net 图标提取以及图标转换的实例代码

程序员文章站 2024-02-29 17:39:40
复制代码 代码如下:using system;using system.collections.generic;using system.drawing;using sys...

复制代码 代码如下:

using system;
using system.collections.generic;
using system.drawing;
using system.windows.forms;
using system.io;
using system.resources;
using system.reflection;
using system.runtime.interopservices;


namespace newexticon
{
    public partial class mainform : form
    {   
        public assembly asm = assembly.getexecutingassembly();
        public mainform()
        {
            initializecomponent();       
        }

        void mainform_load(object sender ,eventargs e)
        {
            lbl_display.text ="no work !";   

        }

        void btnextractclick(object sender, eventargs e)
        {
            random rd = new random();
            openfiledialog1.filter="应用程序|*.dll;*.exe|所有程序|*.*";
            if(openfiledialog1.showdialog()== dialogresult.ok)
            {
                icon icon = icon.extractassociatedicon(openfiledialog1.filename);
                filestream filestream = new filestream("d:\\"+rd.next(1,100000)+".ico",filemode.create);
                icon.save(filestream);
                filestream.close();       
                lbl_display.text="work done!";
            }
            else
            {
                return ;
            }
        }

        void btnchangeclick(object sender, eventargs e)
        {
            size sz = new size(32,32);
            random rd = new random();
            openfiledialog1.filter="图片|*.jpg;*.png;*.bmp|所有程序|*.*";
            //openfiledialog1.filter="图片|*.jpg;*.png;*.bmp|应用程序|*.dll;*.exe";
            if(openfiledialog1.showdialog()== dialogresult.ok)
            {
                using(bitmap bm = new bitmap(openfiledialog1.filename))
                {
                    using(bitmap iconbm = new bitmap(bm,sz))
                    {
                        using(icon icon_t = icon.fromhandle(iconbm.gethicon()))
                        {
                            filestream filestream = new filestream("d:\\"+rd.next(1,100000)+".ico",filemode.create);
                            icon_t.save(filestream);
                            filestream.close();   
                            lbl_display.text="work done!";        
                        }
                    }
                }
            }
            else
            {
                return ;
            }

        }
        void pic_click(object sender, system.eventargs e)
        {
            int int_index = convert.toint32(((picturebox)sender).tag) ;
            switch(int_index)
            {
                case 1:
                    this.close();
                    application.exit();
                    break;
                case 2:
                    windowstate = formwindowstate.minimized;
                    break;
                default :
                    break;

            }

        }

        void pic_hover(object sender,eventargs e)
        {
            string  name_1 = "btn_close_hightligth.png";
            string name_2 ="btn_mini_highlight.png";
            bitmap bm_1 = new bitmap(asm.getmanifestresourcestream(name_1));
            bitmap bm_2 = new bitmap(asm.getmanifestresourcestream(name_2));
            int int_index = convert.toint32(((picturebox)sender).tag) ;
            switch(int_index)
            {
                case 1:
                    picturebox1.backgroundimage=bm_1;
                    break;
                case 2:
                    picturebox2 .backgroundimage = bm_2;
                    break;
                default :
                    break;

            }
        }
        void pic_leave(object sender,eventargs e)
        {
            string  name_1 = "btn_close_down.png";
            string  name_2 ="btn_mini_down.png";
            bitmap bm_1 = new bitmap(asm.getmanifestresourcestream(name_1));
            bitmap bm_2 = new bitmap(asm.getmanifestresourcestream(name_2));
            //getmainfestresourcestream获取的是流
            int int_index = convert.toint32(((picturebox)sender).tag) ;
            switch(int_index)
            {
                case 1:
                    picturebox1.backgroundimage=bm_1;
                    break;
                case 2:
                    picturebox2 .backgroundimage = bm_2;
                    break;
                default :
                    break;

            }
        }

        private const int wm_nclbuttondown = 0xa1;
        private const int htcaption = 2;

        [dllimport("user32.dll")]
        private static extern int sendmessage (int hwnd, int wmsg, int wparam, int lparam);

        [dllimport("user32.dll")]
        private static extern int releasecapture();

        private void form_mousedown(object sender, system.windows.forms.mouseeventargs e)
        {
            //为当前的应用程序释放鼠标捕获
            releasecapture();
            //发送消息,让系统误以为你在标题拦上按下鼠标
            sendmessage((int)this.handle,wm_nclbuttondown,htcaption,0);
        }
    }
}