using System.Drawing;
using System.Drawing.Drawing2D;
using System.Runtime.InteropServices;
[DllImport("user32")]
internal static extern IntPtr GetDC(IntPtr hwnd);
[DllImport("User32.dll")]
internal static extern void ReleaseDC(IntPtr dc);
public void PaintRectangleToScreen()
{
IntPtr deskDC = GetDC(IntPtr.Zero);
Graphics g = Graphics.FromHdc(deskDC);
Font font = new Font("Arial Black", 36);
DrawStringOnCenter(g, "Sale 1387 mobarak!", font, new Point(0, -25));
font = new Font("Arial Black", 18);
DrawStringOnCenter(g, "by: Sinpin", font, new Point(0, 25));
Rectangle rect = new Rectangle(200, 300,
Screen.PrimaryScreen.Bounds.Width - 400,
Screen.PrimaryScreen.Bounds.Height - 600);
g.FillRectangle(new SolidBrush(Color.FromArgb(100, Color.DodgerBlue))
, rect);
g.DrawRectangle(new Pen(Color.DodgerBlue, 3), rect);
g.Dispose();
ReleaseDC(deskDC);
}
private void DrawStringOnCenter(Graphics g, string str, Font font, Point offset)
{
SizeF size = g.MeasureString(str, font);
g.DrawString(str, font, Brushes.White,
new PointF(
(Screen.PrimaryScreen.Bounds.Width - size.Width) / 2 + offset.X,
(Screen.PrimaryScreen.Bounds.Height - size.Height) / 2 + offset.Y)
);
}
و مثالی از طریقه ی استفاده :
private void Form1_Load(object sender, EventArgs e)
{
PaintRectangleToScreen();
this.Close();
}
این یک مثال خیلی ساده از ترسیم مستقیم بر دسکتاپ بود.
و این یک مثال واقعی تر : http://www.codeproject.com/KB/applications/pcalgen.aspx