0%

winfrom 反射实现ListView 和 DataGridView 双缓冲功能

1
lv.GetType().GetProperty("DoubleBuffered", BindingFlags.Instance | BindingFlags.NonPublic).SetValue(dgv, true, null);
1
2
3
4
5
6
protected override void WndProc(ref Message m)  
{
if (m.Msg == 0x0014) // 禁掉清除背景消息
return;
base.WndProc(ref m);
}
1
2
3
4
5
this.SetStyle(ControlStyles.DoubleBuffer |     
ControlStyles.UserPaint |
ControlStyles.AllPaintingInWmPaint,
true);
this.UpdateStyles();

  1. WebClient
    下载webpage 到本地或string中
1
2
3
4
5
6
7
8
9
10
11
12
13
System.Net.WebClient client = new WebClient();
byte[] page = client.DownloadData("http://www.google.com");
string content = System.Text.Encoding.UTF8.GetString(page);
string regex = "href=[\\\"\\\'](http:\\/\\/|\\.\\/|\\/)?\\w+(\\.\\w+)*(\\/\\w+(\\.\\w+)?)*(\\/|\\?\\w*=\\w*(&\\w*=\\w*)*)?[\\\"\\\']";
Regex re = new Regex(regex);
MatchCollection matches = re.Matches(content);

System.Collections.IEnumerator enu = matches.GetEnumerator();
while (enu.MoveNext() && enu.Current != null)
{
Match match = (Match)(enu.Current);
Console.Write(match.Value + "\r\n");
}

根据正则表达式分析,上面是解析 href 的案例。


  1. Winista.Htmlparser.Net
    获取HTML 树结构
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
try
{

txtHtmlWhole.Text = "";
string url = CBUrl.SelectedItem.ToString().Trim();
System.Net.WebClient aWebClient = new System.Net.WebClient();
aWebClient.Encoding = System.Text.Encoding.Default;
string html = aWebClient.DownloadString(url);
txtHtmlWhole.Text = html;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
#endregion

#region 分析网页html节点
Lexer lexer = new Lexer(this.txtHtmlWhole.Text);
Parser parser = new Parser(lexer);
NodeList htmlNodes = parser.Parse(null);


  1. HtmlAgilityPack
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
//初始化网络请求客户端
HtmlWeb webClient = new HtmlWeb();
//初始化文档
HtmlDocument doc = webClient.Load("http://www.cnblogs.com/");
//查找节点
HtmlNodeCollection titleNodes = doc.DocumentNode.SelectNodes("//a[@class='titlelnk']");
if (titleNodes != null)
{
foreach (var item in titleNodes)
{
Console.WriteLine(item.InnerText);
}
}
Console.Read();

  1. SgmlReader
    用这个工具先将html文件转成标准的xml格式文件,再通过制定xpath路径来提取所需要的内容(xpath路径可以通过上面的那个工具生成)。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
XPathDocument pathDoc = null;
using (SgmlReader sgmlReader = new SgmlReader())
{
sgmlReader.DocType = "HTML";
sgmlReader.InputStream = new StringReader(html);
using (StringWriter stringWriter = new StringWriter())
{
using (XmlTextWriter xmlWriter = new XmlTextWriter(stringWriter))
{
while (!sgmlReader.EOF)
{
xmlWriter.WriteNode(sgmlReader, true);
}
string xml = stringWriter.ToString().Replace("xmlns=\"http://www.w3.org/1999/xhtml\"", "");
pathDoc = new XPathDocument(new StringReader(xml));
}
}
}
//提取出整个table
string xpath = "//div[@class=\"infoList\"]/table";//xpath表达式
XPathNavigator nav = pathDoc.CreateNavigator();
XPathNodeIterator nodes = nav.Select(xpath);
if (!nodes.MoveNext())
{
return;
}
nodes = nodes.Current.Select("//tr");
if (!nodes.MoveNext()) return;
string str = "";
while (nodes.MoveNext())
{
//遍历所有行
XPathNodeIterator tdNode = nodes.Current.Select("./td");
while (tdNode.MoveNext())
{
//遍历列
str += tdNode.Current.Value.Trim() + " ";
}
str += "\r\n";
}
//输出结果
Console.WriteLine(str);

1. 下载http-server

使用npm ,在终端输入

1
npm install http-server -g

2. 开启http-server服务

cmd进入目标文件夹,输入:

1
http-server -c-1 

等待开启,代码提示如下

1
2
3
4
5
Starting up http-server, serving ./
Available on:
http://127.0.0.1:8080
http://192.168.8.196:8080
Hit CTRL-C to stop the server

3. 关闭http-server服务

ctrl+c

中断显示

1
http-server stopped.

即服务关闭

  1. Markdig

代码中使用如下:

1
2
3
//str:markdown文本
var pipeline = new MarkdownPipelineBuilder().UseAdvancedExtensions().Build();
result = Markdown.ToHtml(str, pipeline);
  1. MarkDownSharp

代码如下

1
2
3
4
5
6
7
8
//content:markdown文本 writer:Html结果
using (var reader = new StringReader(content))
{
using (var writer = new StringWriter())
{
CommonMark.CommonMarkConverter.Convert(reader, writer);
}
}

原生winfrom的ComboBox 样式设置为DropDownList 样式固定,且大多数重绘多为重绘下拉选择框,此代码主要重绘ComboBox的边框 背景色等。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
/// <summary>
/// 主要为DropDownList样式重绘(特定性较强)
/// </summary>
public partial class EVCombobox : ComboBox
{
public EVCombobox()
{
InitializeComponent();
}
[DllImport("User32.dll")]
public static extern int ReleaseDC(IntPtr hWnd, IntPtr hDC);

public Color BoardColor { get; set; } = Color.White;

protected override void WndProc(ref Message m)
{
base.WndProc(ref m);
//WM_PAINT = 0xf; 要求一个窗口重画自己,即Paint事件时
//WM_CTLCOLOREDIT = 0x133;当一个编辑型控件将要被绘制时发送此消息给它的父窗口;
//通过响应这条消息,所有者窗口可以通过使用给定的相关显示设备的句柄来设置编辑框的文本和背景颜色
//windows消息值表,可参考:
if (m.Msg == 0xf || m.Msg == 0x133)
{
IntPtr hDC = Win32API.GetWindowDC(m.HWnd);
if (hDC.ToInt32() == 0) //如果取设备上下文失败则返回
{
return;
}

//建立Graphics对像
Graphics g = Graphics.FromHdc(hDC);
g.FillRectangle(new SolidBrush(BackColor), new Rectangle(0, 0, Width, Height));
//画边框的
//ControlPaint.DrawBorder(g, new Rectangle(0, 0, Width, Height), BoardColor, ButtonBorderStyle.Solid);
//画坚线
//ControlPaint.DrawBorder(g, new Rectangle(Width - Height, 0, Height, Height), Color.Red, ButtonBorderStyle.Solid);

Point pA = new Point(Width - 20, Height / 2 - 3);
Point pB = pA + new Size(6, 6);
Point pC = pA + new Size(12, 0);
g.DrawLine(new Pen(Color.White,2), pA, pB);
g.DrawLine(new Pen(Color.White,2), pB, pC);

if (this.SelectedIndex > -1)
{
string text = SelectedItem.ToString();
Size strSize = Size.Ceiling(g.MeasureString(text, this.Font));
g.DrawString(text, Font, new SolidBrush(ForeColor), 5, (Height - strSize.Height) / 2);
}

//g.DrawLine(new Pen(Brushes.Blue, 2), new PointF(this.Width - this.Height, 0), new PointF(this.Width - this.Height, this.Height));
//释放DC
ReleaseDC(m.HWnd, hDC);
}
}
}

Hello world

这是第一篇文章