I am making an app that contains two rectangles. The first rectangle covers the entire screen and the second rectangle is only part of it. I need to extract the second rectangle from the first and then move the second rectangle to the 0,0 position.
this is my current code in c#, I hope some can help me. Thank you!
namespace ClipWindowsFormsApplication
{
partial class Form1
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.SuspendLayout();
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(725, 509);
this.Name = "Form1";
this.Text = "Form1";
this.Paint += new System.Windows.Forms.PaintEventHandler(this.Form1_Paint);
this.ResumeLayout(false);
}
#endregion
}
}
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace ClipWindowsFormsApplication
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Paint(object sender, PaintEventArgs e)
{
Graphics g = e.Graphics;
g.Clear(this.BackColor);
//Create rectangles and regions
Rectangle rect1 = new Rectangle(0, 0, 500, 500);
Rectangle rect2 = new Rectangle(0, 100, 500, 200);
Region r1 = new Region(rect1);
Region r2 = new Region(rect2);
//Call SetClip
g.SetClip(r1, CombineMode.Intersect);
g.DrawString("THIS IS GOING TO BE DELETED", this.Font, Brushes.Black, rect1);
//Call IntersectClip
g.IntersectClip(r2);
//Fill rectangle
g.FillRectangle(Brushes.Magenta, 0, 0, 500, 300);
g.DrawString("TEST MY APP", this.Font, Brushes.White, rect2);
//Call ResetClip
g.ResetClip();
//Draw rectangles
g.DrawRectangle(new Pen(Color.Red, 6), rect1);
g.DrawRectangle(new Pen(Color.Black, 6), rect2);
g.SetClip(rect2, CombineMode.Exclude);
g.Clear(Color.White);
g.ResetClip();
}
}
}

