简体   繁体   English

C#:将矩形内容移动到 0,0 position

[英]C#: Moving a Rectangle content At 0,0 position

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.我需要从第一个矩形中提取第二个矩形,然后将第二个矩形移动到 0,0 position。

this is my current code in c#, I hope some can help me.这是我在 c# 中的当前代码,希望对我有所帮助。 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();

       

        
    }
}
}

资源 期望的结果

I found that resource from ms.我从 ms 找到了该资源。

https://docs.microsoft.com/en-us/dotnet/api/system.drawing.graphics.translatetransform?view=dotnet-plat-ext-6.0 https://docs.microsoft.com/en-us/dotnet/api/system.drawing.graphics.translatetransform?view=dotnet-plat-ext-6.0

I found that Graphics.TranslateTransform Method changes the origin of the coordinate system by prepending the specified translation to the transformation matrix of this Graphics.我发现 Graphics.TranslateTransform 方法通过将指定的平移添加到此 Graphics 的变换矩阵来更改坐标系的原点。

This means that I can change the origin to negative to simulate cutting the region.这意味着我可以将原点更改为负值以模拟切割区域。 In the end, the result is the same.最后,结果是一样的。

    private void Form1_Paint(object sender, PaintEventArgs e)
    {
        // Create transform matrix.
        e.Graphics.TranslateTransform(0, -50);

        // Save translated graphics state.
        GraphicsState transState = e.Graphics.Save();

        // Reset transformation matrix to identity and fill rectangle.
        e.Graphics.ResetTransform();
        e.Graphics.FillRectangle(new SolidBrush(Color.Red), 0, 0, 100, 100);

        // Restore graphics state to translated state and fill second

        // rectangle.
        e.Graphics.Restore(transState);
        e.Graphics.FillRectangle(new SolidBrush(Color.Blue), 0, 0, 100, 100);
    }

If anyone has an alternative to this solution I would appreciate it.如果有人可以替代此解决方案,我将不胜感激。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM