在不使用Windows窗体的情况下绘制C#图形

有人可以提供一个不使用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); 

问题有点没有重点。 具体 – 你想在哪里画线条和矩形? 一般来说,您需要一个绘图表面,通常由窗体提供。

避免窗体forms的需求来自哪里?

你在使用另一种窗户吗?

对于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 ); } } } 

您通常可以使用任何可以获取“Graphics”对象(如打印机)句柄的对象来执行此操作。

是的,我完成它的方式是使用窗体,但使背景透明,然后摆脱所有边界……

无论如何,谢谢你的回复..

Ĵ