Skip to content

Conversation

@LeafShi1
Copy link
Member

@LeafShi1 LeafShi1 commented Nov 5, 2025

Fixes #14020

Root Cause

The previous logic (Changed in #12205) allowed early returns or skipped updates under certain conditions (e.g., when regions were considered equal). This prevented PInvoke.SetWindowRgn() logic from executing, causing custom shapes like rounded corners to fail when the control handle was created or refreshed.

Proposed changes

  • Remove conditional logic oldRegion == region from method SetRegionInternal of the Control class

Customer Impact

  • Developers can display the expected shape after setting custom areas (such as ellipses or rounded corners) on controls such as buttons.

Regression?

  • Yes

Risk

  • Minimal

Screenshots

Sample project: RegionTest.zip

private void button1_Click(object sender, EventArgs e)
{
    var button = new Button()
    {
        Size = new Size(200, 200),
    };
    var path = new GraphicsPath();
    path.AddEllipse(0, 0, button.Width, button.Height);
    button.Region = new Region(path);
    this.Controls.Add(button);
}

Before

Image

After

Image

Test methodology

  • Manually

Test environment(s)

  • .net 10.0.0-rc.1.25553.112
Microsoft Reviewers: Open in CodeFlow

Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR modifies the SetRegionInternal method to remove an optimization that prevented reapplying the same region instance when a handle exists. The change ensures that regions are always applied to window handles even when setting the same region instance, which may fix issues with region application during handle creation or recreation scenarios.

Key Changes

  • Removed the oldRegion == region check from the early return condition in SetRegionInternal
  • The method now only checks !IsHandleCreated for early return, ensuring region application always occurs when a handle exists
Comments suppressed due to low confidence (1)

src/System.Windows.Forms/System/Windows/Forms/Control.cs:2830

  • Removing the oldRegion == region check may cause unnecessary calls to PInvoke.SetWindowRgn when the same region instance is being reapplied. Consider whether this could impact performance in scenarios where SetRegionInternal is called frequently with the same region, especially in ActiveX scenarios (line 1824 in ActiveXImpl.cs). If this change is intentional to fix a specific bug, consider adding a comment explaining why the same region should always be reapplied.
        if (!IsHandleCreated)
        {
            // We'll get called when OnHandleCreated runs.
            return oldRegion;
        }

@codecov
Copy link

codecov bot commented Nov 5, 2025

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 77.15805%. Comparing base (cdf5769) to head (4552a8f).
⚠️ Report is 3 commits behind head on main.

Additional details and impacted files
@@                 Coverage Diff                 @@
##                main      #14022         +/-   ##
===================================================
+ Coverage   77.15552%   77.15805%   +0.00253%     
===================================================
  Files           3279        3279                 
  Lines         645263      645317         +54     
  Branches       47718       47718                 
===================================================
+ Hits          497856      497914         +58     
- Misses        143705      143715         +10     
+ Partials        3702        3688         -14     
Flag Coverage Δ
Debug 77.15805% <100.00000%> (+0.00253%) ⬆️
integration 18.99208% <0.00000%> (-0.03135%) ⬇️
production 52.02559% <100.00000%> (+0.00139%) ⬆️
test 97.40749% <100.00000%> (+0.00038%) ⬆️
unit 49.47090% <100.00000%> (+0.00451%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Copy link
Member

@JeremyKuhne JeremyKuhne left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add a regression test, otherwise looks good.

@dotnet-policy-service dotnet-policy-service bot added the waiting-author-feedback The team requires more information from the author label Nov 5, 2025
@dotnet-policy-service dotnet-policy-service bot removed the waiting-author-feedback The team requires more information from the author label Nov 6, 2025
ricardobossan
ricardobossan previously approved these changes Nov 7, 2025
JeremyKuhne
JeremyKuhne previously approved these changes Nov 7, 2025
Copy link
Member

@JeremyKuhne JeremyKuhne left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Assuming you validated that the new test fails without your change this looks good.

@LeafShi1
Copy link
Member Author

Assuming you validated that the new test fails without your change this looks good.

Yes, the test will fail without my changes.

@JeremyKuhne JeremyKuhne merged commit f7dc22e into dotnet:main Nov 10, 2025
8 checks passed
@JeremyKuhne
Copy link
Member

/backport to release/10.0

@github-actions
Copy link
Contributor

Started backporting to release/10.0 (link to workflow run)

@github-actions github-actions bot locked and limited conversation to collaborators Dec 11, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Set Region property before Control handle created take no effect

3 participants