
Form f = new Form();
f.FormBorderStyle = FormBorderStyle.None;
f.Bounds = Screen.PrimaryScreen.Bounds;
f.BackColor = Color.Red;
f.TransparencyKey = f.BackColor;
Application.EnableVisualStyles();
Записывается этот код в файл Program.cs, (я использовал Visual Studio). Код для рисования самих прямоугольников пишется в Form.Designer.cs. Далее выкладываю содержимое этих файлов.
Program.cs
using System;
using System.Windows.Forms;
using System.Drawing;
namespace DesktopApp
{
static class Program
{
[STAThread]
static void Main()
{
Form f = new Form();
f.FormBorderStyle = FormBorderStyle.None;
f.Bounds = Screen.PrimaryScreen.Bounds;
f.BackColor = Color.Red;
f.TransparencyKey = f.BackColor;
Application.EnableVisualStyles();
Application.Run(f);
}
}
}
Form.Designer.cs
using System;
using System.Windows.Forms;
using System.Drawing;
using System.Runtime.InteropServices;
namespace DesktopApp
{
partial class Form
{
///
/// Обязательная переменная конструктора.
///
private System.ComponentModel.IContainer components = null;
[DllImport("User32.dll")]
public static extern IntPtr GetDC(IntPtr hwnd);
[DllImport("User32.dll")]
public static extern void ReleaseDC(IntPtr hwnd, IntPtr dc);
///
/// Освободить все используемые ресурсы.
///
/// истинно, если управляемый ресурс должен быть удален; иначе ложно.
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Код, автоматически созданный конструктором форм Windows
///
/// Требуемый метод для поддержки конструктора — не изменяйте
/// содержимое этого метода с помощью редактора кода.
///
private void InitializeComponent()
{
this.components = new System.ComponentModel.Container();
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.Text = "Form";
this.MouseDown += Form_MouseDown;
this.MouseMove += Form_MouseMove;
this.MouseUp += Form_MouseUp;
this.Paint += Form_Paint;
this.DoubleBuffered = true;
}
#endregion
bool isDown = false;
int initialX;
int initialY;
int currentX;
int currentY;
Rectangle rect;
private void Form_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Right)
{
isDown = true;
initialX = e.X;
initialY = e.Y;
}
if (e.Button == MouseButtons.Left)
{
Graphics g = this.CreateGraphics();
g.Clear(this.BackColor);
}
}
private void Form_MouseMove(object sender, MouseEventArgs e)
{
if (isDown == true)
{
currentX = e.X;
currentY = e.Y;
this.Invalidate();
}
}
private void Form_MouseUp(object sender, MouseEventArgs e)
{
isDown = false;
}
private void Form_Paint(object sender, PaintEventArgs e)
{
Graphics g = e.Graphics;
Pen drwaPen = new Pen(Color.Orange, 2);
rect = new Rectangle(
Math.Min(currentX, initialX),
Math.Min(currentY, initialY),
Math.Abs(currentX - initialX),
Math.Abs(currentY - initialY)
);
g.DrawRectangle(drwaPen, rect);
}
}
}
Оставьте свой комментарий
Комментариев нет