简体   繁体   English

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

[英]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.

相关问题 以ac#形式使用painteventargs; 如何将更大的图像绘制到Graphics对象(以及滚动条)? - using painteventargs in a c# form; how do I draw a larger image to the Graphics object (with scrollbars as well)? 如何在 C# 中绘制简单的图形? - How do I draw simple graphics in C#? 如何在C#中将具有透明截面的图像绘制到Graphics对象? - How do I draw an image with a transparent section to a Graphics object in C#? C#如何在没有长路径的情况下在与运行程序相同的位置上绘制文件 - C# how do I draw on files in the same location as i'm running the program without a long path C#如何在Windows窗体上的两个对象之间画一条线? - C# How do I draw a line between two objects on a windows form? 如何使用图形库C#在图像上绘制小图像? - How can I draw a small image over an image using Graphics library C# ? 如何使用C#graphics.DrawString绘制unicode字符串 - How to draw unicode string using C# graphics.DrawString 如何在Graphics.DrawImage中扩展绘图区域c# - how to extend draw area in Graphics.DrawImage c# 如何在不清晰的情况下绘制当前 Graphics 对象? - How can I draw the current Graphics object without clear? 如何在不使用表单的情况下以C#在ASP.NET中创建消息表单 - How do I Make a message form in ASP.NET in C# without using a form
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM