美文网首页程序员
鼠标指针样式修改(界面或控件)&素材调用方法(引用&am

鼠标指针样式修改(界面或控件)&素材调用方法(引用&am

作者: forsek | 来源:发表于2017-11-14 12:52 被阅读11次

    [引用自百度知道] (https://zhidao.baidu.com/question/551948077

    一,程序界面或控件(picturebox,button等)中的鼠标指针样式修改方法

    1,VS中可以调用windows自带的几种鼠标指针形状,只要在Form或控件的属性里,将cursor属性从Default,设置成想要的样式即可(十字形,手形等)

    2,自定义或下载的鼠标指针样式可以使用下面的代码,将自己的鼠标样式应用在Form或者控件中,注意cur素材文件路径有绝对和相对两种方式(引用素材方法:)

    PicCodemage.Cursor = new Cursor(@"c:\03.cur")//PicCodemege可以是Form名和控件名,将其鼠标指针属性实例化为绝对路径C盘根目录下的03.cur文件。
    //或者
    PicCodemage.Cursor = new Cursor("../../pic/03.cur");//使用相对目录,默认是在工程的Debug目录下,所以使用两次“../”向上返回两级目录(C:\Users\Administrator\Desktop\新建文件夹\WFA2练习\WFA2练习\bin\Debug),进入WFA2练习中的素材文件夹pic,调用内部的03.cur文件。
    

    注意:VS中给工程添加文件夹与素材方法:打开工程,在界面右侧偏上的解决方案资源管理器中,选中工程名,右键,选择添加,新建文件夹,重命名,然后将素材(图片,指针,音乐,图标等)拖动放入该文件夹即可,然后使用代码中的相对路径进行调用。

    3.上述方法可以改变鼠标指针形状,无法改变其颜色,可使用引用中的方法来实现引入外来素材样式

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows.Forms;
    using System.Threading;
    using System.Diagnostics;
    //这两条需要引用
    using System.Runtime.InteropServices;
    using System.Reflection;
    
    namespace WFA2练习
    {
        public partial class Form1 : Form
        {
            //这两行代码需要添加
            [DllImport("user32.dll")]
            static extern IntPtr LoadCursorFromFile(string fileName);
            public Form1()
            {
                InitializeComponent();
                //绝对路径(无法改颜色)
                //Cursor Cur = new Cursor(@"C:\03.cur");
                //相对路径(无法改颜色)
                //Cursor Cur = new Cursor("../../pic/03.cur");
                //this.Cursor = Cur;
                //以下代码为实现方法,现在看不懂,只知道能用
                Cursor customCursor = new Cursor(Cursor.Current.Handle);
                IntPtr customCursorHandle = LoadCursorFromFile("../../pic/03.cur");
                customCursor.GetType().InvokeMember("handle", BindingFlags.Public |
                BindingFlags.NonPublic | BindingFlags.Instance |
                BindingFlags.SetField, null, customCursor,
                new object[] { customCursorHandle });
                //this.Cursor = customCursor;//将样式赋予Form
                pictureBox1.Cursor = customCursor;//将样式赋予控件
            }
    

    二,根据控件中接收变量不同,切换鼠标指针样式
    1,在接收变量后判断,或在控件需要显示时判断,使用上面第三种方法

    //添加引用
    using System.Runtime.InteropServices;
    using System.Reflection;
    
    namespace ImageStaff
    {
        public partial class Main : Form
        {
            //在Form种添加这两行
            [DllImport("user32.dll")]
            static extern IntPtr LoadCursorFromFile(string fileName);
                        //省略中间代码
                        //在图像控件赋值后面添加方法
                        #region 图像控件赋值
                        nowImage = (Image)TopicInfo.Image.Clone();
                        nowTopic = TopicInfo;
                        //添加判断图片类型改变指针形状的语句
                        if (TopicInfo.TypeId.ToString()=="6137")//判断条件
                        {
                            Cursor customCursor = new Cursor(Cursor.Current.Handle);
                            //IntPtr customCursorHandle = LoadCursorFromFile("C:\\03.cur");
                            IntPtr customCursorHandle = LoadCursorFromFile("../../pic/03.cur");
                            customCursor.GetType().InvokeMember("handle", BindingFlags.Public |
                            BindingFlags.NonPublic | BindingFlags.Instance |
                            BindingFlags.SetField, null, customCursor,
                            new object[] { customCursorHandle });
                            PicCodemage.Cursor = customCursor;
                        }
                        else
                        {
                            PicCodemage.Cursor = Cursors.Default;//使用默认样式
                        }
    
    
    二,素材嵌入可执行文件(exe)方法

    参考自新浪微博
    上述一,二,两种方法,需要固定素材路径,并将素材文件夹一起与可执行文件打包后,才能移动至其他电脑上使用。可以使用C#的资源类,新建资源类(Resource1.resx)将素材添加进去后,使用“资源类名.资源名”来进行调用,这样编译好后,exe文件内部就包含了所有的素材文件。
    注意事项:C#资源类可以直接导入并调用名称使用的只有图片与文本(音频wav格式也可以),其他文件需要使用内存流读取,并生成相应的文件后,才能调用,例如:光标素材(cur)等。

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows.Forms;
    using System.Threading;
    using System.Diagnostics;
    //这两条需要引用
    using System.Runtime.InteropServices;
    using System.Reflection;
    //使用内存流读取素材时用到
    using System.IO;
    
    namespace WFA2练习
    {
        public partial class Form1 : Form
        {
            //从资源文件中读取光标文件,保存成一个临时文件(什么名字什么扩展名都可以)zxb.temp
            public static Cursor getCursorFromResource(byte[] resource)
            {
                byte[] b = Resource1.cur04;
                FileStream fileStream = new FileStream("zxb.dat", FileMode.Create);
                fileStream.Write(b, 0, b.Length);
                fileStream.Close();
                Cursor cur = new Cursor(LoadCursorFromFile("zxb.dat"));
                return cur;
            }
            //这两行代码需要添加
            [DllImport("user32.dll")]
            static extern IntPtr LoadCursorFromFile(string fileName);
            public Form1()
            {
                InitializeComponent();
                //绝对路径(无法改颜色)
                //Cursor Cur = new Cursor(@"C:\03.cur");
                //相对路径(无法改颜色)
                //Cursor Cur = new Cursor("../../pic/03.cur");
                //this.Cursor = Cur;
                //直接使用下面的方法(样式固定),或者在判断语句种使用(可根据条件变换样式)
                //Cursor customCursor = new Cursor(Cursor.Current.Handle);
                //IntPtr customCursorHandle = LoadCursorFromFile("../../pic/03.cur");
                //customCursor.GetType().InvokeMember("handle", BindingFlags.Public |
                //BindingFlags.NonPublic | BindingFlags.Instance |
                //BindingFlags.SetField, null, customCursor,
                //new object[] { customCursorHandle });
                ////this.Cursor = customCursor;
                //pictureBox1.Cursor = customCursor;
            }
            private Bitmap bit1;//上提示图片(图片1)
    
            private void button1_Click(object sender, EventArgs e)
            {
                if (this.openFileDialog1.ShowDialog() == DialogResult.OK)
                {
                    string path = this.openFileDialog1.FileName;//获得图片路径
                    bit1 = new Bitmap(path);
                    this.pictureBox1.Image = bit1;
                    //加入条件判断是否改变鼠标指针样式
                    if (pictureBox1.Image != null)
                    {   //使用资源文件时,直接读取嵌入的鼠标文件,然后显示即可
                        //Cursor customCursor = new Cursor(Cursor.Current.Handle);
                        //IntPtr customCursorHandle = LoadCursorFromFile("../../pic/03.cur");
                        Cursor newCur = getCursorFromResource(Resource1.cur04);
                        //IntPtr customCursorHandle = LoadCursorFromFile("newCur");
                       // customCursor.GetType().InvokeMember("handle", BindingFlags.Public |
                       // BindingFlags.NonPublic | BindingFlags.Instance |
                       // BindingFlags.SetField, null, customCursor,
                       //new object[] { customCursorHandle });
                        //this.Cursor = customCursor;
                        pictureBox1.Cursor = newCur;
                    }
                    else 
                    {
                        pictureBox1.Cursor = Cursors.Default;
                    }
    
                }
            }
    
            private void button2_Click(object sender, EventArgs e)
            {
                if (pictureBox1.Image!=null)
                {
                    Color c = new Color();
                    //Bitmap b = new Bitmap(pictureBox1.Image);
                    Bitmap b1 = new Bitmap(pictureBox1.Image);
                    int rr, gg, bb, cc;
    
                    for (int i = 0; i < bit1.Width; i++)
                    {
    
                        for (int j = 0; j < bit1.Height; j++)
                        {
                            c = bit1.GetPixel(i, j);
                            rr = c.R;
                            gg = c.G;
                            bb = c.B;
                            cc = (int)((rr + gg + bb) / 3);
                            if (cc < 0)
                                cc = 0;
                            if (cc > 255)
                                cc = 255;
                            //用FromArgb把整形转换成颜色值
                            Color c1 = Color.FromArgb(cc, cc, cc);
                            b1.SetPixel(i, j, c1);
                        }
                            //pictureBox2.Refresh();
                            //pictureBox2.Image = b1;
                            //b1.Save(@"C:\Users\Administrator\Desktop\gray.jpg");
                    }
                        pictureBox2.Refresh();
                        pictureBox2.Image = b1;
                        b1.Save(@"C:\Users\Administrator\Desktop\gray.jpg");
                }
            }
    
        }
    }
    
    

    相关文章

      网友评论

        本文标题:鼠标指针样式修改(界面或控件)&素材调用方法(引用&am

        本文链接:https://www.haomeiwen.com/subject/eiixvxtx.html