【问题标题】:Refresh screen after drawing a line in c#在c#中画线后刷新屏幕
【发布时间】:2015-07-01 13:53:34
【问题描述】:

我正在尝试编写一个简单的应用程序,它以一定的速度打印一条“移动”的线,这是我第一次使用 c# 和 Windows 创建应用程序,我找到了一个帮助我画线的教程,到目前为止我明白了:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Runtime.InteropServices;
using System.Drawing;
using System.Threading;
using System.Windows.Forms;


namespace ConsoleApplication1
{
    class Program
    {
        [DllImport("User32.dll")]
        static extern IntPtr GetDC(IntPtr hwnd);
        static void draw(Rectangle r, Brush b, IntPtr hwnd)
        {
            using(Graphics g = Graphics.FromHdc(hwnd))
            {

                g.FillRectangle(b, r);
            }
        }
        static void Main(string[] args)
        {
            int x = 0;
            while (true)
            {

                draw(new Rectangle(x, 0, 50, 1080), Brushes.PaleGoldenrod, GetDC(IntPtr.Zero));
                x++;
            }

        }
    }
}

问题是我不知道如何擦除前一行,或者在画完一条线后刷新屏幕。

欢迎任何帮助。

谢谢!

【问题讨论】:

  • 简短的回答,用那种“绘图”的方法,你不会。这些线条会被你在上面画的任何东西抹去。那可能是桌面本身,或其他应用程序窗口。当他们重新绘制自己时,这些线条将被删除。
  • 感谢您的回答,您知道我可以使用什么方法来实现此目的吗?谢谢!
  • 第一步:扔掉教程,清理链接!阅读 Paint 事件和 Timer Tick

标签: c# windows screen line draw


【解决方案1】:

尝试类似:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WinFormDrawGraphics
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            SetStyle(ControlStyles.ResizeRedraw, true);
        }
        protected override void OnPaint(PaintEventArgs e)
        {
            // initialization
            Graphics g = e.Graphics;

            // Create pen.
            Pen blackPen = new Pen(Color.Black, 3);

            // Create points that define line.
            Point point1 = new Point(100, 100);
            Point point2 = new Point(500, 100);

            // Draw line to screen.
            e.Graphics.DrawLine(blackPen, point1, point2);

            // add any other graphics drawing...
        }
    }
}

如果您调整窗口大小,或者隐藏它并重新显示它,它将重新绘制线条。

注意:这适用于 WinForms GUI 应用程序,而不是您创建的控制台应用程序。

【讨论】:

    猜你喜欢
    • 2015-04-06
    • 2012-11-29
    • 1970-01-01
    • 1970-01-01
    • 2021-07-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多