zoukankan      html  css  js  c++  java
  • 想在桌面上涂鸦吗

    我曾经说过一句致理名言:涂鸦是人生一大乐趣。

    只要你懂得涂鸦之道,涂鸦是非常好玩的。在窗口上画多了,不爽了,想不想在桌面上画? 不要惊讶,这是可以的。

    Graphics类可以用一个静态方法FromHwnd来创建实例,如果想在桌面上涂鸦,只要得到桌面的句柄就可以了。那么如何得到桌面的句柄呢?要用到一个非托管API,即

    [csharp] view plain copy
     
    1. [DllImport("User32.dll")]  
    2. public extern static IntPtr GetDesktopWindow();  


    使用它可以得到桌面的句柄,只要有了句柄,就可创建Graphics,只要创建了Graphics对象,你想在它上面画个么鬼啊毛啊都可以了。

    就像我今天给自己题了个辞,一起来欣赏一下我的书法吧。

    为了使它爽一点,我用了一个Timer组件,并随机生成画刷颜色,让它有点闪烁的效果。

    下面是部分代码。

    [csharp] view plain copy
     
    1. using System;  
    2. using System.Collections.Generic;  
    3. using System.ComponentModel;  
    4. using System.Data;  
    5. using System.Drawing;  
    6. using System.Linq;  
    7. using System.Text;  
    8. using System.Threading.Tasks;  
    9. using System.Windows.Forms;  
    10. using System.Drawing.Drawing2D;  
    11. using System.Runtime.InteropServices;  
    12.   
    13. namespace drawInDesktopApp  
    14. {  
    15.     public partial class Form1 : Form  
    16.     {  
    17.         Random rand = null;  
    18.         IntPtr hwndDesktop = IntPtr.Zero;  
    19.         public Form1()  
    20.         {  
    21.             InitializeComponent();  
    22.             hwndDesktop = GetDesktopWindow();  
    23.             rand = new Random();  
    24.             this.FormClosing += (s, a) => timer1.Stop();  
    25.         }  
    26.   
    27.         private void button1_Click(object sender, EventArgs e)  
    28.         {  
    29.             this.WindowState = FormWindowState.Minimized;  
    30.             if (timer1.Enabled == false)  
    31.             {  
    32.                 timer1.Start();  
    33.             }  
    34.         }  
    35.   
    36.         private void DrawInScreen()  
    37.         {  
    38.             using (Graphics g = Graphics.FromHwnd(hwndDesktop))  
    39.             {  
    40.                 // 获取屏幕的宽度和高度  
    41.                 int scrWidth = Screen.PrimaryScreen.Bounds.Width;  
    42.                 int scrHeight = Screen.PrimaryScreen.Bounds.Height;  
    43.                 // 绘制文本  
    44.                 string str = "致青春!!";  
    45.                 Font font = new Font("华文行楷", 170f);  
    46.                 // 创建渐变画刷  
    47.                 Color txtcolor1 = Color.FromArgb(rand.Next(0, 256), rand.Next(0, 256), rand.Next(0, 256));  
    48.                 Color txtcolor2 = Color.FromArgb(rand.Next(0, 256), rand.Next(0, 256), rand.Next(0, 256));  
    49.                 // 计算文字的大小  
    50.                 SizeF size = g.MeasureString(str, font);  
    51.                 LinearGradientBrush lb = new LinearGradientBrush(  
    52.                     new RectangleF(new PointF(scrWidth /2, 0f), size),  
    53.                     txtcolor1,  
    54.                     txtcolor2, LinearGradientMode.Vertical);  
    55.                 // 文本格式  
    56.                 StringFormat sf = new StringFormat();  
    57.                 sf.Alignment = StringAlignment.Center;  
    58.                 sf.LineAlignment = StringAlignment.Center;  
    59.                 g.DrawString(str, font, lb, new RectangleF(0f,0f,scrWidth,scrHeight),sf);  
    60.                 font.Dispose();  
    61.                 lb.Dispose();  
    62.             }  
    63.         }  
    64.   
    65.         [DllImport("User32.dll")]  
    66.         public extern static IntPtr GetDesktopWindow();  
    67.   
    68.         private void timer1_Tick(object sender, EventArgs e)  
    69.         {  
    70.             DrawInScreen();  
    71.         }  
    72.     }  
    73. }  
  • 相关阅读:
    MFC中小笔记(四)
    MFC中小笔记(三)
    MFC中小笔记
    关于小蜘蛛诞生的坎坎坷坷
    Win32Api程序设计 常用域改变(设定)窗口位置、大小的api
    Win32Api程序设计 注册窗口类
    TCP segment of a reassembled PDU【转】
    计算机网络复习 -- 概念梳理
    指针(pointer) -- (上)
    原来我连真正的调试都不会,每次都是靠编译器(⊙﹏⊙)b
  • 原文地址:https://www.cnblogs.com/weekbo/p/8682075.html
Copyright © 2011-2022 走看看