如何在c#中绘制没有表单的图形
本文关键字:表单 图形 绘制 | 更新日期: 2023-09-27 18:12:19
我目前有一个控制台应用程序。如果不使用表单,我该如何在屏幕上绘制图形呢?
EDIT -基于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());
}
}
}
希望有帮助。
旧错误答案
你可以得到另一个窗口的窗口并在其上绘图。我不知道如何在整个屏幕上画,我自己也一直在想这个问题。
一个简单的例子:
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);
}
你必须创建一个可以绘制图形的窗口。你不能直接画到屏幕上
如果你创建一个全屏directdrawsurface,你可以在没有窗口的整个屏幕上使用directx进行绘制。屏幕是你的(没有windows桌面)。