簡體   English   中英

不使用Windows窗體繪制C#圖形

[英]Drawing C# graphics without using Windows Forms

有人可以提供不使用Windows窗體繪制圖形的示例嗎? 我有一個沒有控制台窗口或Windows窗體的應用程序,但是我需要繪制一些基本圖形(線條和矩形等)。

希望有道理。

這應該為您提供一個良好的開始:

  [TestFixture]
  public class DesktopDrawingTests {
    private const int DCX_WINDOW = 0x00000001;
    private const int DCX_CACHE = 0x00000002;
    private const int DCX_LOCKWINDOWUPDATE = 0x00000400;

    [DllImport("user32.dll")]
    private static extern IntPtr GetDesktopWindow();

    [DllImport("user32.dll")]
    private static extern IntPtr GetDCEx(IntPtr hwnd, IntPtr hrgn, uint flags);

    [Test]
    public void TestDrawingOnDesktop() {
      IntPtr hdc = GetDCEx(GetDesktopWindow(),
                           IntPtr.Zero,
                           DCX_WINDOW | DCX_CACHE | DCX_LOCKWINDOWUPDATE);

      using (Graphics g = Graphics.FromHdc(hdc)) {
        g.FillEllipse(Brushes.Red, 0, 0, 400, 400);
      }
    }
  }

像這樣嗎

 using System.Drawing;

 Bitmap bmp = new Bitmap(200, 100);
 Graphics g = Graphics.FromImage(bmp);
 g.DrawLine(Pens.Black, 10, 10, 180, 80);

這個問題沒有重點。 具體來說-您想在哪里繪制線條和矩形? 一般來說,您需要一個繪圖表面,通常由Windows窗體提供。

避免Windows表單的需求從何而來?

您是否正在使用另一種窗口?

對於Windows窗體,您可以使用類似於以下代碼:

namespace WindowsFormsApplication1 {
    public partial class Form1 : Form {
        public Form1() {
            InitializeComponent();
        }

        protected override void OnPaint(PaintEventArgs e) {
            base.OnPaint(e);

            e.Graphics.DrawLine(new Pen(Color.DarkGreen), 1,1, 3, 20 );
            e.Graphics.DrawRectangle(new Pen(Color.Black), 10, 10, 20, 32 );
        }
    }
}

通常,您可以使用任何可以獲取“圖形”對象的句柄的對象(例如打印機)來執行此操作。

是的,我做的方法是使用Windows窗體,但是使背景透明,然后清除所有邊框...

無論如何,謝謝您的答復。

Ĵ

暫無
暫無

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

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