[英]How do I draw graphics in C# without a form
I currently have a Console application. 我目前有一个控制台应用程序。 How would I draw graphics to the screen without having to have a form. 如何在不需要表格的情况下将图形绘制到屏幕上。
EDIT - based on CuddleBunny's comment, I have created a class that will basically "draw graphics on the screen." 编辑 - 根据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());
}
}
}
Hope it helps. 希望能帮助到你。
old faulty answer 老错误的答案
You can get the hwnd of another window and draw on that. 你可以得到另一个窗口的hwnd并绘制它。 I'm not sure how to draw on the entire screen though, I've always wondered that myself. 我不知道如何在整个屏幕上画画,我总是想知道自己。
A simple example : 一个简单的例子:
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);
}
You have to create a window of some kind to draw graphics to. 你必须创建一个窗口来绘制图形。 You can't just draw directly to the screen. 你不能直接画到屏幕上。
You can draw on the entire screen without a window using directx if you create a full screen directdrawsurface. 如果您创建全屏直接绘图表,则可以使用directx在没有窗口的情况下在整个屏幕上绘图。 The screen is all yours (no windows desktop at all). 屏幕全是你的(根本没有Windows桌面)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.