.NET在屏幕上输出文字

方法一:调用API函数获取屏幕的设备驱动器句柄,然后用Graphics在屏幕上输出文字。

using System.Runtime.InteropServices;

namespace Test
{
    public partial class Form1 : Form
    {
        [DllImport("user32.dll")]
        public static extern IntPtr GetDC(IntPtr hwnd);
        [DllImport("user32.dll")]
        private static extern IntPtr ReleaseDC(IntPtr hwnd, IntPtr hdc);

        private void button1_Click(object sender, EventArgs e)
        {
            IntPtr hwnd = IntPtr.Zero;
            IntPtr hdc = GetDC(hwnd);
            using (Graphics graphics = Graphics.FromHdc(hdc))
            {
                graphics.DrawString("木子屋:http://www.mzwu.com/", new Font("宋体", 15), new SolidBrush(Color.Red), 3, 3);
            }
            ReleaseDC(hwnd, hdc);//释放句柄
        }
    }
}

缺点:输出的文字不好用程序擦除,桌面刷新文字就会丢失!

方法二:使用透明的窗体

在Form1中输入如下代码:

Form curForm;

private void Form1_Load(object sender, EventArgs e)
{
    //新建窗体并设置属性
    curForm = new Form();
    curForm.TopMost = true;//最顶层
    curForm.FormBorderStyle = FormBorderStyle.None;//无标题栏,边框
    curForm.TransparencyKey = this.BackColor;//底色透明
    curForm.Width = 600;
    curForm.Height = 30;
    curForm.Left = (Screen.GetWorkingArea(curForm).Width - curForm.Width) / 2;
    curForm.Top = (Screen.GetWorkingArea(curForm).Height - curForm.Height);
    //添加Label
    Label label1 = new Label();
    label1.AutoSize = true;
    label1.Text = "木子屋:http://www.mzwu.com/";
    curForm.Controls.Add(label1);
    //显示新窗体
    curForm.Show();
}

相比方法一,这种方法修改文字非常容易,并且文字不会丢失!

评论: 0 | 引用: 0 | 查看次数: 5435
发表评论
登录后再发表评论!