0

当我移动光标时,我实际上试图在光标周围画一个椭圆,我不希望它像实际那样留下痕迹。

有人可以帮忙吗,我相信这与 invalidaterec 选项有关

任何使用无效记录的示例儿子?

这是我的代码,除了跟踪它必须以与现在相同的方式工作。

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;



namespace MouseTest
{
    public partial class Form1 : Form
    {
        [DllImport("user32.dll", EntryPoint = "GetDC")]
        private static extern IntPtr GetDC(IntPtr hWnd);

        [DllImport("user32.dll", EntryPoint = "ReleaseDC")]
        private static extern int ReleaseDC(IntPtr hWnd, IntPtr hDC);

        [System.Runtime.InteropServices.DllImport("Shell32.dll")]
        private static extern int SHChangeNotify(int eventId, int flags, IntPtr item1,         IntPtr item2);

        System.Timers.Timer t1 = new System.Timers.Timer(100);


        public Form1()
        {
        t1.Elapsed += new System.Timers.ElapsedEventHandler(t1_Elapsed);
        t1.Start();
        // System.Windows.Forms.Cursor.Current = System.Windows.Forms.Cursors.WaitCursor;
        //Mouse.OverrideCursor = System.Windows.Input.Cursors.Hand;
        InitializeComponent();


    }

    void t1_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
    {
        t1.Stop();

        //SolidBrush b = new SolidBrush(Color.Red);
        IntPtr desktopDC = GetDC(IntPtr.Zero);
        Graphics g = Graphics.FromHdc(desktopDC);
        g.FillEllipse(new SolidBrush(Color.BlueViolet), Cursor.Position.X, Cursor.Position.Y, 25, 25);
        g.Dispose();
        ReleaseDC(IntPtr.Zero, desktopDC);

       t1.Start();


    }



}

}

4

1 回答 1

0

是的,在绘制新的新椭圆之前保持先前的坐标并在先前的封闭矩形上调用 InvalidateRect 可能会起作用。我没有任何调用 InvalidateRect 的代码,但如果这正是您的目标:“当我移动光标时在光标周围绘制一个椭圆,我不希望它像实际那样留下痕迹。” 还有其他更直接的方法。

一种方法是在透明表单上绘制椭圆,然后根据光标的移动移动该表单:

在这里,首先是表单定义:

namespace MouseForm
{
    partial class Form1
    {
        /// <summary>
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region Windows Form Designer generated code

        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.components = new System.ComponentModel.Container();
            this.shapeContainer1 = new Microsoft.VisualBasic.PowerPacks.ShapeContainer();
            this.ovalShape1 = new Microsoft.VisualBasic.PowerPacks.OvalShape();
            this.timer1 = new System.Windows.Forms.Timer(this.components);
            this.SuspendLayout();
            // 
            // shapeContainer1
            // 
            this.shapeContainer1.Location = new System.Drawing.Point(0, 0);
            this.shapeContainer1.Margin = new System.Windows.Forms.Padding(0);
            this.shapeContainer1.Name = "shapeContainer1";
            this.shapeContainer1.Shapes.AddRange(new Microsoft.VisualBasic.PowerPacks.Shape[] {
            this.ovalShape1});
            this.shapeContainer1.Size = new System.Drawing.Size(74, 74);
            this.shapeContainer1.TabIndex = 0;
            this.shapeContainer1.TabStop = false;
            // 
            // ovalShape1
            // 
            this.ovalShape1.BorderColor = System.Drawing.Color.Red;
            this.ovalShape1.BorderWidth = 3;
            this.ovalShape1.FillColor = System.Drawing.SystemColors.Control;
            this.ovalShape1.FillStyle = Microsoft.VisualBasic.PowerPacks.FillStyle.Solid;
            this.ovalShape1.Location = new System.Drawing.Point(2, 2);
            this.ovalShape1.Name = "ovalShape1";
            this.ovalShape1.Size = new System.Drawing.Size(70, 70);
            // 
            // timer1
            // 
            this.timer1.Enabled = true;
            this.timer1.Tick += new System.EventHandler(this.timer1_Tick);
            // 
            // Form1
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(74, 74);
            this.ControlBox = false;
            this.Controls.Add(this.shapeContainer1);
            this.Enabled = false;
            this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
            this.Name = "Form1";
            this.ShowIcon = false;
            this.ShowInTaskbar = false;
            this.Text = "Form1";
            this.TopMost = true;
            this.TransparencyKey = System.Drawing.SystemColors.Control;
            this.ResumeLayout(false);

        }

        #endregion

        private Microsoft.VisualBasic.PowerPacks.ShapeContainer shapeContainer1;
        private Microsoft.VisualBasic.PowerPacks.OvalShape ovalShape1;
        private System.Windows.Forms.Timer timer1;
    }
}

这是代码:

using System;
using System.Windows.Forms;

namespace MouseForm
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            this.Left = Cursor.Position.X - this.Width / 2;
            this.Top = Cursor.Position.Y - this.Height / 2;
        }
    }
}

我将把它作为练习留给读者来实现一种关闭透明表单的方法:)

于 2012-04-24T20:42:54.713 回答