项目中新学知识点整理

来源:互联网 发布:捕鱼源码有哪些权限 编辑:程序博客网 时间:2024/04/30 03:57

一、Control.Cursor 属性


获取或设置当鼠标指针位于控件上时显示的光标。

命名空间:System.Windows.Forms
程序集:System.Windows.Forms(在 system.windows.forms.dll 中)

Cursor.Current = Cursors.WaitCursor   :设置当前鼠标为系统 等待鼠标样式(应该是漏斗那个)
Cursor.Current = Cursors.Default      :系统默认的鼠标状态一般为箭头状光标,好像可以更改

第一种:(调用系统API)
首先引入两个命名空间复制代码代码如下:
using System.Runtime.InteropServices;
using System.Reflection;
导入API复制代码代码如下:
[DllImport("user32.dll")]
public static extern IntPtr LoadCursorFromFile(string fileName);
[DllImport("user32.dll")]
public static extern IntPtr SetCursor(IntPtr cursorHandle);
[DllImport("user32.dll")]
 public static extern uint DestroyCursor(IntPtr cursorHandle);
接下来使用自己的鼠标样式复制代码代码如下:
private void Form1_Load(object sender, EventArgs e)
        {
            Cursor myCursor = new Cursor(Cursor.Current.Handle);
            IntPtr colorCursorHandle = LoadCursorFromFile("my.cur");//鼠标图标路径
              myCursor.GetType().InvokeMember("handle", BindingFlags.Public |
            BindingFlags.NonPublic | BindingFlags.Instance |
            BindingFlags.SetField, null, myCursor,
            new object[] { colorCursorHandle });
            this.Cursor = myCursor;
        }
第二种:(不用API方式的,鼠标样式只需要一张背景透明的图片就行了,png或gif格式的)复制代码代码如下:
public void SetCursor(Bitmap cursor, Point hotPoint)
        {
            int hotX = hotPoint.X;
            int hotY = hotPoint.Y;
            Bitmap myNewCursor = new Bitmap(cursor.Width * 2 - hotX, cursor.Height * 2 - hotY);
            Graphics g = Graphics.FromImage(myNewCursor);
            g.Clear(Color.FromArgb(0, 0, 0, 0));
            g.DrawImage(cursor, cursor.Width - hotX, cursor.Height - hotY, cursor.Width, 
            cursor.Height);
            this.Cursor = new Cursor(myNewCursor.GetHicon());
            g.Dispose();
            myNewCursor.Dispose();
        }
在你想要改变鼠标样式的事件里头使用这个方法就行了,如:复制代码代码如下:
private void Form1_Load(object sender, EventArgs e)
        {
            Bitmap a=(Bitmap)Bitmap.FromFile("myCur.png");
            SetCursor(a, new Point(0, 0));
        }

二、c#中virtual, abstract和override的区别和用法

virtual是把一个方法声明为虚方法,使派生类可重写此方法,一般建立的方法是不能够重写的,譬如类A中有个方法
protected void method(){
原代码....;
}
类B继承自类A,类B能够调用类A的method()方法,但不能更改方法体代码,但当类A中使用了virtual声明此方法: protected virtual void method(),那么类B可通过使用override重写此方法
protected override void method(){
新代码....;
}
virtual可在基类、抽象类中使用,而使用abstract声明的方法,为抽象方法,抽象方法没有代码体,只有一个方法名的声明:
protected abstract void method();
当使用abstract声明的方法,其派生类必须要重写此方法,如上面一句为抽象类A中声明的,类B继承自抽象类A,那么类B中必须要使用override重写此方法
protected override void method(){
新代码....;
}
但abstract只能在抽象类中使用,override就是派生类重写父类方法(包括虚方法、抽象方法,接口方法)时的关键字,反正你要重写方法,就使用override声明
0 0