2011-10-26 26 views
5

我目前有一个控制台应用程序。我将如何绘制图形到屏幕上,而不必拥有一个表单。如何在没有表单的C#中绘制图形

+1

为什么你不只是创建一个窗体并在辅助线程中运行它? –

+0

因为我所做的不需要任何形式。 –

+0

窗体只是一个窗口的抽象。您可以跳过它是一种表单的事实,并将其用作常见窗口。你也可以使用windows api CreateWindowEx跳过Windows.Form dll的使用,但是它会要求你做很多工作,因为在这种情况下你没有任何GDI函数。 –

回答

7

编辑 - 基于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); 
     } 
2

你必须创建某种形式的窗口绘制图形。你不能直接画到屏幕上。

+0

我并不完全确定,但这个应用程序称为Puush(可让您上传屏幕截图)http://puush.me/在屏幕上绘制一个矩形以供您选择要捕捉的屏幕区域,不需要任何窗口。不是广告,但我总是想知道它是如何做到的。 – Zhanger

+4

我确定它使用“窗口”,但它所绘制的窗口没有任何铬。您可以通过创建Forms应用程序来重新创建这种效果,将窗口样式设置为“无”,并将背景设置为透明。之后,用户将看到的唯一东西是您添加到表单的对象。 在puush.me的情况下,我相信它会创建一个带有透明背景的全屏窗口。 – CuddleBunny

+0

CuddleBunny,这是值得一个独立的答案(尤其是如果你用一些裸骨头代码榨取它)。 –

1

如果创建全屏直接绘制表面,则可以使用directx在没有窗口的情况下在整个屏幕上进行绘制。屏幕是你所有的(根本没有Windows桌面)。