[英]Drawing C# graphics without using Windows Forms
Could someone provide an example for drawing graphics without using Windows Forms? 有人可以提供不使用Windows窗体绘制图形的示例吗? I have an app that doesn't have a console window or Windows form, but i need to draw some basic graphics (lines and rectangles etc.)
我有一个没有控制台窗口或Windows窗体的应用程序,但是我需要绘制一些基本图形(线条和矩形等)。
Hope that makes sense. 希望有道理。
This should give you a good start: 这应该为您提供一个良好的开始:
[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);
}
}
}
Something like this? 像这样吗
using System.Drawing;
Bitmap bmp = new Bitmap(200, 100);
Graphics g = Graphics.FromImage(bmp);
g.DrawLine(Pens.Black, 10, 10, 180, 80);
The question is a little unfocused. 这个问题没有重点。 Specifically - where do you want to draw the lines and rectangles?
具体来说-您想在哪里绘制线条和矩形? Generally speaking, you need a drawing surface, usually provided by a windows form.
一般来说,您需要一个绘图表面,通常由Windows窗体提供。
Where does the need to avoid windows forms come from? 避免Windows表单的需求从何而来?
Are you using another kind of window? 您是否正在使用另一种窗口?
For a windows form you could use code similar to this: 对于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 );
}
}
}
You can generally do this with any object that lets you get a handle for a "Graphics" object (like a printer). 通常,您可以使用任何可以获取“图形”对象的句柄的对象(例如打印机)来执行此操作。
Right, the way i've done it is with a windows form, but make the background transparent, and then get rid of all the borders... 是的,我做的方法是使用Windows窗体,但是使背景透明,然后清除所有边框...
Thanks for the replys anyway.. 无论如何,谢谢您的答复。
J Ĵ
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.