11.C#编程学习——捕获鼠标

11.C#编程学习——捕获鼠标

源码

//---------------------------------------

// MouseWeb.cs ?2001 by CharlesPetzold

//---------------------------------------

usingSystem;

usingSystem.Drawing;

usingSystem.Windows.Forms;

 

classMouseWeb : Form

{

    Point ptMouse = Point.Empty;

 

    publicstaticvoid Main()

    {

        Application.Run(newMouseWeb());

    }

    public MouseWeb()

    {

        Text = "Mouse Web";

        BackColor = SystemColors.Window;

        ForeColor = SystemColors.WindowText;

        ResizeRedraw = true;

    }

    protectedoverridevoid OnMouseMove(MouseEventArgs mea)

    {

        Graphics grfx = CreateGraphics();

 

        DrawWeb(grfx, BackColor, ptMouse);

        ptMouse = newPoint(mea.X, mea.Y);

        DrawWeb(grfx, ForeColor, ptMouse);

 

        grfx.Dispose();

    }

    protectedoverridevoid OnPaint(PaintEventArgs pea)

    {

        DrawWeb(pea.Graphics, ForeColor,ptMouse);

    }

    void DrawWeb(Graphicsgrfx, Color clr, Point pt)

    {

        int cx = ClientSize.Width;

       int cy = ClientSize.Height;

        Pen pen = newPen(clr);

 

        grfx.DrawLine(pen, pt, newPoint(0,0));

        grfx.DrawLine(pen, pt, newPoint(cx/ 4, 0));

        grfx.DrawLine(pen, pt, newPoint(cx/ 2, 0));

        grfx.DrawLine(pen, pt, newPoint(3* cx / 4, 0));

        grfx.DrawLine(pen, pt, newPoint(cx,0));

        grfx.DrawLine(pen, pt, newPoint(cx,cy / 4));

        grfx.DrawLine(pen, pt, newPoint(cx,cy / 2));

        grfx.DrawLine(pen, pt, newPoint(cx,3 * cy / 4));

        grfx.DrawLine(pen, pt, newPoint(cx,cy));

        grfx.DrawLine(pen, pt, newPoint(3* cx / 4, cy));

        grfx.DrawLine(pen, pt, newPoint(cx/ 2, cy));

        grfx.DrawLine(pen, pt, newPoint(cx/ 4, cy));

        grfx.DrawLine(pen, pt, newPoint(0,cy));

        grfx.DrawLine(pen, pt, newPoint(0,cy / 4));

        grfx.DrawLine(pen, pt, newPoint(0,cy / 2));

        grfx.DrawLine(pen, pt, newPoint(0,3 * cy / 4));

    }

}

获取CHECKER

usingSystem;

usingSystem.Drawing;

usingSystem.Windows.Forms;

 

classChecker : Form

{

    protectedconstint xNum = 5;      // Number of boxes horizontally

    protectedconstint yNum = 4;      // Number of boxes vertically

    protectedbool[,] abChecked = newbool[yNum,xNum];

    protectedint cxBlock, cyBlock;

 

    publicstaticvoid Main()

    {

        Application.Run(newChecker());

    }

    public Checker()

    {

        Text = "Checker";

        BackColor = SystemColors.Window;

        ForeColor = SystemColors.WindowText;

        ResizeRedraw = true;

 

        OnResize(EventArgs.Empty);

    }

    protectedoverridevoid OnResize(EventArgs ea)

    {

        base.OnResize(ea);            // Or else ResizeRedraw doesn't work

 

        cxBlock = ClientSize.Width / xNum;

        cyBlock = ClientSize.Height / yNum;

    }

    protectedoverridevoid OnMouseUp(MouseEventArgs mea)

    {

        int x = mea.X / cxBlock;

        int y = mea.Y / cyBlock;

 

        if (x < xNum && y < yNum)

        {

            abChecked[y, x] ^= true;

            Invalidate(newRectangle(x* cxBlock, y * cyBlock,

                                     cxBlock,cyBlock));

        }

    }

    protectedoverridevoid OnPaint(PaintEventArgs pea)

    {

        Graphics grfx = pea.Graphics;

        Pen pen = newPen(ForeColor);

 

        for (int y = 0; y < yNum;y++)

            for (int x = 0; x < xNum;x++)

            {

                grfx.DrawRectangle(pen, x *cxBlock, y * cyBlock,

                                       cxBlock, cyBlock);

                if (abChecked[y, x])

                {

                    grfx.DrawLine(pen, x *cxBlock, y * cyBlock,

                                      (x + 1) *cxBlock, (y + 1) * cyBlock);

                    grfx.DrawLine(pen, x *cxBlock, (y + 1) * cyBlock,

                                      (x + 1) * cxBlock, y * cyBlock);

                }

            }

    }

}

 

 

 

 

 

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Microsoft Visual Studio 2010做的C#实时监控鼠标位置和左键点击时的位置实例,主要代码: public class MouseHook { private Point point; private Point Point { get { return point; } set { if (point != value) { point = value; if (MouseMoveEvent != null) { var e = new MouseEventArgs(MouseButtons.None, 0, point.X, point.Y, 0); MouseMoveEvent(this, e); } } } } private int hHook; private const int WM_LBUTTONDOWN = 0x201; public const int WH_MOUSE_LL = 14; public Win32Api.HookProc hProc; public MouseHook() { this.Point = new Point(); } public int SetHook() { hProc = new Win32Api.HookProc(MouseHookProc); hHook = Win32Api.SetWindowsHookEx(WH_MOUSE_LL, hProc, IntPtr.Zero, 0); return hHook; } public void UnHook() { Win32Api.UnhookWindowsHookEx(hHook); } private int MouseHookProc(int nCode, IntPtr wParam, IntPtr lParam) { Win32Api.MouseHookStruct MyMouseHookStruct = (Win32Api.MouseHookStruct)Marshal.PtrToStructure(lParam, typeof(Win32Api.MouseHookStruct)); if (nCode < 0) { return Win32Api.CallNextHookEx(hHook, nCode, wParam, lParam); } else { if (MouseClickEvent != null) { MouseButtons button = MouseButtons.None; int clickCount = 0; switch ((Int32)wParam) { case WM_LBUTTONDOWN: button = MouseButtons.Left; clickCount = 1; break; } var e = new MouseEventArgs(button, clickCount, point.X, point.Y, 0); MouseClickEvent(this, e); } this.Point = new Point(MyMouseHookStruct.pt.x, MyMouseHookStruct.pt.y); return Win32Api.CallNextHookEx(hHook, nCode, wParam, lParam); } } public delegate void MouseMoveHandler(object sender, MouseEventArgs e); public event MouseMoveHandler MouseMoveEvent; public delegate void MouseClickHandler(object sender, MouseEventArgs e); public event MouseClickHandler MouseClickEvent; }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值