0%

VB.NET 编译独立dll供exe使用
dll 代码

1
2
3
4
5
Public Class Class1
Public Shared Function sum(ByVal a, ByVal b)
Return a + b
End Function
End Class

exe 代码(记得引用编译好的dll文件)

1
2
3
4
5
6
Public Class Form1

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
MsgBox(ClassLibrary1.Class1.sum(122, 555))
End Sub
End Class

如何在 .NET 程序中解析 JSON 使用 Newtonsoft.Json

第三方类来操纵 Newtonsoft.Json 是.NET 下开源的json格式序列号和反序列化的类库

官方网站: http://json.codeplex.com/

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
Imports System.Text.RegularExpressions
Imports Newtonsoft.Json
Imports Newtonsoft.Json.Linq
''' <summary>
''' json序列化
''' </summary>
Public Class json
''' <summary>
''' 解析JSON数据
''' </summary>
''' <param name="empData">源数据</param>
''' <param name="key">指定值下的数据,可省略。</param>
''' <returns>0 非 json 数据</returns>
Public Shared Function JsonDecode(ByVal empData, Optional ByVal key1 = Nothing, Optional ByVal key2 = Nothing, Optional ByVal arry_i = Nothing) As String
Try
If IsJson(empData) = False Then Return 0

If arry_i IsNot Nothing Then
'http://blog.csdn.net/goodelephant/article/details/24769421
Return CType(JsonConvert.DeserializeObject(empData), JObject)(key1)(arry_i)(key2).ToString() '这个是解析数组 下标。。
End If


If key1 = Nothing Then Return CType(JsonConvert.DeserializeObject(empData), JObject).ToString

If key2 = Nothing Then Return CType(JsonConvert.DeserializeObject(empData), JObject)(key1).ToString()

Return CType(JsonConvert.DeserializeObject(empData), JObject)(key1)(key2).ToString()


Catch ex As Exception
Return ex.Message
End Try
End Function
End Class
阅读全文 »

  • 来源:www.onmpw.com
  • 希尔排序是按照该算法的设计者的名字希尔 命名的,其产生是希尔在插入排序的基础上改进的,可以说是一种特殊的插入排序。
    下面先介绍一下插入排序的性质:
    首先,插入排序算法对于已经有序的数据进行操作的时候,效率很高,可以达到线性排序的效率。
    其次,插入排序进行排序的时候,每一趟排序只能移动一个数据。所以说这样的排序方法相对来说效率又比较低。
    基于此性质,希尔排序的设计者发明了希尔排序算法,其基本思想是:
    先将整个待排序的记录序列分割成为若干子序列分别进行直接插入排序,分割子序列的方法就是设定一个增量,待当下的每个子序列有序的时候,将增量减一半(除以2,取整),再次进行子序列的排序。
    依次进行,待整个序列中的记录基本上有序的时候,再对全体记录进行依次直接插入排序,此时增量减为1,因为直接插入排序在元素基本有序的情况下(根据上述第一点,接近最好的情况),效率是很高的。

    阅读全文 »

    自定义鼠标光标的方法

    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
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    using System;
    using System.Drawing;
    using System.Collections;
    using System.ComponentModel;
    using System.Windows.Forms;
    using System.Data;
    using System.Runtime.InteropServices;

    namespace Example027_显示动画光标
    {
    /// <summary>
    /// Form1 的摘要说明。
    /// </summary>
    public class Form1 : System.Windows.Forms.Form
    {
    /// <summary>
    /// 必需的设计器变量。
    /// </summary>
    private System.ComponentModel.Container components = null;

    public Form1()
    {
    //
    // Windows 窗体设计器支持所必需的
    //
    InitializeComponent();

    //
    // TODO: 在 InitializeComponent 调用后添加任何构造函数代码
    //
    }

    /// <summary>
    /// 清理所有正在使用的资源。
    /// </summary>
    protected override void Dispose(bool disposing)
    {
    if (disposing)
    {
    if (components != null)
    {
    components.Dispose();
    }
    }
    base.Dispose(disposing);
    }

    #region Windows Form Designer generated code
    /// <summary>
    /// 设计器支持所需的方法 - 不要使用代码编辑器修改
    /// 此方法的内容。
    /// </summary>
    private void InitializeComponent()
    {
    //
    // Form1
    //
    this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
    this.ClientSize = new System.Drawing.Size(292, 273);
    this.Name = "Form1";
    this.Text = "Form1";
    this.Load += new System.EventHandler(this.Form1_Load);

    }
    #endregion

    /// <summary>
    /// 应用程序的主入口点。
    /// </summary>
    [STAThread]
    static void Main()
    {
    Application.Run(new Form1());
    }
    [DllImport("user32")]
    private static extern IntPtr SetCursor(IntPtr hCursor);
    [DllImport("user32")]
    private static extern IntPtr LoadCursorFromFile(string lpFileName);
    const int WM_SETCURSOR = 0x0020;

    private void Form1_Load(object sender, System.EventArgs e)
    {
    IntPtr hCursor;
    hCursor = LoadCursorFromFile("..\\..\\pic.ani");
    SetCursor(hCursor);
    }

    protected override void WndProc(ref System.Windows.Forms.Message m)
    {
    switch (m.Msg)
    {
    case WM_SETCURSOR:
    IntPtr hCursor;
    hCursor = LoadCursorFromFile("..\\..\\pic.ani");
    SetCursor(hCursor);
    break;
    default:
    base.WndProc(ref m);
    break;
    }
    }
    }
    }

    QQ图片20160706085555

    在控件获取焦点的情况下模拟按键操作

    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
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    using System;
    using System.Drawing;
    using System.Collections;
    using System.ComponentModel;
    using System.Windows.Forms;
    using System.Data;

    namespace Example108_模拟键盘操作
    {
    /// <summary>
    /// Form1 的摘要说明。
    /// </summary>
    public class Form1 : System.Windows.Forms.Form
    {
    private System.Windows.Forms.RichTextBox richTextBox1;
    private System.Windows.Forms.Button button1;
    /// <summary>
    /// 必需的设计器变量。
    /// </summary>
    private System.ComponentModel.Container components = null;

    public Form1()
    {
    //
    // Windows 窗体设计器支持所必需的
    //
    InitializeComponent();

    //
    // TODO: 在 InitializeComponent 调用后添加任何构造函数代码
    //
    }

    /// <summary>
    /// 清理所有正在使用的资源。
    /// </summary>
    protected override void Dispose(bool disposing)
    {
    if (disposing)
    {
    if (components != null)
    {
    components.Dispose();
    }
    }
    base.Dispose(disposing);
    }

    #region Windows Form Designer generated code
    /// <summary>
    /// 设计器支持所需的方法 - 不要使用代码编辑器修改
    /// 此方法的内容。
    /// </summary>
    private void InitializeComponent()
    {
    this.richTextBox1 = new System.Windows.Forms.RichTextBox();
    this.button1 = new System.Windows.Forms.Button();
    this.SuspendLayout();
    //
    // richTextBox1
    //
    this.richTextBox1.Location = new System.Drawing.Point(8, 8);
    this.richTextBox1.Name = "richTextBox1";
    this.richTextBox1.Size = new System.Drawing.Size(248, 160);
    this.richTextBox1.TabIndex = 0;
    this.richTextBox1.Text = "richTextBox1";
    //
    // button1
    //
    this.button1.Location = new System.Drawing.Point(184, 176);
    this.button1.Name = "button1";
    this.button1.TabIndex = 1;
    this.button1.Text = "Key";
    this.button1.Click += new System.EventHandler(this.button1_Click);
    //
    // Form1
    //
    this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
    this.ClientSize = new System.Drawing.Size(264, 205);
    this.Controls.AddRange(new System.Windows.Forms.Control[] {
    this.button1,
    this.richTextBox1});
    this.Name = "Form1";
    this.Text = "Form1";
    this.ResumeLayout(false);

    }
    #endregion

    /// <summary>
    /// 应用程序的主入口点。
    /// </summary>
    [STAThread]
    static void Main()
    {
    Application.Run(new Form1());
    }

    private void button1_Click(object sender, System.EventArgs e)
    {
    this.richTextBox1.Focus();
    SendKeys.Send("^a");
    SendKeys.Send("{DEL}");
    SendKeys.Send("+(ec)");
    SendKeys.Send("{ENTER}");
    SendKeys.Send("+ec");
    SendKeys.Send("{ENTER}");
    SendKeys.Send("{h 10}");
    SendKeys.Send("{ENTER}");
    }

    }
    }

    C# 模拟按键操作

    使用 CreateMutex 互斥解决单实例运行

    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
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    Using System;
    Using System.Drawing;
    Using System.Collections;
    Using System.ComponentModel;
    Using System.Windows.Forms;
    Using System.Data;
    Using System.Threading;
    Using System.Runtime.InteropServices;

    Namespace Example104_使程序只能够运行一个
    {
    /// <summary>
    /// Form1 的摘要说明。
    /// </summary>
    Public Class Form1 : System.Windows.Forms.Form
    {
    /// <summary>
    /// 必需的设计器变量。
    /// </summary>
    Private System.ComponentModel.Container components = null;

    Public Form1()
    {
    //
    // Windows 窗体设计器支持所必需的
    //
    InitializeComponent();

    //
    // TODO: 在 InitializeComponent 调用后添加任何构造函数代码
    //
    }

    /// <summary>
    /// 清理所有正在使用的资源。
    /// </summary>
    Protected override void Dispose(bool disposing)
    {
    If(disposing)
    {
    If(components! = null)
    {
    components.Dispose();
    }
    }
    base.Dispose(disposing);
    }

    #region Windows Form Designer generated code
    /// <summary>
    /// 设计器支持所需的方法 - 不要使用代码编辑器修改
    /// 此方法的内容。
    /// </summary>
    Private void InitializeComponent()
    {
    //
    // Form1
    //
    this.AutoScaleBaseSize = New System.Drawing.Size(6, 14);
    this.ClientSize = New System.Drawing.Size(272, 189);
    this.Name = "Form1";
    this.Text = "Form1";

    }
    #endregion

    /// <summary>
    /// 应用程序的主入口点。
    /// </summary>

    [StructLayout(LayoutKind.Sequential)]
    Public Class SECURITY_ATTRIBUTES
    {
    Public int nLength;
    Public int lpSecurityDescriptor;
    Public int bInheritHandle;
    }

    [System.Runtime.InteropServices.DllImport("kernel32")]
    Private Static extern int GetLastError();
    [System.Runtime.InteropServices.DllImport("kernel32")]
    Private Static extern IntPtr CreateMutex(SECURITY_ATTRIBUTES lpMutexAttributes, bool bInitialOwner, String lpName);
    [System.Runtime.InteropServices.DllImport("kernel32")]
    Private Static extern int ReleaseMutex(IntPtr hMutex);
    Const int ERROR_ALREADY_EXISTS = 0183;

    [STAThread]
    Static void Main()
    {
    IntPtr hMutex;
    hMutex = CreateMutex(null, false, "test");
    If(GetLastError() != ERROR_ALREADY_EXISTS)
    {
    Application.Run(New Form1());
    }
    Else
    {
    MessageBox.Show("本程序只允许同时运行一个");
    ReleaseMutex(hMutex);
    }
    }
    }
    }

    读写ini文件的方法

    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
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    Using System;
    Using System.Drawing;
    Using System.Collections;
    Using System.ComponentModel;
    Using System.Windows.Forms;
    Using System.Data;
    Using System.Runtime.InteropServices;
    Using System.IO;
    Using System.Text;

    Namespace Example084_读写ini文件
    {
    /// <summary>
    /// Form1 的摘要说明。
    /// </summary>
    Public Class Form1 : System.Windows.Forms.Form
    {
    /// <summary>
    /// 必需的设计器变量。
    /// </summary>
    Private System.ComponentModel.Container components = null;

    Public Form1()
    {
    //
    // Windows 窗体设计器支持所必需的
    //
    InitializeComponent();

    //
    // TODO: 在 InitializeComponent 调用后添加任何构造函数代码
    //
    }

    /// <summary>
    /// 清理所有正在使用的资源。
    /// </summary>
    Protected override void Dispose( bool disposing )
    {
    If (disposing)
    {
    If (components! = null)
    {
    components.Dispose();
    }
    }
    base.Dispose( disposing );
    }

    #region Windows Form Designer generated code
    /// <summary>
    /// 设计器支持所需的方法 - 不要使用代码编辑器修改
    /// 此方法的内容。
    /// </summary>
    Private void InitializeComponent()
    {
    //
    // Form1
    //
    this.AutoScaleBaseSize = New System.Drawing.Size(6, 14);
    this.ClientSize = New System.Drawing.Size(292, 273);
    this.Name = "Form1";
    this.Text = "Form1";
    this.Closing += New System.ComponentModel.CancelEventHandler(this.Form1_Closing);
    this.Load += New System.EventHandler(this.Form1_Load);

    }
    #endregion

    /// <summary>
    /// 应用程序的主入口点。
    /// </summary>
    [STAThread]
    Static void Main()
    {
    Application.Run(New Form1());
    }

    [DllImport("kernel32")]
    Private Static extern int GetPrivateProfileInt(String lpApplicationName,String lpKeyName,int nDefault,String lpFileName);
    [DllImport("kernel32")]
    Private Static extern bool GetPrivateProfileString(String lpApplicationName,String lpKeyName,String lpDefault,StringBuilder lpReturnedString,int nSize,String lpFileName);
    [DllImport("kernel32")]
    Private Static extern bool WritePrivateProfileString(String lpApplicationName,String lpKeyName,String lpString,String lpFileName);
    [DllImport("kernel32")]
    Private Static extern bool GetPrivateProfileSection(String lpAppName,String lpReturnedString,int nSize,String lpFileName);
    [DllImport("kernel32")]
    Private Static extern bool WritePrivateProfileSection(String lpAppName,String lpString,String lpFileName);

    Public Const int MAX_PATH = 256;
    Public Const String FILE_NAME=".\\test.ini";

    Private void Form1_Load(Object sender, System.EventArgs e)
    {
    If (File.Exists(FILE_NAME))
    {
    StringBuilder strCaption = New StringBuilder(256);
    GetPrivateProfileString("Form","Caption","Default Caption",strCaption,strCaption.Capacity,FILE_NAME);
    this.Text=strCaption.ToString();
    int myWidth = GetPrivateProfileInt("Form", "Width", this.Width, FILE_NAME);
    this.Width=myWidth;
    int myHeight = GetPrivateProfileInt("Form", "Height", this.Height, FILE_NAME);
    this.Height=myHeight;
    int myLeft = GetPrivateProfileInt("Form", "Left", this.Left, FILE_NAME);
    this.Left=myLeft;
    int myTop = GetPrivateProfileInt("Form", "Top", this.Top, FILE_NAME);
    this.Top=myTop;
    }
    }

    Private void Form1_Closing(Object sender, System.ComponentModel.CancelEventArgs e)
    {
    String strCaption = this.Text;
    WritePrivateProfileString("Form","Caption",strCaption,FILE_NAME);
    WritePrivateProfileString("Form","Width",this.Width.ToString(),FILE_NAME);
    WritePrivateProfileString("Form","Height",this.Height.ToString(),FILE_NAME);
    WritePrivateProfileString("Form","Left",this.Left.ToString(),FILE_NAME);
    WritePrivateProfileString("Form","Top",this.Top.ToString(),FILE_NAME);
    }
    }
    }

    C# 读写ini

    模拟鼠标进行操作

    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
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    using System;
    using System.Drawing;
    using System.Collections;
    using System.ComponentModel;
    using System.Windows.Forms;
    using System.Data;

    namespace Example107_模拟鼠标
    {
    /// <summary>
    /// Form1 的摘要说明。
    /// </summary>
    public class Form1 : System.Windows.Forms.Form
    {
    /// <summary>
    /// 必需的设计器变量。
    /// </summary>
    private System.ComponentModel.Container components = null;

    public Form1()
    {
    //
    // Windows 窗体设计器支持所必需的
    //
    InitializeComponent();

    //
    // TODO: 在 InitializeComponent 调用后添加任何构造函数代码
    //
    }

    /// <summary>
    /// 清理所有正在使用的资源。
    /// </summary>
    protected override void Dispose(bool disposing)
    {
    if (disposing)
    {
    if (components != null)
    {
    components.Dispose();
    }
    }
    base.Dispose(disposing);
    }

    #region Windows Form Designer generated code
    /// <summary>
    /// 设计器支持所需的方法 - 不要使用代码编辑器修改
    /// 此方法的内容。
    /// </summary>
    private void InitializeComponent()
    {
    this.button1 = new System.Windows.Forms.Button();
    this.SuspendLayout();
    //
    // button1
    //
    this.button1.Location = new System.Drawing.Point(144, 176);
    this.button1.Name = "button1";
    this.button1.TabIndex = 0;
    this.button1.Text = "Mouse";
    this.button1.Click += new System.EventHandler(this.button1_Click);
    //
    // Form1
    //
    this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
    this.ClientSize = new System.Drawing.Size(280, 237);
    this.Controls.AddRange(new System.Windows.Forms.Control[] {
    this.button1});
    this.Name = "Form1";
    this.Text = "Form1";
    this.DoubleClick += new System.EventHandler(this.Form1_DoubleClick);
    this.ResumeLayout(false);

    }
    #endregion

    /// <summary>
    /// 应用程序的主入口点。
    /// </summary>
    [STAThread]
    static void Main()
    {
    Application.Run(new Form1());
    }

    private System.Windows.Forms.Button button1;

    [System.Runtime.InteropServices.DllImport("user32")]
    private static extern int mouse_event(int dwFlags, int dx, int dy, int cButtons, int dwExtraInfo);
    const int MOUSEEVENTF_MOVE = 0x0001;
    const int MOUSEEVENTF_LEFTDOWN = 0x0002;
    const int MOUSEEVENTF_LEFTUP = 0x0004;
    const int MOUSEEVENTF_RIGHTDOWN = 0x0008;
    const int MOUSEEVENTF_RIGHTUP = 0x0010;
    const int MOUSEEVENTF_MIDDLEDOWN = 0x0020;
    const int MOUSEEVENTF_MIDDLEUP = 0x0040;
    const int MOUSEEVENTF_ABSOLUTE = 0x8000;

    private void Form1_DoubleClick(object sender, System.EventArgs e)
    {
    MessageBox.Show("Double Click");
    }

    private void button1_Click(object sender, System.EventArgs e)
    {
    mouse_event(MOUSEEVENTF_MOVE, -10, -10, 0, 0);
    mouse_event(MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0);
    mouse_event(MOUSEEVENTF_LEFTUP, 0, 0, 0, 0);
    mouse_event(MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0);
    mouse_event(MOUSEEVENTF_LEFTUP, 0, 0, 0, 0);
    }
    }
    }

    QQ图片20160704165622

  • 来源:明日科技
  • 显示空心字的实现方法

    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
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    using System;
    using System.Drawing;
    using System.Collections;
    using System.ComponentModel;
    using System.Windows.Forms;
    using System.Data;

    namespace Example040_空心字体效果演示
    {
    /// <summary>
    /// Form1 的摘要说明。
    /// </summary>
    public class Form1 : System.Windows.Forms.Form
    {
    private System.Windows.Forms.Button button1;
    private System.Windows.Forms.TextBox textBox1;
    /// <summary>
    /// 必需的设计器变量。
    /// </summary>
    private System.ComponentModel.Container components = null;

    public Form1()
    {
    //
    // Windows 窗体设计器支持所必需的
    //
    InitializeComponent();

    //
    // TODO: 在 InitializeComponent 调用后添加任何构造函数代码
    //
    }

    /// <summary>
    /// 清理所有正在使用的资源。
    /// </summary>
    protected override void Dispose(bool disposing)
    {
    if (disposing)
    {
    if (components != null)
    {
    components.Dispose();
    }
    }
    base.Dispose(disposing);
    }

    #region Windows Form Designer generated code
    /// <summary>
    /// 设计器支持所需的方法 - 不要使用代码编辑器修改
    /// 此方法的内容。
    /// </summary>
    private void InitializeComponent()
    {
    this.button1 = new System.Windows.Forms.Button();
    this.textBox1 = new System.Windows.Forms.TextBox();
    this.SuspendLayout();
    //
    // button1
    //
    this.button1.Location = new System.Drawing.Point(24, 104);
    this.button1.Name = "button1";
    this.button1.Size = new System.Drawing.Size(97, 23);
    this.button1.TabIndex = 0;
    this.button1.Text = "View";
    this.button1.Click += new System.EventHandler(this.button1_Click);
    //
    // textBox1
    //
    this.textBox1.Location = new System.Drawing.Point(24, 80);
    this.textBox1.Name = "textBox1";
    this.textBox1.Size = new System.Drawing.Size(96, 21);
    this.textBox1.TabIndex = 1;
    this.textBox1.Text = "textBox1";
    //
    // Form1
    //
    this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
    this.ClientSize = new System.Drawing.Size(152, 141);
    this.Controls.AddRange(new System.Windows.Forms.Control[] {
    this.textBox1,
    this.button1});
    this.Name = "Form1";
    this.Text = "Form1";
    this.ResumeLayout(false);

    }
    #endregion

    /// <summary>
    /// 应用程序的主入口点。
    /// </summary>
    [STAThread]
    static void Main()
    {
    Application.Run(new Form1());
    }

    [System.Runtime.InteropServices.DllImport("gdi32")]
    private static extern IntPtr CreateFont(int H, int W, int E, int O, int FW, int I, int u, int S, int C, int OP, int CP, int Q, int PAF, string F);
    [System.Runtime.InteropServices.DllImport("gdi32")]
    private static extern IntPtr BeginPath(IntPtr hdc);
    [System.Runtime.InteropServices.DllImport("gdi32")]
    private static extern IntPtr EndPath(IntPtr hdc);
    [System.Runtime.InteropServices.DllImport("gdi32")]
    private static extern int SetBkMode(IntPtr hdc, int nBkMode);
    [System.Runtime.InteropServices.DllImport("gdi32")]
    private static extern IntPtr StrokePath(IntPtr hdc);
    [System.Runtime.InteropServices.DllImport("gdi32")]
    private static extern int TextOut(IntPtr hdc, int x, int y, string lpString, int nCount);
    [System.Runtime.InteropServices.DllImport("gdi32")]
    private static extern IntPtr SelectObject(IntPtr hdc, IntPtr hObject);
    [System.Runtime.InteropServices.DllImport("user32")]
    private static extern IntPtr GetDC(IntPtr hwnd);
    const int FW_HEAVY = 900;
    const int ANSI_CHARSET = 0;
    const int OUT_DEFAULT_PRECIS = 0;
    const int CLIP_DEFAULT_PRECIS = 0;
    const int DEFAULT_QUALITY = 0;
    const int DEFAULT_PITCH = 0;
    const int FF_SWISS = 32;
    const int TRANSPARENT = 1;

    private void button1_Click(object sender, System.EventArgs e)
    {
    IntPtr dc = GetDC(this.Handle);
    IntPtr m_Font = CreateFont(50, 20, 0, 0, FW_HEAVY, 1, 0,
    0, ANSI_CHARSET, OUT_DEFAULT_PRECIS,
    CLIP_DEFAULT_PRECIS, DEFAULT_QUALITY,
    DEFAULT_PITCH | FF_SWISS, "Times New Roman");
    //创建字体
    //这里的字体一定要是TrueType Font

    BeginPath(dc);
    //开始捕获轮廓
    SetBkMode(dc, TRANSPARENT);
    IntPtr m_OldFont = SelectObject(dc, m_Font);
    TextOut(dc, 10, 0, this.textBox1.Text, this.textBox1.Text.Length);
    SelectObject(dc, m_OldFont);
    EndPath(dc);
    //结束捕获
    StrokePath(dc);
    //将捕获的轮廓用当前的Pen画到Canvas上
    }
    }
    }

    QQ图片20160704164551

  • 来源:明日科技
  • 阴影文字实现方法

    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
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    using System;
    using System.Drawing;
    using System.Collections;
    using System.ComponentModel;
    using System.Windows.Forms;
    using System.Data;

    namespace Example037_阴影效果的文字
    {
    /// <summary>
    /// Form1 的摘要说明。
    /// </summary>
    public class Form1 : System.Windows.Forms.Form
    {
    /// <summary>
    /// 必需的设计器变量。
    /// </summary>
    private System.ComponentModel.Container components = null;

    public Form1()
    {
    //
    // Windows 窗体设计器支持所必需的
    //
    InitializeComponent();

    //
    // TODO: 在 InitializeComponent 调用后添加任何构造函数代码
    //
    }

    /// <summary>
    /// 清理所有正在使用的资源。
    /// </summary>
    protected override void Dispose( bool disposing )
    {
    if( disposing )
    {
    if (components != null)
    {
    components.Dispose();
    }
    }
    base.Dispose( disposing );
    }

    #region Windows Form Designer generated code
    /// <summary>
    /// 设计器支持所需的方法 - 不要使用代码编辑器修改
    /// 此方法的内容。
    /// </summary>
    private void InitializeComponent()
    {
    //
    // Form1
    //
    this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
    this.ClientSize = new System.Drawing.Size(292, 273);
    this.Name = "Form1";
    this.Text = "Form1";
    this.Paint += new System.Windows.Forms.PaintEventHandler(this.Form1_Paint);

    }
    #endregion

    /// <summary>
    /// 应用程序的主入口点。
    /// </summary>
    [STAThread]
    static void Main()
    {
    Application.Run(new Form1());
    }

    private void Form1_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
    {
    Graphics g=e.Graphics;
    Font myFont;
    myFont=new Font("SansSerif",40);

    SolidBrush textShadowBrush = new SolidBrush(Color.FromArgb(70, Color.Blue));

    g.DrawString("Hello C#",myFont,Brushes.Blue, 25, 25 );
    g.DrawString("Hello C#",myFont,textShadowBrush, 30, 30);
    //显示带阴影的文字

    }
    }
    }

    QQ图片20160704162322