2011-10-26 31 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桌面)。