WinFrom、C# 学习记录一

本文汇总了一系列实用的编程技巧,包括自定义线帽绘制、开启ListView/GridView双缓冲、进程管理、Dictionary排序、获取Panel内容为Bitmap、实现文件关联、对象序列化与反序列化、判断闰年、使用ILSpy进行反编译以及数据库初始化策略。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

自定义线帽

/// <summary>
/// 画笔
/// </summary>
private Pen pen = new Pen(Color.Black);
/// <summary>
/// 自定义线帽
/// </summary>
private AdjustableArrowCap lineCap = new AdjustableArrowCap(10, 10, false);
pen.CustomEndCap = lineCap;
e.Graphics.DrawLine(pen, new Point(24,310), new Point(720, 310));

ListView/GridView一句话开启双缓冲

this.listView1.GetType().GetProperty("DoubleBuffered", BindingFlags.Instance | BindingFlags.NonPublic).SetValue(this.listView1, true, null);

杀死进程

[DllImport("User32.dll", CharSet = CharSet.Auto)]
public static extern int GetWindowThreadProcessId(IntPtr hwnd, out int ID);
public static void Kill(MSExcel.Application excel)
{
    IntPtr t = new IntPtr(excel.Hwnd);
    int k = 0;
    GetWindowThreadProcessId(t, out k);
    System.Diagnostics.Process p = System.Diagnostics.Process.GetProcessById(k);
    p.Kill();
}

Dictionary使用linq排序

Dictionary<int, double> percents = new Dictionary<int, double>();
//排序
var dicSort = (from d in percents orderby d.Value select d);
foreach (var item in dicSort)
{
    System.Console.WriteLine(item.Value);
}

获取Panel的内容到Bitmap

Bitmap bmp = new Bitmap(this.panelHistControl1.panel1.Width, this.panelHistControl1.panel1.Height, System.Drawing.Imaging.PixelFormat.Format24bppRgb);
                    this.panelHistControl1.panel1.DrawToBitmap(bmp, new Rectangle(0, 0, this.panelHistControl1.panel1.Width, this.panelHistControl1.panel1.Height));

文件关联,双击打开应用程序

下面是写注册表,写入完注册表之后,双击就会打开你的应用,并且再Main(string[] args) 方法获得双击的文件的完整路径,之后就可以操作文件了

private void WriteApp()
        {
            string strProject = "YourAppName";        //应用名
            string p_FileTypeName = ".selfFileExt"; //自定义文件后缀
            string fileName = System.Windows.Forms.Application.ExecutablePath;// 获取启动了应用程序的可执行文件的路径及文件名
                                                                              //string startPath = System.Windows.Forms.Application.StartupPath;//获取启动了应用程序的可执行文件的路径
                                                                              //注册文件类型
            RegistryKey _RegKey = Registry.ClassesRoot.OpenSubKey("", true);              //打开注册表
            RegistryKey _VRPkey = _RegKey.OpenSubKey(p_FileTypeName, true);
            if (_VRPkey != null)
            {
                _RegKey.DeleteSubKey(p_FileTypeName, true);
            }

            _RegKey.CreateSubKey(p_FileTypeName);
            _VRPkey = _RegKey.OpenSubKey(p_FileTypeName, true);
            _VRPkey.SetValue("", strProject);
            _VRPkey = _RegKey.OpenSubKey(strProject, true);
            if (_VRPkey != null) _RegKey.DeleteSubKeyTree(strProject);         //如果等于空就删除注册表DSKJIVR

            Registry.ClassesRoot.CreateSubKey(p_FileTypeName).SetValue("", strProject, RegistryValueKind.String);
            using (RegistryKey key = Registry.ClassesRoot.CreateSubKey(strProject))
            {
                //设置图标
                RegistryKey iconKey = key.CreateSubKey("DefaultIcon");
                iconKey.SetValue("", System.Windows.Forms.Application.StartupPath + "\\logo.ico");
                //设置默认启动程序
                key.CreateSubKey(@"Shell\Open\Command").SetValue("", "\"" + fileName + "\" \"%1\"");
            }
        }

对象序列化/反序列化

#序列化
using (System.IO.FileStream fs = new System.IO.FileStream("你的欲保存的文件路径", System.IO.FileMode.Create, System.IO.FileAccess.Write))
{
    BinaryFormatter formatter = new BinaryFormatter();
    formatter.Serialize(fs, data);//data是你的可序列化的对象
    fs.Close();
}

#反序列化
System.IO.FileStream fs = new System.IO.FileStream("你的文件路径", FileMode.Open, FileAccess.Read, FileShare.Read);
BinaryFormatter formatter = new BinaryFormatter();
YouSelfData data = formatter.Deserialize(fs) as YouSelfData;//YouSelfData 你的自定义的类

判断闰年

public static bool IsLeapYear(int year)
{
    bool b1 = (year % 4 == 0) && (year % 100 != 0);
    bool b2 = (year % 400 == 0);
    return b1 || b2;
}

反编译器

        ILSpy 是开源的 .NET 程序集浏览器和反编译器。

        下载地址:

https://github.com/icsharpcode/ILSpyhttps://github.com/icsharpcode/ILSpy

 Database.SetInitializer的几种参数

数据库不存在时重新创建数据库

Database.SetInitializer<testContext>(new CreateDatabaseIfNotExists<testContext>());

每次启动应用程序时创建数据库

Database.SetInitializer<testContext>(new DropCreateDatabaseAlways<testContext>());

模型更改时重新创建数据库

Database.SetInitializer<testContext>(new DropCreateDatabaseIfModelChanges<testContext>());

从不创建数据库

Database.SetInitializer<testContext>(null);

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

坐望云起

如果觉得有用,请不吝打赏

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值