資源簡(jiǎn)介
接上次關(guān)于在C# WINFORM下制作透明窗體在系統(tǒng)桌面上畫(huà)圖方案是通過(guò)兩個(gè)透明窗體實(shí)現(xiàn)的,現(xiàn)在討論一下使用API底層鼠標(biāo)鉤子+透明窗體實(shí)現(xiàn)方法,這種實(shí)現(xiàn)方法比效麻煩,需要重新監(jiān)聽(tīng)實(shí)現(xiàn)鼠標(biāo)操作,在這兒只做交流學(xué)習(xí),功能還待完善!

代碼片段和文件信息
using?System;
using?System.Collections.Generic;
using?System.ComponentModel;
using?System.Data;
using?System.Drawing;
using?System.Text;
using?System.Windows.Forms;
using?System.Runtime.InteropServices;
using?System.Diagnostics;
namespace?mousdraw
{
????public?partial?class?Form1?:?Form
????{
????????private?bool?startdraw?=?false;//是否開(kāi)始畫(huà)圖
????????private?Graphics?gs;//畫(huà)版
????????private?Pen?pen;//畫(huà)筆
????????private?Point?startpt;//畫(huà)圖起點(diǎn)
????????public?Rectangle?taskBarRect;//任務(wù)欄位置
????????public?Form1()
????????{
????????????InitializeComponent();
????????????WindowState?=?FormWindowState.Maximized;//本窗體最大化
????????????FormBorderstyle?=?FormBorderstyle.None;//無(wú)邊框窗體
????????????
????????????TransparencyKey?=?BackColor;//背景透明(鼠標(biāo)穿透)
????????????DoubleBuffered?=?true;//雙緩存處理
????????????gs?=?CreateGraphics();//創(chuàng)建窗體畫(huà)板
????????????pen?=?new?Pen(Color.Black?3f);//畫(huà)筆
????????????//taskBarHandle為返回的任務(wù)欄的句柄??
????????????//Shell_TrayWnd為任務(wù)欄的類(lèi)名??
????????????MouseHookProcedure?=?new?HookProc(MouseHookProc);?//聲明鉤子?
????????????int?taskBarHandle?=?FindWindow(“Shell_TrayWnd“?null);
????????????GetWindowRect(taskBarHandle?ref?taskBarRect);
????????????newTaskBarRect?=?new?Rectangle(taskBarRect.X?taskBarRect.Y?taskBarRect.Width?-?taskBarRect.X?taskBarRect.Height?-?taskBarRect.Y);
????????????StartHook();
????????}
????????private?int?hMouseHook?=?0;
????????private?MouseEventArgs?mea;//鼠標(biāo)事件參數(shù)
????????//全局鉤子常量??
????????private?const?int?WH_MOUSE_LL?=?14;
????????//聲明消息的常量鼠標(biāo)按下和釋放??
????????private?const?int?WM_LEFT_RBUTTONDOWN?=?0x201;//鼠標(biāo)左鍵按下事件監(jiān)聽(tīng)值
????????private?const?int?WM_LEFT_RBUTTONUP?=?0x202;//鼠標(biāo)左鍵彈起事件監(jiān)聽(tīng)值
????????private?const?int?WM_RIGHT_RBUTTONDOWN?=?0x204;//鼠標(biāo)右鍵按下事件監(jiān)聽(tīng)值
????????private?const?int?WM_RIGHT_RBUTTONUP?=?0x205;//鼠標(biāo)右鍵按下事件監(jiān)聽(tīng)值
????????private?const?int?WM_MOVE?=?0x200;//鼠標(biāo)移動(dòng)事件監(jiān)聽(tīng)值
????????//保存任務(wù)欄的矩形區(qū)域??
????????public?Rectangle?newTaskBarRect;
????????//定義委托??
????????public?delegate?int?HookProc(int?nCode?int?wParam?IntPtr?lParam);
????????private?HookProc?MouseHookProcedure;
????????//尋找符合條件的窗口??
????????[DllImport(“user32.dll“?EntryPoint?=?“FindWindow“)]
????????public?static?extern?int?FindWindow(
????????????string?lpClassName
????????????string?lpWindowName
????????);
????????//獲取窗口的矩形區(qū)域??
????????[DllImport(“user32.dll“?EntryPoint?=?“GetWindowRect“)]
????????public?static?extern?int?GetWindowRect(
????????????int?hwnd
????????????ref?Rectangle?lpRect
????????);
????????//安裝鉤子??
????????[DllImport(“user32.dll“)]
????????public?static?extern?int?SetWindowsHookEx(
????????????int?idHook
????????????HookProc?lpfn
????????????IntPtr?hInstance
????????????int?threadId
????????);
????????//卸載鉤子??
????????[DllImport(“user32.dll“?EntryPoint?=?“UnhookWindowsHookEx“)]
????????public?static?extern?bool?UnhookWindowsHookEx(
????????????int?hHook
????????);
????????//調(diào)用下一個(gè)鉤子??
????????[DllImport(“user32.dll“)]
????????public?stati
?屬性????????????大小?????日期????時(shí)間???名稱(chēng)
-----------?---------??----------?-----??----
?????文件???????7686??2015-09-17?14:08??mousdraw\Form1.cs
?????文件???????1158??2015-09-17?12:53??mousdraw\Form1.Designer.cs
?????文件???????3071??2015-09-17?12:53??mousdraw\mousdraw.csproj
?????文件????????467??2015-09-17?12:53??mousdraw\Program.cs
?????文件???????1184??2015-09-17?12:53??mousdraw\Properties\AssemblyInfo.cs
?????文件???????2872??2015-09-17?12:53??mousdraw\Properties\Resources.Designer.cs
?????文件???????5612??2015-09-17?12:53??mousdraw\Properties\Resources.resx
?????文件???????1093??2015-09-17?12:53??mousdraw\Properties\Settings.Designer.cs
?????文件????????249??2015-09-17?12:53??mousdraw\Properties\Settings.settings
?????目錄??????????0??2015-09-17?14:16??mousdraw\bin
?????目錄??????????0??2015-09-17?14:17??mousdraw\obj
?????目錄??????????0??2015-09-17?12:53??mousdraw\Properties
?????目錄??????????0??2015-09-17?14:08??mousdraw
-----------?---------??----------?-----??----
????????????????23392????????????????????13
評(píng)論
共有 條評(píng)論