分类: WinForm

  • 如何快速清除 Visual Studio 程序中源码空行及删除空行

    日常编写代码中, 会遇到一些代码格式化的问题, 为了让代码能够看起来规范一点, 那么, 需要使用 vs 自带的格式化的命令, 比如, 可以 Ctrl+K+D(三个键同时按下), 也可以 Ctrl+K 再 Ctrl+D, 或者全选代码后,Ctrl+K 和 Ctrl+F.

    但是, 有时候由于代码排版问题, 会存在一些空行, 这时候, 格式化命令可能就不好使了, 此时, 需要用到 ctrl+H 进行批量替换, 由于换行符是特殊字符, 所以, 需要使用正则表达式进行搜索, 然后, 进行统一的替换, 输入:^(?([^\r\n])\s)*\r?$\r?\n 进行批量替换即可.

    替换空行

    希望这些小技巧, 能够帮助到看到这篇文章的有缘人.^-^

  • Webbrowser 控件设置 IE11 加载页面方法

    不知道大家有没有出现过这种情况, 我 IE 浏览器明明就是 IE11, 为什么使用 webbrowser 控件加载网页, 依然提示浏览器版本过低. 这个是因为,webbrowser 默认采用 IE8/IE9 模式去加载, 会导致页面出现各种错乱. 这次, 因为国拍网拍牌系统更新, 使用了最新的 H5, 所以, 我必须要想办法, 让 IE 浏览器, 使用 IE11 模式去加载. 具体方法如下:

    运行 Regedit,打开注册表,找到:HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Microsoft\Internet Explorer\MAIN\FeatureControl\FEATURE_BROWSER_EMULATION

    然后, 根据图片中, 更改默认 IE 浏览器 exe 的 dword 值

    IE 各版本的值如下:

    • 11001 (0x2EDF) Internet Explorer 11. Webpages are displayed in IE11 Standards mode, regardless of the !DOCTYPE directive
    • 11000 (0x2AF8) :Internet Explorer 11. Webpages containing standards-based !DOCTYPE directives are displayed in IE9 mode
    • 10000 (0x2710) :Internet Explorer 10. Webpages containing standards-based !DOCTYPE directives are displayed in IE9 mode.
    • 10001 (0x2AF7) :Internet Explorer 10. Webpages are displayed in IE10 Standards mode, regardless of the !DOCTYPE directive.
    • 9999 (0x270F) :Internet Explorer 9. Webpages are displayed in IE9 Standards mode, regardless of the !DOCTYPE directive.
    • 9000 (0x2328) :Internet Explorer 9. Webpages containing standards-based !DOCTYPE directives are displayed in IE9 mode.
    • 8888 (0x22B8) :Webpages are displayed in IE8 Standards mode, regardless of the !DOCTYPE directive.
    • 8000 (0x1F40) :Webpages containing standards-based !DOCTYPE directives are displayed in IE8 mode.
    • 7000 (0x1B58) :Webpages containing standards-based !DOCTYPE directives are displayed in IE7 Standards mode.

  • 浅谈 ImageList 中索引超出问题

    最近在进行代码合并的过程中, 遇到了 ImageList 控件运行到 SetKeyName 方法时, 提示 IndexOutOfRangeException 的情况. 也即是索引超出的异常. 分析设计器中图片数量, 以及断点调试, 发现设计界面中图片数量为 9, 实际运行时读取到的图片数量为 6, 导致, 后续 SetKeyName 的时候, 出现了超索引的问题.

    解决思路:

    • 修复对应的 form 窗体的 resx 的资源文件
    • 排查是否改过 form 窗体多语言属性, 注意类似 en.resx,zh.resx 这些多语言的资源文件.

    根本原因:

    ImageList 会从 resx 资源文件中读取图片二进制的 base64 字串内容, 但是, 如果你设置了窗体的多语言, 也即是窗体的 language 属性, 那么, 也要对应的修改 zh.resx,en.resx 这些资源文件. 因为, 设计器界面默认只修改缺省的 resx 文件, 但是, 运行程序的时候, 会去读取对应的语言的 resx 资源文件. 所以, 删掉这些资源文件, 那么 ImageList 就会只从缺省的 Resx 文件中读取了.

    总结:

    写下这篇文章, 主要是网上能够查到的一些解决方案, 大多都没有说明其本质, 不能解决问题. 希望我的这篇文章能够解决和我遇到这个相同问题的朋友.

  • 给ToolStrip工具栏扩展一个CheckBox单选框类型按钮的方法

    给 ToolStrip 工具栏扩展一个 CheckBox 单选框类型按钮的方法

    进行过 winform 开发的朋友,对 toolStrip 的工具栏肯定不陌生,但是,非常遗憾。toolStrip 不自带 CheckBox 类型的菜单。
    效果如下图:

    本篇文章将指导如何完成这个扩展工具栏项目类的写法。直接上代码,如下:
    首先,定义一个类名,叫 ToolStripCheckBox,继承基类 ToolStripItem

    public partial class ToolStripCheckBox : ToolStripItem

    然后,在类上面加上特写类写法,如下:

    [ToolStripItemDesignerAvailability(ToolStripItemDesignerAvailability.ToolStrip)]
    

    添加后,就能正常的在设计时界面中,看到自己新增的特殊扩展类的按钮了。

    接着,有三件事要做,首先,我们需要绘制一个单选框按钮,那么,需要重写绘制事件,具体代码如下:

    protected override void OnPaint(PaintEventArgs e) 
    {
    	if (base.Owner != null) 
    	{
    		Point pLocation = new Point(e.ClipRectangle.X,e.ClipRectangle.Height / 2 - (13 / 2));
    		Size txtSize = TextRenderer.MeasureText(this.Text, this.Font);
    		Rectangle rectText = new Rectangle(pLocation.X + 13, pLocation.Y, txtSize.Width, txtSize.Height);
    		CheckBoxState chkState = IsChecked ? CheckBoxState.CheckedNormal : CheckBoxState.UncheckedNormal;
    		CheckBoxRenderer.DrawCheckBox(e.Graphics, pLocation, rectText, this.Text, this.Font, false, chkState);
    	}
    }

    还要重写宽度与高度,以便适应大小,否则重绘后会出现文字超出边缘的情况,具体代码如下:

    public override Size GetPreferredSize(Size constrainingSize) 
    {
    	base.GetPreferredSize(constrainingSize);
    	Size preferredSize = base.GetPreferredSize(constrainingSize);
    	preferredSize.Width += 13;
    	return preferredSize;
    }

    那么,为了达到能够点击切换选中的效果,那么,必须要重写单击事件,并且定义一个选中属性,具体代码如下:

    public bool HasChecked
    {
        get
        {
            return IsChecked;
        }
        set
        {
            IsChecked = value;
            this.Invalidate();
        }
    }
    protected override void OnClick(EventArgs e)
    {
        IsChecked = !IsChecked;
        base.OnClick(e);
    }

    基本上根据以上方法,就在工具栏上能扩展出一个单选框按钮,当然了,这个只是一个尝试,实际应用控件的话,你要考虑的更多!

    示例下载:

  • C#窗体设计时界面修复方法整理

    很多时候,打开别人的源码,由于 vs 版本的不一致,或多或少会发生设计时窗体打不开,出现各种各样的报错。
    解决的方法,如下:

    • 一、控件或基类中的常用替换

    ComponentResourceManager manager 替换为 ComponentResourceManager resources
    manager.GetObject 替换为 resources.GetObject

    BorderStyle = BorderStyle. 替换为 BorderStyle =

    System.Windows.Forms.BorderStyle.

    base.ImeMode = ImeMode.KatakanaHalf; 替换为 base.ImeMode =

    System.Windows.Forms.ImeMode.KatakanaHalf;
    base.AutoScaleMode = AutoScaleMode.Font; 替换为 base.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
    base.FormBorderStyle = FormBorderStyle.None; 替换为 base.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;

    base.StartPosition = FormStartPosition. 替换为 base.StartPosition =
    System.Windows.Forms.FormStartPosition.

    • 二、恢复资源文件
    1. 打开 Visual Studio 命令提示
    2. resgen 具体路径原始资源.resources 具体路径修复资源.resx)