簡體   English   中英

C#:將矩形內容移動到 0,0 position

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

我正在制作一個包含兩個矩形的應用程序。 第一個矩形覆蓋整個屏幕,第二個矩形只是其中的一部分。 我需要從第一個矩形中提取第二個矩形,然后將第二個矩形移動到 0,0 position。

這是我在 c# 中的當前代碼,希望對我有所幫助。 謝謝!

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();

       

        
    }
}
}

資源 期望的結果

我從 ms 找到了該資源。

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

我發現 Graphics.TranslateTransform 方法通過將指定的平移添加到此 Graphics 的變換矩陣來更改坐標系的原點。

這意味着我可以將原點更改為負值以模擬切割區域。 最后,結果是一樣的。

    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);
    }

如果有人可以替代此解決方案,我將不勝感激。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM