如何在没有表单的情况下在C#中绘制图形

Mar*_*s S 5 c# visual-studio-2010 visual-studio c#-4.0

我目前有一个控制台应用程序.如何在不需要表格的情况下将图形绘制到屏幕上.

Zha*_*ger 7

编辑 - 根据CuddleBunny的评论,我创建了一个基本上"在屏幕上绘制图形"的类.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Windows.Forms;
namespace WindowsFormsApplication4
{
    class test : Form
    {
        public test() : base()
        {
            this.TopMost = true;
            this.DoubleBuffered = true;
            this.ShowInTaskbar = false;
            this.FormBorderStyle = FormBorderStyle.None;
            this.WindowState = FormWindowState.Maximized;
            this.BackColor = Color.Purple;
            this.TransparencyKey = Color.Purple;
        }
        protected override void OnPaint(PaintEventArgs e)
        {
            e.Graphics.DrawRectangle(Pens.Black, 0, 0, 200, 200);
            this.Invalidate(); //cause repaint
        }
        public static void Main(String[] args)
        {
            Application.Run(new test());
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

希望能帮助到你.

老错误的答案


你可以得到另一个窗口的hwnd并绘制它.我不知道如何在整个屏幕上画画,我总是想知道自己.

一个简单的例子:

            Process p = Process.GetProcessById(0); //id of the process or some other method that can get the desired process
        using (Graphics g = Graphics.FromHwnd(p.MainWindowHandle))
        {
            g.DrawRectangle(Pens.Black, 0, 0, 100, 100);
        }
Run Code Online (Sandbox Code Playgroud)