资源简介
接上次关于在C# WINFORM下制作透明窗体在系统桌面上画图方案是通过两个透明窗体实现的,现在讨论一下使用API底层鼠标钩子+透明窗体实现方法,这种实现方法比效麻烦,需要重新监听实现鼠标操作,在这儿只做交流学习,功能还待完善!
代码片段和文件信息
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;//是否开始画图
private Graphics gs;//画版
private Pen pen;//画笔
private Point startpt;//画图起点
public Rectangle taskBarRect;//任务栏位置
public Form1()
{
InitializeComponent();
WindowState = FormWindowState.Maximized;//本窗体最大化
FormBorderstyle = FormBorderstyle.None;//无边框窗体
TransparencyKey = BackColor;//背景透明(鼠标穿透)
DoubleBuffered = true;//双缓存处理
gs = CreateGraphics();//创建窗体画板
pen = new Pen(Color.Black 3f);//画笔
//taskBarHandle为返回的任务栏的句柄
//Shell_TrayWnd为任务栏的类名
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;//鼠标事件参数
//全局钩子常量
private const int WH_MOUSE_LL = 14;
//声明消息的常量鼠标按下和释放
private const int WM_LEFT_RBUTTONDOWN = 0x201;//鼠标左键按下事件监听值
private const int WM_LEFT_RBUTTONUP = 0x202;//鼠标左键弹起事件监听值
private const int WM_RIGHT_RBUTTONDOWN = 0x204;//鼠标右键按下事件监听值
private const int WM_RIGHT_RBUTTONUP = 0x205;//鼠标右键按下事件监听值
private const int WM_MOVE = 0x200;//鼠标移动事件监听值
//保存任务栏的矩形区域
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
);
//获取窗口的矩形区域
[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
);
//调用下一个钩子
[DllImport(“user32.dll“)]
public stati
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
文件 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
- 上一篇:计算两个日期之间工作日天数(c#实现)
- 下一篇:C# 图书系统 SQL
评论
共有 条评论