首页 新闻 会员 周边

如何用C#编写Windows操作系统跟随鼠标的圈圈

0
悬赏园豆:160 [已解决问题] 解决于 2023-08-01 18:52

如题;

效果见我博客里的鼠标跟随圈圈,用JavaScript实现很简单,请给出C#的实现源码。

注:Windows操作系统的鼠标跟随;
这里搜到一个Winform窗体里的例子:https://www.wenjiangs.com/group/topic-590916.html

C#
lzhdim的主页 lzhdim | 小虾三级 | 园豆:630
提问于:2023-07-23 15:30
< >
分享
最佳答案
0
using System;
using System.Drawing;
using System.Runtime.InteropServices;
using System.Windows.Forms;

public class MouseCircle : Form
{
    private const int WM_MOUSEMOVE = 0x0200;
    private const int SIZE = 40;

    private Point mousePosition;

    public MouseCircle()
    {
        this.FormBorderStyle = FormBorderStyle.None;
        this.TopMost = true;
        this.TransparencyKey = Color.Turquoise;
        this.BackColor = Color.Turquoise;
        this.DoubleBuffered = true;
        this.Bounds = Screen.PrimaryScreen.Bounds;

        this.MouseDown += (s, e) => Application.Exit();

        Application.AddMessageFilter(new MouseMessageFilter());
    }

    protected override void OnPaint(PaintEventArgs e)
    {
        using (var g = e.Graphics)
        {
            int alpha = 255;
            for (int i = 0; i < SIZE; i++)
            {
                using (var pen = new Pen(Color.FromArgb(alpha, Color.Black), 2))
                {
                    g.DrawEllipse(pen, mousePosition.X - i / 2, mousePosition.Y - i / 2, i, i);
                }
                alpha -= 255 / SIZE;
            }
        }
    }

    private class MouseMessageFilter : IMessageFilter
    {
        public bool PreFilterMessage(ref Message m)
        {
            if (m.Msg == WM_MOUSEMOVE)
            {
                var mousePos = new Point((int)m.LParam);
                var form = Application.OpenForms[0] as MouseCircle;
                form.mousePosition = mousePos;
                form.Invalidate();
            }
            return false;
        }
    }

    [STAThread]
    public static void Main()
    {
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);
        Application.Run(new MouseCircle());
    }
}

创建一个窗体 MouseCircle,它绘制一个跟随鼠标移动的圆圈。使用了MouseMessageFilter来捕获鼠标移动消息,并在每次鼠标移动时更新 mousePosition 变量,然后在窗体的 OnPaint 方法中绘制圆圈。

你试试这个

收获园豆:80
NotoChen | 菜鸟二级 |园豆:401 | 2023-07-27 16:15
其他回答(4)
0

当你在每次鼠标移动时调用 Invalidate() 方法时,它会触发窗体的重绘,导致之前绘制的圆圈被清除。

为了实现一个圆圈跟随鼠标的效果,你可以在窗体的 Paint 事件中绘制圆圈,并使用一个变量来保存鼠标的当前位置。这样,在每次重绘时,都会根据鼠标的当前位置绘制圆圈。

以下是修改后的代码示例:
private Point currentMousePosition;

private void Form1_MouseMove(object sender, MouseEventArgs e)
{
currentMousePosition = e.Location;
Invalidate(); // 触发窗体的重绘事件
}

private void Form1_Paint(object sender, PaintEventArgs e)
{
if (currentMousePosition != null)
{
int radius = 150;
int diameter = radius * 2;
int x = currentMousePosition.X - radius;
int y = currentMousePosition.Y - radius;

    using (Pen skyBluePen = new Pen(Brushes.DeepSkyBlue))
    {
        e.Graphics.DrawEllipse(skyBluePen, x, y, diameter, diameter);
    }
}

}
在这个修改后的代码中,我们使用了 currentMousePosition 变量来保存鼠标的当前位置。在窗体的 MouseMove 事件中,我们更新了 currentMousePosition 的值,并调用 Invalidate() 方法来触发窗体的重绘。

在窗体的 Paint 事件中,我们使用 currentMousePosition 的值来绘制圆圈。我们使用 using 语句来确保 Pen 对象在使用后被正确释放。

请记得在窗体的构造函数中添加以下代码,以将 MouseMove 事件和 Paint 事件与相应的处理方法关联起来:
public Form1()
{
InitializeComponent();
this.MouseMove += new MouseEventHandler(Form1_MouseMove);
this.Paint += new PaintEventHandler(Form1_Paint);
}
现在,当你移动鼠标时,窗体将重绘并绘制一个跟随鼠标移动的圆圈,而不会保留之前绘制的圆圈。

收获园豆:20
小九九呀 | 园豆:383 (菜鸟二级) | 2023-07-23 19:10

你参考了我链接里的例子了吧,,,我需要的是操作系统上的那个鼠标,不是你New的这个Form1窗体上的鼠标位置。。。

支持(0) 反对(1) lzhdim | 园豆:630 (小虾三级) | 2023-07-23 19:53

看清楚需求,操作系统级别的鼠标跟随圈圈,就是桌面以及其上所有窗体的圈圈啊,不是你C#里打开的这个窗体。。。

不那么简单:
1、先绘制一个小圈圈;
2、移动鼠标绘制更多的圈圈, 同时以前的小圈圈逐渐变大一点,然后逐渐消失;(看上面图片效果啊)
3、圈圈是中间镂空的(参考不规则形状窗体);

支持(0) 反对(0) lzhdim | 园豆:630 (小虾三级) | 2023-07-23 19:57

@lzhdim: 这涉及到对操作系统底层的钩子技术的使用,需要在 C++ 或其他底层编程语言中实现,并且需要谨慎处理以避免潜在的系统不稳定或安全问题。

操作系统级别的代码通常是高度复杂和敏感的,需要更深入的知识和理解。如果你真的有这样的需求,建议你仔细学习操作系统编程、钩子技术以及相关安全性措施,并且在专业人士的指导下进行。不正确地操作操作系统级别可能会导致严重的问题,包括系统崩溃、数据丢失等。

支持(0) 反对(0) 小九九呀 | 园豆:383 (菜鸟二级) | 2023-07-25 14:27
0

要在Windows操作系统中用C#编写一个跟随鼠标的圈圈效果,你可以使用WinForms来实现。以下是一个简单的示例代码,实现一个圆形跟随鼠标的效果:

首先,创建一个新的WinForms项目,然后将以下代码添加到Form的类中:

csharp
Copy code
using System;
using System.Drawing;
using System.Windows.Forms;

namespace MouseFollowCircle
{
public partial class MainForm : Form
{
private const int CircleRadius = 30; // 圆圈的半径

    public MainForm()
    {
        InitializeComponent();

        this.FormBorderStyle = FormBorderStyle.None; // 无边框窗体
        this.BackColor = Color.White; // 背景色设置为白色
        this.TopMost = true; // 窗体置顶

        this.DoubleBuffered = true; // 双缓冲,防止闪烁
        this.FormBorderStyle = FormBorderStyle.None; // 无边框窗体

        // 注册鼠标移动事件
        this.MouseMove += MainForm_MouseMove;

        // 设置窗体大小和位置
        this.Width = CircleRadius * 2;
        this.Height = CircleRadius * 2;
        this.StartPosition = FormStartPosition.Manual;
    }

    private void MainForm_MouseMove(object sender, MouseEventArgs e)
    {
        // 更新窗体的位置,使其始终跟随鼠标
        this.Left = e.X - CircleRadius;
        this.Top = e.Y - CircleRadius;

        // 重绘窗体
        this.Invalidate();
    }

    protected override void OnPaint(PaintEventArgs e)
    {
        base.OnPaint(e);
        Graphics g = e.Graphics;

        // 绘制圆圈
        g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
        g.FillEllipse(Brushes.Red, 0, 0, CircleRadius * 2 - 1, CircleRadius * 2 - 1);
    }
}

}
请注意,此代码创建一个无边框的窗体,并在窗体上绘制一个红色的圆圈。通过注册MouseMove事件,窗体会跟随鼠标移动并不断重绘以实现跟随效果。

你可以在程序的Main方法中启动此窗体:

csharp
Copy code
using System;
using System.Windows.Forms;

namespace MouseFollowCircle
{
static class Program
{
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new MainForm());
}
}
}
编译并运行该程序,你应该能够看到一个圆圈跟随鼠标的效果。记得右键单击窗体并选择退出来关闭应用程序。

注意:此代码只是一个简单的示例,实际应用中你可能需要添加更多的功能和样式来实现更复杂的效果。

收获园豆:30
Technologyforgood | 园豆:5992 (大侠五级) | 2023-07-23 23:06

谢谢你的代码。。。期待下一位网友的代码。

支持(0) 反对(0) lzhdim | 园豆:630 (小虾三级) | 2023-07-23 23:14
0

要实现在Windows操作系统级别上跟随鼠标的小圆圈效果,可以使用Windows API函数来实现。以下是C#代码示例:

  1. 创建一个新的Windows控制台应用程序。

  2. 添加以下命名空间:

using System;
using System.Drawing;
using System.Runtime.InteropServices;
using System.Threading;
  1. 在Program类中定义以下变量:
// 鼠标光标位置
[StructLayout(LayoutKind.Sequential)]
public struct POINT
{
    public int X;
    public int Y;
}

[DllImport("user32.dll")]
public static extern bool GetCursorPos(out POINT lpPoint);

[DllImport("user32.dll")]
public static extern bool DrawIconEx(IntPtr hdc, int xLeft, int yTop, IntPtr hIcon,
                                    int cxWidth, int cyHeight, uint istepIfAniCur,
                                    IntPtr hbrFlickerFreeDraw, uint diFlags);

[DllImport("user32.dll")]
public static extern IntPtr LoadCursor(IntPtr hInstance, int lpCursorName);

[DllImport("user32.dll")]
public static extern IntPtr GetDC(IntPtr hwnd);

[DllImport("user32.dll")]
public static extern int ReleaseDC(IntPtr hwnd, IntPtr hdc);

[DllImport("gdi32.dll")]
public static extern bool DeleteObject(IntPtr hObject);

[DllImport("user32.dll")]
public static extern IntPtr GetDesktopWindow();

// 程序入口
static void Main()
{
    while (true)
    {
        // 获取当前鼠标光标位置
        GetCursorPos(out POINT point);

        // 绘制圆圈
        DrawCircle(point.X, point.Y);

        // 等待一段时间,使圆圈显示
        Thread.Sleep(50);
    }
}

// 绘制圆圈
static void DrawCircle(int x, int y)
{
    IntPtr desktopDC = GetDC(GetDesktopWindow());

    // 设置画刷
    IntPtr brush = CreateSolidBrush(0xFFFFFF); // 白色

    // 设置光标圆圈的半径
    int radius = 20;

    // 绘制圆圈
    Ellipse(desktopDC, x - radius, y - radius, x + radius, y + radius);

    // 释放资源
    DeleteObject(brush);
    ReleaseDC(GetDesktopWindow(), desktopDC);
}

// 创建画刷
static IntPtr CreateSolidBrush(uint color)
{
    return CreateSolidBrush(color & 0x00FFFFFF);
}

// 绘制椭圆
static void Ellipse(IntPtr hdc, int left, int top, int right, int bottom)
{
    IntPtr hBrush = CreateSolidBrush(0x000000); // 黑色
    IntPtr oldBrush = SelectObject(hdc, hBrush);

    int result = GDI.Ellipse(hdc, left, top, right, bottom);

    SelectObject(hdc, oldBrush);
    DeleteObject(hBrush);
}

请注意,由于该代码在操作系统级别上进行绘制,并不绑定到特定的窗口句柄,因此该小圆圈将显示在整个桌面上。可以编译为控制台应用程序exe,也可以修改为c#服务程序跟随windows启动运行。

收获园豆:30
lanedm | 园豆:2381 (老鸟四级) | 2023-07-24 00:03

你这个没实现鼠标移动才显示吧?

支持(0) 反对(0) lzhdim | 园豆:630 (小虾三级) | 2023-07-24 12:06
0

修改下windows底层关于鼠标箭头的程序,样式该为雪花就行。

国产小品牌 | 园豆:199 (初学一级) | 2023-07-25 17:07
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册