C#製做浮動圖標窗體

先看效果:ide

  這個小圖標能夠進行隨意拖拽,點擊還能夠產生事件測試

隨便演示一下,效果就是這樣的。spa

下面直接演示如何製做:code

新建一個窗體,設置窗體的FormBorderStyleNone(去掉窗體邊框);orm

調整窗體的大小,和你的圖標差很少大小就行,至於動態圖,還在測試中,圖標必須是沒有背景的,由於有背景的圖片展現出來,即時窗體透明,也會顯示圖片的白色背景,會很醜,因此要選沒有背景的圖片,能夠網上找到以後本身拿ps把背景去掉,記住這裏是圖片背景,不是窗體背景!blog

窗體背景通常默認BackColorControl,能夠不用改,接口

去屬性中將TransparencyKey改成Control,也就是和窗體背景顏色一致的背景色就好了,這樣整個窗體就透明瞭,在窗體的BackGroundImage中設置你找的無背景圖片,要是太大,就把BackGroundImageLayouyt設置爲Zoom事件

ShowInTaskBar改成False(不會產生任務欄圖標);圖片

必定要設置TopMostTrue(始終置頂);博客

設置好上面的就開始敲代碼了

具體代碼以下

 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.Runtime.InteropServices;
 8 using System.Text;
 9 using System.Threading.Tasks;
10 using System.Windows.Forms;
11 
12 namespace UI
13 {
14     public partial class Test01 : Form
15     {
16         public Test01()
17         {
18             InitializeComponent();
19         }
20         //設置按鼠標移動窗體,調用外部接口
21         [DllImport("user32.dll")]
22         public static extern bool ReleaseCapture();
23         [DllImport("user32.dll")]
24         public static extern bool SendMessage(IntPtr hwnd, int wMsg, int wParam, int lParam);
25         private void Test01_MouseDown(object sender, MouseEventArgs e)
26         {
27             if (e.Button == MouseButtons.Left)
28             {
29                 Form form = sender as Form;
30                 Point point = form.Location;
31                 ReleaseCapture(); //釋放鼠標捕捉
32                 SendMessage(Handle, 0xA1, 0x02, 0);
33                 if (form.Location == point)
34                 {
35                     MessageBox.Show("哈哈");
36                 }
37 
38             }
39         }
40     }
41 }
View Code

 

可能前面有點囉嗦,但願你們不要責怪,由於我是怕新手拿到代碼不知道怎麼用,由於我也是重新手過來的,我也是個新手,深有同感。

但願能仔細看一下本課題中是如何處理在mousedown中式如何處理判斷是點擊仍是拖動的,由於這個可能在之後會用到,這個問題我本人在網上查了很久都沒有找到解決辦法,而後本身想經過判斷位置是否改變來實現的。

轉載請註明出處,謝謝,我不太會排版,抱歉。這是原創的第一篇博客,請你們見諒!

相關文章
相關標籤/搜索