gpt4 book ai didi

c# - 如何在没有窗体的情况下在 C# 中绘制图形

转载 作者:太空狗 更新时间:2023-10-29 22:28:04 27 4
gpt4 key购买 nike

我目前有一个控制台应用程序。如何在没有表单的情况下将图形绘制到屏幕上。

最佳答案

编辑 - 根据 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());
}
}
}

希望对您有所帮助。

旧的错误答案


您可以获得另一个窗口的 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);
}

关于c# - 如何在没有窗体的情况下在 C# 中绘制图形,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7897307/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com