InvalidateRec与User32.dll,试图删除除最后一帧创建的所有其他框架

问题描述:

我实际上试图绘制一个椭圆形在我的光标周围,当我移动它时,我不希望它留下像它实际上一样的线索。InvalidateRec与User32.dll,试图删除除最后一帧创建的所有其他框架

有人能帮助这一点,我相信它有什么做的invalidaterec选项

使用无效REC的任何实例的儿子吗?

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

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(); 


    } 



} 

}

是,保持先前的坐标和绘制新的新的椭圆可能会工作之前调用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; 
     } 
    } 
} 

我会离开它作为一个练习留给读者来实现的方式来关闭透明形式:)