在桌面上绘制时,有时会删除图形,有什么办法可以解决?

时间:2018-12-22 18:51:23

标签: c# winforms

Form1:

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;

namespace Draw_on_desktop
{
    public partial class Form1 : Form
    {
        bool IsColorDialogOpened = false;
        int px = 0, py = 0;

        public Form1()
        {
            InitializeComponent();
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            if (!chkDraw.Checked || IsColorDialogOpened)
                return;
            int x = Cursor.Position.X;
            int y = Cursor.Position.Y;
            DesktopDrawing.DrawALine(px, py, x, y, 50, lblDrawingColor.BackColor);
            px = x;
            py = y;
        }

        private void lblDrawingColor_Click(object sender, EventArgs e)
        {
            IsColorDialogOpened = true;
            cld.Color = lblDrawingColor.BackColor;
            cld.AllowFullOpen = true;
            cld.ShowDialog(this);
            lblDrawingColor.BackColor = cld.Color;
            IsColorDialogOpened = false;
        }

        private void chkDraw_CheckedChanged(object sender, EventArgs e)
        {
            px = Cursor.Position.X;
            py = Cursor.Position.Y;
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }
    }
}

DesktopDrawing类:

using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using System.Drawing.Imaging;
using System.Runtime.InteropServices;

namespace Draw_on_desktop
{
    public static class DesktopDrawing
    {
        [DllImport("GDI32.dll")]
        private static extern bool DeleteDC(int hdc);
        [DllImport("GDI32.dll")]
        private static extern bool DeleteObject(int hObject);
        [DllImport("GDI32.dll")]
        private static extern int SelectObject(int hdc, int hgdiobj);
        [DllImport("User32.dll")]
        private static extern int GetDesktopWindow();
        [DllImport("User32.dll")]
        private static extern int GetWindowDC(int hWnd);
        [DllImport("GDI32.dll")]
        private static extern int LineTo(int hdc, int x, int y);
        [DllImport("GDI32.dll")]
        private static extern int MoveToEx(int hdc, int x, int y, ref Point lppoint);
        [DllImport("GDI32.dll")]
        private static extern int CreatePen(int penstyle, int width, int color);


        public static void DrawALine(int x1, int y1, int x2, int y2, int width, Color clr)
        {
            Point p = new Point();
            int hdcSrc = GetWindowDC(GetDesktopWindow());
            int newcolor = System.Drawing.ColorTranslator.ToWin32(clr);

            int newpen = CreatePen(0, width, newcolor);
            SelectObject(hdcSrc, newpen);
            MoveToEx(hdcSrc, x1, y1, ref p);
            LineTo(hdcSrc, x2, y2);
            DeleteDC(hdcSrc);
            DeleteObject(newpen);
        }
    }
}

form1设计器:

namespace Draw_on_desktop
{
    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.timer1 = new System.Windows.Forms.Timer(this.components);
            this.chkDraw = new System.Windows.Forms.CheckBox();
            this.lblDrawingColor = new System.Windows.Forms.Label();
            this.cld = new System.Windows.Forms.ColorDialog();
            this.SuspendLayout();
            // 
            // timer1
            // 
            this.timer1.Enabled = true;
            this.timer1.Interval = 1;
            this.timer1.Tick += new System.EventHandler(this.timer1_Tick);
            // 
            // chkDraw
            // 
            this.chkDraw.AutoSize = true;
            this.chkDraw.Location = new System.Drawing.Point(375, 132);
            this.chkDraw.Name = "chkDraw";
            this.chkDraw.Size = new System.Drawing.Size(74, 17);
            this.chkDraw.TabIndex = 0;
            this.chkDraw.Text = "Draw Line";
            this.chkDraw.UseVisualStyleBackColor = true;
            this.chkDraw.CheckedChanged += new System.EventHandler(this.chkDraw_CheckedChanged);
            // 
            // lblDrawingColor
            // 
            this.lblDrawingColor.AutoSize = true;
            this.lblDrawingColor.Location = new System.Drawing.Point(174, 133);
            this.lblDrawingColor.Name = "lblDrawingColor";
            this.lblDrawingColor.Size = new System.Drawing.Size(73, 13);
            this.lblDrawingColor.TabIndex = 1;
            this.lblDrawingColor.Text = "Drawing Color";
            this.lblDrawingColor.Click += new System.EventHandler(this.lblDrawingColor_Click);
            // 
            // Form1
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(800, 450);
            this.Controls.Add(this.lblDrawingColor);
            this.Controls.Add(this.chkDraw);
            this.Name = "Form1";
            this.Text = "Form1";
            this.Load += new System.EventHandler(this.Form1_Load);
            this.ResumeLayout(false);
            this.PerformLayout();

        }

        #endregion

        private System.Windows.Forms.Timer timer1;
        private System.Windows.Forms.CheckBox chkDraw;
        private System.Windows.Forms.Label lblDrawingColor;
        private System.Windows.Forms.ColorDialog cld;
    }
}

在Form1上,计时器启用为true,并且间隔设置为1

在桌面/屏幕上进行绘制时,当它在其自身或其他图标或任务栏上的某些位置上移动时,它会在其中留出空间或删除该图形。

Drawing

例如,如果我尝试绘制form1右上角X或调整大小,它将删除整个图稿。

0 个答案:

没有答案