簡體   English   中英

如何在沒有表單的情況下在C#中繪制圖形

[英]How do I draw graphics in C# without a form

我目前有一個控制台應用程序。 如何在不需要表格的情況下將圖形繪制到屏幕上。

編輯 - 根據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);
        }

你必須創建一個窗口來繪制圖形。 你不能直接畫到屏幕上。

如果您創建全屏直接繪圖表,則可以使用directx在沒有窗口的情況下在整個屏幕上繪圖。 屏幕全是你的(根本沒有Windows桌面)。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM