好久没来论坛了,今天在csdn上无意看到一个帖子。另类装饰你的Winform - 在窗口的左上角加一个圣诞帽,有点意思,但只能在C#窗体上就有点儿单调了,所以就稍作修改,在前置窗体上都加上了圣诞帽,大家多多发挥,让圣诞来的更猛烈些吧。
代码
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.Windows.Forms;
9 using System.Runtime.InteropServices;
10
11 namespace Hat
12 {
13 public partial class Form1 : Form
14 {
15 FrmHat hat = new FrmHat();
16
17 [DllImport("user32.dll", EntryPoint = "GetForegroundWindow")]
18 public static extern IntPtr GetForegroundWindow();
19
20 [DllImport("user32.dll")]
21 [return: MarshalAs(UnmanagedType.Bool)]
22 static extern bool GetWindowRect(IntPtr hWnd, ref RECT lpRect);
23
24 public Form1()
25 {
26 InitializeComponent();
27 hat.Location = new Point(this.Location.X - 30, this.Location.Y - 10);
28 hat.Show();
29 }
30
31
32 [StructLayout(LayoutKind.Sequential)]
33 public struct RECT
34 {
35 public int Left; //最左坐标
36 public int Top; //最上坐标
37 public int Right; //最右坐标
38 public int Bottom; //最下坐标
39 }
40
41 private void timer1_Tick(object sender, EventArgs e)
42 {
43 IntPtr ip = GetForegroundWindow();
44 RECT rect = new RECT();
45 GetWindowRect(ip, ref rect);
46
47 hat.Location = new Point(rect.Left - 30,rect.Top - 10);
48 }
49 }
50 }
51
弄个圣诞老人在桌面上乱跳也是不错的,不过没时间弄了。