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 を呼び出すコードはありませんが、これがまさにあなたの目標である場合:「カーソルを動かしたときに楕円を描画するために、実際のように軌跡を残したくありません。」他にももっと簡単な方法があります。

1 つの方法は、透明なフォームに楕円を描画し、カーソルの動きに基づいてそのフォームを移動することです。

ここでは、最初にフォーム定義を示します。

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 に答える