winform相关

winform高分辨率缩放

  1. this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Dpi

winform在管理员运行时Drag

FileDropHandler.cs

  1. using System;
  2. using System.ComponentModel;
  3. using System.Runtime.InteropServices;
  4. using System.Text;
  5. using System.Windows.Forms;
  6. namespace PicCommon
  7. {
  8. public sealed class FileDropHandler : IMessageFilter, IDisposable
  9. {
  10. #region native members
  11. [DllImport("user32.dll", SetLastError = true, CallingConvention = CallingConvention.Winapi)]
  12. [return: MarshalAs(UnmanagedType.Bool)]
  13. private static extern bool ChangeWindowMessageFilterEx(IntPtr hWnd, uint message, ChangeFilterAction action, in ChangeFilterStruct pChangeFilterStruct);
  14. [DllImport("shell32.dll", SetLastError = false, CallingConvention = CallingConvention.Winapi)]
  15. private static extern void DragAcceptFiles(IntPtr hWnd, bool fAccept);
  16. [DllImport("shell32.dll", SetLastError = false, CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Winapi)]
  17. private static extern uint DragQueryFile(IntPtr hWnd, uint iFile, StringBuilder lpszFile, int cch);
  18. [DllImport("shell32.dll", SetLastError = false, CallingConvention = CallingConvention.Winapi)]
  19. private static extern void DragFinish(IntPtr hDrop);
  20. [StructLayout(LayoutKind.Sequential)]
  21. private struct ChangeFilterStruct
  22. {
  23. public uint CbSize;
  24. public ChangeFilterStatu ExtStatus;
  25. }
  26. private enum ChangeFilterAction : uint
  27. {
  28. MSGFLT_RESET,
  29. MSGFLT_ALLOW,
  30. MSGFLT_DISALLOW
  31. }
  32. private enum ChangeFilterStatu : uint
  33. {
  34. MSGFLTINFO_NONE,
  35. MSGFLTINFO_ALREADYALLOWED_FORWND,
  36. MSGFLTINFO_ALREADYDISALLOWED_FORWND,
  37. MSGFLTINFO_ALLOWED_HIGHER
  38. }
  39. private const uint WM_COPYGLOBALDATA = 0x0049;
  40. private const uint WM_COPYDATA = 0x004A;
  41. private const uint WM_DROPFILES = 0x0233;
  42. #endregion
  43. private const uint GetIndexCount = 0xFFFFFFFFU;
  44. private Control _ContainerControl;
  45. private readonly bool _DisposeControl;
  46. public Control ContainerControl { get; }
  47. public FileDropHandler(Control containerControl) : this(containerControl, false) { }
  48. public FileDropHandler(Control containerControl, bool releaseControl)
  49. {
  50. _ContainerControl = containerControl ?? throw new ArgumentNullException("control", "control is null.");
  51. if (containerControl.IsDisposed) throw new ObjectDisposedException("control");
  52. _DisposeControl = releaseControl;
  53. var status = new ChangeFilterStruct() { CbSize = 8 };
  54. if (!ChangeWindowMessageFilterEx(containerControl.Handle, WM_DROPFILES, ChangeFilterAction.MSGFLT_ALLOW, in status)) throw new Win32Exception(Marshal.GetLastWin32Error());
  55. if (!ChangeWindowMessageFilterEx(containerControl.Handle, WM_COPYGLOBALDATA, ChangeFilterAction.MSGFLT_ALLOW, in status)) throw new Win32Exception(Marshal.GetLastWin32Error());
  56. if (!ChangeWindowMessageFilterEx(containerControl.Handle, WM_COPYDATA, ChangeFilterAction.MSGFLT_ALLOW, in status)) throw new Win32Exception(Marshal.GetLastWin32Error());
  57. DragAcceptFiles(containerControl.Handle, true);
  58. Application.AddMessageFilter(this);
  59. }
  60. public bool PreFilterMessage(ref Message m)
  61. {
  62. if (_ContainerControl == null || _ContainerControl.IsDisposed) return false;
  63. if (_ContainerControl.AllowDrop) return _ContainerControl.AllowDrop = false;
  64. if (m.Msg == WM_DROPFILES)
  65. {
  66. var handle = m.WParam;
  67. var fileCount = DragQueryFile(handle, GetIndexCount, null, 0);
  68. var fileNames = new string[fileCount];
  69. var sb = new StringBuilder(262);
  70. var charLength = sb.Capacity;
  71. for (uint i = 0; i < fileCount; i++)
  72. {
  73. if (DragQueryFile(handle, i, sb, charLength) > 0) fileNames[i] = sb.ToString();
  74. }
  75. DragFinish(handle);
  76. _ContainerControl.AllowDrop = true;
  77. _ContainerControl.DoDragDrop(fileNames, DragDropEffects.All);
  78. _ContainerControl.AllowDrop = false;
  79. return true;
  80. }
  81. return false;
  82. }
  83. public void Dispose()
  84. {
  85. if (_ContainerControl == null)
  86. {
  87. if (_DisposeControl && !_ContainerControl.IsDisposed) _ContainerControl.Dispose();
  88. Application.RemoveMessageFilter(this);
  89. _ContainerControl = null;
  90. }
  91. }
  92. }
  93. }

winform中使用.cs

  1. private FileDropHandler fileDrop;
  2. private void MainForm_DragEnter(object sender, DragEventArgs e)
  3. {
  4. var paths = e.Data.GetData(typeof(string[])) as string[];
  5. GetFileDetail(paths[0].ToString());
  6. }
  7. private void MainForm_Load(object sender, EventArgs e)
  8. {
  9. PicDetail.RowHeadersVisible = false;
  10. fileDrop = new FileDropHandler(this); //初始化
  11. }

DataGridView

隐藏行第一列空白
dataGridView1.RowHeadersVisible = false;
属性中,设置标题不换行居中。
DataGridViewCellStyle

控制台启动Winform

有时候想在控制台输出log,所以可以右键点击winform,选择属性
在 应用程序中的输出类型选择 控制台应用程序 就可以了。
image.png

winform工具箱

主窗体.cs

  1. using System;
  2. using System.Drawing;
  3. using System.Windows.Forms;
  4. using GadgetCollectionFactory;
  5. using GadgetCollectionOperation;
  6. using System.Configuration;
  7. namespace GadgetCollection
  8. {
  9. public partial class Form1 : Form
  10. {
  11. public Form1()
  12. {
  13. InitializeComponent();
  14. }
  15. private void Form1_Load(object sender, EventArgs e)
  16. {
  17. //读取配置文件
  18. //string[] config = File.ReadAllLines("Config.txt", Encoding.UTF8);
  19. string[] config = ConfigurationManager.AppSettings.AllKeys;
  20. //遍历配置文件,有多tool就生成多少按钮。
  21. foreach (string item in config)
  22. {
  23. //有几条数据,就创建几个按钮
  24. Button btn = new Button();
  25. btn.Size = new Size(197, 42);
  26. btn.BackColor = Color.AliceBlue;
  27. btn.Text = ConfigurationManager.AppSettings.Get(item);
  28. //flowLayoutPanel1.AutoScroll = true;
  29. flowLayoutPanel1.Controls.Add(btn);
  30. btn.Click += button1_Click;
  31. }
  32. }
  33. private void button1_Click(object sender, EventArgs e)
  34. {
  35. Button btn = sender as Button;
  36. //获得简单工厂提供的父类对象
  37. OperateTools oper = Factory.GetOperation(btn.Text);
  38. //判断对象是否为空
  39. if (oper == null)
  40. {
  41. return;
  42. }
  43. //执行方法。
  44. oper.OperateSelectedTool();
  45. }
  46. /// <summary>
  47. /// 搜索按钮点击的时候
  48. /// </summary>
  49. /// <param name="sender"></param>
  50. /// <param name="e"></param>
  51. private void BtnSearch_Click(object sender, EventArgs e)
  52. {
  53. if (string.IsNullOrEmpty(TxtSearch.Text))
  54. {
  55. MessageBox.Show("请输入查询内容", "错误提醒", MessageBoxButtons.OK, MessageBoxIcon.Error);
  56. return;
  57. }
  58. //把flowLayoutPanel1的Control对象集合传递进去
  59. FindControl(flowLayoutPanel1.Controls);
  60. }
  61. /// <summary>
  62. /// 把flowLayoutPanel1的Control对象集合传进去
  63. /// </summary>
  64. /// <param name="controlCollection">Control对象集合Control.ControlCollection</param>
  65. void FindControl(Control.ControlCollection controlCollection)
  66. {
  67. //遍历控件集合
  68. foreach (Control control in controlCollection)
  69. {
  70. //如果控件是按钮
  71. if (control is Button)
  72. {
  73. //强转
  74. Button btn = (Button)control;
  75. //比较搜索的字符和按钮字符是否相同
  76. if (btn.Text.ToLower().Contains(TxtSearch.Text.ToLower()))
  77. {
  78. //相同就变颜色和聚焦
  79. btn.BackColor = Color.Coral;
  80. btn.Focus();//聚焦
  81. }
  82. }
  83. }
  84. }
  85. /// <summary>
  86. /// 点击搜索输入框的时候重置颜色
  87. /// </summary>
  88. /// <param name="sender"></param>
  89. /// <param name="e"></param>
  90. private void TxtSearch_MouseClick(object sender, MouseEventArgs e)
  91. {
  92. TxtSearch.Text = "";
  93. foreach (System.Windows.Forms.Control control in flowLayoutPanel1.Controls)
  94. {
  95. if (control is System.Windows.Forms.Button)
  96. {
  97. System.Windows.Forms.Button btn = (System.Windows.Forms.Button)control;
  98. btn.BackColor = Color.AliceBlue;
  99. }
  100. }
  101. }
  102. }
  103. }

工厂类.cs

using System;

using System.IO;

using System.Reflection;

using GadgetCollectionOperation;

namespace GadgetCollectionFactory
{
public class Factory
 {
public static OperateTools GetOperation(string SelectedToolName)
 {
 OperateTools toolOperator = null;

//动态的获得程序集
string dllPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "plugins");

//获得dllPath路径下所有程序集文件的全路径
string[] filePath = Directory.GetFiles(dllPath);

//遍历filePath 加载所有的程序集文件
foreach (string item in filePath)

 {
 Assembly ass = Assembly.LoadFile(item);
//获得程序集中所有的公开数据类型
 Type[] types = ass.GetExportedTypes();
foreach (Type tt in types)

 {
//判断当前数据类型是不是自己需要的 是子类吗 是抽象类吗 
if (tt.IsSubclassOf(typeof(OperateTools)) && !tt.IsAbstract)

 {
//创建子类对象,赋值给oper
object o = Activator.CreateInstance(tt);

 toolOperator = (OperateTools)o;
//判断传进来的类型 如果是的话 就返回
if (SelectedToolName == toolOperator.toolName)

 {
return toolOperator;

 }
else//不是就break
 {
 toolOperator = null;

break;

 }
 }
 }
 }
return null;

 }
 }
}

抽象父类.cs

namespace GadgetCollectionOperation
{
public abstract class OperateTools
 {
//小工具的名称
public abstract string toolName { get; }

//开启选中的工具
public abstract void OperateSelectedTool();

 }
}

工具类示例.cs

using System;

using System.Diagnostics;

using GadgetCollectionOperation;

namespace ToolW3SchoolAPI
{
public class W3SchoolAPI : OperateTools
 {
public override string toolName

 {
get { return "W3SchoolAPI"; }//修改toolName
 }
public override void OperateSelectedTool()
 {
//修改启动文件的路径以及名称,在ToolFile文件夹内新建文件夹,然后把路径添加
 Process proc = Process.Start(AppDomain.CurrentDomain.BaseDirectory + "ToolFile/w3schoo.CHM");

 }
 }
}

应用程序图标设置

右键点击源winform程序。点击属性。
image.png
在这里可以修改默认的图标。

切换Debug&Release模式

右键点击解决方案,选择配置管理器。
image.png
在其中可以切换两种模式。
image.png

JavaScript

相关文档

JavaScript基础

简介

是弱类型语言 :不需要指定变量的类型
大小写敏感
分号结尾
注释(单行// 多行 / /)
字符串推荐使用单引号,也可以使用双引号
双等号==判断值是否相等
三等号===先判断类型是否相同,再判断值是否相等
JavaScript不支持方法重载
后声明的函数会覆盖先声明的函数。

可变参数

//可变参数
function sum(a) {
//使用arguments获取所有参数,是一个数组
//alert(arguments.length);//返回数组的元素个数
var sum1 = 0;
for (var i = 0; i < arguments.length; i++) {
                sum1 += arguments[i];
            }
            alert(sum1);
        }
//调用
        sum(1, 2, 3,4,5,6);

数据类型

boolean布尔
number数字
string字符串
Undefined未定义
Null空对象
Object对象类型
Undefined类型、Null类型都是只有一个值的数据类型,值分别为undefined与null
查看变量的类型typeof 变量名或typeof(变量名),返回变量类型的字符串
类型转换:类型名(变量名)或parse*()

匿名函数

匿名函数
不需要定义方法名,直接写参数列表与方法体
方式一:function (参数){...return ...};
可以将方法赋值给一个变量,这个变量表示一个方法
方式二:(function(n1,n2){...})(1,2);
定义的同时完成调用
方式三:new Function("n1","n2","return n1+n2");
赋值给一个变量完成调用
最常使用:方式一
//典型应用:为事件绑定处理函数,传递回调函数
//根据id获取页面元素,为它绑定单击事件
            document.getElementById('btnShow').onclick = function() {
                alert(123);
            };

闭包

<script>
//定义一个函数show
function show(name) {
//返回一个函数
return function () {
//输出name的值
                alert(name);
            };
        }
//运行show函数,将返回值赋给f1
//因为返回值是函数,所以f1现在指向一个函数
var f1 = show('a');
//通过f1可以调用匿名函数执行
        f1();
//闭包:支持在函数内部调用函数之前声明过的变量
//作用域链:变量的作用域在当前函数中,及当前函数内部定义的函数中,形成了一个链条
//建议:避免闭包,每次在用一个变量时,都要先声明再使用
</script>

<script>
//为类增加成员
function Person(name) {

this.Name = name;//在类内部声明一个属性Name,初始化为name值
this.say = function () {

 alert(this.Name);

 };
 }
//使用构造方法创建对象
var p1 = new Person('zhh');

 p1.say();//调用 方法
//alert(typeof (p1));//object
//alert(typeof (Person('zhh')));//因为Person作为函数时,没有返回值,所以是undefined
//alert(typeof(Person));//Person就是一个函数,类型是function
</script>
//定义类的第二种方法
<script>
function Person() {

 }
//为对象增加成员
//var p1 = new Person();
//p1.Name = "zhh";//为对象p1新建了一个属性Name
//p1.say = function() {
// alert(p1.Name);
//};
//p1.say();
//新建出来的对象,是没有Name属性的,因为类Person中没有Name属性
//var p2 = new Person();
//alert(p2.Name);
</script>

原型

原型:就是对象的类型
<script>
function Person() {
this.Age = 100;
        }
var p1 = new Person();
//p1.Title = 'abc';
//访问原型
        p1.__proto__.Title = 'abc';//为原型注释数据成员
//Person.prototype.Title = 'abc';//功能同上面的代码
var p2 = new Person();
        alert(p2.Title);
</script>

数组

<script>
//使用[]定义数组
var array1 = [123, 'abc'];
        array1[0];
//键值对{键:值,...}
var array2 = {            
name: 'zhh',
age: 18,
gender:'你猜'
        };
//alert(array2['name']);//将array2认为是集合,通过键访问值
//alert(array2.name);//将array2认为是json,通过属性访问值
//定义json数组
var temp = [{
title: 'zhh',
age:18
        }, {
title: '牛头',
age:20
        }, {
title: '马面',
age:22
        }];
//输出每个对象的title值
for (var index in temp) {//temp是数组,所以index是索引
            alert(temp[index].title);
        }
</script>

HTML-DOM

HTML DOM 方法

方法 描述
getElementById() 返回带有指定 ID 的元素。
getElementsByTagName() 返回包含带有指定标签名称的所有元素的节点列表(集合/节点数组)。
getElementsByClassName() 返回包含带有指定类名的所有元素的节点列表。
appendChild() 把新的子节点添加到指定节点。
removeChild() 删除子节点。
replaceChild() 替换子节点。
insertBefore() 在指定的子节点前面插入新的子节点。
createAttribute() 创建属性节点。
createElement() 创建元素节点。
createTextNode() 创建文本节点。
getAttribute() 返回指定的属性值。
setAttribute() 把指定属性设置或修改为指定的值。

C#小知识

拆分数组

使用linq。

firstArray = array.Take(array.Length / 2).ToArray();
secondArray = array.Skip(array.Length / 2).ToArray();