diff --git a/.github/Feature-Prompts/Net11/Application.SystemTextAwareness'/Application.SystemTextAwareness.md b/.github/Feature-Prompts/Net11/Application.SystemTextAwareness'/Application.SystemTextAwareness.md new file mode 100644 index 00000000000..da7edcf854e --- /dev/null +++ b/.github/Feature-Prompts/Net11/Application.SystemTextAwareness'/Application.SystemTextAwareness.md @@ -0,0 +1,180 @@ +# Task: Create a new API — `Application` system-text-size awareness — and the respective API proposal + +## What to do + +Create a **new API proposal / API review issue in the upstream `dotnet/winforms` repo** +(`origin` = my fork, `upstream` = the Microsoft repo where this lands). Write it per the +WinForms repo conventions and the relevant skills for authoring new-API issues. Apply the +skills; don't ask me for boilerplate. + +This proposal introduces **runtime awareness of the Windows Accessibility text-size +setting** at the `Application` level, plus a per-`Form` change notification. It is the +**foundation** proposal; a companion `TreeView.NodeLeading` proposal references this one. + +## Before you implement anything + +**Verify every premise below against current source before committing to the design.** +Verify, don't trust. If any premise is wrong, stop and tell me. Key files (VMR @ +`96982699e0dd8c046f397541dc0eb235ea8a4958`): + +- `src/winforms/src/System.Windows.Forms.Primitives/src/System/Windows/Forms/Internals/ScaleHelper.cs` +- `src/winforms/src/System.Windows.Forms/System/Windows/Forms/Application.cs` +- `src/winforms/src/System.Windows.Forms/System/Windows/Forms/Application.ThreadContext.cs` +- `src/winforms/src/System.Windows.Forms/System/Windows/Forms/Application.ParkingWindow.cs` +- `src/runtime/src/libraries/Microsoft.Win32.SystemEvents/...` (SystemEvents / UserPreferenceCategory) + +## Rationale — surface and complete an existing partial implementation + +This is **not** a net-new feature; it **finishes a half-built one**. WinForms already reads +the Accessibility text-size setting, but only once and only for the default font: + +`ScaleHelper.ScaleToSystemTextSize(Font?)` reads +`HKEY_CURRENT_USER\Software\Microsoft\Accessibility` → value `TextScaleFactor` +(REG_DWORD, clamped 100–225), and returns the font scaled by `TextScaleFactor / 100` +(or `null` if 100, if the font `IsSystemFont`, or if the OS is < Windows 10 1507). It is +documented in-source as the **Settings → Display → Make Text Bigger** setting. + +The gaps: (1) the value is **never surfaced** to developers; (2) it is read **once**, at +default-font construction, and the app **never reacts** when the user changes the setting at +runtime; (3) there is **no notification** mechanism. This proposal fills those three gaps. + +## Three-knob disambiguation (MANDATORY callout — reviewers will conflate these) + +Windows surfaces three *different* sizing mechanisms; the Settings UI even puts two on one +page (`System → Display → Custom scaling` shows a "Custom scaling 100–500%" box AND a +"Text size" link). The proposal MUST state plainly which one it targets: + +1. **Display / Custom scaling (100–500%)** → this is **DPI**. Already handled by + `HighDpiMode` and the DPI events (`WM_DPICHANGED`, `Control.DpiChanged*`). **NOT** this + proposal. +2. **Accessibility → Text size (100–225%)** → registry `TextScaleFactor` under + `HKCU\Software\Microsoft\Accessibility`; WinRT `UISettings.TextScaleFactor` / + `TextScaleFactorChanged`. **THIS is the target.** Independent of DPI. +3. **Legacy pre-Win10 per-element text sizing** (title bars/menus) → **removed** in Windows + 10 1703; the Accessibility slider replaced it. Mentioned only to close the loop. + +State explicitly that `Application.SystemTextSize` reflects **#2 only**, and is orthogonal to +DPI (#1). + +## Proposed API + +### `Application` (process-static) + +- **`public static double SystemTextSize { get; }`** — the current Accessibility text-scale + factor (1.0–2.25; i.e. `TextScaleFactor / 100`). **Live getter** — re-reads the value, does + not cache, because it is a system setting that changes at runtime. Well-defined regardless + of whether any `Form` exists or how many UI threads are running (it is process-global). +- **`public static SystemTextSizeAwareness SystemTextSizeAwareness { get; set; }`** — the + mode. **Enum, not bool**, deliberately, to reserve room for a future `Automatic`: + - `Unaware` (default) — no notification raised; fully back-compatible, nothing changes. + - `Notify` — raise change notifications (see below); the app decides how to respond. + - *(reserved, NOT implemented now: `Automatic` — framework re-flows for you. Reserving the + enum slot now avoids a future breaking bool→enum change, the same lesson `HighDpiMode` + learned.)* +- **`public static event EventHandler? SystemTextSizeChanged`** — fires once per process when + the setting changes (only when awareness is `Notify`). + +### `Form` (instance) + +- **`public event EventHandler? SystemTextSizeChanged`** — instance event, raised on each + top-level `Form` when the setting changes. +- **`protected virtual void OnSystemTextSizeChanged(EventArgs e)`** — overridable, fires the + instance event via the `EventHandlerList` pattern (`Events[s_systemTextSizeChangedEvent]`). + +## The trigger architecture (the leak-critical part — get this exactly right) + +A naive design — a static `Application.SystemTextSizeChanged` that `Form`s/`Control`s +subscribe to — **leaks**: the static event strongly roots every subscriber, so no `Form` +that subscribes is ever collected. Avoid this by **mirroring the DPI architecture**, where +`Control`/`Form` learn of DPI changes from their **own `WndProc`** (`WM_DPICHANGED` → +`OnDpiChanged` → instance event via `EventHandlerList`), **not** from a static subscription. + +Verified facts that constrain the design: + +- **`WM_SETTINGCHANGE` is broadcast** (`HWND_BROADCAST`) and delivered directly to top-level + windows' `WndProc`s — it bypasses the thread message queue, so **`IMessageFilter` does NOT + see it.** Do not use a message filter. +- **The WinForms parking window is message-only** (`CreateParams.Parent = HWND_MESSAGE`). + Message-only windows are **excluded from broadcasts**, so the parking window **cannot** + receive `WM_SETTINGCHANGE`. Do not use it. +- **`Application` has no `MainForm`.** The main form lives on `ApplicationContext` (per-run, + per-UI-thread), can be `null` (tray/loop-only apps), and is mutable (splash→main handoff). + So the main form is **not** a reliable receiver. Do not anchor the app-level event to it. +- **`SystemEvents` already owns a hidden top-level broadcast-receiving window** (its + `.NET-BroadcastEventWindow`), and exposes `UserPreferenceChanged` with a + `UserPreferenceCategory` (the relevant value is `Accessibility`, which is **coarse** — it + covers any accessibility change, so you must re-read `TextScaleFactor` and diff to confirm + it was text-scale). + +**Resulting design:** + +- **App-level:** `Application` makes **one internal, process-lifetime** subscription to + `SystemEvents.UserPreferenceChanged`, filters `Category == Accessibility`, re-reads + `TextScaleFactor`, diffs against the cached value, and if changed raises the static + `SystemTextSizeChanged`. This is a single framework-static→framework-static link — it does + **not** root any user object, so it is **not** the leak hazard. Reuses the existing hidden + broadcast window; **no new HWND** required. +- **Form-level:** each top-level `Form` handles `WM_SETTINGCHANGE` in its **own `WndProc`** + (it is a broadcast — every top-level window receives it), re-reads + diffs, and raises its + **instance** `SystemTextSizeChanged` via `OnSystemTextSizeChanged`. `Form`s do **not** + subscribe to `Application` — no rooting, lifetime = the window. + +Caveat to document: there is **no dedicated `WM_TEXTSCALECHANGED`** message. Both paths must +recognize a *relevant* change by re-reading `TextScaleFactor` and comparing, not by the +message alone. + +## Aids vs. leave-it-to-the-user + +**Notify-only. No automatic re-layout / font-rescaling aids in this proposal.** Reasons: +text scale interacts with `AutoScaleMode`, anchored/docked layout, and explicitly-set fonts +in app-specific ways; a generic "scale all fonts by the factor" helper breaks more than it +fixes. The reserved `Automatic` enum value is exactly where such behavior would live later. +`Notify` gives the developer the factor and the event; they decide. (Consistent with the +companion `NodeLeading` proposal's conservative-default philosophy.) + +## Why this matters across controls (evidence — include the matrix) + +Multiple text-measuring controls derive item/row/tile extents from a `Font` that today only +reacts to the text-size setting once at startup (via `ScaleToSystemTextSize` on the default +font) and never again. So **any single cached height scalar is wrong the moment text size +changes at runtime** — which is the core argument for runtime awareness: + +- **ListBox / ComboBox** — have `MeasureItem` + `OwnerDrawVariable` (a real per-item measure + hatch) and `ItemHeight`. Their gap is the legacy default base calc + the missing runtime + text-scale reaction — i.e. exactly this proposal. +- **TreeView** — has neither `MeasureItem` nor a wrapped native height API; only a uniform + native item height. Worst-positioned for a managed fix; addressed by the companion + `TreeView.NodeLeading` proposal, which depends on this one. +- **ListView (Details)** — no `MeasureItem`, no native row-height message; row height is + comctl-computed from `SmallImageList` + control font, while per-item/subitem fonts are + honored via `NM_CUSTOMDRAW` (`CDRF_NEWFONT`). Userland workarounds (phantom `SmallImageList`; + `LVS_OWNERDRAWFIXED` + one-shot reflected `WM_MEASUREITEM`; "inflate control font / shrink + item fonts") each have holes (header leak, set-once, exhaustive per-item font setting, + owner-draw-all). **No clean complete userland solution exists** — strengthening the case + that text-size reaction belongs in the framework. + +## XML doc requirements + +- Document that `SystemTextSize` is the **Accessibility text-size** factor (Settings → + Display → Make Text Bigger), **not** DPI/display scaling, and is process-global / live. +- Document the `Unaware`/`Notify` semantics and that `Automatic` is reserved for future use. +- Document the no-rooting design note on the static event (so consumers understand instance + vs. static). + +## Open questions for review + +- Should `SystemTextSize` be `double` (1.0–2.25) or expose the raw int percent (100–225)? +- Behavior on OS < Windows 10 1507 (where `ScaleToSystemTextSize` no-ops): `SystemTextSize` + returns 1.0 and no events fire? +- Whether to also expose the value/event on `Application` only, leaving `Form` consumers to + use their own `WndProc` override — or provide the `Form` instance event as proposed + (recommended, for parity with the DPI event model). + +## Output + +The upstream issue per the skills: summary; the "complete a partial implementation" +rationale; the mandatory three-knob disambiguation; the proposed API; the leak-safe trigger +architecture with the four verified constraints (broadcast vs. IMessageFilter, message-only +parking window, no MainForm on Application, SystemEvents reuse); Notify-only stance; the +cross-control matrix; XML-doc requirements; open questions. Flag anything the source +contradicts. diff --git a/.github/copilot/Application/net11-VisualStylesMode/TextBoxBase-VisualStyles-WorkOrder.md b/.github/copilot/Application/net11-VisualStylesMode/TextBoxBase-VisualStyles-WorkOrder.md new file mode 100644 index 00000000000..09894f9d23f --- /dev/null +++ b/.github/copilot/Application/net11-VisualStylesMode/TextBoxBase-VisualStyles-WorkOrder.md @@ -0,0 +1,137 @@ +# Work Order — Port `TextBoxBase` NC-Painting + `VisualStylesMode` Chrome onto `VisualStylesNet11` + +**Target branch:** `KlausLoeffelmann/winforms` → `VisualStylesNet11` (current-`main`-based; modern path layout, no `/src/src/` doubling; `TextBoxBase.cs` ≈ 2130 lines). + +**Source of the original implementation (pinned):** `KlausLoeffelmann/winforms` @ `cf32e9c4efeba9d77e1a14025a8590b104e3c705` (old `/src/System.Windows.Forms/src/...` layout; `TextBoxBase.cs` ≈ 2562 lines). Relevant files: +- `.../Controls/TextBox/TextBoxBase.cs` — NC paint, NC calc, focus invalidation, `GetVisualStylesPadding`, helpers. +- `.../Controls/TextBox/TextBoxBase.NonClientBitmapCache.cs` — the offscreen cache class. +- `.../Controls/TextBox/TextBox.cs` — derived-level overrides (`CreateParams`, `WndProc`, `OnBackColorChanged`, `PRF_NONCLIENT` path). + +**Standing approval:** API review board sign-off from the .NET 9 cycle is still valid; DRI owns the call. This is a port + cleanup, **not** a redesign. Do not invent new public API beyond what the original exposed plus the `Padding` unshadowing called out below. + +**House style (apply throughout):** namespaces globally imported (no `using`/`imports` noise); C# 13/14; NRTs on; `var` only for long type names or when the type is obvious from the RHS, explicit type names for primitives; blank line between a new block and a following `return`; pattern matching / `is` / `and` / `or` / switch expressions preferred; collection expressions (`List x = [];`); expression-bodied members for single-line methods/read-only props formatted with the `=>` on the next line, 1-space-indented. Generate XML doc comments; use ``/``. + +--- + +## PROMPT 1 — Carry-over / Port + +> **Role:** You are porting a working-but-imperfect feature across a 3-year gap in the surrounding file. Faithfully reproduce the *mechanism*; do not improve, simplify, or "modernize" the algorithm except where this document explicitly says to. Where the surrounding `VisualStylesNet11` code has moved on, rebase onto the new shape rather than pasting. + +**Task.** Bring the non-client (NC) painting feature for `TextBoxBase` (and the `TextBox` derived touchpoints) from the pinned SHA `cf32e9c4…` onto `VisualStylesNet11`. The target branch currently has **none** of this work (no `WmNcPaint`/`WmNcCalcSize`/`OnNcPaint`/`GetVisualStylesPadding`/`NonClientBitmapCache`), but it **does** have the `VisualStylesMode` property infrastructure referenced in doc comments — wire into that, don't redeclare it. + +**Steps, in order:** + +1. **Fetch and diff the three source files** at SHA `cf32e9c4…` against their `VisualStylesNet11` counterparts. Produce a short inventory of every member you intend to add or modify, grouped by file, before writing any code. + +2. **Port `TextBoxBase.cs` members:** + - `WmNcPaint` / `OnNcPaint` (offscreen bitmap fill → AA rounded/single chrome → blit; `GetWindowDC`/`ReleaseDC` in `finally`). + - `WmNcCalcSize` (carves the padding band from `NCCALCSIZE_PARAMS->rgrc[0]`; gated on the `_triggerNewClientSizeRequest` latch). + - `InitializeClientArea` (the one-shot `SetWindowPos(SWP_FRAMECHANGED|NOMOVE|NOSIZE|NOZORDER|NOACTIVATE)` that provokes the single NC-calc). + - `GetVisualStylesPadding` / `GetScrollBarPadding` and the `VisualStyles{Fixed3D|FixedSingle|NoBorder}BorderPadding`, `BorderThickness` consts. + - The `WM_NCCALCSIZE` / `WM_NCPAINT` cases in `WndProc`. + - `OnGotFocus` / `OnLostFocus` / `OnSizeChanged` NC-frame invalidation (`RedrawWindow` with `RDW_FRAME|RDW_INVALIDATE`). + - `PreferredHeight` split (`PreferredHeightClassic` vs `PreferredHeightCore` selected by `VisualStylesMode`). + - Reconcile `GetPreferredSizeCore` with the modern branch already present on target. + +3. **Port `TextBoxBase.NonClientBitmapCache.cs`** — BUT this is a **decision point**, see Step 6. + +4. **Reconcile `TextBox.cs` (derived):** the target already has `CreateParams`, `WndProc`, `OnBackColorChanged` (special-casing `Fixed3D`), `OnGotFocus`, and a `WM_PRINTCLIENT`/`PRF_NONCLIENT` + `Application.RenderWithVisualStyles` path. Merge the NC behavior so the derived overrides cooperate with the new base NC painting (no double border draw, no fighting the `PRF_NONCLIENT` path). Call out every conflict you resolve. + +5. **Unshadow `Padding`.** On target it is still the neutered shadow (`[Browsable(false)]`, `EditorBrowsableState.Never`, `DesignerSerializationVisibility.Hidden`, `get/set => base.Padding`). Make it a real, browsable, serializable property that feeds the NC band via `GetVisualStylesPadding`. Preserve classic-mode behavior when `VisualStylesMode` is `Disabled`/`Classic`. Note the designer-serialization and back-compat implications in the PR description. + +6. **Retarget the rounded-rectangle helpers to the framework.** The original called fork-local `FillRoundedRectangle`/`DrawRoundedRectangle`. These now ship as **`System.Drawing.Graphics` instance methods** (`(Pen, Rectangle, Size)` + `RectangleF`/`SizeF` overloads; landed in the .NET 9 wave, current on target). **Delete the fork-local helpers and call the shipped methods.** ⚠️ Verify a **`FillRoundedRectangle(Brush, …)`** overload actually exists on target — the public ref only enumerates the `Draw`(`Pen`) overloads. If the `Fill`/`Brush` overload is absent, STOP and flag it; do not re-add a private helper without surfacing the gap. + +7. **Cache → `BufferedGraphics` (DECIDED — implement, do not re-evaluate).** The original used a hand-rolled per-instance `NonClientBitmapCache` (`CreateCompatibleBitmap` + `Image.FromHbitmap` + manual `DeleteObject`, `EnsureSize` realloc). Jeremy's late "use the existing cached bitmap" is confirmed to mean WinForms' own **`BufferedGraphics`/`BufferedGraphicsContext`** (the engine behind `OptimizedDoubleBuffer`; in `System.Drawing.Common` since .NET Framework 2.0, present on target). It is **not** `System.Drawing.Imaging.CachedBitmap` — that type is a read-only, device-dependent, blit-only frozen copy (no `Graphics`, translation-only, dies on bit-depth change) and cannot be a render target. **Delete `NonClientBitmapCache` entirely** (and its file `TextBoxBase.NonClientBitmapCache.cs`) and the `_cachedBitmap` field; replace with the shared buffer. Exact wiring: + - Inside `OnNcPaint`, get the shared context and allocate the buffer against the **window-DC `Graphics` already created in `WmNcPaint`**, sized to the window `bounds`: + `BufferedGraphicsContext context = BufferedGraphicsManager.Current;` + `using BufferedGraphics buffer = context.Allocate(graphics, bounds);` + `Graphics offscreenGraphics = buffer.Graphics;` + - **Do NOT `using`/dispose `buffer.Graphics`** — the `buffer` owns it; `using` the **buffer** only. (The original `using`-disposed its `GetNewGraphics()` because it owned that `Graphics`; that ownership is now the buffer's.) + - **All drawing into `offscreenGraphics` is unchanged** — the `FillRectangle(parentBackgroundBrush…)` corner-fill, the `BorderStyle` switch, the focus line: byte-for-byte identical, just a different `Graphics` target. + - **`ExcludeClip(clientBounds)` stays on the *target* `graphics`** (the window-DC one), exactly where it is now, set *before* the buffer draws. It governs where `Render()` may blit, protecting the client area — unchanged semantics. + - Replace the final blit `graphics.DrawImageUnscaled(offscreenBitmap, Point.Empty);` with **`buffer.Render();`** (no argument — it blits to the `graphics` captured at `Allocate` time). + - While here, fix the pre-existing **double-dispose** in `WmNcPaint`: it has both `using Graphics graphics = …` and an explicit `graphics.Dispose()` in `finally`. Drop the explicit `Dispose()`; keep the `using` (or keep explicit and drop `using` — one, not both). + - **Rationale to record in PR notes:** WinForms paints NC serially (one HWND at a time on the UI thread), so a single shared buffer suffices for any number of controls; the per-instance cache kept N resident GDI bitmaps to serve a one-deep queue. Steady-state allocation is unchanged (zero — shared buffer is reused when size fits); resident GDI memory drops from N× to 1×. The only cost is buffer-resize churn if controls of *wildly varying* sizes paint in a grow/shrink-alternating order — see smoke scenario 7 instrumentation. + +8. **Carve clamp + chrome degradation (settled design — implement exactly as stated, do NOT add a minimum size).** The original `WmNcCalcSize` does raw subtraction on `rgrc[0]` with **no clamp**, so a large `Padding` (made worse because `GetVisualStylesPadding(true)` *adds* the live scrollbar allowance from `GetScrollBarPadding` on top of the border padding) can drive the carved client rect to **zero or inverted**. Two separate fixes, and they are deliberately *not* a `MinimumSize`: + - **(8a) Never-invert clamp in `WmNcCalcSize`.** Floor each carved extent so the client rect can never invert: after the four adjustments, ensure `bottom >= top` and `right >= left` (e.g. clamp so the resulting client width/height is `Math.Max(0, …)`). A 0–1px client area is **acceptable and intended** — shipping multiline `TextBox` already shrinks to ~1px with scrollbars present, and we match that exactly. **Do NOT introduce a min-height/`MinimumSize`**, and do NOT make sizing behavior differ by `VisualStylesMode` (that would fracture the appearance-only contract of the opt-in). The clamp only prevents *underflow past zero*, which raw subtraction does and plain shrinking does not. + - **(8b) Paint-time chrome degradation in `OnNcPaint`.** The rounded `Fixed3D` chrome (15px radius) renders as a broken lozenge below roughly `2 × cornerRadius + BorderThickness` in height. When the available band/height is below that viable threshold, **fall back to the original/simple chrome render** (flat or single-style border) instead of the rounded path. This is a *rendering* fallback only — it does not change size or layout. Rationale on record: if the box is so small there's no usable client area, the control isn't usable anyway, so graceful visual degradation (not a size floor) is the correct response. + +9. **Build** `System.Windows.Forms` for the target TFM. Resolve all errors. Do not suppress new analyzer warnings without a one-line justification each. + +**Deliverable:** a single commit (or tight series) on `VisualStylesNet11` plus a PR description that lists: members added/modified per file, every `TextBox.cs` conflict resolved, confirmation that `NonClientBitmapCache` was removed and replaced by `BufferedGraphics` (Step 7) with the resize-churn rationale, the clamp/degradation (Step 8) confirmed as render-only with no size minimum, the `Padding` unshadowing implications, and any flagged gaps (Step 6 `Fill` overload). + +--- + +## PROMPT 2 — Critical Review (run AFTER Prompt 1, BEFORE smoke test) + +> **Role:** Adversarial reviewer. The author wants the issues a sharp WinForms maintainer would catch, not reassurance. Cite file + line for every finding. Classify each as **MUST-FIX**, **SHOULD-FIX**, or **PRESERVE (do not 'improve')**. + +Audit the ported code against this checklist. For each item, state the finding and the exact location. + +1. **DPI scaling of the corner radius.** The original hardcodes `const int cornerRadius = 15` and `BorderThickness = 1` in device-independent units, then uses them inside a DPI-scaled NC band, while `GetVisualStylesPadding` *does* take a DPI path (`_deviceDpi`). Confirm whether the radius/thickness now scale Per-Monitor-V2. If not → **MUST-FIX** (corners look proportionally too tight at 150/200%). + +2. **Full-frame NC repaint vs. partial `hrgnClip`.** `WmNcPaint` ignores the wParam clip region and repaints the whole frame. This is the **intended** fix for the offscreen-restore "dirty corners" artifact — verify it's preserved. But confirm `base.WndProc(ref m)` is still invoked with the original message and isn't double-painting the native border under the custom chrome. Classify the "ignore clip" behavior as **PRESERVE**. + +3. **Corner-blend source = `Parent?.BackColor ?? BackColor`.** This is the known ceiling: corners blend against the parent's flat back color, so they mismatch over a gradient/image/Mica/sibling. For the common case (solid form/panel) it's correct. **PRESERVE** — do not let it be "improved" into a fake general-case solution. Note it as a documented limitation only. + +4. **`WM_NCCALCSIZE` ↔ `Padding` round-trip + underflow.** Verify the band carved in `WmNcCalcSize` matches what `GetVisualStylesPadding(true)` reports and what `GetPreferredSizeCore`/`SizeFromClientSize` assume, for all three `BorderStyle` values × `Multiline` × scrollbars. Off-by-one here clips text or the caret. **Additionally** confirm the never-invert clamp (Prompt 1 Step 8a) is present and correct: with large `Padding` on a small multiline box *with both scrollbars*, the carved client rect must floor at 0, never invert. Remember the threshold is **border padding + live scrollbar padding** (`GetScrollBarPadding` reads `WS_HSCROLL`/`WS_VSCROLL`), so underflow hits sooner than the `Padding` value alone implies. Classify "0–1px client area is allowed, no min-size" as **PRESERVE** — do not let a reviewer or the agent add a `MinimumSize` floor. + +4b. **Chrome degradation below viable height.** Confirm `OnNcPaint` (Prompt 1 Step 8b) falls back to simple/flat chrome when height < ≈`2 × cornerRadius + BorderThickness`, instead of drawing a corrupted rounded rect. This is **render-only**; assert it does **not** alter size, layout, or `ClientSize`. Verify the fallback path itself is DPI-correct (the threshold scales with the radius, which per item 1 must scale). + +5. **`BorderStyle` fork + native edge suppression.** `Fixed3D` → rounded chrome, `FixedSingle` → single + underline, `None` → fill. Confirm the native `WS_EX_CLIENTEDGE`/`WS_BORDER` from `CreateParams` is suppressed when NC chrome is active, so the native edge isn't drawn under the custom one. + +6. **`VisualStylesMode` gating is total.** Every NC entry point (`WmNcPaint`, `WmNcCalcSize`, `InitializeClientArea`, the focus/size invalidations) must early-out to byte-for-byte classic behavior when `VisualStylesMode` is `Disabled`/`Classic`. One missing guard = a back-compat regression. Note the original's `OnLostFocus` was **missing** the guard that `OnGotFocus` had — verify the port fixed this asymmetry. + +7. **DC / GDI lifetime.** `GetWindowDC`→`ReleaseDC` in `finally`, and `Graphics.FromHdc`+`Dispose` ordering: correct **only** because `FromHdc` doesn't own the DC. **PRESERVE** — flag any "tidy into a single `using`" as a regression. Confirm the `WmNcPaint` **double-dispose** was fixed (it had both `using Graphics` and an explicit `graphics.Dispose()`). For the `BufferedGraphics` swap (Prompt 1 Step 7): verify the **buffer** is `using`-scoped but **`buffer.Graphics` is NOT separately disposed**; verify `Allocate` targets the window-DC `graphics` and `Render()` is called with no argument; confirm `NonClientBitmapCache` and the `_cachedBitmap` field are fully removed with no dangling refs. Audit for any HBITMAP/HDC leak in the new path (there should be none — the buffer owns it). + +8. **DPI-change without handle recreate.** `_triggerNewClientSizeRequest` is a one-shot latch reset on handle recreate. Does a DPI change that does *not* recreate the handle re-carve the band with new padding? If the band can go stale on monitor move → **MUST-FIX** or at least an explicit tracked issue. + +9. **Caret / IME / selection repaint.** The native `EDIT` invalidates aggressively. Confirm NC chrome doesn't go stale on caret blink/IME composition, and conversely that NC isn't thrashing-repainting on every caret tick. (Author never confirmed this was clean in the original.) + +10. **`TextBox.cs` derived reconciliation.** Verify the derived `WndProc`, `OnBackColorChanged` `Fixed3D` special-case, and the `PRF_NONCLIENT`/`Application.RenderWithVisualStyles` path don't conflict with base NC painting (double draw, wrong-mode paint). + +11. **Allocation churn.** Brushes/pens use cached scopes (`GetCachedSolidBrushScope`/`GetCachedPenScope`) — good; confirm preserved. Confirm the offscreen surface isn't reallocated per paint (only on size change). + +**Deliverable:** a findings list (file:line, severity, recommendation). MUST-FIX items get fixed in this pass; SHOULD-FIX either fixed or filed; PRESERVE items annotated in code with a brief `// Intentional:` comment so the next reader doesn't "fix" them. + +--- + +## PROMPT 3 — Smoke Test Harness + +> *("Smoke test" = the shallow "does it power on without catching fire" pass — from hardware bring-up, where first power-on literally checked for smoke — run before any deep/perf testing. Goal here: broad coverage that it comes up and behaves on the obvious axes, with the two known-fragile cases as explicit named tests.)* + +**Task.** Build a throwaway WinForms test app (separate project, not shipped) that exercises the ported feature across its permutation space and **specifically reproduces the two regressions this feature is prone to.** + +**Permutation grid** — generate a form populated with `TextBox`es (and at least one `RichTextBox`, since `TextBoxBase` is the shared base) covering the cross-product of: +- `BorderStyle`: `None` × `FixedSingle` × `Fixed3D` +- `Multiline`: `false` × `true` (+ `WordWrap` on/off for multiline) +- `Padding`: `Empty` × asymmetric (e.g. `2,6,2,6`) × large (`12`) +- Scrollbars: none × vertical × both +- `VisualStylesMode`: `Disabled`/`Classic` (must look exactly like today) × `Net10`+ (new chrome) +- Focused vs unfocused (drive focus programmatically to capture the adorner/underline) + +**Named, must-pass scenarios (the ones that silently regress):** + +1. **Offscreen-restore ("dirty corners").** Move the window partly off the left/top screen edge, then back. Assert the NC corner regions are repainted clean (no stale pixels). This is the artifact that drove the full-frame-repaint design. Automate the drag via `SetWindowPos`/`MoveWindow`; capture before/after. + +2. **Partial NC invalidation.** Trigger a partial `WM_NCPAINT` (e.g. overlap then reveal a sliver of the frame) and assert the whole chrome is coherent, not just the revealed strip. + +3. **Per-Monitor-V2 DPI.** Run DPI-aware; move forms between a 100% and a 150%/200% monitor (or fake via `LogicalToDeviceUnits`/DPI-changed messages). Assert corner radius, border thickness, and padding band all scale; assert no clipped text/caret. + +4. **Classic-mode parity.** With `VisualStylesMode = Disabled`, assert the control is pixel-identical to baseline `main` (native edge, no custom NC). A regression here is the back-compat line breaking. + +5. **`BorderStyle` switch at runtime** (`Fixed3D`↔`FixedSingle`↔`None`) and **`Padding` change at runtime** — assert the band re-carves and chrome redraws without artifacts (exercises the `_triggerNewClientSizeRequest` reset path). + +6. **Focus transitions** — tab through the grid; assert the focus underline (single) / 3D focus line (Fixed3D, shortened to clear the corner curve) appears/clears correctly. + +7. **Shrink-to-collapse (clamp + degradation).** Take a multiline `Fixed3D` box with large `Padding` (e.g. `12`) and **both** scrollbars visible, then programmatically drag/resize its height down toward 1px. Assert: (a) **no crash / no inverted client rect** handed to the native `EDIT` — the carve floors at 0 (Step 8a); (b) below ≈`2 × cornerRadius + thickness` the chrome **falls back to flat/simple render** rather than drawing a corrupted lozenge (Step 8b); (c) the control **still collapses** to ~1px exactly like classic multiline — assert it is **not** held open by any min-size (regression if a floor appeared). Repeat at 150%/200% DPI so the degradation threshold is verified scaled, not fixed at 96-dpi pixels. + +8. **BufferedGraphics allocation churn (perf sanity).** Build two forms: (a) **40 same-size** textboxes, (b) **40 wildly varying-size** textboxes (mix tiny and large), all `VisualStylesMode ≥ Net10`. Force a full repaint storm (invalidate all NC frames repeatedly; resize the form to cascade re-layout). Instrument the shared buffer: wrap/observe `BufferedGraphicsManager.Current` and count actual **bitmap (re)allocations** vs. reuses across the storm. Assert: case (a) allocates the buffer ≈once then reuses (steady-state alloc ≈ 0); case (b) may reallocate on grow but must **not** allocate-per-paint. Log alloc count per case. This empirically confirms the shared-buffer reasoning from Prompt 1 Step 7 and catches any accidental per-paint allocation regression. + +**Harness mechanics:** +- A "capture all" button that screenshots each form to disk per `VisualStylesMode`, for eyeball diffing classic-vs-modern and pre-vs-post-DPI. +- A console/log line per assertion (pass/fail) so it can run semi-automated. +- Keep it dependency-light: raw WinForms + `SetWindowPos`/`RedrawWindow` P/Invoke for the offscreen and invalidation drivers. + +**Deliverable:** the test project + a one-screen README naming the six scenarios and how to run them, plus a results log from one full run on the porter's machine (note DPI of monitors used). diff --git a/.github/skills/building-code/SKILL.md b/.github/skills/building-code/SKILL.md index 040dd437081..67fd8c7ba30 100644 --- a/.github/skills/building-code/SKILL.md +++ b/.github/skills/building-code/SKILL.md @@ -11,6 +11,22 @@ metadata: # Building the WinForms Repository +> ## 🛑 TENET — Build the solution ONLY with `build.cmd` +> +> **Never** build, validate, or declare the WinForms solution "clean" with a plain +> `dotnet build` / `dotnet msbuild` of `Winforms.sln`. Only **`build.cmd`** (Arcade) applies the +> repository's CI configuration — the **PublicAPI analyzer (RS0016/RS0017)**, the code-style and +> documentation analyzers, and **`-warnAsError`**. A plain `dotnet build` silently downgrades or +> skips these, so **"0 warnings" there does NOT mean CI is green** — the very same change can fail +> the official build with errors. +> +> * **Full / release / package / "is it clean?" verification → always `build.cmd`** (see §2). +> * A single-project `dotnet build` (see §3) is an **inner-loop convenience only**. It is fine while +> iterating, but you **must re-verify with `build.cmd` before claiming a change builds cleanly**. +> * If `build.cmd` cannot run in your environment, the closest fallback is +> `dotnet build /p:ContinuousIntegrationBuild=true /p:TreatWarningsAsErrors=true` — and +> you must say so explicitly rather than implying a `build.cmd` result. + ## Prerequisites * Windows is required for WinForms runtime scenarios, test execution, and Visual @@ -45,6 +61,12 @@ You can pass any extra `Build.ps1` flags after `Restore.cmd`, e.g. ## 2 Full Solution Build (preferred) +> **Always use `build.cmd` (Arcade) for full, release, and package builds.** Do **not** use a plain +> `dotnet build` of the solution for these — only `build.cmd` guarantees the Arcade-supported build +> options and the download of the correct base SDK (`global.json`) needed to compile. Plain +> `dotnet build` is reserved for the fast single-project inner loop (see Section 3), and even then +> only after at least one successful `build.cmd` / `Restore.cmd`. + ``` .\build.cmd ``` @@ -57,6 +79,56 @@ Under the hood this runs: eng\common\Build.ps1 -NativeToolsOnMachine -restore -build -bl ``` +### 2.1 Full (clean) test build — required workflow + +For a full, clean build of the whole solution, **clean the artifacts first, then build**: + +```powershell +# 1. Clean the artifacts folder. +.\build -clean + +# 2. Build the full solution. +.\build +``` + +**Reporting requirement:** a full build is long-running, so while it runs **report progress back to +the user in the console to bridge the wait and give early orientation.** As assemblies complete, +report which assemblies have been **built successfully** and which **failed and with how many +errors**. Prefer running the build with a binary log (the default `-bl`) and/or stream the console +output so per-project results can be surfaced as they happen rather than only at the end. + +### 2.2 Release build + +```powershell +.\build -configuration release +``` + +### 2.3 Creating packages + +```powershell +# Debug packages +.\build -pack + +# Release packages +.\build -configuration release -pack +``` + +### 2.4 Full `Build.ps1` parameter list + +`build.cmd` forwards every extra argument to `eng\common\Build.ps1`. The full surface is: + +``` +Build.ps1 [-configuration ] [-platform ] [-projects ] + [-verbosity ] [-msbuildEngine ] [-warnAsError ] + [-warnNotAsError ] [-nodeReuse ] [-buildCheck] [-restore] + [-deployDeps] [-build] [-rebuild] [-deploy] [-test] [-integrationTest] + [-performanceTest] [-sign] [-pack] [-publish] [-clean] [-productBuild] + [-fromVMR] [-binaryLog] [-binaryLogName ] [-excludeCIBinarylog] + [-ci] [-prepareMachine] [-runtimeSourceFeed ] + [-runtimeSourceFeedKey ] [-excludePrereleaseVS] + [-nativeToolsOnMachine] [-help] [-properties ] [] +``` + ### Common flags | Flag | Short | Description | @@ -90,6 +162,12 @@ eng\common\Build.ps1 -NativeToolsOnMachine -restore -build -bl ## 3 Optimized Building a Single Project (fast inner-loop) +> **Inner-loop only.** Use plain `dotnet build` of a single project **only** for quick iteration on +> one project, and **only after** at least one successful `.\build.cmd` / `.\Restore.cmd`. It does +> **not** guarantee the Arcade-supported build options or the download of the correct base SDK, so it +> must **never** be used for a full solution build, a release build, packaging, or any build whose +> result you intend to report as authoritative. For those, always use `build.cmd` (Section 2). + Prefer rebuilding just the project(s) with recent changes by using the standard `dotnet build` command, **after** at least one initial successful full restore (via `.\Restore.cmd` or `.\build.cmd`). diff --git a/.github/skills/control-api-tests/SKILL.md b/.github/skills/control-api-tests/SKILL.md index 5badcc2547c..ff4e9304230 100644 --- a/.github/skills/control-api-tests/SKILL.md +++ b/.github/skills/control-api-tests/SKILL.md @@ -66,6 +66,28 @@ The project uses **xUnit** with **FluentAssertions**. Key attributes: These are custom xUnit attributes that ensure tests run on an STA thread, which WinForms requires for COM interop and UI operations. +### 1.4 Async tests: pass a CancellationToken, respect `#nullable` + +The repository runs **xUnit v3** and enforces the relevant analyzers as **errors** under the CI +build (`build.cmd`). Two pitfalls fail CI even though a plain `dotnet build` may not flag them: + +* **CA2016 / xUnit1051 — always pass a `CancellationToken` to async calls.** Methods such as + `Task.Delay` must receive a token so a cancelled test run stops promptly. In xUnit v3 use + `TestContext.Current.CancellationToken`: + + ```csharp + await Task.Delay(25, TestContext.Current.CancellationToken); + ``` + + When you receive a `CancellationToken ct` (e.g. in a callback), **forward it** rather than dropping it. + +* **CS8632 — nullable annotations need a `#nullable` context.** If a test file uses `?` reference + annotations (e.g. `object? sender`) but the project does not enable nullable, add `#nullable enable` + at the top of the file (or remove the annotation). Match the surrounding files' convention. + +> Verify with `build.cmd` (CI parity) — see the `building-code` skill's build tenet. A plain +> single-project `dotnet build` can report these as 0 warnings while CI fails them as errors. + --- ## 2. Test Method Naming diff --git a/.github/skills/new-control-api/SKILL.md b/.github/skills/new-control-api/SKILL.md index ce6c58b6d2e..3030cf92ef7 100644 --- a/.github/skills/new-control-api/SKILL.md +++ b/.github/skills/new-control-api/SKILL.md @@ -148,7 +148,34 @@ System.Windows.Forms.MyEnum.Value2 = 1 -> System.Windows.Forms.MyEnum **Nullable annotations:** `?` = nullable reference, `!` = non-nullable reference. Value types do not carry these markers unless `Nullable`. -### 2.4 Publicly accessible interfaces +### 2.4 New `override` members must be tracked too + +The PublicAPI analyzer (RS0016) treats a **newly introduced `override`** of a public or +protected member as new API surface — even though the base member is already public. Whenever +you **add an `override` that did not previously exist on that type**, add a line for it to +`PublicAPI.Unshipped.txt` with the `override` prefix. This is easy to miss for paint/lifecycle +overrides added to support a feature. Examples: + +```text +override System.Windows.Forms.CheckBox.OnPaint(System.Windows.Forms.PaintEventArgs! pevent) -> void +override System.Windows.Forms.CheckBox.Dispose(bool disposing) -> void +override System.Windows.Forms.ButtonBase.OnVisualStylesModeChanged(System.EventArgs! e) -> void +``` + +> **CI catches this, a plain `dotnet build` may not.** RS0016 is enforced as an **error** under +> the CI/Arcade build (`build.cmd`); a single-project `dotnet build` can report it as 0 warnings. +> Always re-verify API tracking with `build.cmd` (see the `building-code` skill's build tenet). + +### 2.5 Related pitfalls when adding members to a control + +* **Hiding an inherited member (CS0114):** if your new member intentionally hides an inherited + one (e.g. a `private new bool ShouldSerializePadding()` shadowing `Control.ShouldSerializePadding()`), + you **must** use the `new` keyword, or the CI build fails. +* **`cref` to internal types in another assembly (CS1574):** XML-doc `` cannot + resolve a type that is `internal` in a *different* assembly (even via `InternalsVisibleTo`). Use + `TypeName` (plain code font) instead of a `cref` for such references. + +### 2.6 Publicly accessible interfaces If a new **public or protected interface** is introduced (or an existing one gains new members), every member that is publicly accessible must also appear @@ -468,51 +495,73 @@ protected virtual void OnMyPropertyChanged(EventArgs e) --- -## 7. .NET Version Guard — Mandatory +## 7. API Stability: Experimental vs. Stable — and Version Guards -All new public APIs **must** be guarded with a preprocessor directive for the -target .NET version. Currently, new APIs target at least **.NET 11**: +### 7.1 New APIs are STABLE by default — do NOT mark them `[Experimental]` -```csharp -#if NET11_0_OR_GREATER - /// - /// Gets or sets the corner radius for the control's border. - /// - public int CornerRadius - { - get => Properties.GetValueOrDefault(s_cornerRadiusProperty, 0); - set - { - ArgumentOutOfRangeException.ThrowIfNegative(value); +New public APIs ship as **normal, stable APIs by default**. Do **not** add the +`[Experimental(...)]` attribute, a `WFO5xxx` diagnostic ID, or `[WFO5xxx]` +PublicAPI prefixes unless the work item **explicitly** asks for an experimental +API. - if (Properties.GetValueOrDefault(s_cornerRadiusProperty, 0) != value) - { - Properties.AddOrRemoveValue(s_cornerRadiusProperty, value, defaultValue: 0); - OnCornerRadiusChanged(EventArgs.Empty); - } - } - } -#endif -``` +> **Never make an API experimental implicitly.** Experimental status is a +> deliberate, requested decision (it changes the customer contract and requires a +> diagnostic ID + suppression to consume). If the context does not explicitly call +> for it, the API is stable. + +### 7.2 When an experimental API *is* explicitly requested + +Only when the task explicitly requests an experimental API: + +1. Add (or reuse) a diagnostic ID in the `WFO500x` group in + `src\System.Windows.Forms.Analyzers\src\System\Windows\Forms\Analyzers\Diagnostics\DiagnosticIDs.cs` + (e.g. `ExperimentalDarkMode = "WFO5001"`, `ExperimentalAsync = "WFO5002"`, + `ExperimentalAsyncDropTarget = "WFO5003"`). New IDs continue the sequence. +2. Decorate the API: + ```csharp + [Experimental(DiagnosticIDs.ExperimentalXxx, UrlFormat = DiagnosticIDs.UrlFormat)] + ``` +3. Prefix every PublicAPI entry for that API with the diagnostic ID, e.g. + `[WFO5001]System.Windows.Forms.SomeNewApi.get -> ...`. +4. Add a row to **both** `docs\analyzers\Experimental.Help.md` and + `docs\list-of-diagnostics.md`. +5. Suppress the diagnostic where the framework itself consumes the API + (`#pragma warning disable WFOxxxx` / `#Disable Warning WFOxxxx` in VB). -> **Why?** Version guards ensure new APIs are only available on the .NET version -> they were approved for, preventing accidental use on older runtimes. The guard -> applies to the entire API surface: property, event, `On` method, and any -> associated types. +When the API later **graduates to stable** (typically the next release), reverse +all five steps: remove the attribute, the `[WFOxxxx]` PublicAPI prefixes, the +suppressions, the docs rows, and the unused diagnostic ID. -The matching tests must use the **same** preprocessor guard: +### 7.3 Version guards + +This repository **single-targets the current in-development .NET** (see +`TargetFramework` / `NetCurrent`), so source is **not** wrapped in +`#if NETxx_0_OR_GREATER` guards — there are none in `System.Windows.Forms`. Do +**not** add `#if NET11_0_OR_GREATER` blocks around new APIs. Add the member +directly: ```csharp -#if NET11_0_OR_GREATER - [WinFormsFact] - public void MyControl_CornerRadius_Set_GetReturnsExpected() +/// +/// Gets or sets the corner radius for the control's border. +/// +public int CornerRadius +{ + get => Properties.GetValueOrDefault(s_cornerRadiusProperty, 0); + set { - using MyControl control = new() { CornerRadius = 5 }; - Assert.Equal(5, control.CornerRadius); + ArgumentOutOfRangeException.ThrowIfNegative(value); + + if (Properties.GetValueOrDefault(s_cornerRadiusProperty, 0) != value) + { + Properties.AddOrRemoveValue(s_cornerRadiusProperty, value, defaultValue: 0); + OnCornerRadiusChanged(EventArgs.Empty); + } } -#endif +} ``` +Tests do not need a version guard either. + --- ## 8. Checklist Before Submitting @@ -521,7 +570,8 @@ Before considering the implementation complete, verify: * [ ] API proposal issue exists (upstream or fork) with full proposal format * [ ] All new public/protected members are in `PublicAPI.Unshipped.txt` -* [ ] New APIs guarded with `#if NET11_0_OR_GREATER` (or appropriate version) +* [ ] API is **stable** (no `[Experimental]`/`WFO5xxx`) unless experimental was + explicitly requested; no `#if NETxx_0_OR_GREATER` guards * [ ] Property values stored via `PropertyStore` (not backing fields) * [ ] Every property has a CodeDOM serialization strategy * [ ] Every property has `On[Property]Changed` + `[Property]Changed` event @@ -533,7 +583,7 @@ Before considering the implementation complete, verify: * [ ] XML documentation on every new public/protected member * [ ] Naming follows precedent on the control and its base classes * [ ] Publicly accessible interface members are tracked in PublicAPI files -* [ ] Unit tests cover the new API surface (with matching version guard) +* [ ] Unit tests cover the new API surface ### 8.1 API issue checklist diff --git a/Winforms.sln b/Winforms.sln index fef423d18a4..4deb770ce1b 100644 --- a/Winforms.sln +++ b/Winforms.sln @@ -197,9 +197,6 @@ EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "System.Private.Windows.GdiPlus", "src\System.Private.Windows.GdiPlus\System.Private.Windows.GdiPlus.csproj", "{442C867C-51C0-8CE5-F067-DF065008E3DA}" EndProject Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Copilot", "Copilot", "{02EA681E-C7D8-13C7-8484-4AC65E1B71E8}" - ProjectSection(SolutionItems) = preProject - .github\copilot-instructions.md = .github\copilot-instructions.md - EndProjectSection EndProject Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "GDI", "GDI", "{D619FF8C-D99A-48AB-B16B-2F0E819B46D5}" ProjectSection(SolutionItems) = preProject @@ -218,6 +215,13 @@ Project("{D954291E-2A0B-460D-934E-DC6B0785DB48}") = "Microsoft.Private.Windows.P EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Microsoft.Private.Windows.Core", "src\System.Private.Windows.Core\src\Microsoft.Private.Windows.Core.csproj", "{36A02BBB-B60B-5F23-6AF0-F41561A9275C}" EndProject +Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Application", "Application", "{4D53E144-73EF-49BC-BF11-F416E187944A}" +EndProject +Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "net11-VisualStylesMode", "net11-VisualStylesMode", "{59D2720E-DBA9-4282-869E-0717D6311BDF}" + ProjectSection(SolutionItems) = preProject + .github\copilot\Application\net11-VisualStylesMode\TextBoxBase-VisualStyles-WorkOrder.md = .github\copilot\Application\net11-VisualStylesMode\TextBoxBase-VisualStyles-WorkOrder.md + EndProjectSection +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -1188,6 +1192,8 @@ Global {4A2BD741-482C-4BF7-8A2D-5535A770DB69} = {583F1292-AE8D-4511-B8D8-A81FE4642DDC} {799CC0C2-236B-4A76-8CE3-65C346182CC1} = {77FEDB47-F7F6-490D-AF7C-ABB4A9E0B9D7} {36A02BBB-B60B-5F23-6AF0-F41561A9275C} = {77FEDB47-F7F6-490D-AF7C-ABB4A9E0B9D7} + {4D53E144-73EF-49BC-BF11-F416E187944A} = {02EA681E-C7D8-13C7-8484-4AC65E1B71E8} + {59D2720E-DBA9-4282-869E-0717D6311BDF} = {4D53E144-73EF-49BC-BF11-F416E187944A} EndGlobalSection GlobalSection(ExtensibilityGlobals) = postSolution SolutionGuid = {7B1B0433-F612-4E5A-BE7E-FCF5B9F6E136} diff --git a/docs/Feature-Prompts/SuspendRelocationPaintingFlashPreventing/Create-Github-API-Proposal-Prompt.md b/docs/Feature-Prompts/SuspendRelocationPaintingFlashPreventing/Create-Github-API-Proposal-Prompt.md new file mode 100644 index 00000000000..ce3ad461cab --- /dev/null +++ b/docs/Feature-Prompts/SuspendRelocationPaintingFlashPreventing/Create-Github-API-Proposal-Prompt.md @@ -0,0 +1,120 @@ +# Copilot Prompt 1 — Author the WinForms API Suggestion + +## Your task + +Write a complete API suggestion for the `dotnet/winforms` repository, ready to be filed +as a GitHub issue with the `api-suggestion` label. The issue covers two related features +for reducing visible intermediate states while WinForms applications update their UI: + +1. Painting suspension with optional layout suspension across a control tree. +2. Deferred top-level form reveal. + +Use these issue sections: + +- `## Rationale` +- `## API Proposal` +- `## API Usage` +- `## Alternative Designs` +- `## Risks` +- `## Will this feature affect UI controls?` +- `### Status Checklist` + +## Sub-feature A — painting and layout suspension + +### Settled API surface + +```csharp +namespace System.Windows.Forms; + +public interface ISupportSuspendPainting +{ + void BeginSuspendPainting(); + void EndSuspendPainting(); +} + +public enum LayoutSuspendTraversal +{ + None = 0, + TopLevelOnly = 1, + Traverse = 2, +} + +public sealed class SuspendPaintingScope : IDisposable +{ + public SuspendPaintingScope(ISupportSuspendPainting? target); + public void Dispose(); +} + +public static class ControlMutationExtensions +{ + public static SuspendPaintingScope SuspendPainting( + this ISupportSuspendPainting target); + + public static SuspendPaintingScope SuspendPainting( + this ISupportSuspendPainting target, + LayoutSuspendTraversal layoutSuspendTraversal); + + public static SuspendPaintingScope SuspendPainting( + this ISupportSuspendPainting target, + Func suspendLayoutContainerFilter); +} +``` + +- `Control` implements `ISupportSuspendPainting` explicitly and exposes protected virtual + `BeginSuspendPaintingCore` / `EndSuspendPaintingCore` hooks. +- `ListView`, `ListBox`, `ComboBox`, `TreeView`, and `RichTextBox` route painting + suspension through their existing `BeginUpdate` / `EndUpdate` paths. +- `None` suspends painting only. +- `TopLevelOnly` suspends layout on the target `Control`. +- `Traverse` suspends layout on the target and all existing descendants. +- The predicate is evaluated for the target and every descendant. A `false` result skips + that node but does not prune traversal. +- Selected nodes are suspended even when they currently have no children. +- Layout-aware overloads require the target to derive from `Control`. +- The scope is a sealed class so it can span `await`. + +### Design considerations + +- Snapshot the selected controls when the scope starts. +- Suspend layout root-to-leaf and resume it deepest-first. +- Resume layout before ending painting so recursive invalidation occurs after layout. +- Keep disposal idempotent and preserve nesting through the existing ref counts. +- Explain that controls added after the snapshot are covered by their parent's suspended + layout but do not receive an independently balanced suspension. +- Discuss traversal cost for large control trees and exceptions thrown by predicates. +- Include invalid and non-`Control` target behavior in the proposal. + +## Sub-feature B — deferred form reveal + +Use the current `FormRevealMode` design from the tracked API proposal: + +```csharp +namespace System.Windows.Forms; + +public enum FormRevealMode +{ + Inherit = -1, + Classic = 0, + Deferred = 1, +} + +public partial class Form +{ + public virtual FormRevealMode FormRevealMode { get; set; } +} + +public partial class Application +{ + public static FormRevealMode DefaultFormRevealMode { get; } + public static void SetDefaultFormRevealMode(FormRevealMode mode); + public static bool IsFormRevealDeferred { get; } +} +``` + +Describe DWM cloaking, dark-mode-aware default resolution, designer serialization, +top-level-window limitations, and conservative fallback behavior on unsupported systems. + +## Filing instruction + +Produce a complete proposal rather than an implementation plan. Clearly distinguish fixed +API shape from implementation choices that remain open for review. diff --git a/docs/Feature-Prompts/SuspendRelocationPaintingFlashPreventing/SuspendRelocationAndPainting-API-Feature-Prompt.md b/docs/Feature-Prompts/SuspendRelocationPaintingFlashPreventing/SuspendRelocationAndPainting-API-Feature-Prompt.md new file mode 100644 index 00000000000..35d765ad3ce --- /dev/null +++ b/docs/Feature-Prompts/SuspendRelocationPaintingFlashPreventing/SuspendRelocationAndPainting-API-Feature-Prompt.md @@ -0,0 +1,76 @@ +# Copilot Prompt 2 — Implement the flicker-free UI mutation APIs + +## Prerequisite + +Read the current upstream API suggestion before implementation. Treat its latest +`API Proposal` section as the public contract and report any conflict instead of silently +choosing a different shape. + +## Scope + +### A — painting suspension with optional layout traversal + +- Implement `ISupportSuspendPainting` on `Control` with ref-counted + `BeginSuspendPaintingCore` / `EndSuspendPaintingCore` hooks. +- Route `ListView`, `ListBox`, `ComboBox`, `TreeView`, and `RichTextBox` through their + existing `BeginUpdate` / `EndUpdate` mechanisms. +- Keep `SuspendPaintingScope` as a sealed, idempotent `IDisposable` class so it can span + `await`. +- Provide these extension methods: + + ```csharp + public static SuspendPaintingScope SuspendPainting( + this ISupportSuspendPainting target); + + public static SuspendPaintingScope SuspendPainting( + this ISupportSuspendPainting target, + LayoutSuspendTraversal layoutSuspendTraversal); + + public static SuspendPaintingScope SuspendPainting( + this ISupportSuspendPainting target, + Func suspendLayoutContainerFilter); + ``` + +- `LayoutSuspendTraversal` has `None`, `TopLevelOnly`, and `Traverse`. +- Snapshot selected controls before suspension. Suspend root-to-leaf and resume + deepest-first. +- The predicate is evaluated for the target and every descendant. A `false` result skips + suspension for that node without pruning its descendants. +- Suspend selected controls even when they currently have no children. +- Validate layout-aware calls before beginning painting. A non-`Control` target is invalid. +- Resume all selected layout scopes before ending painting so the recursive invalidation + occurs after layout. + +### B — deferred form reveal + +- Implement the approved `FormRevealMode`, `Form.FormRevealMode`, and + `Application` configuration APIs. +- Preserve classic behavior when deferred reveal is not active. +- Cloak eligible top-level form handles and uncloak according to the timing strategy in the + proposal. +- Keep unsupported DWM and window configurations inert and safe. + +## Engineering requirements + +- Match the current repository language version, nullable annotations, code style, and + interop conventions. +- Add XML documentation for every public or protected API. +- Update `PublicAPI.Unshipped.txt`. +- Keep state lazy where existing `Control` property-store patterns apply. +- Do not change existing public `BeginUpdate` / `EndUpdate` behavior. + +## Tests + +- Painting ref-count balance, nesting, idempotent disposal, handle creation, and handle + recreation. +- `None`, `TopLevelOnly`, and `Traverse` layout selection. +- Predicate selection, continued traversal after a rejected node, and empty containers. +- Deepest-first layout resume and recursive invalidation after layout. +- Null, invalid-enum, and non-`Control` target failures without partial suspension. +- Existing native update paths for the selected built-in controls. +- Classic/deferred form reveal behavior and unsupported-OS fallback. + +## Deliverable + +Provide production code, focused tests, updated API tracking, and an updated API proposal. +Call out any behavior that the implementation proves unsafe or unnecessarily costly. diff --git a/src/BuildAssist/BuildAssist.msbuildproj b/src/BuildAssist/BuildAssist.msbuildproj index 2e188f76ff6..400c78f44e6 100644 --- a/src/BuildAssist/BuildAssist.msbuildproj +++ b/src/BuildAssist/BuildAssist.msbuildproj @@ -37,7 +37,7 @@ @@ -32,6 +36,18 @@ Namespace Microsoft.VisualBasic.ApplicationServices ''' Public Property ColorMode As SystemColorMode + ''' + ''' Setting this property inside the event handler determines the default + ''' for newly created top-level forms. + ''' + Public Property FormRevealMode As FormRevealMode + + ''' + ''' Setting this property inside the event handler determines the + ''' for the application. + ''' + Public Property VisualStylesMode As VisualStylesMode + ''' ''' Setting this property inside the event handler causes a ''' new default for Forms and UserControls to be set. diff --git a/src/Microsoft.VisualBasic.Forms/src/Microsoft/VisualBasic/ApplicationServices/WindowsFormsApplicationBase.vb b/src/Microsoft.VisualBasic.Forms/src/Microsoft/VisualBasic/ApplicationServices/WindowsFormsApplicationBase.vb index 6e14d471534..e5a63f8b79f 100644 --- a/src/Microsoft.VisualBasic.Forms/src/Microsoft/VisualBasic/ApplicationServices/WindowsFormsApplicationBase.vb +++ b/src/Microsoft.VisualBasic.Forms/src/Microsoft/VisualBasic/ApplicationServices/WindowsFormsApplicationBase.vb @@ -3,7 +3,6 @@ Imports System.Collections.ObjectModel Imports System.ComponentModel -Imports System.Diagnostics.CodeAnalysis Imports System.IO.Pipes Imports System.Reflection Imports System.Runtime.CompilerServices @@ -11,7 +10,6 @@ Imports System.Runtime.InteropServices Imports System.Security Imports System.Threading Imports System.Windows.Forms -Imports System.Windows.Forms.Analyzers.Diagnostics Imports VbUtils = Microsoft.VisualBasic.CompilerServices.ExceptionUtils @@ -69,6 +67,12 @@ Namespace Microsoft.VisualBasic.ApplicationServices ' Note: We aim to expose this to the App Designer in later runtime/VS versions. Private _colorMode As SystemColorMode = SystemColorMode.Classic + ' The FormRevealMode the user assigned to the ApplyApplicationsDefault event. + Private _formRevealMode As FormRevealMode = FormRevealMode.Classic + + ' The VisualStylesMode (renderer version) the user assigned to the ApplyApplicationDefaults event. + Private _visualStylesMode As VisualStylesMode = VisualStylesMode.Classic + ' We only need to show the splash screen once. ' Protect the user from himself if they are overriding our app model. Private _didSplashScreen As Boolean @@ -200,6 +204,39 @@ Namespace Microsoft.VisualBasic.ApplicationServices End Set End Property + ''' + ''' Gets or sets the for the Application. + ''' + ''' + ''' The that newly created top-level forms use by + ''' default. + ''' + + Protected Property FormRevealMode As FormRevealMode + Get + Return _formRevealMode + End Get + Set(value As FormRevealMode) + _formRevealMode = value + End Set + End Property + + ''' + ''' Gets or sets the (renderer version) for the application. + ''' + ''' + ''' The that the application uses to render its controls. + ''' + + Protected Property VisualStylesMode As VisualStylesMode + Get + Return _visualStylesMode + End Get + Set(value As VisualStylesMode) + _visualStylesMode = value + End Set + End Property + ''' ''' Determines whether this application will use the XP Windows styles for windows, controls, etc. ''' @@ -734,7 +771,7 @@ Namespace Microsoft.VisualBasic.ApplicationServices ' in a derived class and setting `MyBase.MinimumSplashScreenDisplayTime` there. ' We are picking this (probably) changed value up, and pass it to the ApplyDefaultsEvents ' where it could be modified (again). So event wins over Override over default value (2 seconds). - ' b) We feed the defaults for HighDpiMode, ColorMode, VisualStylesMode to the EventArgs. + ' b) We feed the defaults for HighDpiMode, ColorMode, FormRevealMode, and VisualStylesMode to the EventArgs. ' With the introduction of the HighDpiMode property, we changed Project System the chance to reflect ' those default values in the App Designer UI and have it code-generated based on a modified ' Application.myapp, which would result it to be set in the derived constructor. @@ -746,7 +783,9 @@ Namespace Microsoft.VisualBasic.ApplicationServices Dim applicationDefaultsEventArgs As New ApplyApplicationDefaultsEventArgs( MinimumSplashScreenDisplayTime, HighDpiMode, - ColorMode) With + ColorMode, + FormRevealMode, + VisualStylesMode) With { .MinimumSplashScreenDisplayTime = MinimumSplashScreenDisplayTime } @@ -765,6 +804,8 @@ Namespace Microsoft.VisualBasic.ApplicationServices _highDpiMode = applicationDefaultsEventArgs.HighDpiMode _colorMode = applicationDefaultsEventArgs.ColorMode + _formRevealMode = applicationDefaultsEventArgs.FormRevealMode + _visualStylesMode = applicationDefaultsEventArgs.VisualStylesMode ' Then, it's applying what we got back as HighDpiMode. Dim dpiSetResult As Boolean = Application.SetHighDpiMode(_highDpiMode) @@ -780,7 +821,10 @@ Namespace Microsoft.VisualBasic.ApplicationServices Application.EnableVisualStyles() End If + Application.SetDefaultVisualStylesMode(_visualStylesMode) + Application.SetColorMode(_colorMode) + Application.SetDefaultFormRevealMode(_formRevealMode) ' We'll handle "/nosplash" for you. If Not (commandLineArgs.Contains("/nosplash") OrElse Me.CommandLineArgs.Contains("-nosplash")) Then diff --git a/src/Microsoft.VisualBasic.Forms/src/PublicAPI.Unshipped.txt b/src/Microsoft.VisualBasic.Forms/src/PublicAPI.Unshipped.txt index e69de29bb2d..93b55542d62 100644 --- a/src/Microsoft.VisualBasic.Forms/src/PublicAPI.Unshipped.txt +++ b/src/Microsoft.VisualBasic.Forms/src/PublicAPI.Unshipped.txt @@ -0,0 +1,8 @@ +Microsoft.VisualBasic.ApplicationServices.ApplyApplicationDefaultsEventArgs.FormRevealMode() -> System.Windows.Forms.FormRevealMode +Microsoft.VisualBasic.ApplicationServices.ApplyApplicationDefaultsEventArgs.FormRevealMode(AutoPropertyValue As System.Windows.Forms.FormRevealMode) -> Void +Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.FormRevealMode() -> System.Windows.Forms.FormRevealMode +Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.FormRevealMode(value As System.Windows.Forms.FormRevealMode) -> Void +Microsoft.VisualBasic.ApplicationServices.ApplyApplicationDefaultsEventArgs.VisualStylesMode() -> System.Windows.Forms.VisualStylesMode +Microsoft.VisualBasic.ApplicationServices.ApplyApplicationDefaultsEventArgs.VisualStylesMode(AutoPropertyValue As System.Windows.Forms.VisualStylesMode) -> Void +Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.VisualStylesMode() -> System.Windows.Forms.VisualStylesMode +Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.VisualStylesMode(value As System.Windows.Forms.VisualStylesMode) -> Void diff --git a/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/Forms/WindowsFormsApplicationBaseTests.vb b/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/Forms/WindowsFormsApplicationBaseTests.vb index a03679253d7..701b5d4ca82 100644 --- a/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/Forms/WindowsFormsApplicationBaseTests.vb +++ b/src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/Forms/WindowsFormsApplicationBaseTests.vb @@ -21,6 +21,8 @@ Namespace Microsoft.VisualBasic.Forms.Tests ColorMode = SystemColorMode.Dark ColorMode.Should.Be(SystemColorMode.Dark) + VisualStylesMode.Should.Be(VisualStylesMode.Classic) + EnableVisualStyles.Should.Be(False) EnableVisualStyles = True EnableVisualStyles.Should.Be(True) @@ -113,6 +115,31 @@ Namespace Microsoft.VisualBasic.Forms.Tests End If End Sub + + Public Sub OnInitialize_ApplyApplicationDefaults_VisualStylesModeFlowsToApplication() + If RemoteExecutor.IsSupported Then + Dim test As Action = + Sub() + Dim appModel As New SubWindowsFormsApplicationBase() With + { + .EnableVisualStylesCore = True + } + + AddHandler appModel.ApplyApplicationDefaults, + Sub(sender, e) + e.VisualStylesMode = VisualStylesMode.Latest + End Sub + + appModel.CallOnInitialize(Array.Empty(Of String)()).Should.BeTrue() + System.Windows.Forms.Application.DefaultVisualStylesMode.Should.Be(VisualStylesMode.Latest) + End Sub + + Using handle As RemoteInvokeHandle = RemoteExecutor.Invoke(test) + handle.ExitCode.Should.Be(RemoteExecutor.SuccessExitCode) + End Using + End If + End Sub + Public Sub ShowHideSplashScreenSuccess() Dim testCode As Action @@ -158,5 +185,19 @@ Namespace Microsoft.VisualBasic.Forms.Tests End If End Sub + Private NotInheritable Class SubWindowsFormsApplicationBase + Inherits WindowsFormsApplicationBase + + Public Function CallOnInitialize(commandLineArgs As String()) As Boolean + Return MyBase.OnInitialize(Array.AsReadOnly(commandLineArgs)) + End Function + + Public WriteOnly Property EnableVisualStylesCore As Boolean + Set(value As Boolean) + EnableVisualStyles = value + End Set + End Property + End Class + End Class End Namespace diff --git a/src/System.Private.Windows.Core/src/Microsoft.Private.Windows.Core.csproj b/src/System.Private.Windows.Core/src/Microsoft.Private.Windows.Core.csproj index 028ddf0be71..f63dbaa5496 100644 --- a/src/System.Private.Windows.Core/src/Microsoft.Private.Windows.Core.csproj +++ b/src/System.Private.Windows.Core/src/Microsoft.Private.Windows.Core.csproj @@ -58,6 +58,10 @@ + + + + diff --git a/src/System.Private.Windows.Core/src/NativeMethods.txt b/src/System.Private.Windows.Core/src/NativeMethods.txt index cc17c7c0a45..ddea7b6c144 100644 --- a/src/System.Private.Windows.Core/src/NativeMethods.txt +++ b/src/System.Private.Windows.Core/src/NativeMethods.txt @@ -119,6 +119,7 @@ GetSystemMetrics GetThreadLocale GetViewportExtEx GetViewportOrgEx +GetWindowDC GetWindowOrgEx GetWindowRect GetWindowText @@ -150,6 +151,7 @@ HINSTANCE HPEN HPROPSHEETPAGE HRGN +HSTRING HWND HWND_* IDataObject @@ -164,6 +166,7 @@ IDropTargetHelper IEnumFORMATETC IEnumUnknown IGlobalInterfaceTable +IInspectable ImageFormat* ImageLockMode INK_SERIALIZED_FORMAT @@ -188,6 +191,7 @@ MonitorFromWindow MONITORINFOEXW MONITORINFOF_* MultiByteToWideChar +NCCALCSIZE_PARAMS NONCLIENTMETRICSW NS_E_WMP_CANNOT_FIND_FILE NS_E_WMP_DSHOW_UNSUPPORTED_FORMAT @@ -230,6 +234,7 @@ ReleaseDC ReleaseStgMedium RestoreDC RevokeDragDrop +RoActivateInstance RPC_E_CHANGED_MODE RPC_E_DISCONNECTED RPC_E_SERVERFAULT @@ -277,5 +282,7 @@ WIN32_ERROR WINCODEC_ERR_* WINDOW_LONG_PTR_INDEX WindowFromDC +WindowsCreateString +WindowsDeleteString WM_* WPARAM \ No newline at end of file diff --git a/src/System.Private.Windows.Core/src/Windows/Win32/UI/ViewManagement/IUISettings3.cs b/src/System.Private.Windows.Core/src/Windows/Win32/UI/ViewManagement/IUISettings3.cs new file mode 100644 index 00000000000..15028e00045 --- /dev/null +++ b/src/System.Private.Windows.Core/src/Windows/Win32/UI/ViewManagement/IUISettings3.cs @@ -0,0 +1,63 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. + +namespace Windows.Win32.UI.ViewManagement; + +/// +/// WinRT ABI for Windows.UI.ViewManagement.IUISettings3. +/// +/// +/// +/// Manually defined as the type lives in WinRT metadata, not Win32 metadata, +/// and we do not want a CsWinRT projection dependency. Slots 3-5 are the +/// IInspectable methods. +/// +/// +internal unsafe struct IUISettings3 : IComIID +{ + private readonly void** _vtbl; + + // {03021BE4-5254-4781-8194-5168F7D06D7B} + public static Guid IID_Guid { get; } = new(0x03021be4, 0x5254, 0x4781, 0x81, 0x94, 0x51, 0x68, 0xf7, 0xd0, 0x6d, 0x7b); + + static ref readonly Guid IComIID.Guid + { + [MethodImpl(MethodImplOptions.AggressiveInlining)] + get + { + ReadOnlySpan data = + [ + // 0x03021be4, 0x5254, 0x4781, 0x81, 0x94, 0x51, 0x68, 0xf7, 0xd0, 0x6d, 0x7b + 0xe4, 0x1b, 0x02, 0x03, 0x54, 0x52, 0x81, 0x47, 0x81, 0x94, 0x51, 0x68, 0xf7, 0xd0, 0x6d, 0x7b + ]; + + return ref Unsafe.As(ref MemoryMarshal.GetReference(data)); + } + } + + public HRESULT QueryInterface(Guid* riid, void** ppvObject) + { + fixed (IUISettings3* pThis = &this) + return ((delegate* unmanaged[Stdcall])_vtbl[0])(pThis, riid, ppvObject); + } + + public uint AddRef() + { + fixed (IUISettings3* pThis = &this) + return ((delegate* unmanaged[Stdcall])_vtbl[1])(pThis); + } + + public uint Release() + { + fixed (IUISettings3* pThis = &this) + return ((delegate* unmanaged[Stdcall])_vtbl[2])(pThis); + } + + // Slots 3-5: IInspectable::GetIids, GetRuntimeClassName, GetTrustLevel (unused). + + public HRESULT GetColorValue(UIColorType desiredColor, UIColor* value) + { + fixed (IUISettings3* pThis = &this) + return ((delegate* unmanaged[Stdcall])_vtbl[6])(pThis, desiredColor, value); + } +} diff --git a/src/System.Private.Windows.Core/src/Windows/Win32/UI/ViewManagement/UIColor.cs b/src/System.Private.Windows.Core/src/Windows/Win32/UI/ViewManagement/UIColor.cs new file mode 100644 index 00000000000..94c3036ca5b --- /dev/null +++ b/src/System.Private.Windows.Core/src/Windows/Win32/UI/ViewManagement/UIColor.cs @@ -0,0 +1,37 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. + +namespace Windows.Win32.UI.ViewManagement; + +/// +/// WinRT ABI for Windows.UI.Color, the value returned by +/// . +/// +/// +/// +/// Manually defined to mirror the WinRT ABI layout (four sequential bytes: alpha, red, green, blue) +/// without taking a CsWinRT projection dependency. +/// +/// +internal struct UIColor +{ + /// + /// The alpha channel of the color. + /// + public byte A; + + /// + /// The red channel of the color. + /// + public byte R; + + /// + /// The green channel of the color. + /// + public byte G; + + /// + /// The blue channel of the color. + /// + public byte B; +} diff --git a/src/System.Private.Windows.Core/src/Windows/Win32/UI/ViewManagement/UIColorType.cs b/src/System.Private.Windows.Core/src/Windows/Win32/UI/ViewManagement/UIColorType.cs new file mode 100644 index 00000000000..9725700b888 --- /dev/null +++ b/src/System.Private.Windows.Core/src/Windows/Win32/UI/ViewManagement/UIColorType.cs @@ -0,0 +1,66 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. + +namespace Windows.Win32.UI.ViewManagement; + +/// +/// WinRT ABI for Windows.UI.ViewManagement.UIColorType, identifying which system color to +/// retrieve through . +/// +/// +/// +/// Manually defined to mirror the WinRT enumeration without taking a CsWinRT projection dependency. +/// +/// +internal enum UIColorType +{ + /// + /// The background color. + /// + Background = 0, + + /// + /// The foreground color. + /// + Foreground = 1, + + /// + /// The darkest of the three accent shades. + /// + AccentDark3 = 2, + + /// + /// The second darkest accent shade. + /// + AccentDark2 = 3, + + /// + /// The lightest of the three dark accent shades. + /// + AccentDark1 = 4, + + /// + /// The base accent color. + /// + Accent = 5, + + /// + /// The darkest of the three light accent shades. + /// + AccentLight1 = 6, + + /// + /// The second lightest accent shade. + /// + AccentLight2 = 7, + + /// + /// The lightest of the three accent shades. + /// + AccentLight3 = 8, + + /// + /// The complement of the accent color. + /// + Complement = 9, +} diff --git a/src/System.Windows.Forms.Primitives/src/NativeMethods.txt b/src/System.Windows.Forms.Primitives/src/NativeMethods.txt index a56634792b1..1aefb4bdfd7 100644 --- a/src/System.Windows.Forms.Primitives/src/NativeMethods.txt +++ b/src/System.Windows.Forms.Primitives/src/NativeMethods.txt @@ -7,6 +7,7 @@ ADVF AreDpiAwarenessContextsEqual ARW_* AUTOCOMPLETEOPTIONS +BeginDeferWindowPos BFFM_* BIF_* BITMAP @@ -62,6 +63,7 @@ DATETIMEPICK_CLASS DeactivateActCtx DefFrameProc DefMDIChildProc +DeferWindowPos DESKTOP_ACCESS_FLAGS DestroyAcceleratorTable DestroyCursor @@ -92,6 +94,7 @@ DTM_* DTN_* DTS_* DuplicateHandle +DWMWINDOWATTRIBUTE DWM_WINDOW_CORNER_PREFERENCE DwmGetWindowAttribute DwmSetWindowAttribute @@ -104,6 +107,7 @@ EN_* EnableMenuItem EnableScrollBar EnableWindow +EndDeferWindowPos EndDialog ENM_* EnumDisplaySettings @@ -183,6 +187,7 @@ GetProcessWindowStation GETPROPERTYSTOREFLAGS GetRgnBox GetROP2 +GetScrollBarInfo GetScrollInfo GetShortPathName GetStartupInfo @@ -512,6 +517,9 @@ PD_RESULT_* PostQuitMessage PostQuitMessage PostThreadMessage +PowerCreateRequest +PowerClearRequest +PowerSetRequest PRF_* PROGRESS_CLASS PROPERTYKEY @@ -692,9 +700,11 @@ WC_TABCONTROL WC_TREEVIEW WHEEL_DELTA WindowFromPoint +WTSRegisterSessionNotification +WTSUnRegisterSessionNotification WINDOWPOS WINEVENT_INCONTEXT WSF_VISIBLE XBUTTON1 XBUTTON2 -XFORMCOORDS \ No newline at end of file +XFORMCOORDS diff --git a/src/System.Windows.Forms.Primitives/src/System/Windows/Forms/Animation/HighPrecisionTimer.cs b/src/System.Windows.Forms.Primitives/src/System/Windows/Forms/Animation/HighPrecisionTimer.cs new file mode 100644 index 00000000000..6499e654a72 --- /dev/null +++ b/src/System.Windows.Forms.Primitives/src/System/Windows/Forms/Animation/HighPrecisionTimer.cs @@ -0,0 +1,335 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. + +using System.Collections.Concurrent; +using System.Runtime.InteropServices; +using System.Runtime.Versioning; + +namespace System.Windows.Forms.Animation; + +/// +/// A high-precision static timer designed to trigger WinForms control animations +/// at 60 Hz (or 30 Hz on systems without high-resolution timer support). +/// Controls register callbacks that are marshalled back to the UI thread via +/// the captured . +/// +internal static partial class HighPrecisionTimer +{ + // Target frame intervals. + private const double TargetFrameTimeMs60Hz = 16.667; + private const double TargetFrameTimeMs30Hz = 33.333; + + // We aim the PeriodicTimer earlier than the target frame time to allow + // for spin-wait refinement to hit the target precisely. + private const double TimerTickMs60Hz = 14.0; + private const double TimerTickMs30Hz = 30.0; + + // Maximum drift before we assert (20% of target frame time sustained over 10 frames). + private const int MaxDriftFrames = 10; + private const double MaxDriftThresholdRatio = 0.20; + + private static readonly Lock s_lock = new(); + private static readonly ConcurrentDictionary s_registrations = new(); + private static long s_nextId; + private static CancellationTokenSource? s_cts; + private static Task? s_loopTask; + private static bool s_highResolutionAvailable; + private static double s_targetFrameTimeMs; + private static double s_timerTickMs; + + /// + /// Gets the current target frame time in milliseconds. + /// + internal static double TargetFrameTimeMs => s_targetFrameTimeMs; + + /// + /// Gets whether high-resolution timing (60 Hz) is available on this system. + /// + internal static bool IsHighResolutionAvailable => s_highResolutionAvailable; + + /// + /// Registers a callback to be invoked on each animation frame tick. + /// The current is captured and used + /// to marshal the callback to the appropriate thread. + /// + /// + /// The async callback invoked each frame. Receives timing information and a cancellation token. + /// + /// A that must be disposed to unregister. + /// + /// Thrown when no is available on the current thread. + /// + internal static TimerRegistration Register(Func callback) + { + ArgumentNullException.ThrowIfNull(callback); + + SynchronizationContext? syncContext = SynchronizationContext.Current + ?? throw new InvalidOperationException( + "A SynchronizationContext must be available on the calling thread. " + + "Ensure registration is performed from a UI thread."); + + long id = Interlocked.Increment(ref s_nextId); + Registration registration = new(id, callback, syncContext); + s_registrations.TryAdd(id, registration); + + EnsureRunning(); + + return new TimerRegistration(id); + } + + /// + /// Unregisters a previously registered callback. + /// + internal static void Unregister(long registrationId) + { + s_registrations.TryRemove(registrationId, out _); + + if (s_registrations.IsEmpty) + { + StopTimer(); + } + } + + private static void EnsureRunning() + { + lock (s_lock) + { + if (s_loopTask is not null) + { + return; + } + + s_highResolutionAvailable = TrySetHighResolutionTimerMode(); + s_targetFrameTimeMs = s_highResolutionAvailable ? TargetFrameTimeMs60Hz : TargetFrameTimeMs30Hz; + s_timerTickMs = s_highResolutionAvailable ? TimerTickMs60Hz : TimerTickMs30Hz; + + s_cts = new CancellationTokenSource(); + CancellationToken cancellationToken = s_cts.Token; + s_loopTask = Task.Factory.StartNew( + () => TimerLoopAsync(cancellationToken), + cancellationToken, + TaskCreationOptions.LongRunning, + TaskScheduler.Default).Unwrap(); + } + } + + private static void StopTimer() + { + CancellationTokenSource? cts; + + lock (s_lock) + { + cts = s_cts; + s_cts = null; + s_loopTask = null; + } + + if (cts is not null) + { + cts.Cancel(); + cts.Dispose(); + } + + // Best-effort: restore timer resolution. + if (s_highResolutionAvailable) + { + ResetTimerResolution(); + } + } + + private static async Task TimerLoopAsync(CancellationToken cancellationToken) + { + using PeriodicTimer periodicTimer = new(TimeSpan.FromMilliseconds(s_timerTickMs)); + Stopwatch stopwatch = Stopwatch.StartNew(); + long lastTickTimestamp = 0; + int consecutiveDriftFrames = 0; + + try + { + while (await periodicTimer.WaitForNextTickAsync(cancellationToken).ConfigureAwait(false)) + { + // Spin-wait refinement: if we woke up early, spin until target time. + double targetMs = lastTickTimestamp + s_targetFrameTimeMs; + SpinToTarget(stopwatch, targetMs); + + long currentTimestamp = stopwatch.ElapsedMilliseconds; + double elapsed = currentTimestamp - lastTickTimestamp; + + // Drift detection: check if we are consistently overshooting. + double drift = elapsed - s_targetFrameTimeMs; + if (Math.Abs(drift) > s_targetFrameTimeMs * MaxDriftThresholdRatio) + { + consecutiveDriftFrames++; + Debug.Assert( + consecutiveDriftFrames < MaxDriftFrames, + $"HighPrecisionTimer: Excessive drift detected. " + + $"Drift: {drift:F2}ms over {consecutiveDriftFrames} consecutive frames."); + } + else + { + consecutiveDriftFrames = 0; + } + + lastTickTimestamp = currentTimestamp; + + // Dispatch to all registered callbacks. + DispatchCallbacks( + TimeSpan.FromMilliseconds(currentTimestamp), + TimeSpan.FromMilliseconds(elapsed), + cancellationToken); + } + } + catch (OperationCanceledException) when (cancellationToken.IsCancellationRequested) + { + // Normal shutdown. + } + } + + private static void SpinToTarget(Stopwatch stopwatch, double targetMs) + { + SpinWait spinner = default; + + while (stopwatch.Elapsed.TotalMilliseconds < targetMs) + { + // SpinOnce with sleep1Threshold=-1 ensures we stay in PAUSE/yield + // mode and never escalate to Thread.Sleep(1). + spinner.SpinOnce(sleep1Threshold: -1); + } + } + + private static void DispatchCallbacks( + TimeSpan timestamp, + TimeSpan elapsed, + CancellationToken cancellationToken) + { + foreach (KeyValuePair kvp in s_registrations) + { + Registration registration = kvp.Value; + + // Skip if previous callback is still in flight (frame coalescing). + if (Interlocked.CompareExchange(ref registration.InFlight, 1, 0) != 0) + { + Interlocked.Increment(ref registration.DroppedFrames); + continue; + } + + long frameIndex = Interlocked.Increment(ref registration.FrameIndex) - 1; + int dropped = Interlocked.Exchange(ref registration.DroppedFrames, 0); + + HighPrecisionTimerTick tick = new() + { + Timestamp = timestamp, + Elapsed = elapsed, + DroppedFrames = dropped, + FrameIndex = frameIndex + }; + + registration.SyncContext.Post( + _ => _ = InvokeCallbackAsync(registration, tick, cancellationToken), + null); + } + } + + private static async Task InvokeCallbackAsync( + Registration registration, + HighPrecisionTimerTick tick, + CancellationToken cancellationToken) + { + try + { + await registration.Callback(tick, cancellationToken).ConfigureAwait(false); + } + catch (OperationCanceledException) when (cancellationToken.IsCancellationRequested) + { + // Cancellation while the timer is shutting down is a benign, expected outcome. Swallow it + // here so it does not fault this fire-and-forget task into an unobserved task exception. + } + catch (Exception ex) + { + Debug.Fail($"HighPrecisionTimer: Unhandled exception in callback: {ex.Message}"); + } + finally + { + Interlocked.Exchange(ref registration.InFlight, 0); + } + } + + [SupportedOSPlatform("windows10.0.17134.0")] + private static bool TrySetHighResolutionTimerMode() + { + if (!OperatingSystem.IsWindowsVersionAtLeast(10, 0, 17134)) + { + return false; + } + + try + { + // Use timeBeginPeriod for documented, reliable high-resolution timing. + return NativeMethods.TimeBeginPeriod(1) == 0; // TIMERR_NOERROR + } + catch (Exception ex) when (!ex.IsCriticalException()) + { + return false; + } + } + + private static void ResetTimerResolution() + { + try + { + NativeMethods.TimeEndPeriod(1); + } + catch (Exception ex) when (!ex.IsCriticalException()) + { + // Best effort. + } + } + + private static partial class NativeMethods + { + [LibraryImport("winmm.dll")] + internal static partial int TimeBeginPeriod(int uPeriod); + + [LibraryImport("winmm.dll")] + internal static partial int TimeEndPeriod(int uPeriod); + } + + private sealed class Registration( + long id, + Func callback, + SynchronizationContext syncContext) + { + public long Id { get; } = id; + public Func Callback { get; } = callback; + public SynchronizationContext SyncContext { get; } = syncContext; + public int InFlight; + public int DroppedFrames; + public long FrameIndex; + } + + /// + /// Represents a timer registration. Dispose to unregister. + /// + internal readonly struct TimerRegistration : IDisposable + { + private readonly long _id; + + internal TimerRegistration(long id) => _id = id; + + /// Gets the registration identifier. + public long Id => _id; + + /// Unregisters this callback from the timer. + public void Dispose() => Unregister(_id); + } + + /// + /// Resets internal state. For testing purposes only. + /// + internal static void Reset() + { + StopTimer(); + s_registrations.Clear(); + s_nextId = 0; + } +} diff --git a/src/System.Windows.Forms.Primitives/src/System/Windows/Forms/Animation/HighPrecisionTimerTick.cs b/src/System.Windows.Forms.Primitives/src/System/Windows/Forms/Animation/HighPrecisionTimerTick.cs new file mode 100644 index 00000000000..3b6557e2643 --- /dev/null +++ b/src/System.Windows.Forms.Primitives/src/System/Windows/Forms/Animation/HighPrecisionTimerTick.cs @@ -0,0 +1,30 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. + +namespace System.Windows.Forms.Animation; + +/// +/// Provides timing information for a single animation frame tick. +/// +internal readonly struct HighPrecisionTimerTick +{ + /// + /// The absolute timestamp of this tick from the timer's epoch. + /// + public TimeSpan Timestamp { get; init; } + + /// + /// The elapsed time since the last tick delivered to this registration. + /// + public TimeSpan Elapsed { get; init; } + + /// + /// The number of frames that were dropped (coalesced) since the last delivered tick. + /// + public int DroppedFrames { get; init; } + + /// + /// The zero-based frame index for this registration. + /// + public long FrameIndex { get; init; } +} diff --git a/src/System.Windows.Forms.Primitives/src/System/Windows/Forms/Internals/ScaleHelper.cs b/src/System.Windows.Forms.Primitives/src/System/Windows/Forms/Internals/ScaleHelper.cs index 764a35ca3a1..be410c24340 100644 --- a/src/System.Windows.Forms.Primitives/src/System/Windows/Forms/Internals/ScaleHelper.cs +++ b/src/System.Windows.Forms.Primitives/src/System/Windows/Forms/Internals/ScaleHelper.cs @@ -30,6 +30,12 @@ internal static partial class ScaleHelper private static bool s_processPerMonitorAware; private static Size? s_logicalSmallSystemIconSize; + private const string AccessibilityRegistryKeyPath = "Software\\Microsoft\\Accessibility"; + private const string TextScaleFactorValueName = "TextScaleFactor"; + private const int MinSystemTextScalePercent = 100; + private const int MaxSystemTextScalePercent = 225; + private const double DefaultSystemTextScaleFactor = 1.0; + /// /// The initial primary monitor DPI (logical pixels per inch) for the process. /// @@ -227,20 +233,50 @@ internal static Bitmap ScaleToDpi(Bitmap logicalBitmap, int dpi, bool disposeBit return null; } - // The default(100) and max(225) text scale factor is value what Settings display text scale - // applies and also clamps the text scale factor value between 100 and 225 value. - // See https://docs.microsoft.com/windows/uwp/design/input/text-scaling. - const int MinTextScaleValue = 100; - const int MaxTextScaleValue = 225; + if (!TryGetSystemTextScaleFactor(out double textScaleFactor) || textScaleFactor == DefaultSystemTextScaleFactor) + { + return null; + } + + return font.WithSize(font.Size * (float)textScaleFactor); + } + + /// + /// Gets the current Windows Accessibility text-scale factor as a multiplier. + /// + /// + /// + /// Returns 1.0 when text scaling is unsupported or when the setting cannot be read. + /// + /// + internal static double GetSystemTextScaleFactor() + => TryGetSystemTextScaleFactor(out double textScaleFactor) ? textScaleFactor : DefaultSystemTextScaleFactor; + + /// + /// Attempts to get the current Windows Accessibility text-scale factor as a multiplier. + /// + /// The current text-scale factor. + /// + /// if the value was read successfully; otherwise, . + /// + internal static bool TryGetSystemTextScaleFactor(out double textScaleFactor) + { + textScaleFactor = DefaultSystemTextScaleFactor; + + if (!OsVersion.IsWindows10_1507OrGreater()) + { + return false; + } try { - // Retrieve the text scale factor, which is set via Settings > Display > Make Text Bigger. - using RegistryKey? key = Registry.CurrentUser.OpenSubKey("Software\\Microsoft\\Accessibility"); - if (key is not null && key.GetValue("TextScaleFactor") is int textScale) + // Retrieve the text scale factor, which is set via Settings > Accessibility > Text size. + using RegistryKey? key = Registry.CurrentUser.OpenSubKey(AccessibilityRegistryKeyPath); + if (key is not null && key.GetValue(TextScaleFactorValueName) is int textScale) { - textScale = Math.Clamp(textScale, MinTextScaleValue, MaxTextScaleValue); - return textScale == 100 ? null : font.WithSize(font.Size * (textScale / 100.0f)); + textScale = Math.Clamp(textScale, MinSystemTextScalePercent, MaxSystemTextScalePercent); + textScaleFactor = textScale / 100.0; + return true; } } catch @@ -251,7 +287,7 @@ internal static Bitmap ScaleToDpi(Bitmap logicalBitmap, int dpi, bool disposeBit #endif } - return null; + return false; } /// diff --git a/src/System.Windows.Forms.Primitives/tests/UnitTests/System/Windows/Forms/Animation/HighPrecisionTimerTests.cs b/src/System.Windows.Forms.Primitives/tests/UnitTests/System/Windows/Forms/Animation/HighPrecisionTimerTests.cs new file mode 100644 index 00000000000..89d031dc1ca --- /dev/null +++ b/src/System.Windows.Forms.Primitives/tests/UnitTests/System/Windows/Forms/Animation/HighPrecisionTimerTests.cs @@ -0,0 +1,220 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. + +using System.Collections.Concurrent; +using System.Diagnostics; +using System.Windows.Forms.Animation; + +namespace System.Windows.Forms.Primitives.Tests.Animation; + +/// +/// A test synchronization context that executes posted callbacks immediately +/// on the thread pool, simulating a UI message pump for testing purposes. +/// +internal sealed class TestSynchronizationContext : SynchronizationContext +{ + public override void Post(SendOrPostCallback d, object? state) => ThreadPool.QueueUserWorkItem(_ => d(state)); + + public override void Send(SendOrPostCallback d, object? state) => d(state); +} + +// The timer is process-wide static; disable parallelization so timing-sensitive +// assertions are not perturbed by concurrently running tests. +[Collection(nameof(HighPrecisionTimerTests))] +[CollectionDefinition(nameof(HighPrecisionTimerTests), DisableParallelization = true)] +public sealed class HighPrecisionTimerTests : IDisposable +{ + private readonly SynchronizationContext? _originalContext; + + public HighPrecisionTimerTests() + { + _originalContext = SynchronizationContext.Current; + SynchronizationContext.SetSynchronizationContext(new TestSynchronizationContext()); + } + + public void Dispose() + { + HighPrecisionTimer.Reset(); + SynchronizationContext.SetSynchronizationContext(_originalContext); + } + + [Fact] + public async Task SingleConsumer_ReceivesTicksAtApproximatelyExpectedRate() + { + ConcurrentBag intervals = []; + Stopwatch stopwatch = Stopwatch.StartNew(); + double lastTick = 0; + int tickCount = 0; + const int TargetTicks = 30; + + using HighPrecisionTimer.TimerRegistration registration = HighPrecisionTimer.Register( + (tick, ct) => + { + double now = stopwatch.Elapsed.TotalMilliseconds; + if (lastTick > 0) + { + intervals.Add(now - lastTick); + } + + lastTick = now; + Interlocked.Increment(ref tickCount); + return ValueTask.CompletedTask; + }); + + await WaitForAsync(() => tickCount >= TargetTicks); + + List sorted = [.. intervals.OrderBy(x => x)]; + double targetMs = HighPrecisionTimer.TargetFrameTimeMs; + + // Relaxed bounds to remain robust on loaded CI machines: the median must be in a + // sane band around the target frame time. + double median = Percentile(sorted, 0.50); + median.Should().BeLessThan(targetMs * 3.0, "the median frame interval should stay near the target"); + } + + [Fact] + public async Task MultipleConsumers_AllReceiveTicksIndependently() + { + const int ConsumerCount = 5; + const int TargetTicks = 15; + int[] tickCounts = new int[ConsumerCount]; + HighPrecisionTimer.TimerRegistration[] registrations = new HighPrecisionTimer.TimerRegistration[ConsumerCount]; + + for (int i = 0; i < ConsumerCount; i++) + { + int index = i; + registrations[i] = HighPrecisionTimer.Register( + (tick, ct) => + { + Interlocked.Increment(ref tickCounts[index]); + return ValueTask.CompletedTask; + }); + } + + await WaitForAsync(() => tickCounts.Min() >= TargetTicks); + + foreach (HighPrecisionTimer.TimerRegistration registration in registrations) + { + registration.Dispose(); + } + + tickCounts.Should().OnlyContain(count => count >= TargetTicks); + } + + [Fact] + public async Task SlowConsumer_DropsFramesInsteadOfQueuing() + { + ConcurrentBag ticks = []; + + using HighPrecisionTimer.TimerRegistration registration = HighPrecisionTimer.Register( + async (tick, ct) => + { + ticks.Add(tick); + // Simulate slow rendering (well over one frame time). + await Task.Delay((int)(HighPrecisionTimer.TargetFrameTimeMs * 3), ct).ConfigureAwait(false); + }); + + await WaitForAsync(() => ticks.Sum(t => t.DroppedFrames) > 0, timeoutMs: 4000); + + ticks.Sum(t => t.DroppedFrames).Should().BeGreaterThan(0, "a slow consumer should report dropped frames"); + } + + [Fact] + public async Task Registration_Disposal_StopsCallbacks() + { + int tickCount = 0; + + HighPrecisionTimer.TimerRegistration registration = HighPrecisionTimer.Register( + (tick, ct) => + { + Interlocked.Increment(ref tickCount); + return ValueTask.CompletedTask; + }); + + await WaitForAsync(() => tickCount > 0); + registration.Dispose(); + int ticksAfterDispose = Volatile.Read(ref tickCount); + + await Task.Delay(200, TestContext.Current.CancellationToken); + + // At most a couple of in-flight callbacks may land right after disposal. + (Volatile.Read(ref tickCount) - ticksAfterDispose).Should().BeLessThanOrEqualTo(2); + } + + [Fact] + public void Registration_WithoutSyncContext_Throws() + { + SynchronizationContext? original = SynchronizationContext.Current; + SynchronizationContext.SetSynchronizationContext(null); + + try + { + Action act = () => HighPrecisionTimer.Register((tick, ct) => ValueTask.CompletedTask); + act.Should().Throw(); + } + finally + { + SynchronizationContext.SetSynchronizationContext(original); + } + } + + [Fact] + public async Task TimerTick_ProvidesElapsedAndIncreasingFrameIndex() + { + ConcurrentBag elapsedValues = []; + long lastFrameIndex = -1; + bool frameIndexMonotonic = true; + int tickCount = 0; + const int TargetTicks = 15; + + using HighPrecisionTimer.TimerRegistration registration = HighPrecisionTimer.Register( + (tick, ct) => + { + if (tick.FrameIndex <= lastFrameIndex) + { + frameIndexMonotonic = false; + } + + lastFrameIndex = tick.FrameIndex; + + if (tick.FrameIndex > 0) + { + elapsedValues.Add(tick.Elapsed.TotalMilliseconds); + } + + Interlocked.Increment(ref tickCount); + return ValueTask.CompletedTask; + }); + + await WaitForAsync(() => tickCount >= TargetTicks); + + frameIndexMonotonic.Should().BeTrue("frame indices should increase monotonically"); + elapsedValues.Should().NotBeEmpty(); + elapsedValues.Should().OnlyContain(value => value > 0, "elapsed time between ticks should be positive"); + } + + private static double Percentile(List sortedValues, double percentile) + { + if (sortedValues.Count == 0) + { + return 0; + } + + int index = (int)Math.Ceiling(percentile * sortedValues.Count) - 1; + return sortedValues[Math.Max(0, index)]; + } + + private static async Task WaitForAsync(Func condition, int timeoutMs = 5000) + { + Stopwatch stopwatch = Stopwatch.StartNew(); + while (!condition()) + { + if (stopwatch.ElapsedMilliseconds > timeoutMs) + { + throw new TimeoutException("Timed out waiting for the expected timer ticks."); + } + + await Task.Delay(25, TestContext.Current.CancellationToken).ConfigureAwait(false); + } + } +} diff --git a/src/System.Windows.Forms/GlobalSuppressions.cs b/src/System.Windows.Forms/GlobalSuppressions.cs index 74c366ba97c..b1ed827437e 100644 --- a/src/System.Windows.Forms/GlobalSuppressions.cs +++ b/src/System.Windows.Forms/GlobalSuppressions.cs @@ -269,3 +269,4 @@ [assembly: SuppressMessage("ApiDesign", "RS0026:Do not add multiple public overloads with optional parameters", Justification = "Analyzer wrongly complains for new APIs - known issue.", Scope = "member", Target = "~M:System.Windows.Forms.TaskDialog.ShowDialogAsync(System.Windows.Forms.TaskDialogPage,System.Windows.Forms.TaskDialogStartupLocation)~System.Threading.Tasks.Task{System.Windows.Forms.TaskDialogButton}")] [assembly: SuppressMessage("ApiDesign", "RS0026:Do not add multiple public overloads with optional parameters", Justification = "Analyzer wrongly complains for new APIs - known issue.", Scope = "member", Target = "~M:System.Windows.Forms.TaskDialog.ShowDialogAsync(System.Windows.Forms.IWin32Window,System.Windows.Forms.TaskDialogPage,System.Windows.Forms.TaskDialogStartupLocation)~System.Threading.Tasks.Task{System.Windows.Forms.TaskDialogButton}")] [assembly: SuppressMessage("ApiDesign", "RS0026:Do not add multiple public overloads with optional parameters", Justification = "Analyzer wrongly complains for new APIs - known issue.", Scope = "member", Target = "~M:System.Windows.Forms.TaskDialog.ShowDialogAsync(System.IntPtr,System.Windows.Forms.TaskDialogPage,System.Windows.Forms.TaskDialogStartupLocation)~System.Threading.Tasks.Task{System.Windows.Forms.TaskDialogButton}")] +[assembly: SuppressMessage("DocumentationAnalyzers.StyleRules", "DOC107:Use 'see cref'", Justification = "Has been referenced already in the same paragraph.", Scope = "member", Target = "~P:System.Windows.Forms.TextBoxBase.Padding")] diff --git a/src/System.Windows.Forms/PublicAPI.Unshipped.txt b/src/System.Windows.Forms/PublicAPI.Unshipped.txt index e69de29bb2d..37bc9d82011 100644 --- a/src/System.Windows.Forms/PublicAPI.Unshipped.txt +++ b/src/System.Windows.Forms/PublicAPI.Unshipped.txt @@ -0,0 +1,115 @@ +#nullable enable +System.Windows.Forms.Appearance.ToggleSwitch = 2 -> System.Windows.Forms.Appearance +System.Windows.Forms.Control.VisualStylesModeChanged -> System.EventHandler? +System.Windows.Forms.Form.SystemTextSizeChanged -> System.EventHandler? +virtual System.Windows.Forms.Form.OnSystemTextSizeChanged(System.EventArgs! e) -> void +System.Windows.Forms.TreeView.NodeLeading.get -> float +System.Windows.Forms.TreeView.NodeLeading.set -> void +System.Windows.Forms.TreeView.NodeLeadingChanged -> System.EventHandler? +virtual System.Windows.Forms.TreeView.OnNodeLeadingChanged(System.EventArgs! e) -> void +System.Windows.Forms.ControlMutationExtensions +static System.Windows.Forms.ControlMutationExtensions.SuspendPainting(this System.Windows.Forms.ISupportSuspendPainting! target) -> System.Windows.Forms.SuspendPaintingScope! +static System.Windows.Forms.ControlMutationExtensions.SuspendPainting(this System.Windows.Forms.ISupportSuspendPainting! target, System.Func! suspendLayoutContainerFilter) -> System.Windows.Forms.SuspendPaintingScope! +static System.Windows.Forms.ControlMutationExtensions.SuspendPainting(this System.Windows.Forms.ISupportSuspendPainting! target, System.Windows.Forms.LayoutSuspendTraversal layoutSuspendTraversal) -> System.Windows.Forms.SuspendPaintingScope! +System.Windows.Forms.FormRevealMode +System.Windows.Forms.FormRevealMode.Classic = 0 -> System.Windows.Forms.FormRevealMode +System.Windows.Forms.FormRevealMode.Deferred = 1 -> System.Windows.Forms.FormRevealMode +System.Windows.Forms.FormRevealMode.Inherit = -1 -> System.Windows.Forms.FormRevealMode +System.Windows.Forms.ISupportSuspendPainting +System.Windows.Forms.ISupportSuspendPainting.BeginSuspendPainting() -> void +System.Windows.Forms.ISupportSuspendPainting.EndSuspendPainting() -> void +System.Windows.Forms.LayoutSuspendTraversal +System.Windows.Forms.LayoutSuspendTraversal.None = 0 -> System.Windows.Forms.LayoutSuspendTraversal +System.Windows.Forms.LayoutSuspendTraversal.TopLevelOnly = 1 -> System.Windows.Forms.LayoutSuspendTraversal +System.Windows.Forms.LayoutSuspendTraversal.Traverse = 2 -> System.Windows.Forms.LayoutSuspendTraversal +System.Windows.Forms.SuspendPaintingScope +System.Windows.Forms.SuspendPaintingScope.Dispose() -> void +System.Windows.Forms.SuspendPaintingScope.SuspendPaintingScope(System.Windows.Forms.ISupportSuspendPainting? target) -> void +static System.Windows.Forms.Application.DefaultFormRevealMode.get -> System.Windows.Forms.FormRevealMode +static System.Windows.Forms.Application.IsFormRevealDeferred.get -> bool +static System.Windows.Forms.Application.SetDefaultFormRevealMode(System.Windows.Forms.FormRevealMode mode) -> void +virtual System.Windows.Forms.Control.BeginSuspendPaintingCore() -> void +virtual System.Windows.Forms.Control.EndSuspendPaintingCore() -> void +virtual System.Windows.Forms.Form.FormRevealMode.get -> System.Windows.Forms.FormRevealMode +virtual System.Windows.Forms.Form.FormRevealMode.set -> void +override System.Windows.Forms.ComboBox.BeginSuspendPaintingCore() -> void +override System.Windows.Forms.ComboBox.EndSuspendPaintingCore() -> void +override System.Windows.Forms.ListBox.BeginSuspendPaintingCore() -> void +override System.Windows.Forms.ListBox.EndSuspendPaintingCore() -> void +override System.Windows.Forms.ListView.BeginSuspendPaintingCore() -> void +override System.Windows.Forms.ListView.EndSuspendPaintingCore() -> void +override System.Windows.Forms.RichTextBox.BeginSuspendPaintingCore() -> void +override System.Windows.Forms.RichTextBox.EndSuspendPaintingCore() -> void +override System.Windows.Forms.TreeView.BeginSuspendPaintingCore() -> void +override System.Windows.Forms.TreeView.EndSuspendPaintingCore() -> void +System.Windows.Forms.KioskModeManager +System.Windows.Forms.KioskModeManager.ContainerControl.get -> System.Windows.Forms.ContainerControl? +System.Windows.Forms.KioskModeManager.ContainerControl.set -> void +System.Windows.Forms.KioskModeManager.ContainerControlChanged -> System.EventHandler? +override System.Windows.Forms.KioskModeManager.Dispose(bool disposing) -> void +System.Windows.Forms.KioskModeManager.EscapeExitsFullScreen.get -> bool +System.Windows.Forms.KioskModeManager.EscapeExitsFullScreen.set -> void +System.Windows.Forms.KioskModeManager.FullScreen.get -> bool +System.Windows.Forms.KioskModeManager.FullScreen.set -> void +System.Windows.Forms.KioskModeManager.FullScreenChanged -> System.EventHandler? +System.Windows.Forms.KioskModeManager.HideTaskbar.get -> bool +System.Windows.Forms.KioskModeManager.HideTaskbar.set -> void +System.Windows.Forms.KioskModeManager.KioskModeManager() -> void +System.Windows.Forms.KioskModeManager.KioskModeManager(System.ComponentModel.IContainer! container) -> void +System.Windows.Forms.KioskModeManager.MousePointerAutoHideDelay.get -> int +System.Windows.Forms.KioskModeManager.MousePointerAutoHideDelay.set -> void +virtual System.Windows.Forms.KioskModeManager.OnContainerControlChanged(System.EventArgs! e) -> void +virtual System.Windows.Forms.KioskModeManager.OnFullScreenChanged(System.EventArgs! e) -> void +virtual System.Windows.Forms.KioskModeManager.OnWakeup(System.Windows.Forms.KioskModeWakeupEventArgs! e) -> void +override System.Windows.Forms.KioskModeManager.Site.get -> System.ComponentModel.ISite? +override System.Windows.Forms.KioskModeManager.Site.set -> void +System.Windows.Forms.KioskModeManager.SuppressPowerSaving.get -> bool +System.Windows.Forms.KioskModeManager.SuppressPowerSaving.set -> void +System.Windows.Forms.KioskModeManager.ToggleFullScreen() -> void +System.Windows.Forms.KioskModeManager.ToggleFullScreenKey.get -> System.Windows.Forms.Keys +System.Windows.Forms.KioskModeManager.ToggleFullScreenKey.set -> void +System.Windows.Forms.KioskModeManager.TopMostInFullScreen.get -> bool +System.Windows.Forms.KioskModeManager.TopMostInFullScreen.set -> void +System.Windows.Forms.KioskModeManager.Wakeup -> System.Windows.Forms.KioskModeWakeupEventHandler? +System.Windows.Forms.KioskModeManager.WakeUpCommand.get -> System.Windows.Input.ICommand? +System.Windows.Forms.KioskModeManager.WakeUpCommand.set -> void +System.Windows.Forms.KioskModeWakeupEventArgs +System.Windows.Forms.KioskModeWakeupEventArgs.KioskModeWakeupEventArgs(System.Windows.Forms.KioskModeWakeupSource source) -> void +System.Windows.Forms.KioskModeWakeupEventArgs.Source.get -> System.Windows.Forms.KioskModeWakeupSource +System.Windows.Forms.KioskModeWakeupEventHandler +virtual System.Windows.Forms.KioskModeWakeupEventHandler.Invoke(object? sender, System.Windows.Forms.KioskModeWakeupEventArgs! e) -> void +System.Windows.Forms.KioskModeWakeupSource +System.Windows.Forms.KioskModeWakeupSource.Keyboard = 0 -> System.Windows.Forms.KioskModeWakeupSource +System.Windows.Forms.KioskModeWakeupSource.Mouse = 1 -> System.Windows.Forms.KioskModeWakeupSource +System.Windows.Forms.KioskModeWakeupSource.PowerResume = 2 -> System.Windows.Forms.KioskModeWakeupSource +System.Windows.Forms.KioskModeWakeupSource.Session = 3 -> System.Windows.Forms.KioskModeWakeupSource +System.Windows.Forms.VisualStylesMode +System.Windows.Forms.VisualStylesMode.Classic = 0 -> System.Windows.Forms.VisualStylesMode +System.Windows.Forms.VisualStylesMode.Disabled = 1 -> System.Windows.Forms.VisualStylesMode +System.Windows.Forms.VisualStylesMode.Inherit = -1 -> System.Windows.Forms.VisualStylesMode +System.Windows.Forms.VisualStylesMode.Latest = 32767 -> System.Windows.Forms.VisualStylesMode +System.Windows.Forms.VisualStylesMode.Net11 = 2 -> System.Windows.Forms.VisualStylesMode +override System.Windows.Forms.ButtonBase.OnSystemColorsChanged(System.EventArgs! e) -> void +override System.Windows.Forms.ButtonBase.OnVisualStylesModeChanged(System.EventArgs! e) -> void +override System.Windows.Forms.CheckBox.Dispose(bool disposing) -> void +override System.Windows.Forms.CheckBox.OnPaint(System.Windows.Forms.PaintEventArgs! pevent) -> void +override System.Windows.Forms.CheckBox.OnSystemColorsChanged(System.EventArgs! e) -> void +override System.Windows.Forms.CheckBox.OnVisualStylesModeChanged(System.EventArgs! e) -> void +override System.Windows.Forms.Form.OnSystemColorsChanged(System.EventArgs! e) -> void +override System.Windows.Forms.PropertyGrid.VisualStylesMode.get -> System.Windows.Forms.VisualStylesMode +override System.Windows.Forms.PropertyGrid.VisualStylesMode.set -> void +override System.Windows.Forms.RadioButton.Dispose(bool disposing) -> void +override System.Windows.Forms.RadioButton.OnPaint(System.Windows.Forms.PaintEventArgs! pevent) -> void +override System.Windows.Forms.RadioButton.OnSystemColorsChanged(System.EventArgs! e) -> void +override System.Windows.Forms.RadioButton.OnVisualStylesModeChanged(System.EventArgs! e) -> void +override System.Windows.Forms.UpDownBase.OnVisualStylesModeChanged(System.EventArgs! e) -> void +static System.Windows.Forms.Application.DefaultVisualStylesMode.get -> System.Windows.Forms.VisualStylesMode +static System.Windows.Forms.Application.GetWindowsAccentColor() -> System.Drawing.Color +static System.Windows.Forms.Application.SetDefaultVisualStylesMode(System.Windows.Forms.VisualStylesMode styleSetting) -> void +static System.Windows.Forms.Application.SystemTextSize.get -> double +static System.Windows.Forms.Application.SystemTextSizeChanged -> System.EventHandler? +virtual System.Windows.Forms.Control.DefaultVisualStylesMode.get -> System.Windows.Forms.VisualStylesMode +virtual System.Windows.Forms.Control.OnParentVisualStylesModeChanged(System.EventArgs! e) -> void +virtual System.Windows.Forms.Control.OnVisualStylesModeChanged(System.EventArgs! e) -> void +virtual System.Windows.Forms.Control.VisualStylesMode.get -> System.Windows.Forms.VisualStylesMode +virtual System.Windows.Forms.Control.VisualStylesMode.set -> void \ No newline at end of file diff --git a/src/System.Windows.Forms/Resources/SR.resx b/src/System.Windows.Forms/Resources/SR.resx index 53d5073b748..6cda4198171 100644 --- a/src/System.Windows.Forms/Resources/SR.resx +++ b/src/System.Windows.Forms/Resources/SR.resx @@ -171,6 +171,12 @@ Application exception mode cannot be changed once any Controls are created in the application. + + The default visual styles mode can only be set once and cannot be changed afterwards. + + + VisualStylesMode.Inherit cannot be used as the application-wide default visual styles mode because the application root has no parent to inherit from. + &Apply @@ -1035,6 +1041,9 @@ Specifies the minimum size of the control. + + Layout suspension requires a target derived from Control. + 'child' is not a child control of this parent. @@ -2679,6 +2688,45 @@ To replace this default dialog please handle the DataError event. Manages a collection of images that are typically used by other controls such as ListView, TreeView, or ToolStrip. + + Manages common fullscreen, input, cursor, and power behavior for kiosk-style WinForms applications. + + + The container whose containing form is controlled by the kiosk mode manager. + + + Occurs when the ContainerControl property value changes. + + + Indicates whether pressing Escape exits fullscreen mode. + + + Gets or sets a value indicating whether the resolved form is currently fullscreen. + + + Occurs when the fullscreen state changes. + + + Indicates whether fullscreen mode covers the Windows taskbar. + + + The delay, in milliseconds, before the mouse pointer is hidden while fullscreen. A value of 0 disables automatic pointer hiding. + + + Indicates whether Windows should keep the display and system awake while this component is active. + + + The key that toggles between fullscreen and restored mode. + + + Indicates whether the form is topmost while fullscreen. + + + Occurs when keyboard, mouse, power resume, or session activity wakes the kiosk experience. + + + The command that is executed whenever the kiosk experience is woken. + Provides run-time information or descriptive text for a control. @@ -3286,6 +3334,9 @@ Do you want to replace it? Indicates whether the form always appears above all other forms that do not have this property set to true. + + Indicates how the form is presented while its initial appearance is prepared. Classic never defers reveal. Deferred hides the form behind a DWM cloak until its background has painted, to reduce startup flash. Inherit resolves from the application-wide default. + A color which will appear transparent when painted on the form. @@ -4987,7 +5038,7 @@ Stack trace where the illegal operation occurred was: Controls whether the Network button is displayed. - Controls whether the RadioButton appears as normal or as a Windows PushButton. + Controls whether the RadioButton appears as normal, as a Windows PushButton, or as a toggle switch. Causes the radio button to automatically change state when clicked. @@ -6319,6 +6370,9 @@ Stack trace where the illegal operation occurred was: The color of the lines that connect the nodes of the TreeView. + + The uniform row-height leading factor applied to all nodes. + Occurs when a node is clicked with the mouse. @@ -6334,6 +6388,9 @@ Stack trace where the illegal operation occurred was: The sorting comparer for the TreeView. + + Occurs when the value of the NodeLeading property changes. + The string delimiter used for the path returned by a node's FullPath property. @@ -6568,6 +6625,9 @@ Stack trace where the illegal operation occurred was: Occurs when form is moved to a monitor with a different resolution and scaling level, or when form's monitor scaling level is changed in the Windows settings. + + Occurs when the Windows Accessibility text size setting changes for this top-level form. + System @@ -6931,6 +6991,12 @@ Stack trace where the illegal operation occurred was: Occurs when the value of the DataContext property changes. + + Determines how the control renders itself when visual styles are applied. + + + Occurs when the value of the VisualStylesMode property changes. + Gets or sets the parameter that is passed to the Command property's object on execution or on execution context request. @@ -7045,4 +7111,4 @@ Stack trace where the illegal operation occurred was: The FormScreenCaptureMode property can only be changed on top-level Forms with their TopLevel property set to true. - \ No newline at end of file + diff --git a/src/System.Windows.Forms/Resources/xlf/SR.cs.xlf b/src/System.Windows.Forms/Resources/xlf/SR.cs.xlf index ce2bc9260f9..87e41c4f2f6 100644 --- a/src/System.Windows.Forms/Resources/xlf/SR.cs.xlf +++ b/src/System.Windows.Forms/Resources/xlf/SR.cs.xlf @@ -242,6 +242,16 @@ Režim výjimky vlákna nelze změnit po vytvoření ovládacích prvků ve vlákně. + + The default visual styles mode can only be set once and cannot be changed afterwards. + The default visual styles mode can only be set once and cannot be changed afterwards. + + + + VisualStylesMode.Inherit cannot be used as the application-wide default visual styles mode because the application root has no parent to inherit from. + VisualStylesMode.Inherit cannot be used as the application-wide default visual styles mode because the application root has no parent to inherit from. + + &Apply &Použít @@ -1697,6 +1707,11 @@ Určuje minimální velikost ovládacího prvku. + + Layout suspension requires a target derived from Control. + Layout suspension requires a target derived from Control. + + 'child' is not a child control of this parent. Objekt child není podřízeným ovládacím prvkem tohoto nadřazeného objektu. @@ -2172,6 +2187,16 @@ Určuje, zda je ovládací prvek viditelný nebo skrytý. + + Occurs when the value of the VisualStylesMode property changes. + Occurs when the value of the VisualStylesMode property changes. + + + + Determines how the control renders itself when visual styles are applied. + Determines how the control renders itself when visual styles are applied. + + The width of the control, in container coordinates. Šířka ovládacího prvku, v souřadnicích kontejneru. @@ -4519,6 +4544,11 @@ Chcete-li nahradit toto výchozí dialogové okno, nastavte popisovač události Spravuje kolekci obrázků, které standardně používají ovládací prvky, jako například ListView, TreeView nebo ToolStrip. + + Manages common fullscreen, input, cursor, and power behavior for kiosk-style WinForms applications. + Manages common fullscreen, input, cursor, and power behavior for kiosk-style WinForms applications. + + Provides run-time information or descriptive text for a control. Poskytuje běhové informace nebo popisný text pro ovládací prvek. @@ -5420,6 +5450,11 @@ Chcete ho nahradit? Hodnota, kterou tento formulář vrátí, pokud bude zobrazen jako dialogové okno. + + Indicates how the form is presented while its initial appearance is prepared. Classic never defers reveal. Deferred hides the form behind a DWM cloak until its background has painted, to reduce startup flash. Inherit resolves from the application-wide default. + Indicates how the form is presented while its initial appearance is prepared. Classic never defers reveal. Deferred hides the form behind a DWM cloak until its background has painted, to reduce startup flash. Inherit resolves from the application-wide default. + + Event raised when the help button is clicked. Událost aktivovaná při kliknutí na tlačítko nápovědy @@ -5600,6 +5635,11 @@ Chcete ho nahradit? Vyvolá se při prvním zobrazení formuláře. + + Occurs when the Windows Accessibility text size setting changes for this top-level form. + Occurs when the Windows Accessibility text size setting changes for this top-level form. + + The opacity percentage of the control. Míra průhlednosti ovládacího prvku v procentech @@ -6104,6 +6144,66 @@ Trasování zásobníku, kde došlo k neplatné operaci: Kombinace klíčů je neplatná. + + Occurs when the ContainerControl property value changes. + Occurs when the ContainerControl property value changes. + + + + The container whose containing form is controlled by the kiosk mode manager. + The container whose containing form is controlled by the kiosk mode manager. + + + + Indicates whether pressing Escape exits fullscreen mode. + Indicates whether pressing Escape exits fullscreen mode. + + + + Gets or sets a value indicating whether the resolved form is currently fullscreen. + Gets or sets a value indicating whether the resolved form is currently fullscreen. + + + + Occurs when the fullscreen state changes. + Occurs when the fullscreen state changes. + + + + Indicates whether fullscreen mode covers the Windows taskbar. + Indicates whether fullscreen mode covers the Windows taskbar. + + + + The delay, in milliseconds, before the mouse pointer is hidden while fullscreen. A value of 0 disables automatic pointer hiding. + The delay, in milliseconds, before the mouse pointer is hidden while fullscreen. A value of 0 disables automatic pointer hiding. + + + + Indicates whether Windows should keep the display and system awake while this component is active. + Indicates whether Windows should keep the display and system awake while this component is active. + + + + The key that toggles between fullscreen and restored mode. + The key that toggles between fullscreen and restored mode. + + + + Indicates whether the form is topmost while fullscreen. + Indicates whether the form is topmost while fullscreen. + + + + Occurs when keyboard, mouse, power resume, or session activity wakes the kiosk experience. + Occurs when keyboard, mouse, power resume, or session activity wakes the kiosk experience. + + + + The command that is executed whenever the kiosk experience is woken. + The command that is executed whenever the kiosk experience is woken. + + Enables the automatic handling of text that extends beyond the width of the label control. Umožňuje automatické zpracování textu, který je delší než šířka ovládacího prvku popisku. @@ -8675,8 +8775,8 @@ Trasování zásobníku, kde došlo k neplatné operaci: - Controls whether the RadioButton appears as normal or as a Windows PushButton. - Určuje, zda je ovládací prvek RadioButton zobrazen standardně nebo jako tlačítko systému Windows (PushButton). + Controls whether the RadioButton appears as normal, as a Windows PushButton, or as a toggle switch. + Controls whether the RadioButton appears as normal, as a Windows PushButton, or as a toggle switch. @@ -11019,6 +11119,11 @@ Trasování zásobníku, kde došlo k neplatné operaci: Barva čar spojujících uzly ovládacího prvku TreeView + + The uniform row-height leading factor applied to all nodes. + The uniform row-height leading factor applied to all nodes. + + Occurs when a node is clicked with the mouse. Vyvolá se při kliknutí myší na uzel. @@ -11044,6 +11149,11 @@ Trasování zásobníku, kde došlo k neplatné operaci: Kořenové uzly v ovládacím prvku TreeView. + + Occurs when the value of the NodeLeading property changes. + Occurs when the value of the NodeLeading property changes. + + The string delimiter used for the path returned by a node's FullPath property. Oddělovač řetězců použitý pro cesty vracené vlastností FullPath uzlu. diff --git a/src/System.Windows.Forms/Resources/xlf/SR.de.xlf b/src/System.Windows.Forms/Resources/xlf/SR.de.xlf index b5cf47b1170..c9f7f30aa9e 100644 --- a/src/System.Windows.Forms/Resources/xlf/SR.de.xlf +++ b/src/System.Windows.Forms/Resources/xlf/SR.de.xlf @@ -242,6 +242,16 @@ Der Threadausnahmemodus kann nicht mehr geändert werden, sobald Steuerelemente in dem Thread erstellt wurden. + + The default visual styles mode can only be set once and cannot be changed afterwards. + The default visual styles mode can only be set once and cannot be changed afterwards. + + + + VisualStylesMode.Inherit cannot be used as the application-wide default visual styles mode because the application root has no parent to inherit from. + VisualStylesMode.Inherit cannot be used as the application-wide default visual styles mode because the application root has no parent to inherit from. + + &Apply &Anwenden @@ -1697,6 +1707,11 @@ Gibt die Mindestgröße des Steuerelements an. + + Layout suspension requires a target derived from Control. + Layout suspension requires a target derived from Control. + + 'child' is not a child control of this parent. "child" ist kein untergeordnetes Steuerelement dieses übergeordneten Elements. @@ -2172,6 +2187,16 @@ Bestimmt, ob das Steuerelement sichtbar oder ausgeblendet ist. + + Occurs when the value of the VisualStylesMode property changes. + Occurs when the value of the VisualStylesMode property changes. + + + + Determines how the control renders itself when visual styles are applied. + Determines how the control renders itself when visual styles are applied. + + The width of the control, in container coordinates. Die Breite des Steuerelements (in Containerkoordinaten). @@ -4519,6 +4544,11 @@ Behandeln Sie das DataError-Ereignis, um dieses Standarddialogfeld zu ersetzen.< Verwaltet eine Sammlung von Bildern, die in der Regel von anderen Steuerelementen wie ListView, TreeView oder ToolStrip verwendet werden. + + Manages common fullscreen, input, cursor, and power behavior for kiosk-style WinForms applications. + Manages common fullscreen, input, cursor, and power behavior for kiosk-style WinForms applications. + + Provides run-time information or descriptive text for a control. Stellt Laufzeitinformationen oder deskriptiven Text für ein Steuerelement bereit. @@ -5420,6 +5450,11 @@ Möchten Sie den Pfad ersetzen? Der Wert, den dieses Formular zurückgibt, wenn es als Dialogfeld angezeigt wird. + + Indicates how the form is presented while its initial appearance is prepared. Classic never defers reveal. Deferred hides the form behind a DWM cloak until its background has painted, to reduce startup flash. Inherit resolves from the application-wide default. + Indicates how the form is presented while its initial appearance is prepared. Classic never defers reveal. Deferred hides the form behind a DWM cloak until its background has painted, to reduce startup flash. Inherit resolves from the application-wide default. + + Event raised when the help button is clicked. Das ausgelöste Ereignis, wenn auf die Schaltfläche "Hilfe" geklickt wird. @@ -5600,6 +5635,11 @@ Möchten Sie den Pfad ersetzen? Tritt ein, wenn das Formular anfangs angezeigt wird. + + Occurs when the Windows Accessibility text size setting changes for this top-level form. + Occurs when the Windows Accessibility text size setting changes for this top-level form. + + The opacity percentage of the control. Der Prozentsatz der Durchlässigkeit des Steuerelements. @@ -6104,6 +6144,66 @@ Stapelüberwachung, in der der unzulässige Vorgang auftrat: Ungültige Schlüsselkombination. + + Occurs when the ContainerControl property value changes. + Occurs when the ContainerControl property value changes. + + + + The container whose containing form is controlled by the kiosk mode manager. + The container whose containing form is controlled by the kiosk mode manager. + + + + Indicates whether pressing Escape exits fullscreen mode. + Indicates whether pressing Escape exits fullscreen mode. + + + + Gets or sets a value indicating whether the resolved form is currently fullscreen. + Gets or sets a value indicating whether the resolved form is currently fullscreen. + + + + Occurs when the fullscreen state changes. + Occurs when the fullscreen state changes. + + + + Indicates whether fullscreen mode covers the Windows taskbar. + Indicates whether fullscreen mode covers the Windows taskbar. + + + + The delay, in milliseconds, before the mouse pointer is hidden while fullscreen. A value of 0 disables automatic pointer hiding. + The delay, in milliseconds, before the mouse pointer is hidden while fullscreen. A value of 0 disables automatic pointer hiding. + + + + Indicates whether Windows should keep the display and system awake while this component is active. + Indicates whether Windows should keep the display and system awake while this component is active. + + + + The key that toggles between fullscreen and restored mode. + The key that toggles between fullscreen and restored mode. + + + + Indicates whether the form is topmost while fullscreen. + Indicates whether the form is topmost while fullscreen. + + + + Occurs when keyboard, mouse, power resume, or session activity wakes the kiosk experience. + Occurs when keyboard, mouse, power resume, or session activity wakes the kiosk experience. + + + + The command that is executed whenever the kiosk experience is woken. + The command that is executed whenever the kiosk experience is woken. + + Enables the automatic handling of text that extends beyond the width of the label control. Aktiviert die automatische Behandlung von Text, der über die Breite des Bezeichnungssteuerelements hinausgeht. @@ -8675,8 +8775,8 @@ Stapelüberwachung, in der der unzulässige Vorgang auftrat: - Controls whether the RadioButton appears as normal or as a Windows PushButton. - Steuert, ob der RadioButton normal oder als Windows-PushButton angezeigt wird. + Controls whether the RadioButton appears as normal, as a Windows PushButton, or as a toggle switch. + Controls whether the RadioButton appears as normal, as a Windows PushButton, or as a toggle switch. @@ -11019,6 +11119,11 @@ Stapelüberwachung, in der der unzulässige Vorgang auftrat: Die Farbe der Linien, die die Knoten der TreeView verbinden. + + The uniform row-height leading factor applied to all nodes. + The uniform row-height leading factor applied to all nodes. + + Occurs when a node is clicked with the mouse. Tritt auf, wenn mit der Maus auf einen Knoten geklickt wird. @@ -11044,6 +11149,11 @@ Stapelüberwachung, in der der unzulässige Vorgang auftrat: Die Stammknoten im TreeView-Steuerelement. + + Occurs when the value of the NodeLeading property changes. + Occurs when the value of the NodeLeading property changes. + + The string delimiter used for the path returned by a node's FullPath property. Das Zeichenfolgen-Trennzeichen für den Pfad, der von der FullPath-Eigenschaft eines Knotens zurückgegeben wird. diff --git a/src/System.Windows.Forms/Resources/xlf/SR.es.xlf b/src/System.Windows.Forms/Resources/xlf/SR.es.xlf index 82eb0cd40bb..eea9e75afb7 100644 --- a/src/System.Windows.Forms/Resources/xlf/SR.es.xlf +++ b/src/System.Windows.Forms/Resources/xlf/SR.es.xlf @@ -242,6 +242,16 @@ El modo de excepción del subproceso no se puede cambiar una vez que se creen Controles en el subproceso. + + The default visual styles mode can only be set once and cannot be changed afterwards. + The default visual styles mode can only be set once and cannot be changed afterwards. + + + + VisualStylesMode.Inherit cannot be used as the application-wide default visual styles mode because the application root has no parent to inherit from. + VisualStylesMode.Inherit cannot be used as the application-wide default visual styles mode because the application root has no parent to inherit from. + + &Apply &Aplicar @@ -1697,6 +1707,11 @@ Especifica el tamaño mínimo del control. + + Layout suspension requires a target derived from Control. + Layout suspension requires a target derived from Control. + + 'child' is not a child control of this parent. 'child' no es un control secundario de este primario. @@ -2172,6 +2187,16 @@ Determina si el control está visible u oculto. + + Occurs when the value of the VisualStylesMode property changes. + Occurs when the value of the VisualStylesMode property changes. + + + + Determines how the control renders itself when visual styles are applied. + Determines how the control renders itself when visual styles are applied. + + The width of the control, in container coordinates. Ancho del control, en las coordenadas del contenedor. @@ -4519,6 +4544,11 @@ Para reemplazar este cuadro de diálogo predeterminado controle el evento DataEr Controla una colección de imágenes que suelen utilizar otros controles como ListView, TreeView o ToolStrip. + + Manages common fullscreen, input, cursor, and power behavior for kiosk-style WinForms applications. + Manages common fullscreen, input, cursor, and power behavior for kiosk-style WinForms applications. + + Provides run-time information or descriptive text for a control. Proporciona información en tiempo de ejecución o texto descriptivo para un control. @@ -5420,6 +5450,11 @@ Do you want to replace it? Valor de este formulario si se muestra como un cuadro de diálogo. + + Indicates how the form is presented while its initial appearance is prepared. Classic never defers reveal. Deferred hides the form behind a DWM cloak until its background has painted, to reduce startup flash. Inherit resolves from the application-wide default. + Indicates how the form is presented while its initial appearance is prepared. Classic never defers reveal. Deferred hides the form behind a DWM cloak until its background has painted, to reduce startup flash. Inherit resolves from the application-wide default. + + Event raised when the help button is clicked. Evento que se desencadena cuando se hace clic en el botón Ayuda. @@ -5600,6 +5635,11 @@ Do you want to replace it? Tiene lugar cuando el formulario se muestra por primera vez. + + Occurs when the Windows Accessibility text size setting changes for this top-level form. + Occurs when the Windows Accessibility text size setting changes for this top-level form. + + The opacity percentage of the control. Porcentaje de la opacidad del control. @@ -6104,6 +6144,66 @@ El seguimiento de la pila donde tuvo lugar la operación no válida fue: La combinación de teclas no es válida. + + Occurs when the ContainerControl property value changes. + Occurs when the ContainerControl property value changes. + + + + The container whose containing form is controlled by the kiosk mode manager. + The container whose containing form is controlled by the kiosk mode manager. + + + + Indicates whether pressing Escape exits fullscreen mode. + Indicates whether pressing Escape exits fullscreen mode. + + + + Gets or sets a value indicating whether the resolved form is currently fullscreen. + Gets or sets a value indicating whether the resolved form is currently fullscreen. + + + + Occurs when the fullscreen state changes. + Occurs when the fullscreen state changes. + + + + Indicates whether fullscreen mode covers the Windows taskbar. + Indicates whether fullscreen mode covers the Windows taskbar. + + + + The delay, in milliseconds, before the mouse pointer is hidden while fullscreen. A value of 0 disables automatic pointer hiding. + The delay, in milliseconds, before the mouse pointer is hidden while fullscreen. A value of 0 disables automatic pointer hiding. + + + + Indicates whether Windows should keep the display and system awake while this component is active. + Indicates whether Windows should keep the display and system awake while this component is active. + + + + The key that toggles between fullscreen and restored mode. + The key that toggles between fullscreen and restored mode. + + + + Indicates whether the form is topmost while fullscreen. + Indicates whether the form is topmost while fullscreen. + + + + Occurs when keyboard, mouse, power resume, or session activity wakes the kiosk experience. + Occurs when keyboard, mouse, power resume, or session activity wakes the kiosk experience. + + + + The command that is executed whenever the kiosk experience is woken. + The command that is executed whenever the kiosk experience is woken. + + Enables the automatic handling of text that extends beyond the width of the label control. Permite el control automático del texto que se extiende más allá del ancho del control de la etiqueta. @@ -8675,8 +8775,8 @@ El seguimiento de la pila donde tuvo lugar la operación no válida fue: - Controls whether the RadioButton appears as normal or as a Windows PushButton. - Controla si el RadioButton es de tipo normal o como PushButton de Windows. + Controls whether the RadioButton appears as normal, as a Windows PushButton, or as a toggle switch. + Controls whether the RadioButton appears as normal, as a Windows PushButton, or as a toggle switch. @@ -11019,6 +11119,11 @@ El seguimiento de la pila donde tuvo lugar la operación no válida fue: Color de las líneas que conectan los nodos en TreeView. + + The uniform row-height leading factor applied to all nodes. + The uniform row-height leading factor applied to all nodes. + + Occurs when a node is clicked with the mouse. Tiene lugar cuando se hace clic con el mouse en un nodo. @@ -11044,6 +11149,11 @@ El seguimiento de la pila donde tuvo lugar la operación no válida fue: Nodos raíz en el control TreeView. + + Occurs when the value of the NodeLeading property changes. + Occurs when the value of the NodeLeading property changes. + + The string delimiter used for the path returned by a node's FullPath property. Delimitador de cadena utilizado para la ruta de acceso devuelta por la propiedad FullPath de un nodo. diff --git a/src/System.Windows.Forms/Resources/xlf/SR.fr.xlf b/src/System.Windows.Forms/Resources/xlf/SR.fr.xlf index f02c36b8e65..96b4e226aa0 100644 --- a/src/System.Windows.Forms/Resources/xlf/SR.fr.xlf +++ b/src/System.Windows.Forms/Resources/xlf/SR.fr.xlf @@ -242,6 +242,16 @@ Impossible de modifier le mode d'exceptions du thread une fois que des Controls ont été créés sur le thread. + + The default visual styles mode can only be set once and cannot be changed afterwards. + The default visual styles mode can only be set once and cannot be changed afterwards. + + + + VisualStylesMode.Inherit cannot be used as the application-wide default visual styles mode because the application root has no parent to inherit from. + VisualStylesMode.Inherit cannot be used as the application-wide default visual styles mode because the application root has no parent to inherit from. + + &Apply &Appliquer @@ -1697,6 +1707,11 @@ Indique la taille minimale du contrôle. + + Layout suspension requires a target derived from Control. + Layout suspension requires a target derived from Control. + + 'child' is not a child control of this parent. 'child' n'est pas un contrôle enfant de ce parent. @@ -2172,6 +2187,16 @@ Détermine si le contrôle est visible ou masqué. + + Occurs when the value of the VisualStylesMode property changes. + Occurs when the value of the VisualStylesMode property changes. + + + + Determines how the control renders itself when visual styles are applied. + Determines how the control renders itself when visual styles are applied. + + The width of the control, in container coordinates. La largeur du contrôle, en coordonnées conteneur. @@ -4519,6 +4544,11 @@ Pour remplacer cette boîte de dialogue par défaut, traitez l'événement DataE Gère une collection d'images qui sont généralement utilisées par d'autres contrôles tels que ListView, TreeView et ToolStrip. + + Manages common fullscreen, input, cursor, and power behavior for kiosk-style WinForms applications. + Manages common fullscreen, input, cursor, and power behavior for kiosk-style WinForms applications. + + Provides run-time information or descriptive text for a control. Fournit des informations d'exécution ou un texte descriptif pour un contrôle. @@ -5420,6 +5450,11 @@ Voulez-vous le remplacer ? La valeur que ce formulaire retourne s'il est affiché en tant que boîte de dialogue. + + Indicates how the form is presented while its initial appearance is prepared. Classic never defers reveal. Deferred hides the form behind a DWM cloak until its background has painted, to reduce startup flash. Inherit resolves from the application-wide default. + Indicates how the form is presented while its initial appearance is prepared. Classic never defers reveal. Deferred hides the form behind a DWM cloak until its background has painted, to reduce startup flash. Inherit resolves from the application-wide default. + + Event raised when the help button is clicked. Événement qui est déclenché à la suite d'un clic sur le bouton d'aide. @@ -5600,6 +5635,11 @@ Voulez-vous le remplacer ? Se produit lorsque le formulaire est affiché la première fois. + + Occurs when the Windows Accessibility text size setting changes for this top-level form. + Occurs when the Windows Accessibility text size setting changes for this top-level form. + + The opacity percentage of the control. Le pourcentage d'opacité du contrôle. @@ -6104,6 +6144,66 @@ Cette opération non conforme s'est produite sur la trace de la pile : La combinaison de touches n'est pas valide. + + Occurs when the ContainerControl property value changes. + Occurs when the ContainerControl property value changes. + + + + The container whose containing form is controlled by the kiosk mode manager. + The container whose containing form is controlled by the kiosk mode manager. + + + + Indicates whether pressing Escape exits fullscreen mode. + Indicates whether pressing Escape exits fullscreen mode. + + + + Gets or sets a value indicating whether the resolved form is currently fullscreen. + Gets or sets a value indicating whether the resolved form is currently fullscreen. + + + + Occurs when the fullscreen state changes. + Occurs when the fullscreen state changes. + + + + Indicates whether fullscreen mode covers the Windows taskbar. + Indicates whether fullscreen mode covers the Windows taskbar. + + + + The delay, in milliseconds, before the mouse pointer is hidden while fullscreen. A value of 0 disables automatic pointer hiding. + The delay, in milliseconds, before the mouse pointer is hidden while fullscreen. A value of 0 disables automatic pointer hiding. + + + + Indicates whether Windows should keep the display and system awake while this component is active. + Indicates whether Windows should keep the display and system awake while this component is active. + + + + The key that toggles between fullscreen and restored mode. + The key that toggles between fullscreen and restored mode. + + + + Indicates whether the form is topmost while fullscreen. + Indicates whether the form is topmost while fullscreen. + + + + Occurs when keyboard, mouse, power resume, or session activity wakes the kiosk experience. + Occurs when keyboard, mouse, power resume, or session activity wakes the kiosk experience. + + + + The command that is executed whenever the kiosk experience is woken. + The command that is executed whenever the kiosk experience is woken. + + Enables the automatic handling of text that extends beyond the width of the label control. Active la gestion automatique du texte qui dépasse les limites de largeur du contrôle label. @@ -8675,8 +8775,8 @@ Cette opération non conforme s'est produite sur la trace de la pile : - Controls whether the RadioButton appears as normal or as a Windows PushButton. - Contrôle si le RadioButton s'affiche sous la forme normale ou sous la forme d'un PushButton Windows. + Controls whether the RadioButton appears as normal, as a Windows PushButton, or as a toggle switch. + Controls whether the RadioButton appears as normal, as a Windows PushButton, or as a toggle switch. @@ -11019,6 +11119,11 @@ Cette opération non conforme s'est produite sur la trace de la pile : La couleur des lignes qui connectent les nœuds du TreeView. + + The uniform row-height leading factor applied to all nodes. + The uniform row-height leading factor applied to all nodes. + + Occurs when a node is clicked with the mouse. Se produit lors d'un clic de souris sur un nœud. @@ -11044,6 +11149,11 @@ Cette opération non conforme s'est produite sur la trace de la pile : Les nœuds racine dans le contrôle TreeView. + + Occurs when the value of the NodeLeading property changes. + Occurs when the value of the NodeLeading property changes. + + The string delimiter used for the path returned by a node's FullPath property. Le délimiteur de chaîne utilisé pour le chemin d'accès retourné par la propriété FullPath d'un nœud. diff --git a/src/System.Windows.Forms/Resources/xlf/SR.it.xlf b/src/System.Windows.Forms/Resources/xlf/SR.it.xlf index fc9f709a0be..b3b0a6de713 100644 --- a/src/System.Windows.Forms/Resources/xlf/SR.it.xlf +++ b/src/System.Windows.Forms/Resources/xlf/SR.it.xlf @@ -242,6 +242,16 @@ Una volta creati controlli nel thread, non è possibile modificare la modalità eccezioni del thread. + + The default visual styles mode can only be set once and cannot be changed afterwards. + The default visual styles mode can only be set once and cannot be changed afterwards. + + + + VisualStylesMode.Inherit cannot be used as the application-wide default visual styles mode because the application root has no parent to inherit from. + VisualStylesMode.Inherit cannot be used as the application-wide default visual styles mode because the application root has no parent to inherit from. + + &Apply &Applica @@ -1697,6 +1707,11 @@ Specifica le dimensioni minime del controllo. + + Layout suspension requires a target derived from Control. + Layout suspension requires a target derived from Control. + + 'child' is not a child control of this parent. 'child' non è un controllo figlio di questo elemento. @@ -2172,6 +2187,16 @@ Determina se il controllo è visibile o nascosto. + + Occurs when the value of the VisualStylesMode property changes. + Occurs when the value of the VisualStylesMode property changes. + + + + Determines how the control renders itself when visual styles are applied. + Determines how the control renders itself when visual styles are applied. + + The width of the control, in container coordinates. La larghezza del controllo, nelle coordinate del contenitore. @@ -4519,6 +4544,11 @@ Per sostituire questa finestra di dialogo, gestisci l'evento DataError. Gestisce una raccolta di immagini utilizzate in genere da altri controlli, quali ListView, TreeView o ToolStrip. + + Manages common fullscreen, input, cursor, and power behavior for kiosk-style WinForms applications. + Manages common fullscreen, input, cursor, and power behavior for kiosk-style WinForms applications. + + Provides run-time information or descriptive text for a control. Fornisce informazioni di runtime o testo descrittivo per un controllo. @@ -5420,6 +5450,11 @@ Sostituirlo? Il valore che questo form restituirà se viene visualizzato come finestra di dialogo. + + Indicates how the form is presented while its initial appearance is prepared. Classic never defers reveal. Deferred hides the form behind a DWM cloak until its background has painted, to reduce startup flash. Inherit resolves from the application-wide default. + Indicates how the form is presented while its initial appearance is prepared. Classic never defers reveal. Deferred hides the form behind a DWM cloak until its background has painted, to reduce startup flash. Inherit resolves from the application-wide default. + + Event raised when the help button is clicked. Evento generato quando si fa clic sul pulsante ?. @@ -5600,6 +5635,11 @@ Sostituirlo? Generato alla prima visualizzazione del form. + + Occurs when the Windows Accessibility text size setting changes for this top-level form. + Occurs when the Windows Accessibility text size setting changes for this top-level form. + + The opacity percentage of the control. La percentuale di opacità del controllo. @@ -6104,6 +6144,66 @@ Analisi dello stack dove si è verificata l'operazione non valida: Combinazione di tasti non valida. + + Occurs when the ContainerControl property value changes. + Occurs when the ContainerControl property value changes. + + + + The container whose containing form is controlled by the kiosk mode manager. + The container whose containing form is controlled by the kiosk mode manager. + + + + Indicates whether pressing Escape exits fullscreen mode. + Indicates whether pressing Escape exits fullscreen mode. + + + + Gets or sets a value indicating whether the resolved form is currently fullscreen. + Gets or sets a value indicating whether the resolved form is currently fullscreen. + + + + Occurs when the fullscreen state changes. + Occurs when the fullscreen state changes. + + + + Indicates whether fullscreen mode covers the Windows taskbar. + Indicates whether fullscreen mode covers the Windows taskbar. + + + + The delay, in milliseconds, before the mouse pointer is hidden while fullscreen. A value of 0 disables automatic pointer hiding. + The delay, in milliseconds, before the mouse pointer is hidden while fullscreen. A value of 0 disables automatic pointer hiding. + + + + Indicates whether Windows should keep the display and system awake while this component is active. + Indicates whether Windows should keep the display and system awake while this component is active. + + + + The key that toggles between fullscreen and restored mode. + The key that toggles between fullscreen and restored mode. + + + + Indicates whether the form is topmost while fullscreen. + Indicates whether the form is topmost while fullscreen. + + + + Occurs when keyboard, mouse, power resume, or session activity wakes the kiosk experience. + Occurs when keyboard, mouse, power resume, or session activity wakes the kiosk experience. + + + + The command that is executed whenever the kiosk experience is woken. + The command that is executed whenever the kiosk experience is woken. + + Enables the automatic handling of text that extends beyond the width of the label control. Consente la gestione automatica del testo che non è contenuto all'interno della larghezza del controllo etichetta. @@ -8675,8 +8775,8 @@ Analisi dello stack dove si è verificata l'operazione non valida: - Controls whether the RadioButton appears as normal or as a Windows PushButton. - Determina se il controllo RadioButton verrà visualizzato in modo standard o come un pulsante di comando di Windows. + Controls whether the RadioButton appears as normal, as a Windows PushButton, or as a toggle switch. + Controls whether the RadioButton appears as normal, as a Windows PushButton, or as a toggle switch. @@ -11019,6 +11119,11 @@ Analisi dello stack dove si è verificata l'operazione non valida: Il colore delle righe che connettono i nodi del controllo TreeView. + + The uniform row-height leading factor applied to all nodes. + The uniform row-height leading factor applied to all nodes. + + Occurs when a node is clicked with the mouse. Generato quando si fa clic con il mouse su un nodo. @@ -11044,6 +11149,11 @@ Analisi dello stack dove si è verificata l'operazione non valida: I nodi radice nel controllo TreeView. + + Occurs when the value of the NodeLeading property changes. + Occurs when the value of the NodeLeading property changes. + + The string delimiter used for the path returned by a node's FullPath property. Il delimitatore di stringa utilizzato per il percorso restituito dalla proprietà FullPath di un nodo. diff --git a/src/System.Windows.Forms/Resources/xlf/SR.ja.xlf b/src/System.Windows.Forms/Resources/xlf/SR.ja.xlf index 4511ba6cf76..d360b585db4 100644 --- a/src/System.Windows.Forms/Resources/xlf/SR.ja.xlf +++ b/src/System.Windows.Forms/Resources/xlf/SR.ja.xlf @@ -242,6 +242,16 @@ スレッドでコントロールが作成された後、スレッド例外モードを変更することはできません。 + + The default visual styles mode can only be set once and cannot be changed afterwards. + The default visual styles mode can only be set once and cannot be changed afterwards. + + + + VisualStylesMode.Inherit cannot be used as the application-wide default visual styles mode because the application root has no parent to inherit from. + VisualStylesMode.Inherit cannot be used as the application-wide default visual styles mode because the application root has no parent to inherit from. + + &Apply 適用(&A) @@ -1697,6 +1707,11 @@ コントロールの最小サイズを指定します。 + + Layout suspension requires a target derived from Control. + Layout suspension requires a target derived from Control. + + 'child' is not a child control of this parent. '子' はこの親の子コントロールではありません。 @@ -2172,6 +2187,16 @@ コントロールの表示、非表示を示します。 + + Occurs when the value of the VisualStylesMode property changes. + Occurs when the value of the VisualStylesMode property changes. + + + + Determines how the control renders itself when visual styles are applied. + Determines how the control renders itself when visual styles are applied. + + The width of the control, in container coordinates. コンテナー座標で表したコントロールの幅です。 @@ -4519,6 +4544,11 @@ To replace this default dialog please handle the DataError event. ListView、TreeView、または ToolStrip のような他のコントロールが通常使用するイメージのコレクションを管理します。 + + Manages common fullscreen, input, cursor, and power behavior for kiosk-style WinForms applications. + Manages common fullscreen, input, cursor, and power behavior for kiosk-style WinForms applications. + + Provides run-time information or descriptive text for a control. コントロールの実行時の情報または説明用のテキストを提供します。 @@ -5420,6 +5450,11 @@ Do you want to replace it? ダイアログ ボックスとして表示された場合に、このフォームが返す値です。 + + Indicates how the form is presented while its initial appearance is prepared. Classic never defers reveal. Deferred hides the form behind a DWM cloak until its background has painted, to reduce startup flash. Inherit resolves from the application-wide default. + Indicates how the form is presented while its initial appearance is prepared. Classic never defers reveal. Deferred hides the form behind a DWM cloak until its background has painted, to reduce startup flash. Inherit resolves from the application-wide default. + + Event raised when the help button is clicked. [ヘルプ] ボタンがクリックされたときに発生するイベントです。 @@ -5600,6 +5635,11 @@ Do you want to replace it? フォームが最初に表示されたときに発生します。 + + Occurs when the Windows Accessibility text size setting changes for this top-level form. + Occurs when the Windows Accessibility text size setting changes for this top-level form. + + The opacity percentage of the control. コントロールの不透明度の割合です。 @@ -6104,6 +6144,66 @@ Stack trace where the illegal operation occurred was: キーの組み合わせが有効ではありません。 + + Occurs when the ContainerControl property value changes. + Occurs when the ContainerControl property value changes. + + + + The container whose containing form is controlled by the kiosk mode manager. + The container whose containing form is controlled by the kiosk mode manager. + + + + Indicates whether pressing Escape exits fullscreen mode. + Indicates whether pressing Escape exits fullscreen mode. + + + + Gets or sets a value indicating whether the resolved form is currently fullscreen. + Gets or sets a value indicating whether the resolved form is currently fullscreen. + + + + Occurs when the fullscreen state changes. + Occurs when the fullscreen state changes. + + + + Indicates whether fullscreen mode covers the Windows taskbar. + Indicates whether fullscreen mode covers the Windows taskbar. + + + + The delay, in milliseconds, before the mouse pointer is hidden while fullscreen. A value of 0 disables automatic pointer hiding. + The delay, in milliseconds, before the mouse pointer is hidden while fullscreen. A value of 0 disables automatic pointer hiding. + + + + Indicates whether Windows should keep the display and system awake while this component is active. + Indicates whether Windows should keep the display and system awake while this component is active. + + + + The key that toggles between fullscreen and restored mode. + The key that toggles between fullscreen and restored mode. + + + + Indicates whether the form is topmost while fullscreen. + Indicates whether the form is topmost while fullscreen. + + + + Occurs when keyboard, mouse, power resume, or session activity wakes the kiosk experience. + Occurs when keyboard, mouse, power resume, or session activity wakes the kiosk experience. + + + + The command that is executed whenever the kiosk experience is woken. + The command that is executed whenever the kiosk experience is woken. + + Enables the automatic handling of text that extends beyond the width of the label control. ラベル コントロールの幅を越えるテキストの自動処理を有効にします。 @@ -8675,8 +8775,8 @@ Stack trace where the illegal operation occurred was: - Controls whether the RadioButton appears as normal or as a Windows PushButton. - RadioButton を通常どおり表示するか、Windows のプッシュボタンとして表示するかを制御します。 + Controls whether the RadioButton appears as normal, as a Windows PushButton, or as a toggle switch. + Controls whether the RadioButton appears as normal, as a Windows PushButton, or as a toggle switch. @@ -11019,6 +11119,11 @@ Stack trace where the illegal operation occurred was: ツリー ビューのノード間を接続する線の色です。 + + The uniform row-height leading factor applied to all nodes. + The uniform row-height leading factor applied to all nodes. + + Occurs when a node is clicked with the mouse. ノードがマウスでクリックされたときに発生します。 @@ -11044,6 +11149,11 @@ Stack trace where the illegal operation occurred was: TreeView コントロールのルートのノードです。 + + Occurs when the value of the NodeLeading property changes. + Occurs when the value of the NodeLeading property changes. + + The string delimiter used for the path returned by a node's FullPath property. ノードの FullPath プロパティにより返されるパスで使用する、文字列の区切り文字です。 diff --git a/src/System.Windows.Forms/Resources/xlf/SR.ko.xlf b/src/System.Windows.Forms/Resources/xlf/SR.ko.xlf index 2924f72aab7..05a25166742 100644 --- a/src/System.Windows.Forms/Resources/xlf/SR.ko.xlf +++ b/src/System.Windows.Forms/Resources/xlf/SR.ko.xlf @@ -242,6 +242,16 @@ 스레드에서 컨트롤이 만들어진 이후에는 스레드 예외 모드를 변경할 수 없습니다. + + The default visual styles mode can only be set once and cannot be changed afterwards. + The default visual styles mode can only be set once and cannot be changed afterwards. + + + + VisualStylesMode.Inherit cannot be used as the application-wide default visual styles mode because the application root has no parent to inherit from. + VisualStylesMode.Inherit cannot be used as the application-wide default visual styles mode because the application root has no parent to inherit from. + + &Apply 적용(&A) @@ -1697,6 +1707,11 @@ 컨트롤의 최소 크기를 지정합니다. + + Layout suspension requires a target derived from Control. + Layout suspension requires a target derived from Control. + + 'child' is not a child control of this parent. 'child'는 이 부모의 자식 컨트롤이 아닙니다. @@ -2172,6 +2187,16 @@ 컨트롤을 표시할지 여부를 결정합니다. + + Occurs when the value of the VisualStylesMode property changes. + Occurs when the value of the VisualStylesMode property changes. + + + + Determines how the control renders itself when visual styles are applied. + Determines how the control renders itself when visual styles are applied. + + The width of the control, in container coordinates. 컨테이너 좌표로 표시한 컨트롤의 너비입니다. @@ -4519,6 +4544,11 @@ To replace this default dialog please handle the DataError event. ListView, TreeView 또는 ToolStrip과 같은 다른 컨트롤에서 일반적으로 사용하는 이미지 컬렉션을 관리합니다. + + Manages common fullscreen, input, cursor, and power behavior for kiosk-style WinForms applications. + Manages common fullscreen, input, cursor, and power behavior for kiosk-style WinForms applications. + + Provides run-time information or descriptive text for a control. 컨트롤에 대한 설명 텍스트나 런타임 정보를 제공합니다. @@ -5420,6 +5450,11 @@ Do you want to replace it? 대화 상자로 표시할 경우, 이 폼에서 반환하는 값입니다. + + Indicates how the form is presented while its initial appearance is prepared. Classic never defers reveal. Deferred hides the form behind a DWM cloak until its background has painted, to reduce startup flash. Inherit resolves from the application-wide default. + Indicates how the form is presented while its initial appearance is prepared. Classic never defers reveal. Deferred hides the form behind a DWM cloak until its background has painted, to reduce startup flash. Inherit resolves from the application-wide default. + + Event raised when the help button is clicked. [도움말] 단추를 클릭하면 이벤트가 발생합니다. @@ -5600,6 +5635,11 @@ Do you want to replace it? 폼이 처음 표시될 때마다 발생합니다. + + Occurs when the Windows Accessibility text size setting changes for this top-level form. + Occurs when the Windows Accessibility text size setting changes for this top-level form. + + The opacity percentage of the control. 컨트롤의 불투명도(%)입니다. @@ -6104,6 +6144,66 @@ Stack trace where the illegal operation occurred was: 키 조합이 잘못되었습니다. + + Occurs when the ContainerControl property value changes. + Occurs when the ContainerControl property value changes. + + + + The container whose containing form is controlled by the kiosk mode manager. + The container whose containing form is controlled by the kiosk mode manager. + + + + Indicates whether pressing Escape exits fullscreen mode. + Indicates whether pressing Escape exits fullscreen mode. + + + + Gets or sets a value indicating whether the resolved form is currently fullscreen. + Gets or sets a value indicating whether the resolved form is currently fullscreen. + + + + Occurs when the fullscreen state changes. + Occurs when the fullscreen state changes. + + + + Indicates whether fullscreen mode covers the Windows taskbar. + Indicates whether fullscreen mode covers the Windows taskbar. + + + + The delay, in milliseconds, before the mouse pointer is hidden while fullscreen. A value of 0 disables automatic pointer hiding. + The delay, in milliseconds, before the mouse pointer is hidden while fullscreen. A value of 0 disables automatic pointer hiding. + + + + Indicates whether Windows should keep the display and system awake while this component is active. + Indicates whether Windows should keep the display and system awake while this component is active. + + + + The key that toggles between fullscreen and restored mode. + The key that toggles between fullscreen and restored mode. + + + + Indicates whether the form is topmost while fullscreen. + Indicates whether the form is topmost while fullscreen. + + + + Occurs when keyboard, mouse, power resume, or session activity wakes the kiosk experience. + Occurs when keyboard, mouse, power resume, or session activity wakes the kiosk experience. + + + + The command that is executed whenever the kiosk experience is woken. + The command that is executed whenever the kiosk experience is woken. + + Enables the automatic handling of text that extends beyond the width of the label control. 레이블 컨트롤의 너비보다 큰 텍스트를 자동으로 처리합니다. @@ -8675,8 +8775,8 @@ Stack trace where the illegal operation occurred was: - Controls whether the RadioButton appears as normal or as a Windows PushButton. - RadioButton을 기본 모양으로 표시할지 Windows 누름 단추로 표시할지 제어합니다. + Controls whether the RadioButton appears as normal, as a Windows PushButton, or as a toggle switch. + Controls whether the RadioButton appears as normal, as a Windows PushButton, or as a toggle switch. @@ -11019,6 +11119,11 @@ Stack trace where the illegal operation occurred was: TreeView의 노드를 연결하는 줄의 색입니다. + + The uniform row-height leading factor applied to all nodes. + The uniform row-height leading factor applied to all nodes. + + Occurs when a node is clicked with the mouse. 마우스로 노드를 클릭할 때 발생합니다. @@ -11044,6 +11149,11 @@ Stack trace where the illegal operation occurred was: TreeView 컨트롤의 루트 노드입니다. + + Occurs when the value of the NodeLeading property changes. + Occurs when the value of the NodeLeading property changes. + + The string delimiter used for the path returned by a node's FullPath property. 노드의 FullPath 속성으로 반환된 경로에 사용되는 문자열 구분 기호입니다. diff --git a/src/System.Windows.Forms/Resources/xlf/SR.pl.xlf b/src/System.Windows.Forms/Resources/xlf/SR.pl.xlf index f86297cb7f6..a334ff539c3 100644 --- a/src/System.Windows.Forms/Resources/xlf/SR.pl.xlf +++ b/src/System.Windows.Forms/Resources/xlf/SR.pl.xlf @@ -242,6 +242,16 @@ Trybu wyjątków wątku nie można zmienić po utworzeniu jakichkolwiek formantów w aplikacji. + + The default visual styles mode can only be set once and cannot be changed afterwards. + The default visual styles mode can only be set once and cannot be changed afterwards. + + + + VisualStylesMode.Inherit cannot be used as the application-wide default visual styles mode because the application root has no parent to inherit from. + VisualStylesMode.Inherit cannot be used as the application-wide default visual styles mode because the application root has no parent to inherit from. + + &Apply &Zastosuj @@ -1697,6 +1707,11 @@ Określa minimalny rozmiar formantu. + + Layout suspension requires a target derived from Control. + Layout suspension requires a target derived from Control. + + 'child' is not a child control of this parent. Element 'child' nie jest formantem podrzędnym dla tego formantu. @@ -2172,6 +2187,16 @@ Określa, czy formant jest widoczny, czy ukryty. + + Occurs when the value of the VisualStylesMode property changes. + Occurs when the value of the VisualStylesMode property changes. + + + + Determines how the control renders itself when visual styles are applied. + Determines how the control renders itself when visual styles are applied. + + The width of the control, in container coordinates. Szerokość formantu we współrzędnych kontenera. @@ -4519,6 +4544,11 @@ Aby zamienić to domyślne okno dialogowe, obsłuż zdarzenie DataError.Zarządza kolekcją obrazów, które są zazwyczaj używane przez inne formanty, takie jak ListView, TreeView lub ToolStrip. + + Manages common fullscreen, input, cursor, and power behavior for kiosk-style WinForms applications. + Manages common fullscreen, input, cursor, and power behavior for kiosk-style WinForms applications. + + Provides run-time information or descriptive text for a control. Wyświetla informacje o wykonaniu lub tekst opisowy dla formantu. @@ -5420,6 +5450,11 @@ Czy chcesz zastąpić? Wartość zwracana przez formularz, gdy jest on wyświetlany jako okno dialogowe. + + Indicates how the form is presented while its initial appearance is prepared. Classic never defers reveal. Deferred hides the form behind a DWM cloak until its background has painted, to reduce startup flash. Inherit resolves from the application-wide default. + Indicates how the form is presented while its initial appearance is prepared. Classic never defers reveal. Deferred hides the form behind a DWM cloak until its background has painted, to reduce startup flash. Inherit resolves from the application-wide default. + + Event raised when the help button is clicked. Zdarzenie wywoływane po kliknięciu przycisku pomocy. @@ -5600,6 +5635,11 @@ Czy chcesz zastąpić? Występuje zawsze, gdy formant jest pokazywany jako pierwszy. + + Occurs when the Windows Accessibility text size setting changes for this top-level form. + Occurs when the Windows Accessibility text size setting changes for this top-level form. + + The opacity percentage of the control. Stopień nieprzezroczystości formantu. @@ -6104,6 +6144,66 @@ Stos śledzenia, w którym wystąpiła zabroniona operacja: Kombinacja kluczy jest nieprawidłowa. + + Occurs when the ContainerControl property value changes. + Occurs when the ContainerControl property value changes. + + + + The container whose containing form is controlled by the kiosk mode manager. + The container whose containing form is controlled by the kiosk mode manager. + + + + Indicates whether pressing Escape exits fullscreen mode. + Indicates whether pressing Escape exits fullscreen mode. + + + + Gets or sets a value indicating whether the resolved form is currently fullscreen. + Gets or sets a value indicating whether the resolved form is currently fullscreen. + + + + Occurs when the fullscreen state changes. + Occurs when the fullscreen state changes. + + + + Indicates whether fullscreen mode covers the Windows taskbar. + Indicates whether fullscreen mode covers the Windows taskbar. + + + + The delay, in milliseconds, before the mouse pointer is hidden while fullscreen. A value of 0 disables automatic pointer hiding. + The delay, in milliseconds, before the mouse pointer is hidden while fullscreen. A value of 0 disables automatic pointer hiding. + + + + Indicates whether Windows should keep the display and system awake while this component is active. + Indicates whether Windows should keep the display and system awake while this component is active. + + + + The key that toggles between fullscreen and restored mode. + The key that toggles between fullscreen and restored mode. + + + + Indicates whether the form is topmost while fullscreen. + Indicates whether the form is topmost while fullscreen. + + + + Occurs when keyboard, mouse, power resume, or session activity wakes the kiosk experience. + Occurs when keyboard, mouse, power resume, or session activity wakes the kiosk experience. + + + + The command that is executed whenever the kiosk experience is woken. + The command that is executed whenever the kiosk experience is woken. + + Enables the automatic handling of text that extends beyond the width of the label control. Włącza automatyczną obsługę tekstu, który mieści się w szerokości formantu etykiety. @@ -8675,8 +8775,8 @@ Stos śledzenia, w którym wystąpiła zabroniona operacja: - Controls whether the RadioButton appears as normal or as a Windows PushButton. - Określa, czy element RadioButton ma mieć wygląd normalny, czy wygląd przycisku polecenia systemu Windows. + Controls whether the RadioButton appears as normal, as a Windows PushButton, or as a toggle switch. + Controls whether the RadioButton appears as normal, as a Windows PushButton, or as a toggle switch. @@ -11019,6 +11119,11 @@ Stos śledzenia, w którym wystąpiła zabroniona operacja: Kolor linii łączących węzły elementu TreeView. + + The uniform row-height leading factor applied to all nodes. + The uniform row-height leading factor applied to all nodes. + + Occurs when a node is clicked with the mouse. Występuje, gdy węzeł zostanie kliknięty przy użyciu myszy. @@ -11044,6 +11149,11 @@ Stos śledzenia, w którym wystąpiła zabroniona operacja: Węzły główne w formancie TreeView. + + Occurs when the value of the NodeLeading property changes. + Occurs when the value of the NodeLeading property changes. + + The string delimiter used for the path returned by a node's FullPath property. Ogranicznik ciągu używany w ścieżce zwracanej przez właściwość FullPath węzła. diff --git a/src/System.Windows.Forms/Resources/xlf/SR.pt-BR.xlf b/src/System.Windows.Forms/Resources/xlf/SR.pt-BR.xlf index 5dcda8ac793..b24e288fdb1 100644 --- a/src/System.Windows.Forms/Resources/xlf/SR.pt-BR.xlf +++ b/src/System.Windows.Forms/Resources/xlf/SR.pt-BR.xlf @@ -242,6 +242,16 @@ Não é possível alterar o modo de exceção de thread após a criação de qualquer controle no thread. + + The default visual styles mode can only be set once and cannot be changed afterwards. + The default visual styles mode can only be set once and cannot be changed afterwards. + + + + VisualStylesMode.Inherit cannot be used as the application-wide default visual styles mode because the application root has no parent to inherit from. + VisualStylesMode.Inherit cannot be used as the application-wide default visual styles mode because the application root has no parent to inherit from. + + &Apply &Aplicar @@ -1697,6 +1707,11 @@ Especifica o tamanho mínimo do controle. + + Layout suspension requires a target derived from Control. + Layout suspension requires a target derived from Control. + + 'child' is not a child control of this parent. 'child' não é um controle filho deste pai. @@ -2172,6 +2187,16 @@ Determina se o controle está visível ou oculto. + + Occurs when the value of the VisualStylesMode property changes. + Occurs when the value of the VisualStylesMode property changes. + + + + Determines how the control renders itself when visual styles are applied. + Determines how the control renders itself when visual styles are applied. + + The width of the control, in container coordinates. A largura do controle, nas coordenadas do recipiente. @@ -4519,6 +4544,11 @@ Para substituir a caixa de diálogo padrão, manipule o evento DataError.Gerencia uma coleção de imagens geralmente usadas por outros controles, como ListView, TreeView ou ToolStrip. + + Manages common fullscreen, input, cursor, and power behavior for kiosk-style WinForms applications. + Manages common fullscreen, input, cursor, and power behavior for kiosk-style WinForms applications. + + Provides run-time information or descriptive text for a control. Fornece informações em tempo de execução ou texto descritivo para um controle. @@ -5420,6 +5450,11 @@ Deseja substituí-lo? O valor que este formulário retornará se exibido como uma caixa de diálogo. + + Indicates how the form is presented while its initial appearance is prepared. Classic never defers reveal. Deferred hides the form behind a DWM cloak until its background has painted, to reduce startup flash. Inherit resolves from the application-wide default. + Indicates how the form is presented while its initial appearance is prepared. Classic never defers reveal. Deferred hides the form behind a DWM cloak until its background has painted, to reduce startup flash. Inherit resolves from the application-wide default. + + Event raised when the help button is clicked. Evento gerado quando o usuário clica no botão de ajuda. @@ -5600,6 +5635,11 @@ Deseja substituí-lo? Ocorre sempre que o formulário é mostrado pela primeira vez. + + Occurs when the Windows Accessibility text size setting changes for this top-level form. + Occurs when the Windows Accessibility text size setting changes for this top-level form. + + The opacity percentage of the control. A porcentagem de opacidade do controle. @@ -6104,6 +6144,66 @@ O rastreamento de pilha em que a operação ilegal ocorreu foi: A combinação de chave não é válida. + + Occurs when the ContainerControl property value changes. + Occurs when the ContainerControl property value changes. + + + + The container whose containing form is controlled by the kiosk mode manager. + The container whose containing form is controlled by the kiosk mode manager. + + + + Indicates whether pressing Escape exits fullscreen mode. + Indicates whether pressing Escape exits fullscreen mode. + + + + Gets or sets a value indicating whether the resolved form is currently fullscreen. + Gets or sets a value indicating whether the resolved form is currently fullscreen. + + + + Occurs when the fullscreen state changes. + Occurs when the fullscreen state changes. + + + + Indicates whether fullscreen mode covers the Windows taskbar. + Indicates whether fullscreen mode covers the Windows taskbar. + + + + The delay, in milliseconds, before the mouse pointer is hidden while fullscreen. A value of 0 disables automatic pointer hiding. + The delay, in milliseconds, before the mouse pointer is hidden while fullscreen. A value of 0 disables automatic pointer hiding. + + + + Indicates whether Windows should keep the display and system awake while this component is active. + Indicates whether Windows should keep the display and system awake while this component is active. + + + + The key that toggles between fullscreen and restored mode. + The key that toggles between fullscreen and restored mode. + + + + Indicates whether the form is topmost while fullscreen. + Indicates whether the form is topmost while fullscreen. + + + + Occurs when keyboard, mouse, power resume, or session activity wakes the kiosk experience. + Occurs when keyboard, mouse, power resume, or session activity wakes the kiosk experience. + + + + The command that is executed whenever the kiosk experience is woken. + The command that is executed whenever the kiosk experience is woken. + + Enables the automatic handling of text that extends beyond the width of the label control. Permite a manipulação automática de texto que ultrapassa a largura do controle de rótulo. @@ -8675,8 +8775,8 @@ O rastreamento de pilha em que a operação ilegal ocorreu foi: - Controls whether the RadioButton appears as normal or as a Windows PushButton. - Controla se RadioButton é exibido como normal ou como um botão de ação do Windows. + Controls whether the RadioButton appears as normal, as a Windows PushButton, or as a toggle switch. + Controls whether the RadioButton appears as normal, as a Windows PushButton, or as a toggle switch. @@ -11019,6 +11119,11 @@ O rastreamento de pilha em que a operação ilegal ocorreu foi: A cor das linhas que conectam os nós de TreeView. + + The uniform row-height leading factor applied to all nodes. + The uniform row-height leading factor applied to all nodes. + + Occurs when a node is clicked with the mouse. Ocorre quando o usuário clica com o mouse em um nó. @@ -11044,6 +11149,11 @@ O rastreamento de pilha em que a operação ilegal ocorreu foi: Nós raiz no controle TreeView. + + Occurs when the value of the NodeLeading property changes. + Occurs when the value of the NodeLeading property changes. + + The string delimiter used for the path returned by a node's FullPath property. O delimitador de cadeia de caracteres usado para o caminho retornado pela propriedade FullPath de um nó. diff --git a/src/System.Windows.Forms/Resources/xlf/SR.ru.xlf b/src/System.Windows.Forms/Resources/xlf/SR.ru.xlf index 89af4e75583..4b5822f5d32 100644 --- a/src/System.Windows.Forms/Resources/xlf/SR.ru.xlf +++ b/src/System.Windows.Forms/Resources/xlf/SR.ru.xlf @@ -242,6 +242,16 @@ Режим исключений потоков нельзя изменить, если в потоке были созданы элементы управления. + + The default visual styles mode can only be set once and cannot be changed afterwards. + The default visual styles mode can only be set once and cannot be changed afterwards. + + + + VisualStylesMode.Inherit cannot be used as the application-wide default visual styles mode because the application root has no parent to inherit from. + VisualStylesMode.Inherit cannot be used as the application-wide default visual styles mode because the application root has no parent to inherit from. + + &Apply &Применить @@ -1697,6 +1707,11 @@ Определяет минимальный размер элемента управления. + + Layout suspension requires a target derived from Control. + Layout suspension requires a target derived from Control. + + 'child' is not a child control of this parent. 'child' не является дочерним элементом управления этого родительского элемента. @@ -2172,6 +2187,16 @@ Определяет, отображается или скрыт данный элемент управления. + + Occurs when the value of the VisualStylesMode property changes. + Occurs when the value of the VisualStylesMode property changes. + + + + Determines how the control renders itself when visual styles are applied. + Determines how the control renders itself when visual styles are applied. + + The width of the control, in container coordinates. Ширина элемента управления в координатах контейнера. @@ -4519,6 +4544,11 @@ To replace this default dialog please handle the DataError event. Управляет коллекцией изображений, которые обычно используются другими элементами управления (например, ListView, TreeView или ToolStrip). + + Manages common fullscreen, input, cursor, and power behavior for kiosk-style WinForms applications. + Manages common fullscreen, input, cursor, and power behavior for kiosk-style WinForms applications. + + Provides run-time information or descriptive text for a control. Предоставляет элементу управления текст описания либо информацию во время выполнения. @@ -5420,6 +5450,11 @@ Do you want to replace it? Значение, возвращаемое этой формой при ее отображении в виде диалогового окна. + + Indicates how the form is presented while its initial appearance is prepared. Classic never defers reveal. Deferred hides the form behind a DWM cloak until its background has painted, to reduce startup flash. Inherit resolves from the application-wide default. + Indicates how the form is presented while its initial appearance is prepared. Classic never defers reveal. Deferred hides the form behind a DWM cloak until its background has painted, to reduce startup flash. Inherit resolves from the application-wide default. + + Event raised when the help button is clicked. Событие возникает при нажатии кнопки справки. @@ -5600,6 +5635,11 @@ Do you want to replace it? Возникает при первом отображении формы. + + Occurs when the Windows Accessibility text size setting changes for this top-level form. + Occurs when the Windows Accessibility text size setting changes for this top-level form. + + The opacity percentage of the control. Степень прозрачности элемента управления (в процентах). @@ -6104,6 +6144,66 @@ Stack trace where the illegal operation occurred was: Недопустимая комбинация ключа. + + Occurs when the ContainerControl property value changes. + Occurs when the ContainerControl property value changes. + + + + The container whose containing form is controlled by the kiosk mode manager. + The container whose containing form is controlled by the kiosk mode manager. + + + + Indicates whether pressing Escape exits fullscreen mode. + Indicates whether pressing Escape exits fullscreen mode. + + + + Gets or sets a value indicating whether the resolved form is currently fullscreen. + Gets or sets a value indicating whether the resolved form is currently fullscreen. + + + + Occurs when the fullscreen state changes. + Occurs when the fullscreen state changes. + + + + Indicates whether fullscreen mode covers the Windows taskbar. + Indicates whether fullscreen mode covers the Windows taskbar. + + + + The delay, in milliseconds, before the mouse pointer is hidden while fullscreen. A value of 0 disables automatic pointer hiding. + The delay, in milliseconds, before the mouse pointer is hidden while fullscreen. A value of 0 disables automatic pointer hiding. + + + + Indicates whether Windows should keep the display and system awake while this component is active. + Indicates whether Windows should keep the display and system awake while this component is active. + + + + The key that toggles between fullscreen and restored mode. + The key that toggles between fullscreen and restored mode. + + + + Indicates whether the form is topmost while fullscreen. + Indicates whether the form is topmost while fullscreen. + + + + Occurs when keyboard, mouse, power resume, or session activity wakes the kiosk experience. + Occurs when keyboard, mouse, power resume, or session activity wakes the kiosk experience. + + + + The command that is executed whenever the kiosk experience is woken. + The command that is executed whenever the kiosk experience is woken. + + Enables the automatic handling of text that extends beyond the width of the label control. Включает автоматическую обработку текста, выходящего за пределы ширины элемента управления с подписью. @@ -8675,8 +8775,8 @@ Stack trace where the illegal operation occurred was: - Controls whether the RadioButton appears as normal or as a Windows PushButton. - Указывает, будет ли RadioButton отображаться стандартно или как объект Windows PushButton. + Controls whether the RadioButton appears as normal, as a Windows PushButton, or as a toggle switch. + Controls whether the RadioButton appears as normal, as a Windows PushButton, or as a toggle switch. @@ -11019,6 +11119,11 @@ Stack trace where the illegal operation occurred was: Цвет линий, соединяющих узлы в TreeView. + + The uniform row-height leading factor applied to all nodes. + The uniform row-height leading factor applied to all nodes. + + Occurs when a node is clicked with the mouse. Возникает при щелчке мыши на узле. @@ -11044,6 +11149,11 @@ Stack trace where the illegal operation occurred was: Корневые узлы в элементе управления TreeView. + + Occurs when the value of the NodeLeading property changes. + Occurs when the value of the NodeLeading property changes. + + The string delimiter used for the path returned by a node's FullPath property. Разделитель строк в пути, возвращаемом свойством FullPath узла. diff --git a/src/System.Windows.Forms/Resources/xlf/SR.tr.xlf b/src/System.Windows.Forms/Resources/xlf/SR.tr.xlf index 9e72305a890..69b3c986ff0 100644 --- a/src/System.Windows.Forms/Resources/xlf/SR.tr.xlf +++ b/src/System.Windows.Forms/Resources/xlf/SR.tr.xlf @@ -242,6 +242,16 @@ İş parçacığında herhangi bir Denetim oluşturulduktan sonra iş parçacığı özel durum modu değiştirilemez. + + The default visual styles mode can only be set once and cannot be changed afterwards. + The default visual styles mode can only be set once and cannot be changed afterwards. + + + + VisualStylesMode.Inherit cannot be used as the application-wide default visual styles mode because the application root has no parent to inherit from. + VisualStylesMode.Inherit cannot be used as the application-wide default visual styles mode because the application root has no parent to inherit from. + + &Apply &Uygula @@ -1697,6 +1707,11 @@ Denetimin en küçük boyutunu belirtir. + + Layout suspension requires a target derived from Control. + Layout suspension requires a target derived from Control. + + 'child' is not a child control of this parent. 'alt' bu üstün alt denetimi değil. @@ -2172,6 +2187,16 @@ Denetimin görünür mü yoksa gizli mi olduğunu belirler. + + Occurs when the value of the VisualStylesMode property changes. + Occurs when the value of the VisualStylesMode property changes. + + + + Determines how the control renders itself when visual styles are applied. + Determines how the control renders itself when visual styles are applied. + + The width of the control, in container coordinates. Denetimin genişliği (kapsayıcı koordinatlarında). @@ -4519,6 +4544,11 @@ Bu varsayılan iletişim kutusunu değiştirmek için, lütfen DataError olayın Genellikle ListView, TreeView veya ToolStrip gibi diğer denetimler tarafından kullanılan resim koleksiyonunu düzenler. + + Manages common fullscreen, input, cursor, and power behavior for kiosk-style WinForms applications. + Manages common fullscreen, input, cursor, and power behavior for kiosk-style WinForms applications. + + Provides run-time information or descriptive text for a control. Bir denetim için çalışma zamanı bilgileri veya açıklayıcı metin sağlar. @@ -5420,6 +5450,11 @@ Değiştirmek istiyor musunuz? Bu formun iletişim kutusu olarak görüntülendiğinde döndüreceği değer. + + Indicates how the form is presented while its initial appearance is prepared. Classic never defers reveal. Deferred hides the form behind a DWM cloak until its background has painted, to reduce startup flash. Inherit resolves from the application-wide default. + Indicates how the form is presented while its initial appearance is prepared. Classic never defers reveal. Deferred hides the form behind a DWM cloak until its background has painted, to reduce startup flash. Inherit resolves from the application-wide default. + + Event raised when the help button is clicked. Yardım düğmesi tıklatıldığında harekete geçirilen olay. @@ -5600,6 +5635,11 @@ Değiştirmek istiyor musunuz? Formun her ilk görüntülenişinde gerçekleşir. + + Occurs when the Windows Accessibility text size setting changes for this top-level form. + Occurs when the Windows Accessibility text size setting changes for this top-level form. + + The opacity percentage of the control. Formun donukluk yüzdesi. @@ -6104,6 +6144,66 @@ Geçersiz işlemin gerçekleştiği yığın izi: Anahtar birleşimi geçerli değil. + + Occurs when the ContainerControl property value changes. + Occurs when the ContainerControl property value changes. + + + + The container whose containing form is controlled by the kiosk mode manager. + The container whose containing form is controlled by the kiosk mode manager. + + + + Indicates whether pressing Escape exits fullscreen mode. + Indicates whether pressing Escape exits fullscreen mode. + + + + Gets or sets a value indicating whether the resolved form is currently fullscreen. + Gets or sets a value indicating whether the resolved form is currently fullscreen. + + + + Occurs when the fullscreen state changes. + Occurs when the fullscreen state changes. + + + + Indicates whether fullscreen mode covers the Windows taskbar. + Indicates whether fullscreen mode covers the Windows taskbar. + + + + The delay, in milliseconds, before the mouse pointer is hidden while fullscreen. A value of 0 disables automatic pointer hiding. + The delay, in milliseconds, before the mouse pointer is hidden while fullscreen. A value of 0 disables automatic pointer hiding. + + + + Indicates whether Windows should keep the display and system awake while this component is active. + Indicates whether Windows should keep the display and system awake while this component is active. + + + + The key that toggles between fullscreen and restored mode. + The key that toggles between fullscreen and restored mode. + + + + Indicates whether the form is topmost while fullscreen. + Indicates whether the form is topmost while fullscreen. + + + + Occurs when keyboard, mouse, power resume, or session activity wakes the kiosk experience. + Occurs when keyboard, mouse, power resume, or session activity wakes the kiosk experience. + + + + The command that is executed whenever the kiosk experience is woken. + The command that is executed whenever the kiosk experience is woken. + + Enables the automatic handling of text that extends beyond the width of the label control. Etiket denetimi genişliğini aşan metnin otomatik işlenmesini sağlar. @@ -8675,8 +8775,8 @@ Geçersiz işlemin gerçekleştiği yığın izi: - Controls whether the RadioButton appears as normal or as a Windows PushButton. - RadioButton'ın normal mi ya da bir Windows PushButton olarak mı görüneceğini denetler. + Controls whether the RadioButton appears as normal, as a Windows PushButton, or as a toggle switch. + Controls whether the RadioButton appears as normal, as a Windows PushButton, or as a toggle switch. @@ -11019,6 +11119,11 @@ Geçersiz işlemin gerçekleştiği yığın izi: TreeView'in düğümlerini bağlayan çizgilerin rengi. + + The uniform row-height leading factor applied to all nodes. + The uniform row-height leading factor applied to all nodes. + + Occurs when a node is clicked with the mouse. Öğe fareyle tıklatıldığında gerçekleşir. @@ -11044,6 +11149,11 @@ Geçersiz işlemin gerçekleştiği yığın izi: TreeView denetiminde kök düğümler. + + Occurs when the value of the NodeLeading property changes. + Occurs when the value of the NodeLeading property changes. + + The string delimiter used for the path returned by a node's FullPath property. Düğümün FullPath özelliği tarafından döndürülen yol için kullanılan dize sınırlayıcı. diff --git a/src/System.Windows.Forms/Resources/xlf/SR.zh-Hans.xlf b/src/System.Windows.Forms/Resources/xlf/SR.zh-Hans.xlf index 6827f20269f..0bcd8145b9a 100644 --- a/src/System.Windows.Forms/Resources/xlf/SR.zh-Hans.xlf +++ b/src/System.Windows.Forms/Resources/xlf/SR.zh-Hans.xlf @@ -242,6 +242,16 @@ 只要在线程上创建了任何控件,则线程异常模式将不能再有任何更改。 + + The default visual styles mode can only be set once and cannot be changed afterwards. + The default visual styles mode can only be set once and cannot be changed afterwards. + + + + VisualStylesMode.Inherit cannot be used as the application-wide default visual styles mode because the application root has no parent to inherit from. + VisualStylesMode.Inherit cannot be used as the application-wide default visual styles mode because the application root has no parent to inherit from. + + &Apply 应用(&A) @@ -1697,6 +1707,11 @@ 指定控件的最小大小。 + + Layout suspension requires a target derived from Control. + Layout suspension requires a target derived from Control. + + 'child' is not a child control of this parent. “child”不是此父级的子控件。 @@ -2172,6 +2187,16 @@ 确定该控件是可见的还是隐藏的。 + + Occurs when the value of the VisualStylesMode property changes. + Occurs when the value of the VisualStylesMode property changes. + + + + Determines how the control renders itself when visual styles are applied. + Determines how the control renders itself when visual styles are applied. + + The width of the control, in container coordinates. 控件的宽度(以容器坐标表示)。 @@ -4519,6 +4544,11 @@ To replace this default dialog please handle the DataError event. 管理通常由其他控件(如 ListView、TreeView 或 ToolStrip)使用的图像集合。 + + Manages common fullscreen, input, cursor, and power behavior for kiosk-style WinForms applications. + Manages common fullscreen, input, cursor, and power behavior for kiosk-style WinForms applications. + + Provides run-time information or descriptive text for a control. 为控件提供运行时信息或说明性文字。 @@ -5420,6 +5450,11 @@ Do you want to replace it? 此窗体在显示为对话框时将返回的值。 + + Indicates how the form is presented while its initial appearance is prepared. Classic never defers reveal. Deferred hides the form behind a DWM cloak until its background has painted, to reduce startup flash. Inherit resolves from the application-wide default. + Indicates how the form is presented while its initial appearance is prepared. Classic never defers reveal. Deferred hides the form behind a DWM cloak until its background has painted, to reduce startup flash. Inherit resolves from the application-wide default. + + Event raised when the help button is clicked. 单击“帮助”按钮时引发的事件。 @@ -5600,6 +5635,11 @@ Do you want to replace it? 每当窗体第一次显示时发生。 + + Occurs when the Windows Accessibility text size setting changes for this top-level form. + Occurs when the Windows Accessibility text size setting changes for this top-level form. + + The opacity percentage of the control. 控件的不透明度百分比。 @@ -6104,6 +6144,66 @@ Stack trace where the illegal operation occurred was: 组合键无效。 + + Occurs when the ContainerControl property value changes. + Occurs when the ContainerControl property value changes. + + + + The container whose containing form is controlled by the kiosk mode manager. + The container whose containing form is controlled by the kiosk mode manager. + + + + Indicates whether pressing Escape exits fullscreen mode. + Indicates whether pressing Escape exits fullscreen mode. + + + + Gets or sets a value indicating whether the resolved form is currently fullscreen. + Gets or sets a value indicating whether the resolved form is currently fullscreen. + + + + Occurs when the fullscreen state changes. + Occurs when the fullscreen state changes. + + + + Indicates whether fullscreen mode covers the Windows taskbar. + Indicates whether fullscreen mode covers the Windows taskbar. + + + + The delay, in milliseconds, before the mouse pointer is hidden while fullscreen. A value of 0 disables automatic pointer hiding. + The delay, in milliseconds, before the mouse pointer is hidden while fullscreen. A value of 0 disables automatic pointer hiding. + + + + Indicates whether Windows should keep the display and system awake while this component is active. + Indicates whether Windows should keep the display and system awake while this component is active. + + + + The key that toggles between fullscreen and restored mode. + The key that toggles between fullscreen and restored mode. + + + + Indicates whether the form is topmost while fullscreen. + Indicates whether the form is topmost while fullscreen. + + + + Occurs when keyboard, mouse, power resume, or session activity wakes the kiosk experience. + Occurs when keyboard, mouse, power resume, or session activity wakes the kiosk experience. + + + + The command that is executed whenever the kiosk experience is woken. + The command that is executed whenever the kiosk experience is woken. + + Enables the automatic handling of text that extends beyond the width of the label control. 启用对扩展到标签控件宽度以外的文本的自动处理。 @@ -8675,8 +8775,8 @@ Stack trace where the illegal operation occurred was: - Controls whether the RadioButton appears as normal or as a Windows PushButton. - 控制 RadioButton 是按通常情况显示还是显示为 Windows PushButton。 + Controls whether the RadioButton appears as normal, as a Windows PushButton, or as a toggle switch. + Controls whether the RadioButton appears as normal, as a Windows PushButton, or as a toggle switch. @@ -11019,6 +11119,11 @@ Stack trace where the illegal operation occurred was: 连接树视图节点的线条的颜色。 + + The uniform row-height leading factor applied to all nodes. + The uniform row-height leading factor applied to all nodes. + + Occurs when a node is clicked with the mouse. 用鼠标单击节点时发生。 @@ -11044,6 +11149,11 @@ Stack trace where the illegal operation occurred was: TreeView 控件中的根节点。 + + Occurs when the value of the NodeLeading property changes. + Occurs when the value of the NodeLeading property changes. + + The string delimiter used for the path returned by a node's FullPath property. 用于由节点的 FullPath 属性返回的路径的字符串分隔符。 diff --git a/src/System.Windows.Forms/Resources/xlf/SR.zh-Hant.xlf b/src/System.Windows.Forms/Resources/xlf/SR.zh-Hant.xlf index 15f5f06c126..fff244d28d6 100644 --- a/src/System.Windows.Forms/Resources/xlf/SR.zh-Hant.xlf +++ b/src/System.Windows.Forms/Resources/xlf/SR.zh-Hant.xlf @@ -242,6 +242,16 @@ 一旦在執行緒上建立了控制項,就不能變更執行緒例外狀況模式。 + + The default visual styles mode can only be set once and cannot be changed afterwards. + The default visual styles mode can only be set once and cannot be changed afterwards. + + + + VisualStylesMode.Inherit cannot be used as the application-wide default visual styles mode because the application root has no parent to inherit from. + VisualStylesMode.Inherit cannot be used as the application-wide default visual styles mode because the application root has no parent to inherit from. + + &Apply 套用(&A) @@ -1697,6 +1707,11 @@ 指定控制項大小的最小值。 + + Layout suspension requires a target derived from Control. + Layout suspension requires a target derived from Control. + + 'child' is not a child control of this parent. 'child' 不是此父系的子控制項。 @@ -2172,6 +2187,16 @@ 決定控制項是可見或隱藏。 + + Occurs when the value of the VisualStylesMode property changes. + Occurs when the value of the VisualStylesMode property changes. + + + + Determines how the control renders itself when visual styles are applied. + Determines how the control renders itself when visual styles are applied. + + The width of the control, in container coordinates. 容器座標中控制項的寬度。 @@ -4519,6 +4544,11 @@ To replace this default dialog please handle the DataError event. 管理通常由其他控制項 (例如 ListView、TreeView 或 ToolStrip) 使用的影像集合。 + + Manages common fullscreen, input, cursor, and power behavior for kiosk-style WinForms applications. + Manages common fullscreen, input, cursor, and power behavior for kiosk-style WinForms applications. + + Provides run-time information or descriptive text for a control. 提供控制項的執行階段資訊或描述文字。 @@ -5420,6 +5450,11 @@ Do you want to replace it? 如果顯示為對話方塊,此表單會傳回的值。 + + Indicates how the form is presented while its initial appearance is prepared. Classic never defers reveal. Deferred hides the form behind a DWM cloak until its background has painted, to reduce startup flash. Inherit resolves from the application-wide default. + Indicates how the form is presented while its initial appearance is prepared. Classic never defers reveal. Deferred hides the form behind a DWM cloak until its background has painted, to reduce startup flash. Inherit resolves from the application-wide default. + + Event raised when the help button is clicked. 按下說明按鈕時引發的事件。 @@ -5600,6 +5635,11 @@ Do you want to replace it? 當第一次顯示表單時發生。 + + Occurs when the Windows Accessibility text size setting changes for this top-level form. + Occurs when the Windows Accessibility text size setting changes for this top-level form. + + The opacity percentage of the control. 控制項的透明度百分比。 @@ -6104,6 +6144,66 @@ Stack trace where the illegal operation occurred was: 組合鍵無效。 + + Occurs when the ContainerControl property value changes. + Occurs when the ContainerControl property value changes. + + + + The container whose containing form is controlled by the kiosk mode manager. + The container whose containing form is controlled by the kiosk mode manager. + + + + Indicates whether pressing Escape exits fullscreen mode. + Indicates whether pressing Escape exits fullscreen mode. + + + + Gets or sets a value indicating whether the resolved form is currently fullscreen. + Gets or sets a value indicating whether the resolved form is currently fullscreen. + + + + Occurs when the fullscreen state changes. + Occurs when the fullscreen state changes. + + + + Indicates whether fullscreen mode covers the Windows taskbar. + Indicates whether fullscreen mode covers the Windows taskbar. + + + + The delay, in milliseconds, before the mouse pointer is hidden while fullscreen. A value of 0 disables automatic pointer hiding. + The delay, in milliseconds, before the mouse pointer is hidden while fullscreen. A value of 0 disables automatic pointer hiding. + + + + Indicates whether Windows should keep the display and system awake while this component is active. + Indicates whether Windows should keep the display and system awake while this component is active. + + + + The key that toggles between fullscreen and restored mode. + The key that toggles between fullscreen and restored mode. + + + + Indicates whether the form is topmost while fullscreen. + Indicates whether the form is topmost while fullscreen. + + + + Occurs when keyboard, mouse, power resume, or session activity wakes the kiosk experience. + Occurs when keyboard, mouse, power resume, or session activity wakes the kiosk experience. + + + + The command that is executed whenever the kiosk experience is woken. + The command that is executed whenever the kiosk experience is woken. + + Enables the automatic handling of text that extends beyond the width of the label control. 啟用自動處理,以處理超出標籤控制項寬度的文件。 @@ -8675,8 +8775,8 @@ Stack trace where the illegal operation occurred was: - Controls whether the RadioButton appears as normal or as a Windows PushButton. - 控制 RadioButton 顯示為一般按鈕或 Windows PushButton。 + Controls whether the RadioButton appears as normal, as a Windows PushButton, or as a toggle switch. + Controls whether the RadioButton appears as normal, as a Windows PushButton, or as a toggle switch. @@ -11019,6 +11119,11 @@ Stack trace where the illegal operation occurred was: 連接樹狀檢視節點的線條色彩。 + + The uniform row-height leading factor applied to all nodes. + The uniform row-height leading factor applied to all nodes. + + Occurs when a node is clicked with the mouse. 以滑鼠按一下節點時發生。 @@ -11044,6 +11149,11 @@ Stack trace where the illegal operation occurred was: TreeView 控制項中的根節點。 + + Occurs when the value of the NodeLeading property changes. + Occurs when the value of the NodeLeading property changes. + + The string delimiter used for the path returned by a node's FullPath property. 節點 FullPath 屬性用來傳回路徑的字串分隔符號。 diff --git a/src/System.Windows.Forms/System/Windows/Forms/Application.SystemTextSize.cs b/src/System.Windows.Forms/System/Windows/Forms/Application.SystemTextSize.cs new file mode 100644 index 00000000000..9fcf5d314a2 --- /dev/null +++ b/src/System.Windows.Forms/System/Windows/Forms/Application.SystemTextSize.cs @@ -0,0 +1,127 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. + +using Microsoft.Win32; + +namespace System.Windows.Forms; + +public sealed partial class Application +{ +#if NET11_0_OR_GREATER + private static readonly object s_eventSystemTextSizeChanged = new(); + + private static double s_lastSystemTextSize = 1.0; + private static bool s_systemTextSizeNotificationsInitialized; + + /// + /// Gets the current Windows Accessibility text-scale factor + /// (Settings → Accessibility → Text size), as a multiplier in the range 1.0–2.25. + /// + /// + /// + /// Live getter — re-reads the underlying system value on every access; does not cache. + /// + /// + /// This property is orthogonal to display DPI; see for the DPI-scaling story. + /// + /// + /// On operating systems earlier than Windows 10 version 1507, this property returns 1.0. + /// + /// + public static double SystemTextSize => ScaleHelper.GetSystemTextScaleFactor(); + + /// + /// Occurs once per process when the Windows Accessibility text-scale setting changes. + /// + /// + /// + /// WinForms detects relevant changes by re-reading the underlying text-scale factor when Windows reports an + /// accessibility preference change. Because there is no dedicated text-scale notification, unrelated accessibility + /// changes do not raise this event unless the factor actually changed. + /// + /// + /// The framework listens through a single internal subscription. + /// Individual instances receive their own + /// notifications from their window procedures and do not subscribe to this static event, avoiding framework-managed + /// static-event rooting of forms. + /// + /// + /// On operating systems earlier than Windows 10 version 1507, this event is not raised. + /// + /// + public static event EventHandler? SystemTextSizeChanged + { + add + { + if (value is null) + { + return; + } + + lock (s_internalSyncObject) + { + EnsureSystemTextSizeNotificationsInitialized(); + AddEventHandler(s_eventSystemTextSizeChanged, value); + } + } + remove + { + if (value is null) + { + return; + } + + lock (s_internalSyncObject) + { + RemoveEventHandler(s_eventSystemTextSizeChanged, value); + + if (s_systemTextSizeNotificationsInitialized + && s_eventHandlers?[s_eventSystemTextSizeChanged] is null) + { + SystemEvents.UserPreferenceChanged -= OnSystemTextSizeUserPreferenceChanged; + s_systemTextSizeNotificationsInitialized = false; + } + } + } + } + + private static void EnsureSystemTextSizeNotificationsInitialized() + { + lock (s_internalSyncObject) + { + if (s_systemTextSizeNotificationsInitialized) + { + return; + } + + s_lastSystemTextSize = ScaleHelper.GetSystemTextScaleFactor(); + SystemEvents.UserPreferenceChanged += OnSystemTextSizeUserPreferenceChanged; + s_systemTextSizeNotificationsInitialized = true; + } + } + + private static void OnSystemTextSizeUserPreferenceChanged(object? sender, UserPreferenceChangedEventArgs e) + { + if (e.Category != UserPreferenceCategory.Accessibility + || !ScaleHelper.TryGetSystemTextScaleFactor(out double systemTextSize)) + { + return; + } + + EventHandler? handler; + + lock (s_internalSyncObject) + { + if (systemTextSize == s_lastSystemTextSize) + { + return; + } + + s_lastSystemTextSize = systemTextSize; + handler = s_eventHandlers?[s_eventSystemTextSizeChanged] as EventHandler; + } + + handler?.Invoke(null, EventArgs.Empty); + } +#endif +} diff --git a/src/System.Windows.Forms/System/Windows/Forms/Application.WindowsAccentColor.cs b/src/System.Windows.Forms/System/Windows/Forms/Application.WindowsAccentColor.cs new file mode 100644 index 00000000000..0fbce5b214b --- /dev/null +++ b/src/System.Windows.Forms/System/Windows/Forms/Application.WindowsAccentColor.cs @@ -0,0 +1,52 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. + +using Windows.Win32.System.WinRT; +using Windows.Win32.UI.ViewManagement; + +namespace System.Windows.Forms; + +public sealed partial class Application +{ + /// + /// Gets the user's current Windows accent color. + /// + /// + /// The accent color the user has selected in the Windows personalization settings. + /// + /// + /// + /// The value is read from the Windows UISettings runtime component and reflects the color the + /// user picked (or that Windows derived automatically from the desktop background). If the user has not + /// chosen an accent color, Windows returns a default accent color defined by the operating system, so + /// this method always yields a usable color rather than throwing or returning an empty value. + /// + /// + public static unsafe Color GetWindowsAccentColor() + { + HSTRING className = default; + + fixed (char* pClassName = "Windows.UI.ViewManagement.UISettings") + { + PInvokeCore.WindowsCreateString((PCWSTR)pClassName, 36u, &className).ThrowOnFailure(); + } + + try + { + using ComScope inspectable = new(null); + PInvokeCore.RoActivateInstance(className, inspectable).ThrowOnFailure(); + + using ComScope settings = inspectable.TryQuery(out HRESULT hr); + hr.ThrowOnFailure(); + + UIColor color; + settings.Value->GetColorValue(UIColorType.Accent, &color).ThrowOnFailure(); + + return Color.FromArgb(color.A, color.R, color.G, color.B); + } + finally + { + PInvokeCore.WindowsDeleteString(className); + } + } +} diff --git a/src/System.Windows.Forms/System/Windows/Forms/Application.cs b/src/System.Windows.Forms/System/Windows/Forms/Application.cs index aab496c2744..4b82d013a66 100644 --- a/src/System.Windows.Forms/System/Windows/Forms/Application.cs +++ b/src/System.Windows.Forms/System/Windows/Forms/Application.cs @@ -44,6 +44,11 @@ public sealed partial class Application private static bool s_useWaitCursor; private static SystemColorMode? s_colorMode; +#if NET11_0_OR_GREATER + private static FormRevealMode? s_defaultFormRevealMode; +#endif + + private static VisualStylesMode? s_defaultVisualStylesMode; private const string DarkModeKeyPath = "HKEY_CURRENT_USER\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Themes\\Personalize"; private const string DarkModeKey = "AppsUseLightTheme"; @@ -249,6 +254,45 @@ internal static bool CustomThreadExceptionHandlerAttached /// public static SystemColorMode ColorMode => s_colorMode ?? SystemColorMode.Classic; +#if NET11_0_OR_GREATER + /// + /// Gets the configured default used as the reveal behavior for + /// top-level forms that do not set explicitly. + /// + /// + /// + /// If no mode has been configured with , this + /// returns . Unlike , this + /// getter may return the unresolved sentinel; use + /// for the fully resolved answer. + /// + /// + public static FormRevealMode DefaultFormRevealMode + => s_defaultFormRevealMode ?? FormRevealMode.Inherit; + + /// + /// Gets a value indicating whether newly created top-level forms use deferred reveal by default. + /// + /// + /// + /// This is the fully resolved answer: when + /// is , or when it is + /// (the default, when + /// has never been called) and is . + /// Accessibility trumps compatibility here: an application that opts into dark mode gets + /// flash-reduced form reveal automatically, without also having to call + /// explicitly. + /// + /// + public static bool IsFormRevealDeferred => DefaultFormRevealMode switch + { + FormRevealMode.Deferred => true, + FormRevealMode.Classic => false, + _ => IsDarkModeEnabled + }; + +#endif + /// /// True if the has been set at least once. /// @@ -381,6 +425,36 @@ static void NotifySystemEventsOfColorChange() } } +#if NET11_0_OR_GREATER + /// + /// Sets the process-wide default used for top-level forms that do + /// not set explicitly. + /// + /// The default form reveal mode to use for newly created top-level forms. + /// + /// + /// Set the default form reveal mode before creating UI to ensure newly created forms use the + /// intended startup presentation behavior. Unlike the visual-styles default-mode setter (which is + /// write-once, since rendering-version selection is a static, one-time choice), this method can be + /// called more than once, and is a valid argument (it resets + /// the default back to its own ambient resolution via ). Free + /// reassignment is intentional: the effective default is derived in part from + /// and , which can themselves change for the lifetime of the process; + /// locking this value after first use would make forms created after a later dark-mode change use a + /// stale reveal behavior. + /// + /// + /// + /// is not a valid value. + /// + public static void SetDefaultFormRevealMode(FormRevealMode mode) + { + SourceGenerated.EnumValidator.Validate(mode, nameof(mode)); + s_defaultFormRevealMode = mode; + } + +#endif + internal static Font DefaultFont => s_defaultFontScaled ?? s_defaultFont!; /// @@ -430,8 +504,8 @@ private static int GetSystemColorModeInternal() return systemColorMode; } - private static bool IsSystemDarkModeAvailable => - !SystemInformation.HighContrast && OsVersion.IsWindows11_OrGreater(); + private static bool IsSystemDarkModeAvailable + => !SystemInformation.HighContrast && OsVersion.IsWindows11_OrGreater(); /// /// Gets a value indicating whether the application is running in a dark system color context. @@ -592,6 +666,86 @@ public static void RegisterMessageLoop(MessageLoopCallback? callback) public static bool RenderWithVisualStyles => ComCtlSupportsVisualStyles && VisualStyleRenderer.IsSupported; + /// + /// Gets the default used as the rendering style guideline for the + /// application's controls. + /// + /// + /// The used as the rendering style guideline for the application's + /// controls. This is when visual styles are enabled and + /// otherwise, unless it has been changed by a call to + /// . + /// + /// + /// + /// The default value is so that applications that simply + /// recompile against a newer framework keep their existing look. Opt in to a newer renderer by + /// calling . + /// + /// + /// While visual styles are disabled, the effective value remains , + /// even if a different default has already been requested through + /// . + /// + /// + public static VisualStylesMode DefaultVisualStylesMode + => s_defaultVisualStylesMode switch + { + { } visualStylesMode when UseVisualStyles => visualStylesMode, + { } => VisualStylesMode.Disabled, + _ => UseVisualStyles + ? VisualStylesMode.Classic + : VisualStylesMode.Disabled + }; + + /// + /// Sets the default used as the rendering style guideline for the + /// application's controls. + /// + /// The version of the visual styles renderer to use by default. + /// + /// The default visual styles mode has already been set to a different value. It can only be set once. + /// + /// + /// is , which is not valid as + /// the application-wide default because the application root has no parent to inherit from. + /// + /// + /// is not a defined value. + /// + /// + /// + /// Call this method before creating any window. If visual styles have not been enabled through + /// , the effective mode remains . + /// Passing has the same effect as not calling + /// . + /// + /// + public static void SetDefaultVisualStylesMode(VisualStylesMode styleSetting) + { + // Validate the value. Inherit is the ambient sentinel and is invalid as the application default, + // since the application root has no parent to inherit from. The non-contiguous members (Inherit, + // Latest) prevent using the source generated enum validator. + _ = styleSetting switch + { + VisualStylesMode.Classic => styleSetting, + VisualStylesMode.Disabled => styleSetting, + VisualStylesMode.Net11 => styleSetting, + VisualStylesMode.Latest => styleSetting, + VisualStylesMode.Inherit => throw new ArgumentException( + SR.Application_VisualStylesModeInheritInvalidAsDefault, + nameof(styleSetting)), + _ => throw new InvalidEnumArgumentException(nameof(styleSetting), (int)styleSetting, typeof(VisualStylesMode)) + }; + + if (s_defaultVisualStylesMode is { } current && current != styleSetting) + { + throw new InvalidOperationException(SR.Application_VisualStylesModeCanOnlyBeSetOnce); + } + + s_defaultVisualStylesMode = styleSetting; + } + /// /// Gets or sets the format string to apply to top level window captions /// when they are displayed with a warning banner. diff --git a/src/System.Windows.Forms/System/Windows/Forms/ErrorProvider/ErrorBlinkStyle.cs b/src/System.Windows.Forms/System/Windows/Forms/Components/ErrorProvider/ErrorBlinkStyle.cs similarity index 100% rename from src/System.Windows.Forms/System/Windows/Forms/ErrorProvider/ErrorBlinkStyle.cs rename to src/System.Windows.Forms/System/Windows/Forms/Components/ErrorProvider/ErrorBlinkStyle.cs diff --git a/src/System.Windows.Forms/System/Windows/Forms/ErrorProvider/ErrorIconAlignment.cs b/src/System.Windows.Forms/System/Windows/Forms/Components/ErrorProvider/ErrorIconAlignment.cs similarity index 100% rename from src/System.Windows.Forms/System/Windows/Forms/ErrorProvider/ErrorIconAlignment.cs rename to src/System.Windows.Forms/System/Windows/Forms/Components/ErrorProvider/ErrorIconAlignment.cs diff --git a/src/System.Windows.Forms/System/Windows/Forms/ErrorProvider/ErrorProvider.ControlItem.ControlItemAccessibleObject.cs b/src/System.Windows.Forms/System/Windows/Forms/Components/ErrorProvider/ErrorProvider.ControlItem.ControlItemAccessibleObject.cs similarity index 100% rename from src/System.Windows.Forms/System/Windows/Forms/ErrorProvider/ErrorProvider.ControlItem.ControlItemAccessibleObject.cs rename to src/System.Windows.Forms/System/Windows/Forms/Components/ErrorProvider/ErrorProvider.ControlItem.ControlItemAccessibleObject.cs diff --git a/src/System.Windows.Forms/System/Windows/Forms/ErrorProvider/ErrorProvider.ControlItem.cs b/src/System.Windows.Forms/System/Windows/Forms/Components/ErrorProvider/ErrorProvider.ControlItem.cs similarity index 100% rename from src/System.Windows.Forms/System/Windows/Forms/ErrorProvider/ErrorProvider.ControlItem.cs rename to src/System.Windows.Forms/System/Windows/Forms/Components/ErrorProvider/ErrorProvider.ControlItem.cs diff --git a/src/System.Windows.Forms/System/Windows/Forms/ErrorProvider/ErrorProvider.ErrorProviderStates.cs b/src/System.Windows.Forms/System/Windows/Forms/Components/ErrorProvider/ErrorProvider.ErrorProviderStates.cs similarity index 100% rename from src/System.Windows.Forms/System/Windows/Forms/ErrorProvider/ErrorProvider.ErrorProviderStates.cs rename to src/System.Windows.Forms/System/Windows/Forms/Components/ErrorProvider/ErrorProvider.ErrorProviderStates.cs diff --git a/src/System.Windows.Forms/System/Windows/Forms/ErrorProvider/ErrorProvider.ErrorWindow.ErrorWindowAccessibleObject.cs b/src/System.Windows.Forms/System/Windows/Forms/Components/ErrorProvider/ErrorProvider.ErrorWindow.ErrorWindowAccessibleObject.cs similarity index 100% rename from src/System.Windows.Forms/System/Windows/Forms/ErrorProvider/ErrorProvider.ErrorWindow.ErrorWindowAccessibleObject.cs rename to src/System.Windows.Forms/System/Windows/Forms/Components/ErrorProvider/ErrorProvider.ErrorWindow.ErrorWindowAccessibleObject.cs diff --git a/src/System.Windows.Forms/System/Windows/Forms/ErrorProvider/ErrorProvider.ErrorWindow.cs b/src/System.Windows.Forms/System/Windows/Forms/Components/ErrorProvider/ErrorProvider.ErrorWindow.cs similarity index 100% rename from src/System.Windows.Forms/System/Windows/Forms/ErrorProvider/ErrorProvider.ErrorWindow.cs rename to src/System.Windows.Forms/System/Windows/Forms/Components/ErrorProvider/ErrorProvider.ErrorWindow.cs diff --git a/src/System.Windows.Forms/System/Windows/Forms/ErrorProvider/ErrorProvider.IconRegion.cs b/src/System.Windows.Forms/System/Windows/Forms/Components/ErrorProvider/ErrorProvider.IconRegion.cs similarity index 100% rename from src/System.Windows.Forms/System/Windows/Forms/ErrorProvider/ErrorProvider.IconRegion.cs rename to src/System.Windows.Forms/System/Windows/Forms/Components/ErrorProvider/ErrorProvider.IconRegion.cs diff --git a/src/System.Windows.Forms/System/Windows/Forms/ErrorProvider/ErrorProvider.cs b/src/System.Windows.Forms/System/Windows/Forms/Components/ErrorProvider/ErrorProvider.cs similarity index 100% rename from src/System.Windows.Forms/System/Windows/Forms/ErrorProvider/ErrorProvider.cs rename to src/System.Windows.Forms/System/Windows/Forms/Components/ErrorProvider/ErrorProvider.cs diff --git a/src/System.Windows.Forms/System/Windows/Forms/Help/HelpProvider.cs b/src/System.Windows.Forms/System/Windows/Forms/Components/HelpProvider.cs similarity index 100% rename from src/System.Windows.Forms/System/Windows/Forms/Help/HelpProvider.cs rename to src/System.Windows.Forms/System/Windows/Forms/Components/HelpProvider.cs diff --git a/src/System.Windows.Forms/System/Windows/Forms/Controls/ImageList/ColorDepth.cs b/src/System.Windows.Forms/System/Windows/Forms/Components/ImageList/ColorDepth.cs similarity index 100% rename from src/System.Windows.Forms/System/Windows/Forms/Controls/ImageList/ColorDepth.cs rename to src/System.Windows.Forms/System/Windows/Forms/Components/ImageList/ColorDepth.cs diff --git a/src/System.Windows.Forms/System/Windows/Forms/Controls/ImageList/ImageList.ImageCollection.ImageInfo.cs b/src/System.Windows.Forms/System/Windows/Forms/Components/ImageList/ImageList.ImageCollection.ImageInfo.cs similarity index 100% rename from src/System.Windows.Forms/System/Windows/Forms/Controls/ImageList/ImageList.ImageCollection.ImageInfo.cs rename to src/System.Windows.Forms/System/Windows/Forms/Components/ImageList/ImageList.ImageCollection.ImageInfo.cs diff --git a/src/System.Windows.Forms/System/Windows/Forms/Controls/ImageList/ImageList.ImageCollection.cs b/src/System.Windows.Forms/System/Windows/Forms/Components/ImageList/ImageList.ImageCollection.cs similarity index 100% rename from src/System.Windows.Forms/System/Windows/Forms/Controls/ImageList/ImageList.ImageCollection.cs rename to src/System.Windows.Forms/System/Windows/Forms/Components/ImageList/ImageList.ImageCollection.cs diff --git a/src/System.Windows.Forms/System/Windows/Forms/Controls/ImageList/ImageList.Indexer.cs b/src/System.Windows.Forms/System/Windows/Forms/Components/ImageList/ImageList.Indexer.cs similarity index 100% rename from src/System.Windows.Forms/System/Windows/Forms/Controls/ImageList/ImageList.Indexer.cs rename to src/System.Windows.Forms/System/Windows/Forms/Components/ImageList/ImageList.Indexer.cs diff --git a/src/System.Windows.Forms/System/Windows/Forms/Controls/ImageList/ImageList.NativeImageList.cs b/src/System.Windows.Forms/System/Windows/Forms/Components/ImageList/ImageList.NativeImageList.cs similarity index 100% rename from src/System.Windows.Forms/System/Windows/Forms/Controls/ImageList/ImageList.NativeImageList.cs rename to src/System.Windows.Forms/System/Windows/Forms/Components/ImageList/ImageList.NativeImageList.cs diff --git a/src/System.Windows.Forms/System/Windows/Forms/Controls/ImageList/ImageList.Original.cs b/src/System.Windows.Forms/System/Windows/Forms/Components/ImageList/ImageList.Original.cs similarity index 100% rename from src/System.Windows.Forms/System/Windows/Forms/Controls/ImageList/ImageList.Original.cs rename to src/System.Windows.Forms/System/Windows/Forms/Components/ImageList/ImageList.Original.cs diff --git a/src/System.Windows.Forms/System/Windows/Forms/Controls/ImageList/ImageList.OriginalOptions.cs b/src/System.Windows.Forms/System/Windows/Forms/Components/ImageList/ImageList.OriginalOptions.cs similarity index 100% rename from src/System.Windows.Forms/System/Windows/Forms/Controls/ImageList/ImageList.OriginalOptions.cs rename to src/System.Windows.Forms/System/Windows/Forms/Components/ImageList/ImageList.OriginalOptions.cs diff --git a/src/System.Windows.Forms/System/Windows/Forms/Controls/ImageList/ImageList.cs b/src/System.Windows.Forms/System/Windows/Forms/Components/ImageList/ImageList.cs similarity index 100% rename from src/System.Windows.Forms/System/Windows/Forms/Controls/ImageList/ImageList.cs rename to src/System.Windows.Forms/System/Windows/Forms/Components/ImageList/ImageList.cs diff --git a/src/System.Windows.Forms/System/Windows/Forms/Controls/ImageList/ImageListConverter.cs b/src/System.Windows.Forms/System/Windows/Forms/Components/ImageList/ImageListConverter.cs similarity index 100% rename from src/System.Windows.Forms/System/Windows/Forms/Controls/ImageList/ImageListConverter.cs rename to src/System.Windows.Forms/System/Windows/Forms/Components/ImageList/ImageListConverter.cs diff --git a/src/System.Windows.Forms/System/Windows/Forms/Controls/ImageList/ImageListStreamer.cs b/src/System.Windows.Forms/System/Windows/Forms/Components/ImageList/ImageListStreamer.cs similarity index 100% rename from src/System.Windows.Forms/System/Windows/Forms/Controls/ImageList/ImageListStreamer.cs rename to src/System.Windows.Forms/System/Windows/Forms/Components/ImageList/ImageListStreamer.cs diff --git a/src/System.Windows.Forms/System/Windows/Forms/Controls/ImageList/RelatedImageListAttribute.cs b/src/System.Windows.Forms/System/Windows/Forms/Components/ImageList/RelatedImageListAttribute.cs similarity index 100% rename from src/System.Windows.Forms/System/Windows/Forms/Controls/ImageList/RelatedImageListAttribute.cs rename to src/System.Windows.Forms/System/Windows/Forms/Components/ImageList/RelatedImageListAttribute.cs diff --git a/src/System.Windows.Forms/System/Windows/Forms/Components/KioskModeManager/KioskModeManager.cs b/src/System.Windows.Forms/System/Windows/Forms/Components/KioskModeManager/KioskModeManager.cs new file mode 100644 index 00000000000..87befa212f9 --- /dev/null +++ b/src/System.Windows.Forms/System/Windows/Forms/Components/KioskModeManager/KioskModeManager.cs @@ -0,0 +1,1209 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. + +using System.ComponentModel; +using System.ComponentModel.Design; +using System.Drawing; +using System.Runtime.InteropServices; +using System.Windows.Input; +using Windows.Win32.System.Power; +using Windows.Win32.System.Threading; + +namespace System.Windows.Forms; + +#if NET11_0_OR_GREATER +/// +/// Manages common kiosk-mode behavior for a WinForms form. +/// +/// +/// +/// is a component that can be dropped on a +/// form or on a user control at design time. Set +/// to the owning container and the component resolves the containing +/// when fullscreen behavior is needed. +/// +/// +/// The component can make the resolved form fullscreen, optionally cover the +/// taskbar, keep the form topmost while fullscreen, suppress display and +/// system sleep, hide the mouse pointer after inactivity, and notify the +/// application when user or system activity wakes the kiosk experience. +/// +/// +/// +/// +/// public partial class MainForm : Form +/// { +/// private readonly KioskModeManager _kioskModeManager; +/// +/// public MainForm() +/// { +/// InitializeComponent(); +/// +/// _kioskModeManager = new KioskModeManager +/// { +/// ContainerControl = this, +/// HideTaskbar = true, +/// TopMostInFullScreen = true, +/// MousePointerAutoHideDelay = 3000, +/// SuppressPowerSaving = true +/// }; +/// +/// _kioskModeManager.Wakeup += (sender, e) => +/// { +/// if (e.Source == KioskModeWakeupSource.Mouse) +/// { +/// // Refresh the kiosk surface after mouse activity. +/// } +/// }; +/// +/// if (!_kioskModeManager.FullScreen) +/// { +/// _kioskModeManager.ToggleFullScreen(); +/// } +/// } +/// } +/// +/// +[DefaultProperty(nameof(ToggleFullScreenKey))] +[ToolboxItemFilter("System.Windows.Forms")] +[SRDescription(nameof(SR.DescriptionKioskModeManager))] +public class KioskModeManager : Component, ISupportInitialize +{ + private const uint PowerRequestContextVersion = 0; + private const POWER_REQUEST_CONTEXT_FLAGS PowerRequestContextSimpleString = (POWER_REQUEST_CONTEXT_FLAGS)0x00000001; + private const uint NotifyForThisSession = 0; + private const nint PbtApmResumeAutomatic = 0x0012; + private const nint WtsConsoleConnect = 0x0001; + private const nint WtsRemoteConnect = 0x0003; + private const nint WtsSessionLogon = 0x0005; + private const nint WtsSessionUnlock = 0x0008; + private const string PowerRequestReason = "WinForms KioskModeManager: Preventing screen saver and sleep"; + + private static readonly object s_containerControlChangedEvent = new(); + private static readonly object s_fullScreenChangedEvent = new(); + private static readonly object s_wakeupEvent = new(); + + private ContainerControl? _containerControl; + private bool _containerControlExplicitlySet; + private Form? _targetForm; + private bool _isFullScreen; + private bool _pendingFullScreen; + private bool _initializing; + private bool _isCursorHidden; + private readonly List _parentChain = []; + private KioskModeMessageFilter? _messageFilter; + private KioskModeFormObserver? _formObserver; + private Timer? _mousePointerAutoHideTimer; + + private FormBorderStyle _savedBorderStyle; + private FormWindowState _savedWindowState; + private Rectangle _savedBounds; + private bool _savedTopMost; + + private bool _hideTaskbar; + private bool _topMostInFullScreen; + private Keys _toggleFullScreenKey = Keys.F11; + private bool _escapeExitsFullScreen = true; + private bool _suppressPowerSaving; + private int _mousePointerAutoHideDelay; + private ICommand? _wakeUpCommand; + + private HANDLE _powerRequestHandle; + + /// + /// Initializes a new instance of the class. + /// + public KioskModeManager() + { + } + + /// + /// Initializes a new instance of the class + /// and adds it to the specified container. + /// + /// The container that owns the component. + /// + /// is . + /// + public KioskModeManager(IContainer container) + { + ArgumentNullException.ThrowIfNull(container); + container.Add(this); + } + + /// + /// Gets or sets the associated with the component. + /// + /// + /// + /// When the component is sited at design time and + /// has not been set explicitly, the component resolves the root component of the + /// designer host (typically the owning or ) + /// and assigns it as the . An explicitly assigned + /// is never overwritten. + /// + /// + public override ISite? Site + { + get => base.Site; + set + { + base.Site = value; + + if (value is not null + && !_containerControlExplicitlySet + && value.GetService(typeof(IDesignerHost)) is IDesignerHost host + && host.RootComponent is ContainerControl root) + { + SetContainerControl(root, isExplicitAssignment: false); + } + } + } + + /// + /// Gets or sets the container whose containing form is controlled by this + /// component. + /// + /// + /// A that is either a or + /// a child container that can resolve a parent form; otherwise, + /// . + /// + /// + /// + /// This property intentionally uses rather + /// than . A can be placed + /// on a at design time, and that user control + /// can later be hosted by a form. Restricting the property to + /// would make that design-time scenario inconsistent. + /// + /// + [SRCategory(nameof(SR.CatBehavior))] + [SRDescription(nameof(SR.KioskModeManagerContainerControlDescr))] + [DefaultValue(null)] + public ContainerControl? ContainerControl + { + get => _containerControl; + set => SetContainerControl(value, isExplicitAssignment: true); + } + + /// + /// Occurs when the value of changes. + /// + [SRCategory(nameof(SR.CatPropertyChanged))] + [SRDescription(nameof(SR.KioskModeManagerContainerControlChangedDescr))] + public event EventHandler? ContainerControlChanged + { + add => Events.AddHandler(s_containerControlChangedEvent, value); + remove => Events.RemoveHandler(s_containerControlChangedEvent, value); + } + + /// + /// Raises the event. + /// + /// An that contains the event data. + [EditorBrowsable(EditorBrowsableState.Advanced)] + protected virtual void OnContainerControlChanged(EventArgs e) + { + if (Events[s_containerControlChangedEvent] is EventHandler handler) + { + handler(this, e); + } + } + + /// + /// Gets or sets a value indicating whether fullscreen mode covers the + /// Windows taskbar. + /// + /// + /// to size the form to the complete screen bounds; + /// to use normal maximized form behavior. + /// The default is . + /// + /// + /// + /// When this property is , the form is maximized + /// and Windows keeps the taskbar available according to the user's shell + /// settings. When this property is , the form is + /// sized to the screen bounds so the kiosk surface covers the taskbar. + /// + /// + [SRCategory(nameof(SR.CatBehavior))] + [SRDescription(nameof(SR.KioskModeManagerHideTaskbarDescr))] + [DefaultValue(false)] + public bool HideTaskbar + { + get => _hideTaskbar; + set + { + if (_hideTaskbar == value) + { + return; + } + + _hideTaskbar = value; + + if (_isFullScreen && _targetForm is not null) + { + ApplyFullScreen(_targetForm); + } + } + } + + /// + /// Gets or sets a value indicating whether the form is topmost while + /// fullscreen. + /// + /// + /// to set while + /// fullscreen; otherwise, . The default is + /// . + /// + [SRCategory(nameof(SR.CatBehavior))] + [SRDescription(nameof(SR.KioskModeManagerTopMostInFullScreenDescr))] + [DefaultValue(false)] + public bool TopMostInFullScreen + { + get => _topMostInFullScreen; + set + { + if (_topMostInFullScreen == value) + { + return; + } + + _topMostInFullScreen = value; + + if (_isFullScreen && _targetForm is not null) + { + _targetForm.TopMost = value; + } + } + } + + /// + /// Gets or sets the key that toggles fullscreen mode. + /// + /// + /// A value that is handled without modifiers. The + /// default is . + /// + [SRCategory(nameof(SR.CatBehavior))] + [SRDescription(nameof(SR.KioskModeManagerToggleFullScreenKeyDescr))] + [DefaultValue(Keys.F11)] + public Keys ToggleFullScreenKey + { + get => _toggleFullScreenKey; + set + { + if (_toggleFullScreenKey == value) + { + return; + } + + _toggleFullScreenKey = value; + } + } + + /// + /// Gets or sets a value indicating whether pressing Escape exits + /// fullscreen mode. + /// + /// + /// to exit fullscreen mode when Escape is pressed + /// without modifiers; otherwise, . The default is + /// . + /// + [SRCategory(nameof(SR.CatBehavior))] + [SRDescription(nameof(SR.KioskModeManagerEscapeExitsFullScreenDescr))] + [DefaultValue(true)] + public bool EscapeExitsFullScreen + { + get => _escapeExitsFullScreen; + set + { + if (_escapeExitsFullScreen == value) + { + return; + } + + _escapeExitsFullScreen = value; + } + } + + /// + /// Gets or sets a value indicating whether the component requests that + /// Windows keep the display and system awake. + /// + /// + /// to request that Windows suppress display sleep + /// and system sleep; otherwise, . The default is + /// . + /// + /// + /// + /// This property uses the Windows power request APIs. It prevents ordinary + /// display and system idle sleep while enabled, but it does not override + /// every system policy and does not configure wake timers, Wake-on-LAN, or + /// voice activation. + /// + /// + /// + /// Windows could not create or activate the power request. + /// + [SRCategory(nameof(SR.CatBehavior))] + [SRDescription(nameof(SR.KioskModeManagerSuppressPowerSavingDescr))] + [DefaultValue(false)] + public bool SuppressPowerSaving + { + get => _suppressPowerSaving; + set + { + if (_suppressPowerSaving == value) + { + return; + } + + bool previousValue = _suppressPowerSaving; + _suppressPowerSaving = value; + + try + { + UpdatePowerRequest(); + } + catch + { + _suppressPowerSaving = previousValue; + throw; + } + } + } + + /// + /// Gets or sets the amount of time, in milliseconds, before the mouse + /// pointer is hidden while fullscreen. + /// + /// + /// The inactivity delay in milliseconds. A value of 0 disables automatic + /// pointer hiding. The default is 0. + /// + /// + /// + /// The timer is active only while the component is in fullscreen mode. + /// When the user moves the mouse after the component hid the pointer, the + /// pointer is shown immediately and the timer starts again. + /// + /// + /// + /// is less than 0. + /// + [SRCategory(nameof(SR.CatBehavior))] + [SRDescription(nameof(SR.KioskModeManagerMousePointerAutoHideDelayDescr))] + [DefaultValue(0)] + public int MousePointerAutoHideDelay + { + get => _mousePointerAutoHideDelay; + set + { + ArgumentOutOfRangeException.ThrowIfNegative(value); + + if (_mousePointerAutoHideDelay == value) + { + return; + } + + _mousePointerAutoHideDelay = value; + + if (value == 0) + { + StopMousePointerAutoHideTimer(); + ShowMousePointerIfHidden(); + } + else if (_isFullScreen) + { + RestartMousePointerAutoHideTimer(); + } + } + } + + /// + /// Gets or sets a value indicating whether the resolved form is currently + /// fullscreen. + /// + /// + /// if the component has placed the form in + /// fullscreen mode; otherwise, . + /// + /// + /// + /// Setting this property to enters fullscreen mode and + /// setting it to restores the previously saved form + /// state. The property is bindable so it can participate in two-way data + /// binding. + /// + /// + [SRCategory(nameof(SR.CatBehavior))] + [SRDescription(nameof(SR.KioskModeManagerFullScreenDescr))] + [Bindable(true)] + [DefaultValue(false)] + [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)] + public bool FullScreen + { + get => _pendingFullScreen || _isFullScreen; + set + { + if (!value) + { + _pendingFullScreen = false; + ExitFullScreen(); + } + else if (_initializing || DesignMode || !EnterFullScreen()) + { + _pendingFullScreen = true; + } + else + { + _pendingFullScreen = false; + } + } + } + + /// + /// Occurs when the fullscreen state changes. + /// + [SRCategory(nameof(SR.CatPropertyChanged))] + [SRDescription(nameof(SR.KioskModeManagerFullScreenChangedDescr))] + public event EventHandler? FullScreenChanged + { + add => Events.AddHandler(s_fullScreenChangedEvent, value); + remove => Events.RemoveHandler(s_fullScreenChangedEvent, value); + } + + /// + /// Raises the event. + /// + /// An that contains the event data. + [EditorBrowsable(EditorBrowsableState.Advanced)] + protected virtual void OnFullScreenChanged(EventArgs e) + { + if (Events[s_fullScreenChangedEvent] is EventHandler handler) + { + handler(this, e); + } + } + + /// + /// Occurs when keyboard, mouse, power resume, or session activity wakes + /// the kiosk experience. + /// + /// + /// + /// The event reports activity that the component can observe on the UI + /// thread or through Windows power/session notifications. It does not mean + /// that the component caused the computer to wake from sleep, and it does + /// not configure voice wake, network wake, or wake timers. + /// + /// + [SRCategory(nameof(SR.CatAction))] + [SRDescription(nameof(SR.KioskModeManagerWakeupDescr))] + public event KioskModeWakeupEventHandler? Wakeup + { + add => Events.AddHandler(s_wakeupEvent, value); + remove => Events.RemoveHandler(s_wakeupEvent, value); + } + + /// + /// Raises the event. + /// + /// A that contains the event data. + [EditorBrowsable(EditorBrowsableState.Advanced)] + protected virtual void OnWakeup(KioskModeWakeupEventArgs e) + { + if (Events[s_wakeupEvent] is KioskModeWakeupEventHandler handler) + { + handler(this, e); + } + + if (_wakeUpCommand is not null) + { + // The wakeup source is passed as its string name so the bound command stays + // independent of the WinForms-specific enum type. + string parameter = e.Source.ToString(); + if (_wakeUpCommand.CanExecute(parameter)) + { + _wakeUpCommand.Execute(parameter); + } + } + } + + /// + /// Gets or sets the command that is executed whenever the + /// event is raised. + /// + /// + /// An to execute on every wakeup, or + /// to execute no command. The default is . + /// + /// + /// + /// The command is executed with the wakeup source name (the result of + /// .) as the command + /// parameter, keeping the command independent of the WinForms-specific enum type. + /// + /// + [SRCategory(nameof(SR.CatAction))] + [SRDescription(nameof(SR.KioskModeManagerWakeUpCommandDescr))] + [Bindable(true)] + [DefaultValue(null)] + public ICommand? WakeUpCommand + { + get => _wakeUpCommand; + set => _wakeUpCommand = value; + } + + /// + /// Places the resolved form in fullscreen mode. + /// + /// + /// +/// The component saves the form's border style, window state, bounds, and +/// topmost state before applying fullscreen mode. Call +/// again to restore the saved state. + /// + /// + private bool EnterFullScreen() + { + if (_isFullScreen || _initializing || DesignMode) + { + return _isFullScreen; + } + + Form? targetForm = ResolveTargetForm(); + if (targetForm is null) + { + return false; + } + + _savedBorderStyle = targetForm.FormBorderStyle; + _savedWindowState = targetForm.WindowState; + _savedBounds = targetForm.WindowState == FormWindowState.Normal + ? targetForm.Bounds + : targetForm.RestoreBounds; + _savedTopMost = targetForm.TopMost; + + ApplyFullScreen(targetForm); + SetFullScreenState(true); + _pendingFullScreen = false; + RestartMousePointerAutoHideTimer(); + return true; + } + + /// + /// Restores the form state that was saved when fullscreen mode was + /// entered. + /// + private void ExitFullScreen() + { + if (!_isFullScreen || _targetForm is null) + { + return; + } + + StopMousePointerAutoHideTimer(); + ShowMousePointerIfHidden(); + + _targetForm.FormBorderStyle = _savedBorderStyle; + _targetForm.TopMost = _savedTopMost; + _targetForm.WindowState = FormWindowState.Normal; + _targetForm.Bounds = _savedBounds; + _targetForm.WindowState = _savedWindowState; + + SetFullScreenState(false); + } + + /// + /// Updates the fullscreen state and raises + /// when the value changes. + /// + /// The new fullscreen state. + private void SetFullScreenState(bool value) + { + if (_isFullScreen == value) + { + return; + } + + _isFullScreen = value; + OnFullScreenChanged(EventArgs.Empty); + } + + /// + /// Toggles the resolved form between fullscreen and restored mode. + /// + /// + /// + /// Use to determine the current state before + /// calling this method when the application needs an explicit target + /// state. + /// + /// + public void ToggleFullScreen() + => FullScreen = !FullScreen; + + void ISupportInitialize.BeginInit() + { + _initializing = true; + } + + void ISupportInitialize.EndInit() + { + _initializing = false; + + AttachToForm(); + UpdatePowerRequest(); + + if (_pendingFullScreen) + { + FullScreen = true; + } + } + + /// + protected override void Dispose(bool disposing) + { + if (disposing) + { + _pendingFullScreen = false; + ExitFullScreen(); + DetachFromForm(); + _mousePointerAutoHideTimer?.Dispose(); + _mousePointerAutoHideTimer = null; + ReleasePowerRequest(); + } + + base.Dispose(disposing); + } + + private void SetContainerControl(ContainerControl? value, bool isExplicitAssignment) + { + if (_containerControl == value) + { + if (isExplicitAssignment) + { + _containerControlExplicitlySet = true; + } + + return; + } + + if (_isFullScreen) + { + _pendingFullScreen = false; + ExitFullScreen(); + } + + if (value is null) + { + _pendingFullScreen = false; + } + + DetachFromForm(); + _containerControl = value; + if (isExplicitAssignment) + { + _containerControlExplicitlySet = true; + } + + AttachToForm(); + OnContainerControlChanged(EventArgs.Empty); + } + + private void AttachToForm() + { + if (_initializing || DesignMode || _containerControl is null) + { + return; + } + + UpdateParentChangedSubscriptions(); + EnsureMessageMonitoring(); + ResolveTargetForm(); + if (_pendingFullScreen && _targetForm is not null) + { + EnterFullScreen(); + } + } + + private void DetachFromForm() + { + ClearParentChangedSubscriptions(); + + if (_messageFilter is not null) + { + Application.RemoveMessageFilter(_messageFilter); + _messageFilter = null; + } + + _formObserver?.Detach(); + StopMousePointerAutoHideTimer(); + ShowMousePointerIfHidden(); + _targetForm = null; + } + + private void OnContainerControlParentChanged(object? sender, EventArgs e) + { + UpdateParentChangedSubscriptions(); + + Form? newTarget = _containerControl as Form ?? _containerControl?.FindForm(); + if (ReferenceEquals(_targetForm, newTarget)) + { + return; + } + + bool restoreFullScreen = _pendingFullScreen || _isFullScreen; + + if (_isFullScreen) + { + ExitFullScreen(); + } + + ResolveTargetForm(); + + if (restoreFullScreen) + { + FullScreen = true; + } + } + + private void UpdateParentChangedSubscriptions() + { + ClearParentChangedSubscriptions(); + + for (Control? current = _containerControl; current is not null; current = current.Parent) + { + current.ParentChanged += OnContainerControlParentChanged; + _parentChain.Add(current); + } + } + + private void ClearParentChangedSubscriptions() + { + foreach (Control control in _parentChain) + { + control.ParentChanged -= OnContainerControlParentChanged; + } + + _parentChain.Clear(); + } + + private Form? ResolveTargetForm() + { + // ContainerControl intentionally supports both Form and UserControl + // design-time placement. FindForm handles the UserControl case. + Form? targetForm = _containerControl as Form ?? _containerControl?.FindForm(); + if (!ReferenceEquals(_targetForm, targetForm)) + { + _targetForm = targetForm; + UpdateFormObserver(); + } + + return _targetForm; + } + + private void EnsureMessageMonitoring() + { + // IMessageFilter observes input for child controls without changing + // Form.KeyPreview or installing global keyboard/mouse hooks. + if (_messageFilter is null) + { + _messageFilter = new KioskModeMessageFilter(this); + Application.AddMessageFilter(_messageFilter); + } + } + + private void ApplyFullScreen(Form form) + { + // Apply fullscreen from a normal state so bounds and border changes do + // not operate on Windows' maximized window rectangle. + form.WindowState = FormWindowState.Normal; + form.FormBorderStyle = FormBorderStyle.None; + + form.TopMost = _topMostInFullScreen; + + if (_hideTaskbar) + { + Screen screen = Screen.FromControl(form); + form.Bounds = screen.Bounds; + } + else + { + form.WindowState = FormWindowState.Maximized; + } + } + + private void UpdateFormObserver() + { + if (DesignMode) + { + return; + } + + _formObserver ??= new KioskModeFormObserver(this); + _formObserver.Attach(_targetForm); + } + + private void UpdatePowerRequest() + { + if (_initializing || DesignMode) + { + return; + } + + if (_suppressPowerSaving) + { + CreatePowerRequest(); + } + else + { + ReleasePowerRequest(); + } + } + + private void ProcessPowerBroadcast(WPARAM powerEvent) + { + if ((nint)powerEvent.Value == PbtApmResumeAutomatic) + { + RaiseWakeup(KioskModeWakeupSource.PowerResume); + } + } + + private void ProcessSessionChange(WPARAM sessionEvent) + { + nint value = (nint)sessionEvent.Value; + if (value is WtsConsoleConnect or WtsRemoteConnect or WtsSessionLogon or WtsSessionUnlock) + { + RaiseWakeup(KioskModeWakeupSource.Session); + } + } + + private void RestartMousePointerAutoHideTimer() + { + // Use a WinForms timer so cursor changes happen on the UI thread that + // receives the input messages restarting this timer. + if (!_isFullScreen || _mousePointerAutoHideDelay == 0) + { + return; + } + + _mousePointerAutoHideTimer ??= new Timer(); + _mousePointerAutoHideTimer.Stop(); + _mousePointerAutoHideTimer.Interval = _mousePointerAutoHideDelay; + _mousePointerAutoHideTimer.Tick -= OnMousePointerAutoHideTimerTick; + _mousePointerAutoHideTimer.Tick += OnMousePointerAutoHideTimerTick; + _mousePointerAutoHideTimer.Start(); + } + + private void StopMousePointerAutoHideTimer() + { + _mousePointerAutoHideTimer?.Stop(); + } + + private void OnMousePointerAutoHideTimerTick(object? sender, EventArgs e) + { + StopMousePointerAutoHideTimer(); + + if (!_isCursorHidden) + { + Cursor.Hide(); + _isCursorHidden = true; + } + } + + private void ProcessKeyboardActivity(Keys keyData, Keys modifiers, bool isRepeat) + { + // Any keyboard input wakes the kiosk experience, even when it does not + // match one of the configured fullscreen control keys. + RaiseWakeup(KioskModeWakeupSource.Keyboard); + + if (isRepeat) + { + return; + } + + Keys keyCode = keyData & Keys.KeyCode; + + if (keyCode == _toggleFullScreenKey && modifiers == Keys.None) + { + ToggleFullScreen(); + } + else if (keyCode == Keys.Escape && modifiers == Keys.None + && _escapeExitsFullScreen && _isFullScreen) + { + ExitFullScreen(); + } + } + + private void ProcessMouseActivity() + { + ShowMousePointerIfHidden(); + RestartMousePointerAutoHideTimer(); + RaiseWakeup(KioskModeWakeupSource.Mouse); + } + + private void ShowMousePointerIfHidden() + { + // Cursor.Hide and Cursor.Show are counter-based. Only balance hides + // performed by this component. + if (!_isCursorHidden) + { + return; + } + + Cursor.Show(); + _isCursorHidden = false; + } + + private void RaiseWakeup(KioskModeWakeupSource source) + => OnWakeup(new KioskModeWakeupEventArgs(source)); + + private unsafe void CreatePowerRequest() + { + // PowerClearRequest clears the active request, but only CloseHandle + // releases the native HANDLE returned by PowerCreateRequest. + if (!_powerRequestHandle.IsNull) + { + return; + } + + REASON_CONTEXT context = new() + { + Version = PowerRequestContextVersion, + Flags = PowerRequestContextSimpleString, + }; + + nint reasonString = Marshal.StringToHGlobalUni(PowerRequestReason); + + try + { + context.Reason.SimpleReasonString = (PWSTR)(char*)reasonString; + _powerRequestHandle = PInvoke.PowerCreateRequest(in context); + } + finally + { + Marshal.FreeHGlobal(reasonString); + } + + if (_powerRequestHandle.IsNull) + { + throw new Win32Exception(); + } + + try + { + if (!PInvoke.PowerSetRequest(_powerRequestHandle, POWER_REQUEST_TYPE.PowerRequestDisplayRequired) + || !PInvoke.PowerSetRequest(_powerRequestHandle, POWER_REQUEST_TYPE.PowerRequestSystemRequired)) + { + throw new Win32Exception(); + } + } + catch + { + ReleasePowerRequest(); + throw; + } + } + + private void ReleasePowerRequest() + { + if (_powerRequestHandle.IsNull) + { + return; + } + + PInvoke.PowerClearRequest(_powerRequestHandle, POWER_REQUEST_TYPE.PowerRequestDisplayRequired); + PInvoke.PowerClearRequest(_powerRequestHandle, POWER_REQUEST_TYPE.PowerRequestSystemRequired); + PInvoke.CloseHandle(_powerRequestHandle); + _powerRequestHandle = default; + } + + private bool IsMessageFromMonitoredControl(HWND hwnd) + { + if (_targetForm is not null + && !_targetForm.IsDisposed + && (hwnd == _targetForm.HWND || PInvoke.IsChild(_targetForm, hwnd))) + { + return true; + } + + return _containerControl is not null + && !_containerControl.IsDisposed + && _containerControl.IsHandleCreated + && (hwnd == _containerControl.HWND || PInvoke.IsChild(_containerControl, hwnd)); + } + + private bool ProcessMessage(Message message) + { + if (_containerControl is null || DesignMode) + { + return false; + } + + if (_targetForm is null || _targetForm.IsDisposed) + { + ResolveTargetForm(); + if (_pendingFullScreen && _targetForm is not null) + { + EnterFullScreen(); + } + } + + if (!IsMessageFromMonitoredControl(message.HWND)) + { + return false; + } + + if (message.MsgInternal == PInvokeCore.WM_KEYDOWN + || message.MsgInternal == PInvokeCore.WM_SYSKEYDOWN) + { + bool isRepeat = ((nuint)(nint)message.LParamInternal & (1u << 30)) != 0; + ProcessKeyboardActivity( + (Keys)(nint)message.WParamInternal, + Control.ModifierKeys & Keys.Modifiers, + isRepeat); + } + else if (message.MsgInternal >= PInvokeCore.WM_MOUSEFIRST + && message.MsgInternal <= PInvokeCore.WM_MOUSELAST) + { + ProcessMouseActivity(); + } + + return false; + } + + /// + /// Observes keyboard and mouse messages that belong to the target form. + /// + private sealed class KioskModeMessageFilter : IMessageFilter + { + private readonly KioskModeManager _owner; + + public KioskModeMessageFilter(KioskModeManager owner) + { + _owner = owner; + } + + public bool PreFilterMessage(ref Message m) + => _owner.ProcessMessage(m); + } + + /// + /// Observes power and session messages that belong to the target form. + /// + private sealed class KioskModeFormObserver : NativeWindow + { + private readonly KioskModeManager _owner; + private Form? _form; + private bool _sessionNotificationsRegistered; + + public KioskModeFormObserver(KioskModeManager owner) + { + _owner = owner; + } + + public void Attach(Form? form) + { + if (ReferenceEquals(_form, form)) + { + return; + } + + Detach(); + + if (form is null || form.IsDisposed) + { + return; + } + + _form = form; + form.HandleCreated += OnFormHandleCreated; + form.HandleDestroyed += OnFormHandleDestroyed; + + if (form.IsHandleCreated) + { + OnFormHandleCreated(form, EventArgs.Empty); + } + } + + public void Detach() + { + if (_form is not null) + { + _form.HandleCreated -= OnFormHandleCreated; + _form.HandleDestroyed -= OnFormHandleDestroyed; + _form = null; + } + + UnregisterSessionNotifications(); + if (!HWND.IsNull) + { + ReleaseHandle(); + } + } + + private void OnFormHandleCreated(object? sender, EventArgs e) + { + if (_form is null || _form.IsDisposed || !_form.IsHandleCreated) + { + return; + } + + AssignHandle(_form.HWND); + RegisterSessionNotifications(); + } + + private void OnFormHandleDestroyed(object? sender, EventArgs e) + { + UnregisterSessionNotifications(); + if (!HWND.IsNull) + { + ReleaseHandle(); + } + } + + private void RegisterSessionNotifications() + { + if (_sessionNotificationsRegistered || HWND.IsNull) + { + return; + } + + _sessionNotificationsRegistered = PInvoke.WTSRegisterSessionNotification(HWND, NotifyForThisSession); + } + + private void UnregisterSessionNotifications() + { + if (!_sessionNotificationsRegistered) + { + return; + } + + PInvoke.WTSUnRegisterSessionNotification(HWND); + _sessionNotificationsRegistered = false; + } + + protected override void WndProc(ref Message m) + { + if (m.MsgInternal == PInvokeCore.WM_POWERBROADCAST) + { + _owner.ProcessPowerBroadcast(m.WParamInternal); + } + else if (m.MsgInternal == PInvokeCore.WM_WTSSESSION_CHANGE) + { + _owner.ProcessSessionChange(m.WParamInternal); + } + + base.WndProc(ref m); + } + } +} +#endif diff --git a/src/System.Windows.Forms/System/Windows/Forms/Components/KioskModeManager/KioskModeWakeupEventArgs.cs b/src/System.Windows.Forms/System/Windows/Forms/Components/KioskModeManager/KioskModeWakeupEventArgs.cs new file mode 100644 index 00000000000..1ad14d4fcc2 --- /dev/null +++ b/src/System.Windows.Forms/System/Windows/Forms/Components/KioskModeManager/KioskModeWakeupEventArgs.cs @@ -0,0 +1,36 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. + +namespace System.Windows.Forms; + +#if NET11_0_OR_GREATER +/// +/// Provides data for the event. +/// +/// +/// +/// The property identifies the kind of activity the +/// component observed. Applications can use this value to distinguish direct +/// user activity, such as mouse or keyboard input, from system activity such +/// as power resume or session unlock. +/// +/// +public class KioskModeWakeupEventArgs : EventArgs +{ + /// + /// Initializes a new instance of the + /// class. + /// + /// The source of the wakeup notification. + public KioskModeWakeupEventArgs(KioskModeWakeupSource source) + { + SourceGenerated.EnumValidator.Validate(source, nameof(source)); + Source = source; + } + + /// + /// Gets the source of the wakeup notification. + /// + public KioskModeWakeupSource Source { get; } +} +#endif diff --git a/src/System.Windows.Forms/System/Windows/Forms/Components/KioskModeManager/KioskModeWakeupEventHandler.cs b/src/System.Windows.Forms/System/Windows/Forms/Components/KioskModeManager/KioskModeWakeupEventHandler.cs new file mode 100644 index 00000000000..b2784bb3b3c --- /dev/null +++ b/src/System.Windows.Forms/System/Windows/Forms/Components/KioskModeManager/KioskModeWakeupEventHandler.cs @@ -0,0 +1,14 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. + +namespace System.Windows.Forms; + +#if NET11_0_OR_GREATER +/// +/// Represents the method that will handle the +/// event. +/// +/// The source of the event. +/// A that contains the event data. +public delegate void KioskModeWakeupEventHandler(object? sender, KioskModeWakeupEventArgs e); +#endif diff --git a/src/System.Windows.Forms/System/Windows/Forms/Components/KioskModeManager/KioskModeWakeupSource.cs b/src/System.Windows.Forms/System/Windows/Forms/Components/KioskModeManager/KioskModeWakeupSource.cs new file mode 100644 index 00000000000..6c9a05784c4 --- /dev/null +++ b/src/System.Windows.Forms/System/Windows/Forms/Components/KioskModeManager/KioskModeWakeupSource.cs @@ -0,0 +1,35 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. + +namespace System.Windows.Forms; + +#if NET11_0_OR_GREATER +/// +/// Specifies the source of a +/// notification. +/// +public enum KioskModeWakeupSource +{ + /// + /// The wakeup notification was caused by keyboard activity. + /// + Keyboard = 0, + + /// + /// The wakeup notification was caused by mouse activity. + /// + Mouse = 1, + + /// + /// The wakeup notification was caused by the system resuming from a low + /// power state. + /// + PowerResume = 2, + + /// + /// The wakeup notification was caused by a Windows session activity, such + /// as logon or unlock. + /// + Session = 3, +} +#endif diff --git a/src/System.Windows.Forms/System/Windows/Forms/Timer.cs b/src/System.Windows.Forms/System/Windows/Forms/Components/Timer.cs similarity index 100% rename from src/System.Windows.Forms/System/Windows/Forms/Timer.cs rename to src/System.Windows.Forms/System/Windows/Forms/Components/Timer.cs diff --git a/src/System.Windows.Forms/System/Windows/Forms/Control.SuspendMutation.cs b/src/System.Windows.Forms/System/Windows/Forms/Control.SuspendMutation.cs new file mode 100644 index 00000000000..dad7898f68c --- /dev/null +++ b/src/System.Windows.Forms/System/Windows/Forms/Control.SuspendMutation.cs @@ -0,0 +1,129 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. + +namespace System.Windows.Forms; + +#if NET11_0_OR_GREATER +public unsafe partial class Control : + ISupportSuspendPainting +#else +public unsafe partial class Control +#endif +{ +#if NET11_0_OR_GREATER + /// + /// Begins a painting suspension region for this control. + /// + void ISupportSuspendPainting.BeginSuspendPainting() => BeginSuspendPaintingCore(); + + /// + /// Ends a painting suspension region for this control. + /// + void ISupportSuspendPainting.EndSuspendPainting() + { + EndSuspendPaintingCore(); + CompleteRecursiveInvalidateAfterSuspendPainting(); + } + + /// + /// When overridden in a derived class, begins a painting suspension region for this control. + /// + /// + /// + /// The default implementation suppresses native painting through . + /// Controls that already have a public update mechanism (such as ) + /// should override this method to route through that existing mechanism instead of introducing a + /// second, competing suspension path. + /// + /// + protected virtual void BeginSuspendPaintingCore() + { + BeginSuspendPaintingScope(); + BeginUpdateInternal(); + } + + /// + /// When overridden in a derived class, ends a painting suspension region for this control. + /// + protected virtual void EndSuspendPaintingCore() + { + if (EndSuspendPaintingScope()) + { + EndUpdateInternal(invalidate: !IsRecursiveInvalidateAfterSuspendPaintingRequested); + } + } + + internal bool IsRecursiveInvalidateAfterSuspendPaintingRequested + => Properties.ContainsKey(s_recursiveInvalidateAfterSuspendPaintingProperty); + + internal int SuspendPaintingCount + => Properties.GetValueOrDefault(s_suspendPaintingCountProperty, 0); + + internal void RequestRecursiveInvalidateAfterSuspendPainting() + => Properties.AddValue(s_recursiveInvalidateAfterSuspendPaintingProperty, true); + + internal bool BeginSuspendPaintingScope() + { + int suspendPaintingCount = Properties.GetValueOrDefault( + key: s_suspendPaintingCountProperty, + defaultValue: 0); + + Properties.AddOrRemoveValue( + key: s_suspendPaintingCountProperty, + value: suspendPaintingCount + 1, + defaultValue: 0); + + return true; + } + + internal bool EndSuspendPaintingScope() + { + int suspendPaintingCount = Properties.GetValueOrDefault( + key: s_suspendPaintingCountProperty, + defaultValue: 0); + + if (suspendPaintingCount == 0) + { + return false; + } + + Properties.AddOrRemoveValue( + s_suspendPaintingCountProperty, + suspendPaintingCount - 1, + defaultValue: 0); + + return true; + } + + internal void ResumeLayoutAfterSuspendPainting() + { + byte layoutSuspendCount = LayoutSuspendCount; + + try + { + ResumeLayout(); + } + catch + { + if (LayoutSuspendCount == layoutSuspendCount && LayoutSuspendCount > 0) + { + LayoutSuspendCount--; + } + + throw; + } + } + + private void CompleteRecursiveInvalidateAfterSuspendPainting() + { + if (Properties.GetValueOrDefault(s_suspendPaintingCountProperty, 0) != 0 + || !Properties.ContainsKey(s_recursiveInvalidateAfterSuspendPaintingProperty)) + { + return; + } + + Properties.RemoveValue(s_recursiveInvalidateAfterSuspendPaintingProperty); + Invalidate(invalidateChildren: true); + } +#endif +} diff --git a/src/System.Windows.Forms/System/Windows/Forms/Control.cs b/src/System.Windows.Forms/System/Windows/Forms/Control.cs index 14efc5014ff..b7e3533f08c 100644 --- a/src/System.Windows.Forms/System/Windows/Forms/Control.cs +++ b/src/System.Windows.Forms/System/Windows/Forms/Control.cs @@ -146,6 +146,7 @@ public unsafe partial class Control : private protected static readonly object s_paddingChangedEvent = new(); private static readonly object s_previewKeyDownEvent = new(); private static readonly object s_dataContextEvent = new(); + private static readonly object s_visualStylesModeChangedEvent = new(); private static MessageId s_threadCallbackMessage; private static ContextCallback? s_invokeMarshaledCallbackHelperDelegate; @@ -224,9 +225,15 @@ public unsafe partial class Control : private static readonly int s_cacheTextFieldProperty = PropertyStore.CreateKey(); private static readonly int s_ambientPropertiesServiceProperty = PropertyStore.CreateKey(); private static readonly int s_dataContextProperty = PropertyStore.CreateKey(); + private static readonly int s_visualStylesModeProperty = PropertyStore.CreateKey(); private static readonly int s_deviceDpiInternal = PropertyStore.CreateKey(); private static readonly int s_originalDeviceDpiInternal = PropertyStore.CreateKey(); +#if NET11_0_OR_GREATER + private static readonly int s_recursiveInvalidateAfterSuspendPaintingProperty = PropertyStore.CreateKey(); + private static readonly int s_suspendPaintingCountProperty = PropertyStore.CreateKey(); +#endif + private static readonly int s_updateCountProperty = PropertyStore.CreateKey(); private static bool s_needToLoadComCtl = true; @@ -268,7 +275,6 @@ public unsafe partial class Control : // bits 0-4: BoundsSpecified stored in RequiredScaling property. Bit 5: RequiredScalingEnabled property. private byte _requiredScaling; private TRACKMOUSEEVENT _trackMouseEvent; - private short _updateCount; private LayoutEventArgs? _cachedLayoutEventArgs; private Queue? _threadCallbackList; @@ -824,6 +830,18 @@ internal HBRUSH BackColorBrush [SRCategory(nameof(SR.CatData))] [Browsable(false)] [Bindable(true)] + // Note: unlike Font/Cursor (which use [AmbientValue(null)]) or RightToLeft (which uses + // [AmbientValue(RightToLeft.Inherit)]), DataContext intentionally has NO [AmbientValue]. + // [AmbientValue] only matters for the designer's CodeDOM serializer: when it is forced to emit an + // otherwise-ambient property (inherited/"difference" forms, member relationships, absolute + // serialization), it writes the AmbientValue as the "reset to ambient" sentinel. That only works + // when the setter treats that sentinel as "clear the local override." Font does (its setter uses + // AddOrRemoveValue, so Font = null re-inherits); RightToLeft does (the getter resolves Inherit to + // the parent). DataContext's setter instead stores an explicit null when the parent value differs + // (see below), so null would SUPPRESS inheritance rather than restore it - making [AmbientValue(null)] + // a leaky, incorrect sentinel. Combined with this property being [Browsable(false)], runtime-oriented, + // and typically holding a non-CodeDOM-serializable object, the forced-serialization "bake-in" is a + // non-issue, so we deliberately leave it off rather than introduce a setter behavior change. public virtual object? DataContext { get => Properties.TryGetValue(s_dataContextProperty, out object? value) @@ -856,6 +874,122 @@ private bool ShouldSerializeDataContext() private void ResetDataContext() => Properties.RemoveValue(s_dataContextProperty); + /// + /// Gets or sets how the control renders itself when visual styles are applied. This is an ambient property. + /// + /// + /// The for the control. When not explicitly set, the value is inherited + /// from the parent control, or, for a top-level control, from . + /// + /// + /// + /// As an ambient property, a control that does not have its set explicitly + /// inherits the value from its parent, or, if it has no parent, from + /// . Derived controls can override + /// to pin themselves to a specific renderer version for backward + /// compatibility (see for an example). + /// + /// + [SRCategory(nameof(SR.CatAppearance))] + [EditorBrowsable(EditorBrowsableState.Always)] + [AmbientValue(VisualStylesMode.Inherit)] + [SRDescription(nameof(SR.ControlVisualStylesModeDescr))] + public virtual VisualStylesMode VisualStylesMode + { + get + { + if (!Properties.TryGetValue(s_visualStylesModeProperty, out VisualStylesMode value) + || value == VisualStylesMode.Inherit) + { + value = ParentInternal?.VisualStylesMode ?? DefaultVisualStylesMode; + } + + return value; + } + set + { + // Can't use the source generated enum validator here, since it cannot deal with the + // non-contiguous Inherit (-1) and Latest (short.MaxValue) members. + _ = value switch + { + VisualStylesMode.Inherit => value, + VisualStylesMode.Classic => value, + VisualStylesMode.Disabled => value, + VisualStylesMode.Net11 => value, + VisualStylesMode.Latest => value, + _ => throw new InvalidEnumArgumentException(nameof(value), (int)value, typeof(VisualStylesMode)) + }; + + VisualStylesMode oldValue = VisualStylesMode; + + // Inherit was requested explicitly, or the requested value matches the ambient (parent) value: + // drop any local override so the value is inherited again. + if (value == VisualStylesMode.Inherit + || (ParentInternal is { } parent && parent.VisualStylesMode == value)) + { + Properties.RemoveValue(s_visualStylesModeProperty); + } + else + { + Properties.AddValue(s_visualStylesModeProperty, value); + } + + if (oldValue != VisualStylesMode) + { + OnVisualStylesModeChanged(EventArgs.Empty); + } + } + } + + private bool ShouldSerializeVisualStylesMode() + => Properties.ContainsKey(s_visualStylesModeProperty); + + private void ResetVisualStylesMode() + => VisualStylesMode = VisualStylesMode.Inherit; + + /// + /// Gets the that controls should actually honor when deciding + /// or paint behavior, after applying the Windows High Contrast clamp. + /// + /// + /// when visual styles are explicitly disabled; + /// when Windows High Contrast is active (so custom non-client + /// painting, which does not honor the High Contrast palette, is bypassed); otherwise the raw + /// value. + /// + /// + /// + /// In-box controls MUST read this property, not the raw , whenever they + /// decide their or paint behavior. The raw stays + /// pure so that before/after comparisons across a High Contrast transition remain meaningful; the High + /// Contrast clamp lives here instead. + /// + /// + /// Windows High Contrast can be toggled while the application is running, so this value is not stable + /// across such a transition and must not be cached. Affected controls rebuild their handles on the + /// transition (see ), which re-reads this value. + /// + /// + private protected VisualStylesMode EffectiveVisualStylesMode + => VisualStylesMode is VisualStylesMode.Disabled + ? VisualStylesMode.Disabled + : SystemInformation.HighContrast + ? VisualStylesMode.Classic + : VisualStylesMode; + + /// + /// Gets the default for the control, which is ambient to + /// . + /// + /// The default visual styles mode for the control. + /// + /// + /// Derived controls can override this property to pin themselves to a specific renderer version when their + /// rendering or layout depends on it, independent of the application-wide default. + /// + /// + protected virtual VisualStylesMode DefaultVisualStylesMode => Application.DefaultVisualStylesMode; + /// /// The background color of this control. This is an ambient property and /// will always return a non-null value. @@ -3739,6 +3873,19 @@ public event EventHandler? DataContextChanged remove => Events.RemoveHandler(s_dataContextEvent, value); } + /// + /// Occurs when the value of the property changes. + /// + [SRCategory(nameof(SR.CatAppearance))] + [Browsable(true)] + [EditorBrowsable(EditorBrowsableState.Advanced)] + [SRDescription(nameof(SR.ControlVisualStylesModeChangedDescr))] + public event EventHandler? VisualStylesModeChanged + { + add => Events.AddHandler(s_visualStylesModeChangedEvent, value); + remove => Events.RemoveHandler(s_visualStylesModeChangedEvent, value); + } + [SRCategory(nameof(SR.CatDragDrop))] [SRDescription(nameof(SR.ControlOnDragDropDescr))] public event DragEventHandler? DragDrop @@ -4246,6 +4393,7 @@ internal virtual void AssignParent(Control? value) RightToLeft oldRtl = RightToLeft; bool oldEnabled = Enabled; bool oldVisible = Visible; + VisualStylesMode oldVisualStylesMode = VisualStylesMode; // Update the parent _parent = value; @@ -4291,6 +4439,11 @@ internal virtual void AssignParent(Control? value) OnRightToLeftChanged(EventArgs.Empty); } + if (oldVisualStylesMode != VisualStylesMode) + { + OnVisualStylesModeChanged(EventArgs.Empty); + } + if (!Properties.ContainsKey(s_bindingManagerProperty) && Created) { // We do not want to call our parent's BindingContext property here. @@ -4374,17 +4527,16 @@ public IAsyncResult BeginInvoke(Delegate method, params object?[]? args) internal void BeginUpdateInternal() { - if (!IsHandleCreated) - { - return; - } - - if (_updateCount == 0) + int updateCount = Properties.GetValueOrDefault(s_updateCountProperty, 0); + if (updateCount == 0 && IsHandleCreated) { PInvokeCore.SendMessage(this, PInvokeCore.WM_SETREDRAW, (WPARAM)(BOOL)false); } - _updateCount++; + Properties.AddOrRemoveValue( + s_updateCountProperty, + updateCount + 1, + defaultValue: 0); } /// @@ -5070,13 +5222,21 @@ public void DrawToBitmap(Bitmap bitmap, Rectangle targetBounds) internal bool EndUpdateInternal(bool invalidate) { - if (_updateCount > 0) + int updateCount = Properties.GetValueOrDefault(s_updateCountProperty, 0); + + if (updateCount > 0) { - Debug.Assert(IsHandleCreated, "Handle should be created by now"); - _updateCount--; - if (_updateCount == 0) + updateCount--; + + Properties.AddOrRemoveValue( + s_updateCountProperty, + updateCount, + defaultValue: 0); + + if (updateCount == 0 && IsHandleCreated) { PInvokeCore.SendMessage(this, PInvokeCore.WM_SETREDRAW, (WPARAM)(BOOL)true); + if (invalidate) { Invalidate(); @@ -5349,7 +5509,8 @@ private static bool IsFocusManagingContainerControl(Control ctl) /// by calling "WM_SETREDRAW" even if the control in "Begin - End" update cycle. Using this Function we can guard /// against repetitively redrawing the control. /// - internal bool IsUpdating() => _updateCount > 0; + internal bool IsUpdating() + => Properties.GetValueOrDefault(s_updateCountProperty, 0) > 0; /// /// This is a helper method that is called by ScaleControl to retrieve the bounds @@ -6829,6 +6990,36 @@ protected virtual void OnDataContextChanged(EventArgs e) } } + /// + /// Raises the event. Inheriting classes should override this method + /// to handle the event, and call . + /// to forward the event to any registered listeners. + /// + /// An that contains the event data. + [EditorBrowsable(EditorBrowsableState.Advanced)] + protected virtual void OnVisualStylesModeChanged(EventArgs e) + { + if (GetAnyDisposingInHierarchy()) + { + return; + } + + Invalidate(); + + if (Events[s_visualStylesModeChangedEvent] is EventHandler eventHandler) + { + eventHandler(this, e); + } + + if (ChildControls is { } children) + { + for (int i = 0; i < children.Count; i++) + { + children[i].OnParentVisualStylesModeChanged(e); + } + } + } + [EditorBrowsable(EditorBrowsableState.Advanced)] protected virtual void OnDockChanged(EventArgs e) { @@ -7042,6 +7233,30 @@ protected virtual void OnParentDataContextChanged(EventArgs e) OnDataContextChanged(e); } + /// + /// Occurs when the property of the parent of this control changes. + /// + /// An that contains the event data. + [EditorBrowsable(EditorBrowsableState.Advanced)] + protected virtual void OnParentVisualStylesModeChanged(EventArgs e) + { + if (Properties.ContainsKey(s_visualStylesModeProperty)) + { + if (Properties.GetValueOrDefault(s_visualStylesModeProperty) == Parent?.VisualStylesMode) + { + // Same as the parent value, make it ambient again by removing it. + Properties.RemoveValue(s_visualStylesModeProperty); + } + + // A local value isolates this subtree from parent changes. If the local value matched the + // parent's new value, removing it preserves the effective value while making it ambient again. + return; + } + + // In every other case we're going to raise the event. + OnVisualStylesModeChanged(e); + } + [EditorBrowsable(EditorBrowsableState.Advanced)] protected virtual void OnParentEnabledChanged(EventArgs e) { @@ -7407,6 +7622,11 @@ protected virtual void OnHandleCreated(EventArgs e) pszSubAppName: $"{DarkModeIdentifier}_{ExplorerThemeIdentifier}", pszSubIdList: null); } + + if (IsUpdating()) + { + PInvokeCore.SendMessage(this, PInvokeCore.WM_SETREDRAW, (WPARAM)(BOOL)false); + } } ((EventHandler?)Events[s_handleCreatedEvent])?.Invoke(this, e); @@ -12651,6 +12871,7 @@ protected virtual void WndProc(ref Message m) break; + case PInvokeCore.WM_DWMCOLORIZATIONCOLORCHANGED: case PInvokeCore.WM_SYSCOLORCHANGE: if (GetExtendedState(ExtendedStates.InterestedInUserPreferenceChanged) && GetTopLevel()) { diff --git a/src/System.Windows.Forms/System/Windows/Forms/ControlMutationExtensions.cs b/src/System.Windows.Forms/System/Windows/Forms/ControlMutationExtensions.cs new file mode 100644 index 00000000000..963199a4782 --- /dev/null +++ b/src/System.Windows.Forms/System/Windows/Forms/ControlMutationExtensions.cs @@ -0,0 +1,57 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. + +namespace System.Windows.Forms; + +using System.ComponentModel; + +#if NET11_0_OR_GREATER +/// +/// Provides extension methods for batching synchronous WinForms UI mutations. +/// +public static class ControlMutationExtensions +{ + /// + /// Suspends painting for the specified target until the returned scope is disposed. + /// + /// The target whose painting should be suspended. + /// A scope that resumes painting when disposed. + public static SuspendPaintingScope SuspendPainting(this ISupportSuspendPainting target) + => new(target); + + /// + /// Suspends painting and the selected layout work until the returned scope is disposed. + /// + /// The target whose painting and layout should be suspended. + /// + /// A value that specifies which controls in the target's control tree should suspend layout. + /// + /// A scope that resumes layout and painting when disposed. + /// is . + /// + /// is not a valid value. + /// + /// is not a . + public static SuspendPaintingScope SuspendPainting( + this ISupportSuspendPainting target, + LayoutSuspendTraversal layoutSuspendTraversal) + => new(target, layoutSuspendTraversal); + + /// + /// Suspends painting and layout for selected controls until the returned scope is disposed. + /// + /// The target whose painting and selected layout should be suspended. + /// + /// A predicate that returns for each control whose layout should be suspended. + /// + /// A scope that resumes layout and painting when disposed. + /// + /// or is . + /// + /// is not a . + public static SuspendPaintingScope SuspendPainting( + this ISupportSuspendPainting target, + Func suspendLayoutContainerFilter) + => new(target, suspendLayoutContainerFilter); +} +#endif diff --git a/src/System.Windows.Forms/System/Windows/Forms/Controls/Buttons/Appearance.cs b/src/System.Windows.Forms/System/Windows/Forms/Controls/Buttons/Appearance.cs index fb144cac774..83aacaaf244 100644 --- a/src/System.Windows.Forms/System/Windows/Forms/Controls/Buttons/Appearance.cs +++ b/src/System.Windows.Forms/System/Windows/Forms/Controls/Buttons/Appearance.cs @@ -1,4 +1,4 @@ -// Licensed to the .NET Foundation under one or more agreements. +// Licensed to the .NET Foundation under one or more agreements. // The .NET Foundation licenses this file to you under the MIT license. namespace System.Windows.Forms; @@ -17,4 +17,15 @@ public enum Appearance /// The appearance of a Windows button. /// Button = 1, + + /// + /// The appearance of a modern UI toggle switch. + /// + /// + /// + /// This value has no effect when is set to + /// or . + /// + /// + ToggleSwitch = 2 } diff --git a/src/System.Windows.Forms/System/Windows/Forms/Controls/Buttons/Button.cs b/src/System.Windows.Forms/System/Windows/Forms/Controls/Buttons/Button.cs index e76dd755f29..6f514d31bfa 100644 --- a/src/System.Windows.Forms/System/Windows/Forms/Controls/Buttons/Button.cs +++ b/src/System.Windows.Forms/System/Windows/Forms/Controls/Buttons/Button.cs @@ -148,51 +148,23 @@ public virtual DialogResult DialogResult } } - /// - /// Defines, whether the control is owner-drawn. Based on this, - /// the UserPaint flags get set, which in turn makes it later - /// a Win32 controls, which we wrap (OwnerDraw == false) or not, and then - /// we draw ourselves. If the user wants to opt out of DarkMode, we can no - /// longer force (wrapping) System-Painting for FlatStyle.Standard, and we - /// need this then also here and now, before CreateParams is called. - /// - private protected override bool OwnerDraw - { - get - { - if (Application.IsDarkModeEnabled - - // The SystemRenderer cannot render images. So, we flip to our - // own DarkMode renderer, if we need to render images, except if... - && Image is null - // ...or a BackgroundImage, except if... - && BackgroundImage is null - // ...the user wants to opt out of implicit DarkMode rendering. - && DarkModeRequestState is true - - // And all of this only counts for FlatStyle.Standard. For the - // rest, we're using specific renderers anyway, which check - // themselves on demand, if they need to apply Light- or DarkMode. - && FlatStyle == FlatStyle.Standard) - { - return false; - } - - return base.OwnerDraw; - } - } - internal override bool SupportsUiaProviders => true; /// /// Raises the event. /// - protected override void OnMouseEnter(EventArgs e) => base.OnMouseEnter(e); + protected override void OnMouseEnter(EventArgs e) + { + base.OnMouseEnter(e); + } /// /// Raises the event. /// - protected override void OnMouseLeave(EventArgs e) => base.OnMouseLeave(e); + protected override void OnMouseLeave(EventArgs e) + { + base.OnMouseLeave(e); + } /// [Browsable(false)] diff --git a/src/System.Windows.Forms/System/Windows/Forms/Controls/Buttons/ButtonBase.cs b/src/System.Windows.Forms/System/Windows/Forms/Controls/Buttons/ButtonBase.cs index 26c3292e175..358d4953622 100644 --- a/src/System.Windows.Forms/System/Windows/Forms/Controls/Buttons/ButtonBase.cs +++ b/src/System.Windows.Forms/System/Windows/Forms/Controls/Buttons/ButtonBase.cs @@ -6,6 +6,7 @@ using System.Drawing.Design; using System.Windows.Forms.ButtonInternal; using System.Windows.Forms.Layout; +using System.Windows.Forms.Rendering.Button; using Windows.Win32.System.Variant; using Windows.Win32.UI.Accessibility; @@ -46,6 +47,8 @@ public abstract partial class ButtonBase : Control, ICommandBindingTargetProvide private ButtonBaseAdapter? _adapter; private FlatStyle _cachedAdapterType; + private ButtonBackColorAnimator? _backColorAnimator; + private AnimatedPopupButtonRenderer? _popupKeyCapRenderer; // Backing fields for the infrastructure to make ToolStripItem bindable and introduce (bindable) ICommand. private Input.ICommand? _command; @@ -838,6 +841,10 @@ protected override void Dispose(bool disposing) _imageList?.Disposed -= DetachImageList; _textToolTip?.Dispose(); _textToolTip = null; + _backColorAnimator?.Dispose(); + _backColorAnimator = null; + _popupKeyCapRenderer?.Dispose(); + _popupKeyCapRenderer = null; } base.Dispose(disposing); @@ -1067,6 +1074,29 @@ internal virtual ButtonBaseAdapter CreateStandardAdapter() return null; } + internal ButtonBackColorAnimator BackColorAnimator + => _backColorAnimator ??= new(this); + + private AnimatedPopupButtonRenderer PopupKeyCapRenderer + => _popupKeyCapRenderer ??= new(this); + + private bool IsPopupKeyCapAppearance + => FlatStyle == FlatStyle.Popup + && (Application.IsDarkModeEnabled || EffectiveVisualStylesMode >= VisualStylesMode.Net11) + && this is Button + or CheckBox { Appearance: Appearance.Button } + or RadioButton { Appearance: Appearance.Button }; + + private bool IsPopupKeyCapSelected + => this is CheckBox { Checked: true } + or RadioButton { Checked: true }; + + private protected void ResetAdapter() + { + _adapter = null; + _cachedAdapterType = (FlatStyle)(-1); + } + internal virtual StringFormat CreateStringFormat() { if (Adapter is null) @@ -1250,12 +1280,70 @@ protected override void OnPaint(PaintEventArgs pevent) Animate(); ImageAnimator.UpdateFrames(Image); - PaintControl(pevent); + if (IsPopupKeyCapAppearance) + { + PopupKeyCapRenderer.SetInteractionState( + hovered: MouseIsOver, + pressed: MouseIsDown, + selected: IsPopupKeyCapSelected); + + using GraphicsStateScope scope = new(pevent.Graphics); + PopupKeyCapRenderer.RenderControl(pevent.Graphics); + } + else + { + PaintControl(pevent); + } } base.OnPaint(pevent); } + /// + protected override void OnVisualStylesModeChanged(EventArgs e) + { + using (LayoutTransaction.CreateTransactionIf( + AutoSize, + ParentInternal, + this, + PropertyNames.VisualStylesMode)) + { + base.OnVisualStylesModeChanged(e); + + // Renderer padding can change with VisualStylesMode, so recreate the adapter before layout + // recomputes the preferred size. + ResetAdapter(); + + _backColorAnimator?.Dispose(); + _backColorAnimator = null; + _popupKeyCapRenderer?.Dispose(); + _popupKeyCapRenderer = null; + + if (IsHandleCreated) + { + Invalidate(); + } + } + } + + /// + protected override void OnSystemColorsChanged(EventArgs e) + { + ResetAdapter(); + _backColorAnimator?.Dispose(); + _backColorAnimator = null; + _popupKeyCapRenderer?.Dispose(); + _popupKeyCapRenderer = null; + base.OnSystemColorsChanged(e); + } + + /// + /// Exposes the (otherwise private protected) + /// to the owner-drawn button adapters in the ButtonInternal namespace, so that renderer selection + /// honors the Windows High Contrast clamp just like the control's own paint and . + /// + internal VisualStylesMode EffectiveVisualStylesModeInternal => EffectiveVisualStylesMode; + protected override void OnParentChanged(EventArgs e) { base.OnParentChanged(e); diff --git a/src/System.Windows.Forms/System/Windows/Forms/Controls/Buttons/ButtonInternal/ButtonBaseAdapter.LayoutOptions.cs b/src/System.Windows.Forms/System/Windows/Forms/Controls/Buttons/ButtonInternal/ButtonBaseAdapter.LayoutOptions.cs index d8c7b6b6a8d..fc275d1919d 100644 --- a/src/System.Windows.Forms/System/Windows/Forms/Controls/Buttons/ButtonInternal/ButtonBaseAdapter.LayoutOptions.cs +++ b/src/System.Windows.Forms/System/Windows/Forms/Controls/Buttons/ButtonInternal/ButtonBaseAdapter.LayoutOptions.cs @@ -17,6 +17,12 @@ internal partial class LayoutOptions private bool _disableWordWrapping; + internal bool DisableWordWrapping + { + get => _disableWordWrapping; + set => _disableWordWrapping = value; + } + // If this is changed to a property callers will need to be updated // as they modify fields in the Rectangle. public Rectangle Client; @@ -42,6 +48,8 @@ internal partial class LayoutOptions public bool LayoutRTL { get; set; } public bool VerticalText { get; set; } public bool UseCompatibleTextRendering { get; set; } + public bool ClipImagesToClient { get; set; } + public bool EnsureImagePreferredSizeInset { get; set; } /// /// .NET Framework 1.0/1.1 compatibility @@ -217,6 +225,7 @@ internal Size GetPreferredSizeCore(Size proposedSize) // will happen but the layout would not be adjusted to allow text wrapping. If someone has a // carriage return in the text we'll honor that for preferred size, but we won't wrap based // on constraints. + bool disableWordWrapping = _disableWordWrapping; try { _disableWordWrapping = true; @@ -224,12 +233,18 @@ internal Size GetPreferredSizeCore(Size proposedSize) } finally { - _disableWordWrapping = false; + _disableWordWrapping = disableWordWrapping; } } // Combine pieces to get final preferred size. Size requiredSize = Compose(checkSize, ImageSize, textSize); + if (EnsureImagePreferredSizeInset && ImageSize != Size.Empty) + { + Size imageRequiredSize = Compose(checkSize, requiredImageSize, Size.Empty); + requiredSize = LayoutUtils.UnionSizes(requiredSize, imageRequiredSize); + } + requiredSize += bordersAndPadding; return requiredSize; diff --git a/src/System.Windows.Forms/System/Windows/Forms/Controls/Buttons/ButtonInternal/ButtonBaseAdapter.cs b/src/System.Windows.Forms/System/Windows/Forms/Controls/Buttons/ButtonInternal/ButtonBaseAdapter.cs index 39b5b0ab852..042979aaf57 100644 --- a/src/System.Windows.Forms/System/Windows/Forms/Controls/Buttons/ButtonInternal/ButtonBaseAdapter.cs +++ b/src/System.Windows.Forms/System/Windows/Forms/Controls/Buttons/ButtonInternal/ButtonBaseAdapter.cs @@ -4,6 +4,7 @@ using System.Drawing; using System.Runtime.CompilerServices; using System.Windows.Forms.Layout; +using System.Windows.Forms.Rendering.Button; namespace System.Windows.Forms.ButtonInternal; @@ -59,8 +60,57 @@ internal virtual Size GetPreferredSizeCore(Size proposedSize) return options.GetPreferredSizeCore(proposedSize); } + protected static Size GetPopupPreferredSizeCore(LayoutOptions layout, Size proposedSize) + { + layout.GrowBorderBy1PxWhenDefault = false; + layout.MaxFocus = false; + layout.BorderSize = 0; + layout.PaddingSize = 1; + + return layout.GetPreferredSizeCore(proposedSize); + } + + protected static Size GetModernPopupPreferredSizeCore( + LayoutOptions layout, + Size proposedSize, + int deviceDpi, + int borderSize) + => GetModernPreferredSizeCore( + layout, + proposedSize, + PopupButtonKeyCapRenderer.GetPreferredSizeChrome(deviceDpi, borderSize)); + + protected static Size GetModernPreferredSizeCore( + LayoutOptions layout, + Size proposedSize, + Size chromeSize) + { + layout.GrowBorderBy1PxWhenDefault = false; + layout.MaxFocus = false; + layout.BorderSize = 0; + layout.PaddingSize = 0; + + Size contentConstraint = new( + proposedSize.Width > 0 ? Math.Max(1, proposedSize.Width - chromeSize.Width) : proposedSize.Width, + proposedSize.Height > 0 ? Math.Max(1, proposedSize.Height - chromeSize.Height) : proposedSize.Height); + + return layout.GetPreferredSizeCore(contentConstraint) + chromeSize; + } + protected abstract LayoutOptions Layout(PaintEventArgs e); + internal LayoutData GetLayoutData(Rectangle contentBounds) + { + LayoutOptions options = CommonLayout(); + options.Client = LayoutUtils.DeflateRect(contentBounds, Control.Padding); + options.GrowBorderBy1PxWhenDefault = false; + options.BorderSize = 0; + options.PaddingSize = 0; + options.MaxFocus = false; + + return options.Layout(); + } + internal abstract void PaintUp(PaintEventArgs e, CheckState state); internal abstract void PaintDown(PaintEventArgs e, CheckState state); @@ -379,18 +429,24 @@ internal virtual void DrawImageCore(Graphics graphics, Image image, Rectangle im if (!layout.Options.DotNetOneButtonCompat) { - Rectangle bounds = new( - ButtonBorderSize, - ButtonBorderSize, - Control.Width - (2 * ButtonBorderSize), - Control.Height - (2 * ButtonBorderSize)); + Rectangle bounds = layout.Options.ClipImagesToClient + ? layout.Client + : new Rectangle( + ButtonBorderSize, + ButtonBorderSize, + Control.Width - (2 * ButtonBorderSize), + Control.Height - (2 * ButtonBorderSize)); Region newClip = oldClip.Clone(); newClip.Intersect(bounds); - // If we don't do this, DrawImageUnscaled will happily draw the entire image, even though imageBounds - // is smaller than the image size. - newClip.Intersect(imageBounds); + // DrawImageUnscaled ignores a reduced destination size, so clip only when layout had to squeeze + // the image. Avoiding an unnecessary exact-edge clip preserves the outer image pixels. + if (imageBounds.Width < image.Width || imageBounds.Height < image.Height) + { + newClip.Intersect(imageBounds); + } + graphics.Clip = newClip; } else @@ -408,6 +464,10 @@ internal virtual void DrawImageCore(Graphics graphics, Image image, Rectangle im // Need to specify width and height ControlPaint.DrawImageDisabled(graphics, image, imageBounds, unscaledImage: true); } + else if (!layout.Options.DotNetOneButtonCompat) + { + graphics.DrawImageUnscaled(image, imageBounds.Location); + } else { graphics.DrawImage(image, imageBounds.X, imageBounds.Y, image.Width, image.Height); diff --git a/src/System.Windows.Forms/System/Windows/Forms/Controls/Buttons/ButtonInternal/ButtonPopupAdapter.cs b/src/System.Windows.Forms/System/Windows/Forms/Controls/Buttons/ButtonInternal/ButtonPopupAdapter.cs index 31fe458ea77..ac7aa638a75 100644 --- a/src/System.Windows.Forms/System/Windows/Forms/Controls/Buttons/ButtonInternal/ButtonPopupAdapter.cs +++ b/src/System.Windows.Forms/System/Windows/Forms/Controls/Buttons/ButtonInternal/ButtonPopupAdapter.cs @@ -145,6 +145,9 @@ protected override LayoutOptions Layout(PaintEventArgs e) return layout; } + internal override Size GetPreferredSizeCore(Size proposedSize) + => GetPopupPreferredSizeCore(PaintPopupLayout(up: false, 0), proposedSize); + internal static LayoutOptions PaintPopupLayout( bool up, int paintedBorder, diff --git a/src/System.Windows.Forms/System/Windows/Forms/Controls/Buttons/ButtonInternal/CheckBoxFlatAdapter.cs b/src/System.Windows.Forms/System/Windows/Forms/Controls/Buttons/ButtonInternal/CheckBoxFlatAdapter.cs index c14017ce380..441bca05a33 100644 --- a/src/System.Windows.Forms/System/Windows/Forms/Controls/Buttons/ButtonInternal/CheckBoxFlatAdapter.cs +++ b/src/System.Windows.Forms/System/Windows/Forms/Controls/Buttons/ButtonInternal/CheckBoxFlatAdapter.cs @@ -84,9 +84,7 @@ private void PaintFlatWorker(PaintEventArgs e, Color checkColor, Color checkBack PaintField(e, layout, colors, checkColor, drawFocus: true); } - private new ButtonFlatAdapter ButtonAdapter => (ButtonFlatAdapter)base.ButtonAdapter; - - protected override ButtonBaseAdapter CreateButtonAdapter() => new ButtonFlatAdapter(Control); + protected override ButtonBaseAdapter CreateButtonAdapter() => DarkModeAdapterFactory.CreateFlatAdapter(Control); protected override LayoutOptions Layout(PaintEventArgs e) { diff --git a/src/System.Windows.Forms/System/Windows/Forms/Controls/Buttons/ButtonInternal/CheckBoxModernAdapter.cs b/src/System.Windows.Forms/System/Windows/Forms/Controls/Buttons/ButtonInternal/CheckBoxModernAdapter.cs new file mode 100644 index 00000000000..d47ba67f8ce --- /dev/null +++ b/src/System.Windows.Forms/System/Windows/Forms/Controls/Buttons/ButtonInternal/CheckBoxModernAdapter.cs @@ -0,0 +1,109 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. + +using System.Drawing; + +namespace System.Windows.Forms.ButtonInternal; + +/// +/// Paints a normal-appearance with the modern animated glyph renderer. +/// +internal sealed class CheckBoxModernAdapter : CheckBoxBaseAdapter +{ + private readonly FlatStyle _flatStyle; + + internal CheckBoxModernAdapter(CheckBox control, FlatStyle flatStyle) : base(control) + { + _flatStyle = flatStyle; + } + + internal override void PaintUp(PaintEventArgs e, CheckState state) + { + if (Control.Appearance == Appearance.Button) + { + ButtonAdapter.PaintUp(e, Control.CheckState); + return; + } + + PaintCore(e); + } + + internal override void PaintDown(PaintEventArgs e, CheckState state) + { + if (Control.Appearance == Appearance.Button) + { + ButtonAdapter.PaintDown(e, Control.CheckState); + return; + } + + PaintCore(e); + } + + internal override void PaintOver(PaintEventArgs e, CheckState state) + { + if (Control.Appearance == Appearance.Button) + { + ButtonAdapter.PaintOver(e, Control.CheckState); + return; + } + + PaintCore(e); + } + + protected override ButtonBaseAdapter CreateButtonAdapter() + => _flatStyle switch + { + FlatStyle.Flat => DarkModeAdapterFactory.CreateFlatAdapter(Control), + FlatStyle.Popup => DarkModeAdapterFactory.CreatePopupAdapter(Control), + _ => DarkModeAdapterFactory.CreateStandardAdapter(Control) + }; + + protected override LayoutOptions Layout(PaintEventArgs e) + { + LayoutOptions layout = CommonLayout(); + layout.CheckPaddingSize = Control.LogicalToDeviceUnits(2); + layout.CheckSize = Math.Max( + Control.LogicalToDeviceUnits(13), + (int)(Control.Font.Height * 0.9f)); + + return layout; + } + + private void PaintCore(PaintEventArgs e) + { + Graphics graphics = e.GraphicsInternal; + graphics.Clear(Control.Parent?.BackColor ?? Control.BackColor); + + LayoutData layout = Layout(e).Layout(); + AdjustFocusRectangle(layout); + + Color? customOnColor = Control.ShouldSerializeBackColor() + ? Control.BackColor + : null; + + Color? customBorderColor = Control.FlatAppearance.BorderColor.IsEmpty + ? null + : Control.FlatAppearance.BorderColor; + + Control.CheckGlyphRenderer.NotifyCheckStateChanged(Control.CheckState); + Control.CheckGlyphRenderer.DrawGlyph( + graphics, + layout.CheckBounds, + _flatStyle, + Control.Enabled, + Control.MouseIsOver, + Control.Focused && Control.ShowFocusCues, + customOnColor, + customBorderColor); + + PaintImage(e, layout); + + Color textColor = Control.ShouldSerializeForeColor() + ? Control.ForeColor + : Application.IsDarkModeEnabled + ? Color.FromArgb(0xF0, 0xF0, 0xF0) + : SystemColors.WindowText; + + PaintField(e, layout, PaintRender(e).Calculate(), textColor, drawFocus: true); + } +} diff --git a/src/System.Windows.Forms/System/Windows/Forms/Controls/Buttons/ButtonInternal/CheckBoxPopupAdapter.cs b/src/System.Windows.Forms/System/Windows/Forms/Controls/Buttons/ButtonInternal/CheckBoxPopupAdapter.cs index c45c832d02c..0513250b873 100644 --- a/src/System.Windows.Forms/System/Windows/Forms/Controls/Buttons/ButtonInternal/CheckBoxPopupAdapter.cs +++ b/src/System.Windows.Forms/System/Windows/Forms/Controls/Buttons/ButtonInternal/CheckBoxPopupAdapter.cs @@ -16,7 +16,7 @@ internal override void PaintUp(PaintEventArgs e, CheckState state) { if (Control.Appearance == Appearance.Button) { - ButtonPopupAdapter adapter = new(Control); + ButtonBaseAdapter adapter = DarkModeAdapterFactory.CreatePopupAdapter(Control); adapter.PaintUp(e, Control.CheckState); } else @@ -51,7 +51,7 @@ internal override void PaintOver(PaintEventArgs e, CheckState state) { if (Control.Appearance == Appearance.Button) { - ButtonPopupAdapter adapter = new(Control); + ButtonBaseAdapter adapter = DarkModeAdapterFactory.CreatePopupAdapter(Control); adapter.PaintOver(e, Control.CheckState); } else @@ -94,7 +94,7 @@ internal override void PaintDown(PaintEventArgs e, CheckState state) { if (Control.Appearance == Appearance.Button) { - ButtonPopupAdapter adapter = new(Control); + ButtonBaseAdapter adapter = DarkModeAdapterFactory.CreatePopupAdapter(Control); adapter.PaintDown(e, Control.CheckState); } else @@ -115,7 +115,7 @@ internal override void PaintDown(PaintEventArgs e, CheckState state) } } - protected override ButtonBaseAdapter CreateButtonAdapter() => new ButtonPopupAdapter(Control); + protected override ButtonBaseAdapter CreateButtonAdapter() => DarkModeAdapterFactory.CreatePopupAdapter(Control); protected override LayoutOptions Layout(PaintEventArgs e) { diff --git a/src/System.Windows.Forms/System/Windows/Forms/Controls/Buttons/ButtonInternal/CheckBoxStandardAdapter.cs b/src/System.Windows.Forms/System/Windows/Forms/Controls/Buttons/ButtonInternal/CheckBoxStandardAdapter.cs index 3a7f7d3b0c8..9768de8f69a 100644 --- a/src/System.Windows.Forms/System/Windows/Forms/Controls/Buttons/ButtonInternal/CheckBoxStandardAdapter.cs +++ b/src/System.Windows.Forms/System/Windows/Forms/Controls/Buttons/ButtonInternal/CheckBoxStandardAdapter.cs @@ -105,9 +105,7 @@ internal override Size GetPreferredSizeCore(Size proposedSize) } } - private new ButtonStandardAdapter ButtonAdapter => (ButtonStandardAdapter)base.ButtonAdapter; - - protected override ButtonBaseAdapter CreateButtonAdapter() => new ButtonStandardAdapter(Control); + protected override ButtonBaseAdapter CreateButtonAdapter() => DarkModeAdapterFactory.CreateStandardAdapter(Control); protected override LayoutOptions Layout(PaintEventArgs e) { diff --git a/src/System.Windows.Forms/System/Windows/Forms/Controls/Buttons/ButtonInternal/DarkMode/ButtonBackColorAnimator.cs b/src/System.Windows.Forms/System/Windows/Forms/Controls/Buttons/ButtonInternal/DarkMode/ButtonBackColorAnimator.cs new file mode 100644 index 00000000000..0a325246403 --- /dev/null +++ b/src/System.Windows.Forms/System/Windows/Forms/Controls/Buttons/ButtonInternal/DarkMode/ButtonBackColorAnimator.cs @@ -0,0 +1,88 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. + +using System.Drawing; +using System.Windows.Forms.Rendering.Animation; + +namespace System.Windows.Forms.ButtonInternal; + +/// +/// Animates a button's background color between interaction states. +/// +internal sealed class ButtonBackColorAnimator : AnimatedControlRenderer +{ + private const int AnimationDuration = 350; + + private Color _fromColor; + private Color _toColor; + private bool _hasColor; + + public ButtonBackColorAnimator(Control control) : base(control) + { + } + + public Color CurrentColor { get; private set; } + + public void AnimateTo(Color targetColor) + { + if (!_hasColor) + { + _hasColor = true; + CurrentColor = targetColor; + _toColor = targetColor; + return; + } + + if (targetColor == _toColor) + { + return; + } + + _fromColor = CurrentColor; + _toColor = targetColor; + RestartAnimation(); + } + + public override void AnimationProc(float animationProgress) + { + base.AnimationProc(animationProgress); + CurrentColor = Lerp(_fromColor, _toColor, animationProgress); + Invalidate(); + } + + public override void RenderControl(Graphics graphics) + { + } + + protected override (int animationDuration, AnimationCycle animationCycle) OnAnimationStarted() + { + AnimationProgress = 0; + return (AnimationDuration, AnimationCycle.Once); + } + + protected override void OnAnimationStopped() + { + } + + protected override void OnAnimationEnded() + { + StopAnimation(); + CurrentColor = _toColor; + AnimationProgress = 1; + Invalidate(); + } + + private static Color Lerp(Color from, Color to, float progress) + { + progress = Math.Clamp(progress, 0f, 1f); + + return Color.FromArgb( + LerpChannel(from.A, to.A, progress), + LerpChannel(from.R, to.R, progress), + LerpChannel(from.G, to.G, progress), + LerpChannel(from.B, to.B, progress)); + + static int LerpChannel(int from, int to, float progress) + => from + (int)((to - from) * progress); + } +} diff --git a/src/System.Windows.Forms/System/Windows/Forms/Controls/Buttons/ButtonInternal/DarkMode/ButtonDarkModeAdapter.cs b/src/System.Windows.Forms/System/Windows/Forms/Controls/Buttons/ButtonInternal/DarkMode/ButtonDarkModeAdapter.cs index a85dd3ed4cc..c7ec35c0584 100644 --- a/src/System.Windows.Forms/System/Windows/Forms/Controls/Buttons/ButtonInternal/DarkMode/ButtonDarkModeAdapter.cs +++ b/src/System.Windows.Forms/System/Windows/Forms/Controls/Buttons/ButtonInternal/DarkMode/ButtonDarkModeAdapter.cs @@ -2,36 +2,68 @@ // The .NET Foundation licenses this file to you under the MIT license. using System.Drawing; +using System.Windows.Forms.Rendering.Button; using System.Windows.Forms.VisualStyles; namespace System.Windows.Forms.ButtonInternal; internal class ButtonDarkModeAdapter : ButtonBaseAdapter { + private readonly bool _animateBackgroundColors; private readonly ButtonDarkModeRendererBase _buttonDarkModeRenderer; + private readonly bool _modern; internal ButtonDarkModeAdapter(ButtonBase control) : base(control) { + _modern = control.EffectiveVisualStylesModeInternal >= VisualStylesMode.Net11; + _animateBackgroundColors = _modern && !SystemInformation.HighContrast; + _buttonDarkModeRenderer = control.FlatStyle switch { - FlatStyle.Standard => new FlatButtonDarkModeRenderer(), - FlatStyle.Flat => new FlatButtonDarkModeRenderer(), - FlatStyle.Popup => new PopupButtonDarkModeRenderer(), + // With VisualStyles (.NET 11+) the modern, WinUI-inspired renderer is used for the owner-drawn + // styles. Otherwise FlatStyle.Standard renders with a conservative owner-drawn renderer that mimics + // the dark-mode system button (instead of delegating to the Win32 control); this makes the owner-drawn + // path reachable and lets Standard buttons support images, focus cues, etc. + FlatStyle.Standard => _modern ? new ModernButtonDarkModeRenderer() : new SystemButtonDarkModeRenderer(), + FlatStyle.Flat => _modern ? new ModernFlatButtonRenderer() : new FlatButtonDarkModeRenderer(), + // FlatStyle.Popup is owner-painted directly by ButtonBase using the animated key-cap renderer; the + // adapter is used only for layout/sizing here, for which the + // modern renderer's metrics are a good fit. + FlatStyle.Popup => new ModernButtonDarkModeRenderer(), FlatStyle.System => new SystemButtonDarkModeRenderer(), _ => throw new ArgumentOutOfRangeException(nameof(control)) }; + + _buttonDarkModeRenderer.DeviceDpi = control.DeviceDpi; + _buttonDarkModeRenderer.FlatAppearance = control.FlatAppearance; } - private ButtonDarkModeRendererBase ButtonDarkModeRenderer => - _buttonDarkModeRenderer; + private ButtonDarkModeRendererBase ButtonDarkModeRenderer + { + get + { + _buttonDarkModeRenderer.DeviceDpi = Control.DeviceDpi; + return _buttonDarkModeRenderer; + } + } - private Color GetButtonTextColor(IDeviceContext deviceContext, PushButtonState state) + private Color GetButtonTextColor( + IDeviceContext deviceContext, + PushButtonState state, + Color backColor) { Color textColor; - if (Control.ForeColor != Forms.Control.DefaultForeColor) + bool useEffectiveForeColor = _modern + ? Control.ShouldSerializeForeColor() + : Control.ForeColor != Forms.Control.DefaultForeColor; + + if (useEffectiveForeColor) { - textColor = new ColorOptions(deviceContext, Control.ForeColor, Control.BackColor) + textColor = Control.Enabled + && Control.EffectiveVisualStylesModeInternal >= VisualStylesMode.Net11 + ? Control.ForeColor + : new ColorOptions(deviceContext, Control.ForeColor, Control.BackColor) { Enabled = Control.Enabled }.Calculate().WindowText; @@ -43,7 +75,7 @@ private Color GetButtonTextColor(IDeviceContext deviceContext, PushButtonState s } else { - textColor = ButtonDarkModeRenderer.GetTextColor(state, Control.IsDefault); + textColor = ButtonDarkModeRenderer.GetTextColor(state, Control.IsDefault, backColor); } return textColor; @@ -55,7 +87,10 @@ private Color GetButtonBackColor(PushButtonState state) if (Control.BackColor != Forms.Control.DefaultBackColor) { - backColor = Control.BackColor; + backColor = ButtonDarkModeRenderer.GetBackgroundColor( + state, + Control.IsDefault, + Control.BackColor); if (IsHighContrastHighlighted()) { @@ -64,132 +99,93 @@ private Color GetButtonBackColor(PushButtonState state) } else { - backColor = ButtonDarkModeRenderer.GetBackgroundColor(state, Control.IsDefault); + backColor = ButtonDarkModeRenderer.GetBackgroundColor( + state, + Control.IsDefault, + customBaseColor: Color.Empty); + } + + if (_animateBackgroundColors) + { + Control.BackColorAnimator.AnimateTo(backColor); + backColor = Control.BackColorAnimator.CurrentColor; } return backColor; } internal override void PaintUp(PaintEventArgs e, CheckState state) - { - try - { - // Use GraphicsInternal for better performance (GDI+ best practice) - var g = e.GraphicsInternal; - var smoothingMode = g.SmoothingMode; - g.SmoothingMode = Drawing.Drawing2D.SmoothingMode.AntiAlias; - - LayoutData layout = CommonLayout().Layout(); - - PushButtonState pushButtonState = ToPushButtonState(state, Control.Enabled); - ButtonDarkModeRenderer.RenderButton( - g, - Control.ClientRectangle, - Control.FlatStyle, - pushButtonState, - Control.IsDefault, - Control.Focused, - Control.ShowFocusCues, - Control.Parent?.BackColor ?? Control.BackColor, - GetButtonBackColor(pushButtonState), - _ => PaintImage(e, layout), - () => PaintField( - e, - layout, - PaintDarkModeRender(e).Calculate(), - GetButtonTextColor(e, pushButtonState), - drawFocus: false) - ); - - g.SmoothingMode = smoothingMode; - } - catch (Exception) - { - // Handle exceptions gracefully, possibly logging them or showing a message - Debug.Assert(false, "Exception in PaintUp: Unable to render button in dark mode."); - } - } + => PaintCore(e, ToPushButtonState(state, Control.Enabled)); internal override void PaintDown(PaintEventArgs e, CheckState state) - { - try - { - // Use GraphicsInternal for better performance (GDI+ best practice) - var g = e.GraphicsInternal; - var smoothingMode = g.SmoothingMode; - g.SmoothingMode = Drawing.Drawing2D.SmoothingMode.AntiAlias; - - LayoutData layout = CommonLayout().Layout(); - ButtonDarkModeRenderer.RenderButton( - g, - Control.ClientRectangle, - Control.FlatStyle, - PushButtonState.Pressed, - Control.IsDefault, - Control.Focused, - Control.ShowFocusCues, - Control.Parent?.BackColor ?? Control.BackColor, - GetButtonBackColor(PushButtonState.Pressed), - _ => PaintImage(e, layout), - () => PaintField( - e, - layout, - PaintDarkModeRender(e).Calculate(), - GetButtonTextColor(e, PushButtonState.Pressed), - drawFocus: false) - ); - - g.SmoothingMode = smoothingMode; - } - catch (Exception) - { - // Handle exceptions gracefully, possibly logging them or showing a message - Debug.Assert(false, "Exception in PaintDown: Unable to render button in dark mode."); - } - } + => PaintCore(e, PushButtonState.Pressed); internal override void PaintOver(PaintEventArgs e, CheckState state) + => PaintCore(e, PushButtonState.Hot); + + private void PaintCore(PaintEventArgs e, PushButtonState state) { + var graphics = e.GraphicsInternal; + Drawing.Drawing2D.SmoothingMode smoothingMode = graphics.SmoothingMode; + try { - // Use GraphicsInternal for better performance (GDI+ best practice) - var g = e.GraphicsInternal; - var smoothingMode = g.SmoothingMode; - g.SmoothingMode = Drawing.Drawing2D.SmoothingMode.AntiAlias; - - LayoutData layout = CommonLayout().Layout(); + graphics.SmoothingMode = Drawing.Drawing2D.SmoothingMode.AntiAlias; + Color backColor = GetButtonBackColor(state); + Color parentBackColor = Control.Parent?.BackColor ?? Control.BackColor; + Color textBackColor = PopupButtonColorMath.Composite(backColor, parentBackColor); ButtonDarkModeRenderer.RenderButton( - g, + graphics, + Control, Control.ClientRectangle, Control.FlatStyle, - PushButtonState.Hot, + state, Control.IsDefault, Control.Focused, Control.ShowFocusCues, - Control.Parent?.BackColor ?? Control.BackColor, - GetButtonBackColor(PushButtonState.Hot), - _ => PaintImage(e, layout), - () => PaintField( - e, - layout, - PaintDarkModeRender(e).Calculate(), - GetButtonTextColor(e, PushButtonState.Hot), - drawFocus: false) - ); - - g.SmoothingMode = smoothingMode; + parentBackColor, + backColor, + contentBounds => + { + LayoutData layout = GetLayoutData(contentBounds); + PaintImage(e, layout); + PaintField( + e, + layout, + PaintDarkModeRender(e).Calculate(), + GetButtonTextColor(e, state, textBackColor), + drawFocus: false); + }); } - catch (Exception ex) + finally { - Debug.Assert(false, $"Exception in PaintOver: {ex.Message}"); + graphics.SmoothingMode = smoothingMode; } } protected override LayoutOptions Layout(PaintEventArgs e) => CommonLayout(); - private new LayoutOptions CommonLayout() + internal override Size GetPreferredSizeCore(Size proposedSize) + => Control.FlatStyle == FlatStyle.Popup + ? GetModernPopupPreferredSizeCore( + CommonLayout(), + proposedSize, + Control.DeviceDpi, + Control.FlatAppearance.BorderSize) + : _modern + ? GetModernPreferredSizeCore( + CommonLayout(), + proposedSize, + ButtonDarkModeRenderer.GetPreferredSizePadding().Size) + : base.GetPreferredSizeCore(proposedSize); + + internal override LayoutOptions CommonLayout() { LayoutOptions layout = base.CommonLayout(); + layout.DisableWordWrapping = Control.AutoSize; + layout.DotNetOneButtonCompat = !_modern; + layout.ClipImagesToClient = _modern; + layout.EnsureImagePreferredSizeInset = _modern; layout.FocusOddEvenFixup = false; layout.ShadowedText = false; diff --git a/src/System.Windows.Forms/System/Windows/Forms/Controls/Buttons/ButtonInternal/DarkMode/ButtonDarkModeRendererBase.cs b/src/System.Windows.Forms/System/Windows/Forms/Controls/Buttons/ButtonInternal/DarkMode/ButtonDarkModeRendererBase.cs index 9d655a618b0..ae744bc04fb 100644 --- a/src/System.Windows.Forms/System/Windows/Forms/Controls/Buttons/ButtonInternal/DarkMode/ButtonDarkModeRendererBase.cs +++ b/src/System.Windows.Forms/System/Windows/Forms/Controls/Buttons/ButtonInternal/DarkMode/ButtonDarkModeRendererBase.cs @@ -14,6 +14,80 @@ internal abstract partial class ButtonDarkModeRendererBase : IButtonRenderer // Define padding values for each renderer type private protected abstract Padding PaddingCore { get; } + private protected virtual bool UseModernStateDefaults => false; + + /// + /// Gets the padding that insets the button body from the control bounds for the given focus state. + /// + /// + /// when the focus ring will be drawn for this paint; otherwise . + /// + /// + /// + /// By default this returns regardless of focus. Renderers that reserve room for + /// an outer focus ring can override this to return a smaller padding when the ring is not drawn, letting + /// the button body grow into the space the ring and its gap would otherwise occupy. + /// + /// + private protected virtual Padding GetContentPadding(bool focusRingVisible) => PaddingCore; + + internal virtual Padding GetPreferredSizePadding() => PaddingCore; + + /// + /// The device DPI of the control being rendered. Set by the adapter before each paint so renderers + /// can DPI-scale their logical (96-DPI) constants. Defaults to 96 (100%). + /// + internal int DeviceDpi { get; set; } = 96; + + /// + /// Gets or sets the appearance settings for the button being rendered. + /// + internal FlatButtonAppearance? FlatAppearance { get; set; } + + /// + /// Scales a logical (96-DPI) value to the current . + /// + private protected int Scale(int logicalValue) => (int)Math.Round(logicalValue * (DeviceDpi / 96.0)); + + private protected int ScaleBorderThickness(int scaledThickness) + { + int borderSize = FlatAppearance?.BorderSize ?? 1; + if (borderSize == 0) + { + return 0; + } + + float dpiScale = Math.Max(1f, DeviceDpi / 96f); + float factor = 1f + ((borderSize - 1) / dpiScale); + + return Math.Max(1, (int)Math.Round(scaledThickness * factor)); + } + + private protected Color ResolveBorderColor(Color designColor) + => FlatAppearance is { BorderColor.IsEmpty: false } appearance + ? appearance.BorderColor + : designColor; + + private protected static Color DeriveHoverColor(Color baseColor) + => baseColor.GetBrightness() < 0.5f + ? ControlPaint.Light(baseColor, 0.15f) + : ControlPaint.Dark(baseColor, 0.05f); + + private protected static Color DerivePressedColor(Color baseColor) + => baseColor.GetBrightness() < 0.5f + ? ControlPaint.Light(baseColor, 0.30f) + : ControlPaint.Dark(baseColor, 0.12f); + + private protected static Color DeriveDisabledColor(Color baseColor) + { + int average = (baseColor.R + baseColor.G + baseColor.B) / 3; + + return Color.FromArgb( + ((average * 6) + (baseColor.R * 4)) / 10, + ((average * 6) + (baseColor.G * 4)) / 10, + ((average * 6) + (baseColor.B * 4)) / 10); + } + /// /// Clears the background with the parent's background color or the control's background color if no parent is available. /// @@ -26,10 +100,11 @@ private static void ClearBackground(Graphics graphics, Color parentBackgroundCol } /// - /// Renders a button with the specified properties and delegates for painting image and field. + /// Renders a button with the specified properties and a delegate for painting its content. /// public void RenderButton( Graphics graphics, + Control control, Rectangle bounds, FlatStyle flatStyle, PushButtonState state, @@ -38,21 +113,17 @@ public void RenderButton( bool showFocusCues, Color parentBackgroundColor, Color backColor, - Action paintImage, - Action paintField) + Action paintContent) { ArgumentNullException.ThrowIfNull(graphics); - ArgumentNullException.ThrowIfNull(paintImage); - ArgumentNullException.ThrowIfNull(paintField); + ArgumentNullException.ThrowIfNull(paintContent); // Scope the graphics state so all changes are reverted after rendering using (new GraphicsStateScope(graphics)) { - // Clear the background over the whole button area. - ClearBackground(graphics, parentBackgroundColor); - - // Use padding from ButtonDarkModeRenderer - Padding padding = PaddingCore; + // Use padding from the renderer. When the focus ring is not drawn, renderers may return a smaller + // padding so the button body expands into the space the ring and its gap would otherwise occupy. + Padding padding = GetContentPadding(focused && showFocusCues); Rectangle paddedBounds = new( x: bounds.X + padding.Left, @@ -60,13 +131,20 @@ public void RenderButton( width: bounds.Width - padding.Horizontal, height: bounds.Height - padding.Vertical); - // Draw button background and get content bounds - Rectangle contentBounds = DrawButtonBackground(graphics, paddedBounds, state, isDefault, backColor); + if (PaintParentBackground && paddedBounds.Width > 0 && paddedBounds.Height > 0) + { + ParentBackgroundRenderer.Paint(control, graphics, bounds, parentBackgroundColor); + } + else + { + // Rectangular renderers still need a complete background before painting their body. + ClearBackground(graphics, parentBackgroundColor); + } - // Paint image and field using the provided delegates - paintImage(contentBounds); + // Draw button background and get content bounds + Rectangle contentBounds = DrawButtonBackground(graphics, paddedBounds, state, isDefault, focused, backColor); - paintField(); + paintContent(contentBounds); if (focused && showFocusCues) { @@ -76,11 +154,54 @@ public void RenderButton( } } - public abstract Rectangle DrawButtonBackground(Graphics graphics, Rectangle bounds, PushButtonState state, bool isDefault, Color backColor); + private protected virtual bool PaintParentBackground => false; + + public abstract Rectangle DrawButtonBackground( + Graphics graphics, + Rectangle bounds, + PushButtonState state, + bool isDefault, + bool focused, + Color backColor); public abstract void DrawFocusIndicator(Graphics graphics, Rectangle contentBounds, bool isDefault); - public abstract Color GetTextColor(PushButtonState state, bool isDefault); + public abstract Color GetTextColor(PushButtonState state, bool isDefault, Color backColor); + + public Color GetBackgroundColor(PushButtonState state, bool isDefault, Color customBaseColor) + { + if (state == PushButtonState.Hot + && FlatAppearance is { MouseOverBackColorCore.IsEmpty: false } hoverAppearance) + { + return hoverAppearance.MouseOverBackColorCore; + } + + if (state == PushButtonState.Pressed + && FlatAppearance is { MouseDownBackColorCore.IsEmpty: false } pressedAppearance) + { + return pressedAppearance.MouseDownBackColorCore; + } + + if (UseModernStateDefaults + && FlatAppearance is not null + && state is PushButtonState.Hot or PushButtonState.Pressed) + { + return ModernButtonColorMath.GetStateColor(this, state, isDefault, customBaseColor); + } + + if (!customBaseColor.IsEmpty) + { + return state switch + { + PushButtonState.Disabled => DeriveDisabledColor(customBaseColor), + PushButtonState.Hot => DeriveHoverColor(customBaseColor), + PushButtonState.Pressed => DerivePressedColor(customBaseColor), + _ => customBaseColor + }; + } + + return GetBackgroundColor(state, isDefault); + } public abstract Color GetBackgroundColor(PushButtonState state, bool isDefault); } diff --git a/src/System.Windows.Forms/System/Windows/Forms/Controls/Buttons/ButtonInternal/DarkMode/DarkModeAdapterFactory.cs b/src/System.Windows.Forms/System/Windows/Forms/Controls/Buttons/ButtonInternal/DarkMode/DarkModeAdapterFactory.cs index 55552647bce..c32a3a00c79 100644 --- a/src/System.Windows.Forms/System/Windows/Forms/Controls/Buttons/ButtonInternal/DarkMode/DarkModeAdapterFactory.cs +++ b/src/System.Windows.Forms/System/Windows/Forms/Controls/Buttons/ButtonInternal/DarkMode/DarkModeAdapterFactory.cs @@ -5,18 +5,25 @@ namespace System.Windows.Forms.ButtonInternal; internal static class DarkModeAdapterFactory { + // The owner-drawn dark/modern adapter is used when dark mode is enabled (conservative renderer) or when + // the control opts into the modern .NET 11 visual styles (modern renderer, in either dark or light scheme). + private static bool UseOwnerDrawnAdapter(ButtonBase control) + { + return Application.IsDarkModeEnabled || control.EffectiveVisualStylesModeInternal >= VisualStylesMode.Net11; + } + public static ButtonBaseAdapter CreateFlatAdapter(ButtonBase control) => - Application.IsDarkModeEnabled + UseOwnerDrawnAdapter(control) ? new ButtonDarkModeAdapter(control) : new ButtonFlatAdapter(control); public static ButtonBaseAdapter CreateStandardAdapter(ButtonBase control) => - Application.IsDarkModeEnabled + UseOwnerDrawnAdapter(control) ? new ButtonDarkModeAdapter(control) : new ButtonStandardAdapter(control); public static ButtonBaseAdapter CreatePopupAdapter(ButtonBase control) => - Application.IsDarkModeEnabled + UseOwnerDrawnAdapter(control) ? new ButtonDarkModeAdapter(control) : new ButtonPopupAdapter(control); } diff --git a/src/System.Windows.Forms/System/Windows/Forms/Controls/Buttons/ButtonInternal/DarkMode/FlatButtonDarkModeRenderer.cs b/src/System.Windows.Forms/System/Windows/Forms/Controls/Buttons/ButtonInternal/DarkMode/FlatButtonDarkModeRenderer.cs index 2541cb1b6ad..5b27822ed35 100644 --- a/src/System.Windows.Forms/System/Windows/Forms/Controls/Buttons/ButtonInternal/DarkMode/FlatButtonDarkModeRenderer.cs +++ b/src/System.Windows.Forms/System/Windows/Forms/Controls/Buttons/ButtonInternal/DarkMode/FlatButtonDarkModeRenderer.cs @@ -23,7 +23,12 @@ internal sealed class FlatButtonDarkModeRenderer : ButtonDarkModeRendererBase private protected override Padding PaddingCore { get; } = new(0); public override Rectangle DrawButtonBackground( - Graphics graphics, Rectangle bounds, PushButtonState state, bool isDefault, Color backColor) + Graphics graphics, + Rectangle bounds, + PushButtonState state, + bool isDefault, + bool focused, + Color backColor) { // fill background using var back = backColor.GetCachedSolidBrushScope(); @@ -51,7 +56,7 @@ public override void DrawFocusIndicator(Graphics g, Rectangle contentBounds, boo focusBackColor); } - public override Color GetTextColor(PushButtonState state, bool isDefault) => + public override Color GetTextColor(PushButtonState state, bool isDefault, Color backColor) => state == PushButtonState.Disabled ? DefaultColors.DisabledTextColor : isDefault @@ -77,32 +82,37 @@ public override Color GetBackgroundColor(PushButtonState state, bool isDefault) _ => DefaultColors.StandardBackColor }; - private static void DrawButtonBorder(Graphics g, Rectangle bounds, PushButtonState state, bool isDefault) + private void DrawButtonBorder(Graphics g, Rectangle bounds, PushButtonState state, bool isDefault) { g.SmoothingMode = SmoothingMode.AntiAlias; - // Win32 draws its stroke fully *inside* the control → inset by 1 px + int thickness = ScaleBorderThickness(1); + if (thickness == 0) + { + return; + } + + // Win32 draws its stroke fully inside the control. Rectangle outer = Rectangle.Inflate(bounds, -1, -1); - DrawSingleBorder(g, outer, GetBorderColor(state)); + DrawSingleBorder(g, outer, ResolveBorderColor(GetBorderColor(state)), thickness); - // Default button gets a second 1‑px border one pixel further inside + // Default button gets a second border one pixel further inside. if (isDefault) { Rectangle inner = Rectangle.Inflate(outer, -1, -1); - DrawSingleBorder(g, inner, DefaultColors.AcceptFocusIndicatorBackColor); + DrawSingleBorder(g, inner, DefaultColors.AcceptFocusIndicatorBackColor, thickness); } } - private static void DrawSingleBorder(Graphics g, Rectangle rect, Color color) + private static void DrawSingleBorder(Graphics g, Rectangle rect, Color color, int thickness) { g.SmoothingMode = SmoothingMode.AntiAlias; using var path = new GraphicsPath(); path.AddRoundedRectangle(rect, s_corner); - // a 1‑px stroke, aligned *inside*, is exactly what Win32 draws - using var pen = new Pen(color) { Alignment = PenAlignment.Inset }; + using var pen = new Pen(color, thickness) { Alignment = PenAlignment.Inset }; g.DrawPath(pen, path); } diff --git a/src/System.Windows.Forms/System/Windows/Forms/Controls/Buttons/ButtonInternal/DarkMode/IButtonRenderer.cs b/src/System.Windows.Forms/System/Windows/Forms/Controls/Buttons/ButtonInternal/DarkMode/IButtonRenderer.cs index ab1f7844042..522f52d85b3 100644 --- a/src/System.Windows.Forms/System/Windows/Forms/Controls/Buttons/ButtonInternal/DarkMode/IButtonRenderer.cs +++ b/src/System.Windows.Forms/System/Windows/Forms/Controls/Buttons/ButtonInternal/DarkMode/IButtonRenderer.cs @@ -30,6 +30,7 @@ static void DrawButtonBorder(Graphics graphics, GraphicsPath path, Color borderC /// Renders the button with the specified style, state, and content. /// /// The graphics context to draw on. + /// The button control whose parent surface is used for exposed regions. /// The bounds of the button. /// The flat style of the button. /// The visual state of the button (normal, hot, pressed, disabled, default). @@ -37,10 +38,10 @@ static void DrawButtonBorder(Graphics graphics, GraphicsPath path, Color borderC /// True if the button is focused; otherwise, false. /// True to show focus cues; otherwise, false. /// The background color of the parent control. - /// An action to paint the image within the specified rectangle. - /// An action to paint the text or field within the specified rectangle, color, and enabled state. + /// An action to lay out and paint the image and text within the content rectangle. void RenderButton( Graphics graphics, + Control control, Rectangle bounds, FlatStyle flatStyle, PushButtonState state, @@ -49,8 +50,7 @@ void RenderButton( bool showFocusCues, Color parentBackgroundColor, Color backColor, - Action paintImage, - Action paintField); + Action paintContent); /// /// Draws button background with appropriate styling. @@ -59,8 +59,15 @@ void RenderButton( /// Bounds of the button /// State of the button (normal, hot, pressed, disabled) /// True if button is the default button + /// True if the button is focused /// The content bounds (area inside the button for text/image) - Rectangle DrawButtonBackground(Graphics graphics, Rectangle bounds, PushButtonState state, bool isDefault, Color backColor); + Rectangle DrawButtonBackground( + Graphics graphics, + Rectangle bounds, + PushButtonState state, + bool isDefault, + bool focused, + Color backColor); /// /// Draws focus indicator appropriate for this style. @@ -73,5 +80,5 @@ void RenderButton( /// /// Gets the text color appropriate for the button state and type. /// - Color GetTextColor(PushButtonState state, bool isDefault); + Color GetTextColor(PushButtonState state, bool isDefault, Color backColor); } diff --git a/src/System.Windows.Forms/System/Windows/Forms/Controls/Buttons/ButtonInternal/DarkMode/ModernButtonColorMath.cs b/src/System.Windows.Forms/System/Windows/Forms/Controls/Buttons/ButtonInternal/DarkMode/ModernButtonColorMath.cs new file mode 100644 index 00000000000..bb08116fd05 --- /dev/null +++ b/src/System.Windows.Forms/System/Windows/Forms/Controls/Buttons/ButtonInternal/DarkMode/ModernButtonColorMath.cs @@ -0,0 +1,99 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. + +using System.Windows.Forms.Rendering.Button; +using System.Windows.Forms.VisualStyles; + +namespace System.Windows.Forms; + +/// +/// Resolves the modern button palette and state colors shared by button renderers and appearance defaults. +/// +internal static class ModernButtonColorMath +{ + internal const float AccentBlendAmount = 0.2f; + internal const float MinimumTextContrastRatio = PopupButtonColorMath.MinimumReadableContrastRatio; + private const float DefaultHoverLightenAmount = 0.08f; + private const float DefaultPressedDarkenAmount = 0.12f; + + internal static Color GetMouseDownColor() + => Application.GetWindowsAccentColor(); + + internal static Color GetMouseOverColor(ButtonBase owner, FlatButtonAppearance appearance) + { + Color baseColor = GetRenderedBaseColor(owner, appearance); + + return BlendWithAccent(baseColor); + } + + internal static Color BlendWithAccent(Color baseColor) + => PopupButtonColorMath.Blend(baseColor, Application.GetWindowsAccentColor(), AccentBlendAmount); + + internal static Color GetDefaultButtonColor(PushButtonState state) + => GetDefaultButtonColor(Application.GetWindowsAccentColor(), state); + + internal static Color GetDefaultButtonColor(Color accentColor, PushButtonState state) + => state switch + { + PushButtonState.Hot => PopupButtonColorMath.Lighten(accentColor, DefaultHoverLightenAmount), + PushButtonState.Pressed => PopupButtonColorMath.Darken(accentColor, DefaultPressedDarkenAmount), + _ => accentColor + }; + + internal static Color GetReadableForeColor(Color backColor) + { + float blackContrast = PopupButtonColorMath.GetContrastRatio(Color.Black, backColor); + float whiteContrast = PopupButtonColorMath.GetContrastRatio(Color.White, backColor); + Color foreColor = PopupButtonColorMath.GetReadableForeColor(backColor); + + Debug.Assert( + Math.Max(blackContrast, whiteContrast) >= MinimumTextContrastRatio, + "Either black or white should meet WCAG AA contrast against an opaque background."); + + return foreColor; + } + + internal static Color GetStateColor( + ButtonDarkModeRendererBase renderer, + PushButtonState state, + bool isDefault, + Color customBaseColor) + { + if (customBaseColor.IsEmpty && isDefault) + { + return GetDefaultButtonColor(state); + } + + Color baseColor = customBaseColor.IsEmpty + ? renderer.GetBackgroundColor(PushButtonState.Normal, isDefault) + : customBaseColor; + + return state switch + { + PushButtonState.Pressed => GetMouseDownColor(), + PushButtonState.Hot => BlendWithAccent(baseColor), + _ => baseColor + }; + } + + internal static Color GetRenderedBaseColor(ButtonBase owner, FlatButtonAppearance appearance) + { + ButtonDarkModeRendererBase renderer = owner.FlatStyle switch + { + FlatStyle.Standard or FlatStyle.Popup => new ModernButtonDarkModeRenderer(), + FlatStyle.Flat => new ModernFlatButtonRenderer(), + FlatStyle.System => new SystemButtonDarkModeRenderer(), + _ => throw new ArgumentOutOfRangeException(nameof(owner)) + }; + + renderer.DeviceDpi = owner.DeviceDpi; + renderer.FlatAppearance = appearance; + + if (owner.BackColor != Control.DefaultBackColor) + { + return owner.BackColor; + } + + return renderer.GetBackgroundColor(PushButtonState.Normal, owner.IsDefault); + } +} diff --git a/src/System.Windows.Forms/System/Windows/Forms/Controls/Buttons/ButtonInternal/DarkMode/ModernButtonDarkModeRenderer.cs b/src/System.Windows.Forms/System/Windows/Forms/Controls/Buttons/ButtonInternal/DarkMode/ModernButtonDarkModeRenderer.cs new file mode 100644 index 00000000000..b2805b1ff89 --- /dev/null +++ b/src/System.Windows.Forms/System/Windows/Forms/Controls/Buttons/ButtonInternal/DarkMode/ModernButtonDarkModeRenderer.cs @@ -0,0 +1,255 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. + +using System.Drawing; +using System.Drawing.Drawing2D; +using System.Windows.Forms.VisualStyles; + +namespace System.Windows.Forms; + +/// +/// Modern, WinUI-inspired push button renderer used when is +/// or later. It supports both dark and light color schemes and draws a +/// rounded button area, an optional dark gap ring, and a rounded focus ring for the focused button. +/// +/// +/// +/// The visual is intentionally driven by a small set of DPI-scaled constants so the appearance can be +/// fine-tuned during exploratory testing. All widths are expressed in logical (96-DPI) pixels. +/// +/// +internal sealed class ModernButtonDarkModeRenderer : ButtonDarkModeRendererBase +{ + // Logical (96-DPI) layout constants. DPI-scaled via the base Scale() helper. + private const int FocusRingThicknessLogical = 2; + private const int FocusGapThicknessLogical = 1; + private const int FocusedCornerRadiusLogical = 6; + private const int UnfocusedCornerRadiusLogical = 8; + private const int BorderThicknessLogical = 1; + private const int ContentInsetLogical = 4; + + // Dark scheme - normal button area. + private static readonly Color s_darkNormal = Color.FromArgb(0x2D, 0x2D, 0x2D); + private static readonly Color s_darkNormalHover = Color.FromArgb(0x32, 0x32, 0x32); + private static readonly Color s_darkNormalPressed = Color.FromArgb(0x2A, 0x2A, 0x2A); + private static readonly Color s_darkDisabled = Color.FromArgb(0x25, 0x25, 0x25); + + private static readonly Color s_darkDisabledText = Color.FromArgb(0x88, 0x88, 0x88); + + private static readonly Color s_darkGap = Color.FromArgb(0x0A, 0x0A, 0x0A); + private static readonly Color s_darkFocusRing = Color.White; + + // Light (WinUI) scheme - normal button area. + private static readonly Color s_lightNormal = Color.FromArgb(0xFB, 0xFB, 0xFB); + private static readonly Color s_lightNormalHover = Color.FromArgb(0xF9, 0xF9, 0xF9); + private static readonly Color s_lightNormalPressed = Color.FromArgb(0xF5, 0xF5, 0xF5); + private static readonly Color s_lightDisabled = Color.FromArgb(0xFA, 0xFA, 0xFA); + private static readonly Color s_lightBorder = Color.FromArgb(0xD0, 0xD0, 0xD0); + + private static readonly Color s_lightDisabledText = Color.FromArgb(0xA0, 0xA0, 0xA0); + + private static bool IsDark => Application.IsDarkModeEnabled; + + private int FocusRingThickness + => ScaleBorderThickness(Math.Max(1, Scale(FocusRingThicknessLogical) - HighDpiCorrection)); + + private int FocusGapThickness + => Math.Max(1, Scale(FocusGapThicknessLogical) - HighDpiCorrection); + + private int FocusBodyInset + => Math.Max( + FocusRingThickness + FocusGapThickness, + Math.Max(1, Scale(FocusRingThicknessLogical)) + + Math.Max(1, Scale(FocusGapThicknessLogical))); + + private int HighDpiCorrection => DeviceDpi > ScaleHelper.OneHundredPercentLogicalDpi ? 1 : 0; + + private int GetCornerRadius(bool focused, bool isDefault) + => Math.Max(1, Scale(focused || isDefault ? FocusedCornerRadiusLogical : UnfocusedCornerRadiusLogical)); + + // When focused, the body is inset just enough to leave room for the focus ring and a single-pixel gap, + // keeping that gap tight so the rounded body claims as much real estate as possible. + private protected override Padding PaddingCore + => new(FocusBodyInset); + + // When the focus ring is not drawn there is nothing to inset for, so the rounded body expands to fill the + // whole client area - covering the band the ring and its gap would otherwise occupy. This gives the button + // a more generous, less cramped background without changing the control's own Padding. + private protected override Padding GetContentPadding(bool focusRingVisible) + => focusRingVisible ? PaddingCore : Padding.Empty; + + internal override Padding GetPreferredSizePadding() + => new(FocusBodyInset + Scale(ContentInsetLogical)); + + private protected override bool UseModernStateDefaults => true; + + private protected override bool PaintParentBackground => true; + + public override Rectangle DrawButtonBackground( + Graphics graphics, + Rectangle bounds, + PushButtonState state, + bool isDefault, + bool focused, + Color backColor) + { + GraphicsState? saved = graphics.Save(); + try + { + graphics.SmoothingMode = SmoothingMode.AntiAlias; + + RectangleF pathBounds = GetPathBounds(bounds); + float radius = GetCornerRadius(focused, isDefault); + int borderThickness = ScaleBorderThickness( + Math.Max(1, Scale(BorderThicknessLogical))); + + if (!IsDark + && !isDefault + && state != PushButtonState.Disabled + && borderThickness > 0) + { + Color borderColor = ResolveBorderColor(s_lightBorder); + using var borderBrush = borderColor.GetCachedSolidBrushScope(); + using GraphicsPath borderPath = CreateRingPath( + pathBounds, + radius, + borderThickness); + graphics.FillPath(borderBrush, borderPath); + + pathBounds = Inset(pathBounds, borderThickness); + radius = Math.Max(1, radius - (2 * borderThickness)); + } + + using var brush = backColor.GetCachedSolidBrushScope(); + using GraphicsPath bodyPath = CreateRoundedPath(pathBounds, radius); + graphics.FillPath(brush, bodyPath); + } + finally + { + if (saved is not null) + { + graphics.Restore(saved); + } + } + + int inset = Scale(ContentInsetLogical); + return Rectangle.Inflate(bounds, -inset, -inset); + } + + public override void DrawFocusIndicator(Graphics graphics, Rectangle bounds, bool isDefault) + { + GraphicsState? saved = graphics.Save(); + try + { + graphics.SmoothingMode = SmoothingMode.AntiAlias; + + if (FocusRingThickness == 0) + { + return; + } + + RectangleF outerBounds = GetPathBounds(bounds); + int bodyInset = FocusRingThickness + FocusGapThickness; + float outerRadius = GetCornerRadius(focused: true, isDefault: isDefault) + + (2 * bodyInset); + + Color gapColor = IsDark ? s_darkGap : SystemColors.Window; + using (var gapBrush = gapColor.GetCachedSolidBrushScope()) + { + using GraphicsPath gapPath = CreateRingPath( + Inset(outerBounds, FocusRingThickness), + outerRadius - (2 * FocusRingThickness), + FocusGapThickness); + graphics.FillPath(gapBrush, gapPath); + } + + Color ringColor = ResolveBorderColor(IsDark ? s_darkFocusRing : SystemColors.WindowText); + using var ringBrush = ringColor.GetCachedSolidBrushScope(); + using GraphicsPath ringPath = CreateRingPath( + outerBounds, + outerRadius, + FocusRingThickness); + graphics.FillPath(ringBrush, ringPath); + } + finally + { + if (saved is not null) + { + graphics.Restore(saved); + } + } + } + + public override Color GetTextColor(PushButtonState state, bool isDefault, Color backColor) + { + if (state == PushButtonState.Disabled) + { + return IsDark ? s_darkDisabledText : s_lightDisabledText; + } + + return ModernButtonColorMath.GetReadableForeColor(backColor); + } + + public override Color GetBackgroundColor(PushButtonState state, bool isDefault) + { + if (state == PushButtonState.Disabled) + { + return IsDark ? s_darkDisabled : s_lightDisabled; + } + + if (isDefault) + { + return ModernButtonColorMath.GetDefaultButtonColor(state); + } + + return IsDark + ? state switch + { + PushButtonState.Hot => s_darkNormalHover, + PushButtonState.Pressed => s_darkNormalPressed, + _ => s_darkNormal + } + : state switch + { + PushButtonState.Hot => s_lightNormalHover, + PushButtonState.Pressed => s_lightNormalPressed, + _ => s_lightNormal + }; + } + + private static RectangleF GetPathBounds(Rectangle bounds) + => new( + bounds.X, + bounds.Y, + Math.Max(1, bounds.Width - 1), + Math.Max(1, bounds.Height - 1)); + + private static RectangleF Inset(RectangleF bounds, float inset) + => new( + bounds.X + inset, + bounds.Y + inset, + Math.Max(1, bounds.Width - (2 * inset)), + Math.Max(1, bounds.Height - (2 * inset))); + + private static GraphicsPath CreateRoundedPath(RectangleF bounds, float radius) + { + GraphicsPath path = new(); + float clampedRadius = Math.Clamp(radius, 1, Math.Min(bounds.Width, bounds.Height)); + path.AddRoundedRectangle(bounds, new SizeF(clampedRadius, clampedRadius)); + return path; + } + + private static GraphicsPath CreateRingPath( + RectangleF outerBounds, + float outerRadius, + float thickness) + { + GraphicsPath path = CreateRoundedPath(outerBounds, outerRadius); + RectangleF innerBounds = Inset(outerBounds, thickness); + float innerRadius = Math.Max(1, outerRadius - (2 * thickness)); + using GraphicsPath innerPath = CreateRoundedPath(innerBounds, innerRadius); + path.FillMode = FillMode.Alternate; + path.AddPath(innerPath, connect: false); + return path; + } +} diff --git a/src/System.Windows.Forms/System/Windows/Forms/Controls/Buttons/ButtonInternal/DarkMode/ModernFlatButtonRenderer.cs b/src/System.Windows.Forms/System/Windows/Forms/Controls/Buttons/ButtonInternal/DarkMode/ModernFlatButtonRenderer.cs new file mode 100644 index 00000000000..c7592e2fd8b --- /dev/null +++ b/src/System.Windows.Forms/System/Windows/Forms/Controls/Buttons/ButtonInternal/DarkMode/ModernFlatButtonRenderer.cs @@ -0,0 +1,128 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. + +using System.Drawing; +using System.Drawing.Drawing2D; +using System.Windows.Forms.VisualStyles; + +namespace System.Windows.Forms; + +/// +/// Renders a modern button with a rectangular body and a single border. +/// +internal sealed class ModernFlatButtonRenderer : ButtonDarkModeRendererBase +{ + private const int BorderThicknessLogical = 1; + private const int ContentInsetLogical = 4; + private const int FocusInsetLogical = 3; + + private static readonly Color s_lightBorder = Color.FromArgb(0xD0, 0xD0, 0xD0); + private static readonly Color s_darkBorder = Color.FromArgb(0x55, 0x55, 0x55); + private static readonly Color s_lightNormal = Color.FromArgb(0xFB, 0xFB, 0xFB); + private static readonly Color s_lightDisabled = Color.FromArgb(0xFA, 0xFA, 0xFA); + private static readonly Color s_darkNormal = Color.FromArgb(0x2D, 0x2D, 0x2D); + private static readonly Color s_darkDisabled = Color.FromArgb(0x25, 0x25, 0x25); + + private static bool IsDark => Application.IsDarkModeEnabled; + + private int BorderThickness + => ScaleBorderThickness(Math.Max(1, Scale(BorderThicknessLogical))); + + private protected override Padding PaddingCore => Padding.Empty; + + private protected override bool UseModernStateDefaults => true; + + internal override Padding GetPreferredSizePadding() + => new(Math.Max(BorderThickness, Scale(ContentInsetLogical))); + + public override Rectangle DrawButtonBackground( + Graphics graphics, + Rectangle bounds, + PushButtonState state, + bool isDefault, + bool focused, + Color backColor) + { + GraphicsState? saved = graphics.Save(); + try + { + graphics.SmoothingMode = SmoothingMode.Default; + + using (var brush = backColor.GetCachedSolidBrushScope()) + { + graphics.FillRectangle(brush, bounds); + } + + int thickness = BorderThickness; + if (thickness > 0 && bounds.Width > 0 && bounds.Height > 0) + { + Color designBorderColor = isDefault + ? (IsDark ? Color.White : SystemColors.Highlight) + : (IsDark ? s_darkBorder : s_lightBorder); + + using var pen = new Pen(ResolveBorderColor(designBorderColor), thickness) + { + Alignment = PenAlignment.Inset + }; + + graphics.DrawRectangle(pen, bounds.X, bounds.Y, bounds.Width - 1, bounds.Height - 1); + } + } + finally + { + if (saved is not null) + { + graphics.Restore(saved); + } + } + + int inset = Math.Max(BorderThickness, Scale(ContentInsetLogical)); + return Rectangle.Inflate(bounds, -inset, -inset); + } + + public override void DrawFocusIndicator(Graphics graphics, Rectangle contentBounds, bool isDefault) + { + int inset = Scale(FocusInsetLogical); + Rectangle focusRectangle = Rectangle.Inflate(contentBounds, -inset, -inset); + if (focusRectangle.Width <= 0 || focusRectangle.Height <= 0) + { + return; + } + + Color focusColor = ResolveBorderColor(IsDark ? Color.White : SystemColors.WindowText); + using var focusPen = new Pen(focusColor) { DashStyle = DashStyle.Dot }; + graphics.DrawRectangle(focusPen, focusRectangle); + } + + public override Color GetTextColor(PushButtonState state, bool isDefault, Color backColor) + { + if (state == PushButtonState.Disabled) + { + return IsDark ? Color.FromArgb(0x88, 0x88, 0x88) : Color.FromArgb(0xA0, 0xA0, 0xA0); + } + + return ModernButtonColorMath.GetReadableForeColor(backColor); + } + + public override Color GetBackgroundColor(PushButtonState state, bool isDefault) + { + if (state == PushButtonState.Disabled) + { + return IsDark ? s_darkDisabled : s_lightDisabled; + } + + if (isDefault) + { + return ModernButtonColorMath.GetDefaultButtonColor(state); + } + + Color baseColor = IsDark ? s_darkNormal : s_lightNormal; + + return state switch + { + PushButtonState.Hot => DeriveHoverColor(baseColor), + PushButtonState.Pressed => DerivePressedColor(baseColor), + _ => baseColor + }; + } +} diff --git a/src/System.Windows.Forms/System/Windows/Forms/Controls/Buttons/ButtonInternal/DarkMode/PopupButtonDarkModeRenderer.cs b/src/System.Windows.Forms/System/Windows/Forms/Controls/Buttons/ButtonInternal/DarkMode/PopupButtonDarkModeRenderer.cs deleted file mode 100644 index 5970ad57fca..00000000000 --- a/src/System.Windows.Forms/System/Windows/Forms/Controls/Buttons/ButtonInternal/DarkMode/PopupButtonDarkModeRenderer.cs +++ /dev/null @@ -1,231 +0,0 @@ -// Licensed to the .NET Foundation under one or more agreements. -// The .NET Foundation licenses this file to you under the MIT license. - -using System.Drawing; -using System.Drawing.Drawing2D; -using System.Windows.Forms.VisualStyles; -using static System.Windows.Forms.DarkModeButtonColors; - -namespace System.Windows.Forms; - -/// -/// Provides methods for rendering a button with Popup FlatStyle in dark mode. -/// -internal class PopupButtonDarkModeRenderer : ButtonDarkModeRendererBase -{ - // UI constants - private const int ButtonCornerRadius = 5; - private const int FocusCornerRadius = 3; - private const int FocusPadding = 2; - private const int BorderThickness = 2; - private const int ContentOffset = 1; // Offset for content when pressed - - private protected override Padding PaddingCore { get; } = new(0); - - // Default border color adjustment constants - private const int DefaultBorderROffset = 30; - private const int DefaultBorderGOffset = 20; - private const int DefaultBorderBOffset = 40; - - /// - /// Draws button background with popup styling, including subtle 3D effect. - /// - public override Rectangle DrawButtonBackground(Graphics graphics, Rectangle bounds, PushButtonState state, bool isDefault, Color backColor) - { - // Use padding from ButtonDarkModeRenderer - Padding padding = PaddingCore; - Rectangle paddedBounds = Rectangle.Inflate(bounds, -padding.Left, -padding.Top); - - // Content rect will be used to position text and images - Rectangle contentBounds = Rectangle.Inflate(paddedBounds, -padding.Left, -padding.Top); - - // Adjust content position when pressed to enhance 3D effect - if (state == PushButtonState.Pressed) - { - contentBounds.Offset(ContentOffset, ContentOffset); - } - - // Create path for rounded corners - using GraphicsPath path = CreateRoundedRectanglePath(paddedBounds, ButtonCornerRadius); - - // Fill the background using cached brush - using var brush = backColor.GetCachedSolidBrushScope(); - graphics.FillPath(brush, path); - - // Draw 3D effect borders - DrawButtonBorder(graphics, paddedBounds, state, isDefault); - - // Return content bounds (area inside the button for text/image) - return contentBounds; - } - - /// - /// Draws a focus rectangle with dotted lines inside the button. - /// Adjusts for the 3D effect based on the button's state. - /// - public override void DrawFocusIndicator(Graphics graphics, Rectangle contentBounds, bool isDefault) - { - // Create a slightly smaller rectangle for the focus indicator - Rectangle focusRect = Rectangle.Inflate(contentBounds, -FocusPadding, -FocusPadding); - - // Create dotted pen with appropriate color - Color focusColor = isDefault - ? DefaultColors.AcceptFocusIndicatorBackColor - : DefaultColors.FocusIndicatorBackColor; - - // See GDI+ best practices: pens with custom DashStyle must not use cached pens. - using var focusPen = new Pen(focusColor) - { - DashStyle = DashStyle.Dot - }; - - // Draw the focus rectangle with rounded corners - using GraphicsPath focusPath = CreateRoundedRectanglePath(focusRect, FocusCornerRadius); - graphics.DrawPath(focusPen, focusPath); - } - - /// - /// Gets the text color appropriate for the button state and type. - /// Adjusts color for 3D effect when needed. - /// - public override Color GetTextColor(PushButtonState state, bool isDefault) => - state == PushButtonState.Disabled - ? DefaultColors.DisabledTextColor - : isDefault - ? DefaultColors.AcceptButtonTextColor - : DefaultColors.NormalTextColor; - - /// - /// Gets the background color appropriate for the button state and type. - /// - public override Color GetBackgroundColor(PushButtonState state, bool isDefault) => - isDefault - ? state switch - { - PushButtonState.Normal => DefaultColors.StandardBackColor, - PushButtonState.Hot => DefaultColors.HoverBackColor, - PushButtonState.Pressed => DefaultColors.PressedBackColor, - PushButtonState.Disabled => DefaultColors.DisabledBackColor, - _ => DefaultColors.StandardBackColor - } - : state switch - { - PushButtonState.Normal => DefaultColors.StandardBackColor, - PushButtonState.Hot => DefaultColors.HoverBackColor, - PushButtonState.Pressed => DefaultColors.PressedBackColor, - PushButtonState.Disabled => DefaultColors.DisabledBackColor, - _ => DefaultColors.StandardBackColor - }; - - /// - /// Draws the 3D effect border for the button. - /// - private static void DrawButtonBorder(Graphics graphics, Rectangle bounds, PushButtonState state, bool isDefault) - { - // Save original smoothing mode to restore later - SmoothingMode originalMode = graphics.SmoothingMode; - - try - { - graphics.SmoothingMode = SmoothingMode.AntiAlias; - - // Create a GraphicsPath for the border to ensure consistent alignment - // The path needs to match exactly the same dimensions as the filled background - using GraphicsPath path = CreateRoundedRectanglePath(bounds, ButtonCornerRadius); - - // Popup style has a 3D effect border - Rectangle borderRect = bounds; - - // Use slightly more contrasting border colors for dark mode 3D effect - Color topLeftOuter, bottomRightOuter, topLeftInner, bottomRightInner; - - if (state == PushButtonState.Pressed) - { - // In pressed state, invert the 3D effect: highlight bottom/right, shadow top/left - topLeftOuter = DefaultColors.ShadowColor; // shadow - bottomRightOuter = DefaultColors.HighlightColor; // highlight - topLeftInner = DefaultColors.ShadowDarkColor; // deeper shadow - bottomRightInner = DefaultColors.HighlightBrightColor; // brighter highlight - } - else if (state == PushButtonState.Disabled) - { - // Disabled: subtle, low-contrast border - topLeftOuter = DefaultColors.DisabledBorderLightColor; - bottomRightOuter = DefaultColors.DisabledBorderDarkColor; - topLeftInner = DefaultColors.DisabledBorderMidColor; - bottomRightInner = DefaultColors.DisabledBorderMidColor; - } - else - { - // Normal/hot: highlight top/left, shadow bottom/right - topLeftOuter = DefaultColors.HighlightColor; // highlight - bottomRightOuter = DefaultColors.ShadowColor; // shadow - topLeftInner = DefaultColors.HighlightBrightColor; // brighter highlight - bottomRightInner = DefaultColors.ShadowDarkColor; // deeper shadow - } - - // Custom pen needed for PenAlignment.Inset - can't use cached version - // See GDI+ best practices: pens with custom alignment must not use cached pens. - using (var topLeftOuterPen = new Pen(topLeftOuter) { Alignment = PenAlignment.Inset }) - using (var bottomRightOuterPen = new Pen(bottomRightOuter) { Alignment = PenAlignment.Inset }) - { - // Draw the outer 3D border lines - // Top - graphics.DrawLine(topLeftOuterPen, borderRect.Left, borderRect.Top, borderRect.Right - 1, borderRect.Top); - // Left - graphics.DrawLine(topLeftOuterPen, borderRect.Left, borderRect.Top, borderRect.Left, borderRect.Bottom - 1); - // Bottom - graphics.DrawLine(bottomRightOuterPen, borderRect.Left, borderRect.Bottom - 1, borderRect.Right - 1, borderRect.Bottom - 1); - // Right - graphics.DrawLine(bottomRightOuterPen, borderRect.Right - 1, borderRect.Top, borderRect.Right - 1, borderRect.Bottom - 1); - } - - // Inner border for more depth - borderRect.Inflate(-BorderThickness, -BorderThickness); - - using (var topLeftInnerPen = new Pen(topLeftInner) { Alignment = PenAlignment.Inset }) - using (var bottomRightInnerPen = new Pen(bottomRightInner) { Alignment = PenAlignment.Inset }) - { - // Draw the inner 3D border lines - // Top - graphics.DrawLine(topLeftInnerPen, borderRect.Left, borderRect.Top, borderRect.Right - 1, borderRect.Top); - // Left - graphics.DrawLine(topLeftInnerPen, borderRect.Left, borderRect.Top, borderRect.Left, borderRect.Bottom - 1); - // Bottom - graphics.DrawLine(bottomRightInnerPen, borderRect.Left, borderRect.Bottom - 1, borderRect.Right - 1, borderRect.Bottom - 1); - // Right - graphics.DrawLine(bottomRightInnerPen, borderRect.Right - 1, borderRect.Top, borderRect.Right - 1, borderRect.Bottom - 1); - } - - // For default buttons, add an additional inner border - if (isDefault && state != PushButtonState.Disabled) - { - borderRect.Inflate(-BorderThickness, -BorderThickness); - Color innerBorderColor = Color.FromArgb( - Math.Max(0, DefaultColors.StandardBackColor.R - DefaultBorderROffset), - Math.Max(0, DefaultColors.StandardBackColor.G - DefaultBorderGOffset), - Math.Max(0, DefaultColors.StandardBackColor.B - DefaultBorderBOffset)); - - // Custom pen needed for PenAlignment.Inset - can't use cached version - using var defaultInnerBorderPen = new Pen(innerBorderColor) { Alignment = PenAlignment.Inset }; - graphics.DrawRectangle(defaultInnerBorderPen, borderRect); - } - } - finally - { - // Restore original smoothing mode - graphics.SmoothingMode = originalMode; - } - } - - /// - /// Creates a GraphicsPath for a rounded rectangle. - /// - private static GraphicsPath CreateRoundedRectanglePath(Rectangle bounds, int radius) - { - GraphicsPath path = new(); - path.AddRoundedRectangle(bounds, new Size(radius, radius)); - - return path; - } -} diff --git a/src/System.Windows.Forms/System/Windows/Forms/Controls/Buttons/ButtonInternal/DarkMode/SystemButtonDarkModeRenderer.cs b/src/System.Windows.Forms/System/Windows/Forms/Controls/Buttons/ButtonInternal/DarkMode/SystemButtonDarkModeRenderer.cs index 706ccf777cd..774ad412f63 100644 --- a/src/System.Windows.Forms/System/Windows/Forms/Controls/Buttons/ButtonInternal/DarkMode/SystemButtonDarkModeRenderer.cs +++ b/src/System.Windows.Forms/System/Windows/Forms/Controls/Buttons/ButtonInternal/DarkMode/SystemButtonDarkModeRenderer.cs @@ -27,22 +27,39 @@ internal class SystemButtonDarkModeRenderer : ButtonDarkModeRendererBase private protected override Padding PaddingCore { get; } = new Padding(SystemStylePadding); + private protected override bool PaintParentBackground => true; + /// /// Draws button background with system styling (larger rounded corners). /// - public override Rectangle DrawButtonBackground(Graphics graphics, Rectangle bounds, PushButtonState state, bool isDefault, Color backColor) + public override Rectangle DrawButtonBackground( + Graphics graphics, + Rectangle bounds, + PushButtonState state, + bool isDefault, + bool focused, + Color backColor) { - // Shrink for DarkBorderGap and FocusBorderThickness - Rectangle fillBounds = Rectangle.Inflate(bounds, -SystemStylePadding, -SystemStylePadding); - - using GraphicsPath fillPath = CreateRoundedRectanglePath(fillBounds, CornerRadius - DarkBorderGapThickness); + GraphicsState? saved = graphics.Save(); + try + { + graphics.SmoothingMode = SmoothingMode.AntiAlias; - // Fill the background using cached brush - using var brush = backColor.GetCachedSolidBrushScope(); - graphics.FillPath(brush, fillPath); + RectangleF pathBounds = GetPathBounds(bounds); + using GraphicsPath fillPath = CreateRoundedPath(pathBounds, FocusIndicatorCornerRadius); + using var brush = backColor.GetCachedSolidBrushScope(); + graphics.FillPath(brush, fillPath); + } + finally + { + if (saved is not null) + { + graphics.Restore(saved); + } + } // Return content bounds (area inside the button for text/image) - return fillBounds; + return bounds; } /// @@ -50,26 +67,34 @@ public override Rectangle DrawButtonBackground(Graphics graphics, Rectangle boun /// public override void DrawFocusIndicator(Graphics graphics, Rectangle contentBounds, bool isDefault) { - // We need the bottom and the right border one pixel inside the button - Rectangle focusRect = new( - x: contentBounds.X, - y: contentBounds.Y, - width: contentBounds.Width - 1, - height: contentBounds.Height - 1); - - // Create path for the focus outline - using GraphicsPath focusPath = CreateRoundedRectanglePath(focusRect, FocusIndicatorCornerRadius); - - // System style uses a solid white border instead of dotted lines - using var focusPen = Color.White.GetCachedPenScope(FocusedButtonBorderThickness); + GraphicsState? saved = graphics.Save(); + try + { + graphics.SmoothingMode = SmoothingMode.AntiAlias; - graphics.DrawPath(focusPen, focusPath); + RectangleF outerBounds = GetPathBounds(contentBounds); + float outerRadius = FocusIndicatorCornerRadius + (2 * SystemStylePadding); + Color focusColor = ResolveBorderColor(Color.White); + using var focusBrush = focusColor.GetCachedSolidBrushScope(); + using GraphicsPath focusPath = CreateRingPath( + outerBounds, + outerRadius, + FocusedButtonBorderThickness); + graphics.FillPath(focusBrush, focusPath); + } + finally + { + if (saved is not null) + { + graphics.Restore(saved); + } + } } /// /// Gets the text color appropriate for the button state and type. /// - public override Color GetTextColor(PushButtonState state, bool isDefault) => + public override Color GetTextColor(PushButtonState state, bool isDefault, Color backColor) => state == PushButtonState.Disabled ? DefaultColors.DisabledTextColor : isDefault @@ -133,7 +158,9 @@ public void DrawButtonBorder( // Outer border path Rectangle borderRect = Rectangle.Inflate(bounds, -SystemStylePadding, -SystemStylePadding); - using GraphicsPath borderPath = CreateRoundedRectanglePath(borderRect, CornerRadius); + using GraphicsPath borderPath = CreateRoundedPath( + GetPathBounds(borderRect), + CornerRadius); // We need to implement a subtle 3d effect around the already // painted filling. We do this by drawing a border with a 1px pen, @@ -268,15 +295,39 @@ private static GraphicsPath GetBottomRightSegmentPath(Rectangle bounds, int radi return path; } - /// - /// Creates a GraphicsPath for a rounded rectangle. - /// - private static GraphicsPath CreateRoundedRectanglePath(Rectangle bounds, int radius) + private static RectangleF GetPathBounds(Rectangle bounds) + => new( + bounds.X, + bounds.Y, + Math.Max(1, bounds.Width - 1), + Math.Max(1, bounds.Height - 1)); + + private static RectangleF Inset(RectangleF bounds, float inset) + => new( + bounds.X + inset, + bounds.Y + inset, + Math.Max(1, bounds.Width - (2 * inset)), + Math.Max(1, bounds.Height - (2 * inset))); + + private static GraphicsPath CreateRoundedPath(RectangleF bounds, float radius) { GraphicsPath path = new(); + float clampedRadius = Math.Clamp(radius, 1, Math.Min(bounds.Width, bounds.Height)); + path.AddRoundedRectangle(bounds, new SizeF(clampedRadius, clampedRadius)); + return path; + } - path.AddRoundedRectangle(bounds, new Size(radius, radius)); - + private static GraphicsPath CreateRingPath( + RectangleF outerBounds, + float outerRadius, + float thickness) + { + GraphicsPath path = CreateRoundedPath(outerBounds, outerRadius); + RectangleF innerBounds = Inset(outerBounds, thickness); + float innerRadius = Math.Max(1, outerRadius - (2 * thickness)); + using GraphicsPath innerPath = CreateRoundedPath(innerBounds, innerRadius); + path.FillMode = FillMode.Alternate; + path.AddPath(innerPath, connect: false); return path; } } diff --git a/src/System.Windows.Forms/System/Windows/Forms/Controls/Buttons/ButtonInternal/RadioButtonFlatAdapter.cs b/src/System.Windows.Forms/System/Windows/Forms/Controls/Buttons/ButtonInternal/RadioButtonFlatAdapter.cs index 9986c57642c..02aff0a81c9 100644 --- a/src/System.Windows.Forms/System/Windows/Forms/Controls/Buttons/ButtonInternal/RadioButtonFlatAdapter.cs +++ b/src/System.Windows.Forms/System/Windows/Forms/Controls/Buttons/ButtonInternal/RadioButtonFlatAdapter.cs @@ -13,7 +13,7 @@ internal override void PaintDown(PaintEventArgs e, CheckState state) { if (Control.Appearance == Appearance.Button) { - ButtonFlatAdapter adapter = new(Control); + ButtonBaseAdapter adapter = DarkModeAdapterFactory.CreateFlatAdapter(Control); adapter.PaintDown(e, Control.Checked ? CheckState.Checked : CheckState.Unchecked); return; } @@ -33,7 +33,7 @@ internal override void PaintOver(PaintEventArgs e, CheckState state) { if (Control.Appearance == Appearance.Button) { - ButtonFlatAdapter adapter = new(Control); + ButtonBaseAdapter adapter = DarkModeAdapterFactory.CreateFlatAdapter(Control); adapter.PaintOver(e, Control.Checked ? CheckState.Checked : CheckState.Unchecked); return; } @@ -53,7 +53,7 @@ internal override void PaintUp(PaintEventArgs e, CheckState state) { if (Control.Appearance == Appearance.Button) { - ButtonFlatAdapter adapter = new(Control); + ButtonBaseAdapter adapter = DarkModeAdapterFactory.CreateFlatAdapter(Control); adapter.PaintUp(e, Control.Checked ? CheckState.Checked : CheckState.Unchecked); return; } @@ -80,7 +80,7 @@ private void PaintFlatWorker(PaintEventArgs e, Color checkColor, Color checkBack PaintField(e, layout, colors, checkColor, drawFocus: true); } - protected override ButtonBaseAdapter CreateButtonAdapter() => new ButtonFlatAdapter(Control); + protected override ButtonBaseAdapter CreateButtonAdapter() => DarkModeAdapterFactory.CreateFlatAdapter(Control); // RadioButtonPopupLayout also uses this layout for down and over protected override LayoutOptions Layout(PaintEventArgs e) diff --git a/src/System.Windows.Forms/System/Windows/Forms/Controls/Buttons/ButtonInternal/RadioButtonModernAdapter.cs b/src/System.Windows.Forms/System/Windows/Forms/Controls/Buttons/ButtonInternal/RadioButtonModernAdapter.cs new file mode 100644 index 00000000000..ed924e23d35 --- /dev/null +++ b/src/System.Windows.Forms/System/Windows/Forms/Controls/Buttons/ButtonInternal/RadioButtonModernAdapter.cs @@ -0,0 +1,108 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. + +using System.Drawing; + +namespace System.Windows.Forms.ButtonInternal; + +/// +/// Paints a normal-appearance with the modern animated glyph renderer. +/// +internal sealed class RadioButtonModernAdapter : RadioButtonBaseAdapter +{ + private readonly FlatStyle _flatStyle; + + internal RadioButtonModernAdapter(RadioButton control, FlatStyle flatStyle) : base(control) + { + _flatStyle = flatStyle; + } + + internal override void PaintUp(PaintEventArgs e, CheckState state) + { + if (Control.Appearance == Appearance.Button) + { + ButtonAdapter.PaintUp(e, Control.Checked ? CheckState.Checked : CheckState.Unchecked); + return; + } + + PaintCore(e); + } + + internal override void PaintDown(PaintEventArgs e, CheckState state) + { + if (Control.Appearance == Appearance.Button) + { + ButtonAdapter.PaintDown(e, Control.Checked ? CheckState.Checked : CheckState.Unchecked); + return; + } + + PaintCore(e); + } + + internal override void PaintOver(PaintEventArgs e, CheckState state) + { + if (Control.Appearance == Appearance.Button) + { + ButtonAdapter.PaintOver(e, Control.Checked ? CheckState.Checked : CheckState.Unchecked); + return; + } + + PaintCore(e); + } + + protected override ButtonBaseAdapter CreateButtonAdapter() + => _flatStyle switch + { + FlatStyle.Flat => DarkModeAdapterFactory.CreateFlatAdapter(Control), + FlatStyle.Popup => DarkModeAdapterFactory.CreatePopupAdapter(Control), + _ => DarkModeAdapterFactory.CreateStandardAdapter(Control) + }; + + protected override LayoutOptions Layout(PaintEventArgs e) + { + LayoutOptions layout = CommonLayout(); + layout.CheckSize = Math.Max( + Control.LogicalToDeviceUnits(13), + (int)(Control.Font.Height * 0.9f)); + + return layout; + } + + private void PaintCore(PaintEventArgs e) + { + Graphics graphics = e.GraphicsInternal; + graphics.Clear(Control.Parent?.BackColor ?? Control.BackColor); + + LayoutData layout = Layout(e).Layout(); + AdjustFocusRectangle(layout); + + Color? customOnColor = Control.ShouldSerializeBackColor() + ? Control.BackColor + : null; + + Color? customBorderColor = Control.FlatAppearance.BorderColor.IsEmpty + ? null + : Control.FlatAppearance.BorderColor; + + Control.RadioGlyphRenderer.NotifyCheckedChanged(Control.Checked); + Control.RadioGlyphRenderer.DrawGlyph( + graphics, + layout.CheckBounds, + _flatStyle, + Control.Enabled, + Control.MouseIsOver, + Control.Focused && Control.ShowFocusCues, + customOnColor, + customBorderColor); + + PaintImage(e, layout); + + Color textColor = Control.ShouldSerializeForeColor() + ? Control.ForeColor + : Application.IsDarkModeEnabled + ? Color.FromArgb(0xF0, 0xF0, 0xF0) + : SystemColors.WindowText; + + PaintField(e, layout, PaintRender(e).Calculate(), textColor, drawFocus: true); + } +} diff --git a/src/System.Windows.Forms/System/Windows/Forms/Controls/Buttons/ButtonInternal/RadioButtonPopupAdapter.cs b/src/System.Windows.Forms/System/Windows/Forms/Controls/Buttons/ButtonInternal/RadioButtonPopupAdapter.cs index bf3aa38a2fd..e59c0b91d9a 100644 --- a/src/System.Windows.Forms/System/Windows/Forms/Controls/Buttons/ButtonInternal/RadioButtonPopupAdapter.cs +++ b/src/System.Windows.Forms/System/Windows/Forms/Controls/Buttons/ButtonInternal/RadioButtonPopupAdapter.cs @@ -11,7 +11,7 @@ internal override void PaintUp(PaintEventArgs e, CheckState state) { if (Control.Appearance == Appearance.Button) { - ButtonPopupAdapter adapter = new(Control); + ButtonBaseAdapter adapter = DarkModeAdapterFactory.CreatePopupAdapter(Control); adapter.PaintUp(e, Control.Checked ? CheckState.Checked : CheckState.Unchecked); } else @@ -40,7 +40,7 @@ internal override void PaintOver(PaintEventArgs e, CheckState state) { if (Control.Appearance == Appearance.Button) { - ButtonPopupAdapter adapter = new(Control); + ButtonBaseAdapter adapter = DarkModeAdapterFactory.CreatePopupAdapter(Control); adapter.PaintOver(e, Control.Checked ? CheckState.Checked : CheckState.Unchecked); } else @@ -65,7 +65,7 @@ internal override void PaintDown(PaintEventArgs e, CheckState state) { if (Control.Appearance == Appearance.Button) { - ButtonPopupAdapter adapter = new(Control); + ButtonBaseAdapter adapter = DarkModeAdapterFactory.CreatePopupAdapter(Control); adapter.PaintDown(e, Control.Checked ? CheckState.Checked : CheckState.Unchecked); } else @@ -85,7 +85,7 @@ internal override void PaintDown(PaintEventArgs e, CheckState state) } } - protected override ButtonBaseAdapter CreateButtonAdapter() => new ButtonPopupAdapter(Control); + protected override ButtonBaseAdapter CreateButtonAdapter() => DarkModeAdapterFactory.CreatePopupAdapter(Control); protected override LayoutOptions Layout(PaintEventArgs e) { diff --git a/src/System.Windows.Forms/System/Windows/Forms/Controls/Buttons/ButtonInternal/RadioButtonStandardAdapter.cs b/src/System.Windows.Forms/System/Windows/Forms/Controls/Buttons/ButtonInternal/RadioButtonStandardAdapter.cs index 0181286c263..7546f46c31a 100644 --- a/src/System.Windows.Forms/System/Windows/Forms/Controls/Buttons/ButtonInternal/RadioButtonStandardAdapter.cs +++ b/src/System.Windows.Forms/System/Windows/Forms/Controls/Buttons/ButtonInternal/RadioButtonStandardAdapter.cs @@ -50,9 +50,7 @@ internal override void PaintOver(PaintEventArgs e, CheckState state) } } - private new ButtonStandardAdapter ButtonAdapter => (ButtonStandardAdapter)base.ButtonAdapter; - - protected override ButtonBaseAdapter CreateButtonAdapter() => new ButtonStandardAdapter(Control); + protected override ButtonBaseAdapter CreateButtonAdapter() => DarkModeAdapterFactory.CreateStandardAdapter(Control); protected override LayoutOptions Layout(PaintEventArgs e) { diff --git a/src/System.Windows.Forms/System/Windows/Forms/Controls/Buttons/CheckBox.cs b/src/System.Windows.Forms/System/Windows/Forms/Controls/Buttons/CheckBox.cs index 57526b2644b..58aeb5dfd18 100644 --- a/src/System.Windows.Forms/System/Windows/Forms/Controls/Buttons/CheckBox.cs +++ b/src/System.Windows.Forms/System/Windows/Forms/Controls/Buttons/CheckBox.cs @@ -28,6 +28,10 @@ public partial class CheckBox : ButtonBase private ContentAlignment _checkAlign = ContentAlignment.MiddleLeft; private CheckState _checkState; private Appearance _appearance; + private bool _threeState; + + private Rendering.CheckBox.AnimatedCheckGlyphRenderer? _checkGlyphRenderer; + private Rendering.CheckBox.AnimatedToggleSwitchRenderer? _toggleSwitchRenderer; private int _flatSystemStylePaddingWidth; private int _flatSystemStyleMinimumHeight; @@ -75,11 +79,13 @@ public Appearance Appearance using (LayoutTransaction.CreateTransactionIf(AutoSize, ParentInternal, this, PropertyNames.Appearance)) { _appearance = value; + ResetAdapter(); // UpdateOwnerDraw synchronizes control styles with the OwnerDraw state and recreates // the handle if they differ. Since we hijack FlatStyle.Standard for DarkMode, the transition // between Normal and Button appearance is critical for updating the OwnerDraw flag. UpdateOwnerDraw(); + UpdateToggleSwitchStyles(); // If handle wasn't recreated (OwnerDraw state didn't change), refresh the appearance. if (OwnerDraw) @@ -97,16 +103,52 @@ public Appearance Appearance } private protected override bool OwnerDraw => + // The modern toggle switch is always owner-drawn (so UserPaint is enabled for it). + IsToggleSwitchAppearance + || // We want NO owner draw ONLY when we're // * In Dark Mode // * When _then_ the Appearance is Button // * But then ONLY when we're rendering with FlatStyle.Standard // (because that would let us usually let us draw with the VisualStyleRenderers, // which cause HighDPI issues in Dark Mode). - (!Application.IsDarkModeEnabled + ((!Application.IsDarkModeEnabled || Appearance != Appearance.Button || FlatStyle != FlatStyle.Standard) - && base.OwnerDraw; + && base.OwnerDraw); + + /// + /// Gets a value indicating whether the check box should render as the modern, animated toggle switch. + /// + private bool IsToggleSwitchAppearance + { + get + { + return Appearance == Appearance.ToggleSwitch + && EffectiveVisualStylesMode >= VisualStylesMode.Net11 + && !ThreeState; + } + } + + private Rendering.CheckBox.AnimatedToggleSwitchRenderer ToggleSwitchRenderer => + _toggleSwitchRenderer ??= new(this, Rendering.CheckBox.ModernCheckBoxStyle.Rounded); + + internal Rendering.CheckBox.AnimatedCheckGlyphRenderer CheckGlyphRenderer + => _checkGlyphRenderer ??= new(this); + + private void UpdateToggleSwitchStyles() + { + if (IsToggleSwitchAppearance) + { + // Owner-paint with WinForms double buffering for a flicker-free, fluent animation. + SetStyle(ControlStyles.OptimizedDoubleBuffer | ControlStyles.ResizeRedraw, true); + _toggleSwitchRenderer?.SynchronizeState(); + } + else + { + _toggleSwitchRenderer?.StopAnimation(); + } + } [SRCategory(nameof(SR.CatPropertyChanged))] [SRDescription(nameof(SR.CheckBoxOnAppearanceChangedDescr))] @@ -199,6 +241,14 @@ public CheckState CheckState return; } + // For the animated toggle switch, stop any in-flight animation (leaving the thumb at its current + // position) before the state changes, then start a fresh animation toward the new position. + bool animateToggleSwitch = IsToggleSwitchAppearance && IsHandleCreated; + if (animateToggleSwitch) + { + ToggleSwitchRenderer.PrepareStateChange(); + } + bool oldChecked = Checked; _checkState = value; @@ -218,6 +268,11 @@ public CheckState CheckState _notifyAccessibilityStateChangedNeeded = !checkedChanged; OnCheckStateChanged(EventArgs.Empty); _notifyAccessibilityStateChangedNeeded = false; + + if (animateToggleSwitch) + { + ToggleSwitchRenderer.StartAnimation(); + } } } @@ -288,8 +343,29 @@ private void ScaleConstants() internal override Size GetPreferredSizeCore(Size proposedConstraints) { + if (IsToggleSwitchAppearance) + { + return Rendering.CheckBox.ToggleSwitchMetrics.Create(this).GetPreferredSize(this); + } + + if (Appearance == Appearance.Button && FlatStyle == FlatStyle.Popup) + { + return DarkModeAdapterFactory.CreatePopupAdapter(this).GetPreferredSizeCore(proposedConstraints) + + Padding.Size; + } + if (Appearance == Appearance.Button) { + if (EffectiveVisualStylesModeInternal >= VisualStylesMode.Net11 + && FlatStyle is FlatStyle.Standard or FlatStyle.Flat) + { + ButtonBaseAdapter modernAdapter = FlatStyle == FlatStyle.Flat + ? DarkModeAdapterFactory.CreateFlatAdapter(this) + : DarkModeAdapterFactory.CreateStandardAdapter(this); + + return modernAdapter.GetPreferredSizeCore(proposedConstraints) + Padding.Size; + } + ButtonStandardAdapter adapter = new(this); return adapter.GetPreferredSizeCore(proposedConstraints); } @@ -365,7 +441,29 @@ public override ContentAlignment TextAlign [DefaultValue(false)] [SRCategory(nameof(SR.CatBehavior))] [SRDescription(nameof(SR.CheckBoxThreeStateDescr))] - public bool ThreeState { get; set; } + public bool ThreeState + { + get => _threeState; + set + { + if (_threeState == value) + { + return; + } + + _toggleSwitchRenderer?.StopAnimation(); + _threeState = value; + ResetAdapter(); + UpdateOwnerDraw(); + UpdateToggleSwitchStyles(); + LayoutTransaction.DoLayoutIf(AutoSize, ParentInternal, this, nameof(ThreeState)); + + if (IsHandleCreated) + { + Invalidate(); + } + } + } /// /// Occurs when the value of the property changes. @@ -497,6 +595,64 @@ protected override void OnHandleCreated(EventArgs e) { PInvokeCore.SendMessage(this, PInvoke.BM_SETCHECK, (WPARAM)(int)_checkState); } + + UpdateToggleSwitchStyles(); + } + + /// + protected override void OnPaint(PaintEventArgs pevent) + { + if (IsToggleSwitchAppearance) + { + using GraphicsStateScope scope = new(pevent.Graphics); + ToggleSwitchRenderer.RenderControl(pevent.Graphics); + return; + } + + base.OnPaint(pevent); + } + + /// + protected override void OnVisualStylesModeChanged(EventArgs e) + { + base.OnVisualStylesModeChanged(e); + + // Entering or leaving the modern toggle-switch appearance changes whether the control is owner-drawn. + UpdateOwnerDraw(); + UpdateToggleSwitchStyles(); + + if (IsHandleCreated) + { + Invalidate(); + } + } + + /// + protected override void OnSystemColorsChanged(EventArgs e) + { + base.OnSystemColorsChanged(e); + _checkGlyphRenderer?.InvalidateAccentColor(); + _toggleSwitchRenderer?.InvalidateAccentColor(); + UpdateOwnerDraw(); + UpdateToggleSwitchStyles(); + } + + internal ContentAlignment RtlTranslatedCheckAlign + => RtlTranslateContent(CheckAlign); + + internal bool ShowFocusCuesInternal => ShowFocusCues; + + protected override void Dispose(bool disposing) + { + if (disposing) + { + _checkGlyphRenderer?.Dispose(); + _checkGlyphRenderer = null; + _toggleSwitchRenderer?.Dispose(); + _toggleSwitchRenderer = null; + } + + base.Dispose(disposing); } /// @@ -526,11 +682,24 @@ protected override void OnMouseUp(MouseEventArgs mevent) base.OnMouseUp(mevent); } - internal override ButtonBaseAdapter CreateFlatAdapter() => new CheckBoxFlatAdapter(this); + internal override ButtonBaseAdapter CreateFlatAdapter() + => UseModernGlyphRenderer + ? new CheckBoxModernAdapter(this, FlatStyle.Flat) + : new CheckBoxFlatAdapter(this); + + internal override ButtonBaseAdapter CreatePopupAdapter() + => UseModernGlyphRenderer + ? new CheckBoxModernAdapter(this, FlatStyle.Popup) + : new CheckBoxPopupAdapter(this); - internal override ButtonBaseAdapter CreatePopupAdapter() => new CheckBoxPopupAdapter(this); + internal override ButtonBaseAdapter CreateStandardAdapter() + => UseModernGlyphRenderer + ? new CheckBoxModernAdapter(this, FlatStyle.Standard) + : new CheckBoxStandardAdapter(this); - internal override ButtonBaseAdapter CreateStandardAdapter() => new CheckBoxStandardAdapter(this); + private bool UseModernGlyphRenderer + => Appearance == Appearance.Normal + && EffectiveVisualStylesMode >= VisualStylesMode.Net11; /// /// Overridden to handle mnemonics properly. diff --git a/src/System.Windows.Forms/System/Windows/Forms/Controls/Buttons/FlatButtonAppearance.cs b/src/System.Windows.Forms/System/Windows/Forms/Controls/Buttons/FlatButtonAppearance.cs index 2377fbf24f9..adbd5c5e3f7 100644 --- a/src/System.Windows.Forms/System/Windows/Forms/Controls/Buttons/FlatButtonAppearance.cs +++ b/src/System.Windows.Forms/System/Windows/Forms/Controls/Buttons/FlatButtonAppearance.cs @@ -114,10 +114,13 @@ public Color CheckedBackColor [SRCategory(nameof(SR.CatAppearance))] [SRDescription(nameof(SR.ButtonMouseDownBackColorDescr))] [EditorBrowsable(EditorBrowsableState.Always)] - [DefaultValue(typeof(Color), "")] public Color MouseDownBackColor { - get => _mouseDownBackColor; + get => _mouseDownBackColor.IsEmpty + && _owner.FlatStyle != FlatStyle.Popup + && _owner.EffectiveVisualStylesModeInternal >= VisualStylesMode.Net11 + ? ModernButtonColorMath.GetMouseDownColor() + : _mouseDownBackColor; set { if (_mouseDownBackColor != value) @@ -138,10 +141,13 @@ public Color MouseDownBackColor [SRCategory(nameof(SR.CatAppearance))] [SRDescription(nameof(SR.ButtonMouseOverBackColorDescr))] [EditorBrowsable(EditorBrowsableState.Always)] - [DefaultValue(typeof(Color), "")] public Color MouseOverBackColor { - get => _mouseOverBackColor; + get => _mouseOverBackColor.IsEmpty + && _owner.FlatStyle != FlatStyle.Popup + && _owner.EffectiveVisualStylesModeInternal >= VisualStylesMode.Net11 + ? ModernButtonColorMath.GetMouseOverColor(_owner, this) + : _mouseOverBackColor; set { if (_mouseOverBackColor != value) @@ -151,4 +157,16 @@ public Color MouseOverBackColor } } } + + internal Color MouseDownBackColorCore => _mouseDownBackColor; + + internal Color MouseOverBackColorCore => _mouseOverBackColor; + + private void ResetMouseDownBackColor() => MouseDownBackColor = Color.Empty; + + private bool ShouldSerializeMouseDownBackColor() => !_mouseDownBackColor.IsEmpty; + + private void ResetMouseOverBackColor() => MouseOverBackColor = Color.Empty; + + private bool ShouldSerializeMouseOverBackColor() => !_mouseOverBackColor.IsEmpty; } diff --git a/src/System.Windows.Forms/System/Windows/Forms/Controls/Buttons/RadioButton.cs b/src/System.Windows.Forms/System/Windows/Forms/Controls/Buttons/RadioButton.cs index a6674f1ea69..bf101c70025 100644 --- a/src/System.Windows.Forms/System/Windows/Forms/Controls/Buttons/RadioButton.cs +++ b/src/System.Windows.Forms/System/Windows/Forms/Controls/Buttons/RadioButton.cs @@ -32,6 +32,8 @@ public partial class RadioButton : ButtonBase private bool _autoCheck = true; private ContentAlignment _checkAlign = ContentAlignment.MiddleLeft; private Appearance _appearance = Appearance.Normal; + private Rendering.RadioButton.AnimatedRadioGlyphRenderer? _radioGlyphRenderer; + private Rendering.CheckBox.AnimatedToggleSwitchRenderer? _toggleSwitchRenderer; private int _flatSystemStylePaddingWidth; private int _flatSystemStyleMinimumHeight; @@ -89,9 +91,11 @@ public Appearance Appearance using (LayoutTransaction.CreateTransactionIf(AutoSize, ParentInternal, this, PropertyNames.Appearance)) { _appearance = value; + ResetAdapter(); // UpdateOwnerDraw checks if OwnerDraw state changed and calls RecreateHandle if needed. UpdateOwnerDraw(); + UpdateToggleSwitchStyles(); // If handle wasn't recreated (OwnerDraw state didn't change), refresh the appearance. if (OwnerDraw) @@ -158,6 +162,12 @@ public bool Checked { if (_isChecked != value) { + bool animateToggleSwitch = IsToggleSwitchAppearance && IsHandleCreated; + if (animateToggleSwitch) + { + ToggleSwitchRenderer.PrepareStateChange(); + } + _isChecked = value; if (IsHandleCreated) @@ -169,6 +179,11 @@ public bool Checked Update(); PerformAutoUpdates(tabbedInto: false); OnCheckedChanged(EventArgs.Empty); + + if (animateToggleSwitch) + { + ToggleSwitchRenderer.StartAnimation(); + } } } } @@ -181,10 +196,15 @@ public bool Checked // * but then ONLY when we're rendering with FlatStyle.Standard // (because that would let us usually let us draw with the VisualStyleRenderers, // which cause HighDPI issues in Dark Mode). - (!Application.IsDarkModeEnabled - || Appearance != Appearance.Button - || FlatStyle != FlatStyle.Standard) - && base.OwnerDraw; + IsToggleSwitchAppearance + || ((!Application.IsDarkModeEnabled + || Appearance != Appearance.Button + || FlatStyle != FlatStyle.Standard) + && base.OwnerDraw); + + private bool IsToggleSwitchAppearance + => Appearance == Appearance.ToggleSwitch + && EffectiveVisualStylesMode >= VisualStylesMode.Net11; /// [Browsable(false)] @@ -263,6 +283,17 @@ private void ScaleConstants() internal override Size GetPreferredSizeCore(Size proposedConstraints) { + if (IsToggleSwitchAppearance) + { + return Rendering.CheckBox.ToggleSwitchMetrics.Create(this).GetPreferredSize(this); + } + + if (Appearance == Appearance.Button && FlatStyle == FlatStyle.Popup) + { + return DarkModeAdapterFactory.CreatePopupAdapter(this).GetPreferredSizeCore(proposedConstraints) + + Padding.Size; + } + if (FlatStyle != FlatStyle.System) { return base.GetPreferredSizeCore(proposedConstraints); @@ -348,6 +379,44 @@ protected override void OnHandleCreated(EventArgs e) { PInvokeCore.SendMessage(this, PInvoke.BM_SETCHECK, (WPARAM)(BOOL)_isChecked); } + + UpdateToggleSwitchStyles(); + } + + /// + protected override void OnPaint(PaintEventArgs pevent) + { + if (IsToggleSwitchAppearance) + { + using GraphicsStateScope scope = new(pevent.Graphics); + ToggleSwitchRenderer.RenderControl(pevent.Graphics); + return; + } + + base.OnPaint(pevent); + } + + /// + protected override void OnVisualStylesModeChanged(EventArgs e) + { + base.OnVisualStylesModeChanged(e); + UpdateOwnerDraw(); + UpdateToggleSwitchStyles(); + + if (IsHandleCreated) + { + Invalidate(); + } + } + + /// + protected override void OnSystemColorsChanged(EventArgs e) + { + base.OnSystemColorsChanged(e); + _radioGlyphRenderer?.InvalidateAccentColor(); + _toggleSwitchRenderer?.InvalidateAccentColor(); + UpdateOwnerDraw(); + UpdateToggleSwitchStyles(); } /// @@ -467,11 +536,56 @@ private void WipeTabStops(bool tabbedInto) } } - internal override ButtonBaseAdapter CreateFlatAdapter() => new RadioButtonFlatAdapter(this); + internal override ButtonBaseAdapter CreateFlatAdapter() + => UseModernGlyphRenderer + ? new RadioButtonModernAdapter(this, FlatStyle.Flat) + : new RadioButtonFlatAdapter(this); + + internal override ButtonBaseAdapter CreatePopupAdapter() + => UseModernGlyphRenderer + ? new RadioButtonModernAdapter(this, FlatStyle.Popup) + : new RadioButtonPopupAdapter(this); + + internal override ButtonBaseAdapter CreateStandardAdapter() + => UseModernGlyphRenderer + ? new RadioButtonModernAdapter(this, FlatStyle.Standard) + : new RadioButtonStandardAdapter(this); + + internal Rendering.RadioButton.AnimatedRadioGlyphRenderer RadioGlyphRenderer + => _radioGlyphRenderer ??= new(this); + + internal Rendering.CheckBox.AnimatedToggleSwitchRenderer ToggleSwitchRenderer => + _toggleSwitchRenderer ??= new(this, Rendering.CheckBox.ModernCheckBoxStyle.Rounded); + + private void UpdateToggleSwitchStyles() + { + if (IsToggleSwitchAppearance) + { + SetStyle(ControlStyles.OptimizedDoubleBuffer | ControlStyles.ResizeRedraw, true); + _toggleSwitchRenderer?.SynchronizeState(); + } + else + { + _toggleSwitchRenderer?.StopAnimation(); + } + } + + private bool UseModernGlyphRenderer + => Appearance == Appearance.Normal + && EffectiveVisualStylesMode >= VisualStylesMode.Net11; - internal override ButtonBaseAdapter CreatePopupAdapter() => new RadioButtonPopupAdapter(this); + protected override void Dispose(bool disposing) + { + if (disposing) + { + _radioGlyphRenderer?.Dispose(); + _radioGlyphRenderer = null; + _toggleSwitchRenderer?.Dispose(); + _toggleSwitchRenderer = null; + } - internal override ButtonBaseAdapter CreateStandardAdapter() => new RadioButtonStandardAdapter(this); + base.Dispose(disposing); + } private void OnAppearanceChanged(EventArgs e) { @@ -481,6 +595,11 @@ private void OnAppearanceChanged(EventArgs e) } } + internal ContentAlignment RtlTranslatedCheckAlign + => RtlTranslateContent(CheckAlign); + + internal bool ShowFocusCuesInternal => ShowFocusCues; + protected override void OnMouseUp(MouseEventArgs mevent) { if (mevent.Button == MouseButtons.Left diff --git a/src/System.Windows.Forms/System/Windows/Forms/Controls/ComboBox/ComboBox.SuspendMutation.cs b/src/System.Windows.Forms/System/Windows/Forms/Controls/ComboBox/ComboBox.SuspendMutation.cs new file mode 100644 index 00000000000..26590de34aa --- /dev/null +++ b/src/System.Windows.Forms/System/Windows/Forms/Controls/ComboBox/ComboBox.SuspendMutation.cs @@ -0,0 +1,25 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. + +namespace System.Windows.Forms; + +public partial class ComboBox +{ +#if NET11_0_OR_GREATER + /// + protected override void BeginSuspendPaintingCore() + { + BeginSuspendPaintingScope(); + BeginUpdate(); + } + + /// + protected override void EndSuspendPaintingCore() + { + if (EndSuspendPaintingScope()) + { + EndUpdate(invalidate: !IsRecursiveInvalidateAfterSuspendPaintingRequested); + } + } +#endif +} diff --git a/src/System.Windows.Forms/System/Windows/Forms/Controls/ComboBox/ComboBox.cs b/src/System.Windows.Forms/System/Windows/Forms/Controls/ComboBox/ComboBox.cs index 5e4422eaf9c..947b9575eda 100644 --- a/src/System.Windows.Forms/System/Windows/Forms/Controls/ComboBox/ComboBox.cs +++ b/src/System.Windows.Forms/System/Windows/Forms/Controls/ComboBox/ComboBox.cs @@ -1855,7 +1855,9 @@ protected override void Dispose(bool disposing) /// beginUpdate(), any redrawing caused by operations performed on the /// combo box is deferred until the call to endUpdate(). /// - public unsafe void EndUpdate() + public void EndUpdate() => EndUpdate(invalidate: true); + + private unsafe void EndUpdate(bool invalidate) { _updateCount--; if (_updateCount == 0 && AutoCompleteSource == AutoCompleteSource.ListItems) @@ -1863,7 +1865,7 @@ public unsafe void EndUpdate() SetAutoComplete(false, false); } - if (EndUpdateInternal()) + if (EndUpdateInternal(invalidate) && invalidate) { if (_childEdit is not null && !_childEdit.HWND.IsNull) { diff --git a/src/System.Windows.Forms/System/Windows/Forms/Controls/ListBoxes/ListBox.SuspendMutation.cs b/src/System.Windows.Forms/System/Windows/Forms/Controls/ListBoxes/ListBox.SuspendMutation.cs new file mode 100644 index 00000000000..20964896aa1 --- /dev/null +++ b/src/System.Windows.Forms/System/Windows/Forms/Controls/ListBoxes/ListBox.SuspendMutation.cs @@ -0,0 +1,25 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. + +namespace System.Windows.Forms; + +public partial class ListBox +{ +#if NET11_0_OR_GREATER + /// + protected override void BeginSuspendPaintingCore() + { + BeginSuspendPaintingScope(); + BeginUpdate(); + } + + /// + protected override void EndSuspendPaintingCore() + { + if (EndSuspendPaintingScope()) + { + EndUpdate(invalidate: !IsRecursiveInvalidateAfterSuspendPaintingRequested); + } + } +#endif +} diff --git a/src/System.Windows.Forms/System/Windows/Forms/Controls/ListBoxes/ListBox.cs b/src/System.Windows.Forms/System/Windows/Forms/Controls/ListBoxes/ListBox.cs index ef7c6daab90..bbfa4ad39cc 100644 --- a/src/System.Windows.Forms/System/Windows/Forms/Controls/ListBoxes/ListBox.cs +++ b/src/System.Windows.Forms/System/Windows/Forms/Controls/ListBoxes/ListBox.cs @@ -1350,9 +1350,11 @@ public void ClearSelected() /// way of doing this. BeginUpdate should be called first, and this method /// should be called when you want the control to start painting again. /// - public void EndUpdate() + public void EndUpdate() => EndUpdate(invalidate: true); + + private void EndUpdate(bool invalidate) { - EndUpdateInternal(); + EndUpdateInternal(invalidate); --_updateCount; } diff --git a/src/System.Windows.Forms/System/Windows/Forms/Controls/ListView/ListView.SuspendMutation.cs b/src/System.Windows.Forms/System/Windows/Forms/Controls/ListView/ListView.SuspendMutation.cs new file mode 100644 index 00000000000..2a266c72548 --- /dev/null +++ b/src/System.Windows.Forms/System/Windows/Forms/Controls/ListView/ListView.SuspendMutation.cs @@ -0,0 +1,25 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. + +namespace System.Windows.Forms; + +public partial class ListView +{ +#if NET11_0_OR_GREATER + /// + protected override void BeginSuspendPaintingCore() + { + BeginSuspendPaintingScope(); + BeginUpdate(); + } + + /// + protected override void EndSuspendPaintingCore() + { + if (EndSuspendPaintingScope()) + { + EndUpdate(invalidate: !IsRecursiveInvalidateAfterSuspendPaintingRequested); + } + } +#endif +} diff --git a/src/System.Windows.Forms/System/Windows/Forms/Controls/ListView/ListView.cs b/src/System.Windows.Forms/System/Windows/Forms/Controls/ListView/ListView.cs index f290e8ce8eb..65335aea479 100644 --- a/src/System.Windows.Forms/System/Windows/Forms/Controls/ListView/ListView.cs +++ b/src/System.Windows.Forms/System/Windows/Forms/Controls/ListView/ListView.cs @@ -3158,7 +3158,9 @@ private bool ClearingInnerListOnDispose /// /// Cancels the effect of BeginUpdate. /// - public void EndUpdate() + public void EndUpdate() => EndUpdate(invalidate: true); + + private void EndUpdate(bool invalidate) { // On the final EndUpdate, check to see if we've got any cached items. // If we do, insert them as normal, then turn off the painting freeze. @@ -3167,7 +3169,7 @@ public void EndUpdate() ApplyUpdateCachedItems(); } - EndUpdateInternal(); + EndUpdateInternal(invalidate); } private void EnsureDefaultGroup() diff --git a/src/System.Windows.Forms/System/Windows/Forms/Controls/PropertyGrid/PropertyGrid.cs b/src/System.Windows.Forms/System/Windows/Forms/Controls/PropertyGrid/PropertyGrid.cs index 56110e517ac..c5ad94413eb 100644 --- a/src/System.Windows.Forms/System/Windows/Forms/Controls/PropertyGrid/PropertyGrid.cs +++ b/src/System.Windows.Forms/System/Windows/Forms/Controls/PropertyGrid/PropertyGrid.cs @@ -80,6 +80,10 @@ public partial class PropertyGrid : ContainerControl, IComPropertyBrowser, IProp private Color _selectedItemWithFocusBackColor = SystemColors.Highlight; private bool _canShowVisualStyleGlyphs = true; + // The PropertyGrid pins its effective VisualStylesMode to Classic (see the VisualStylesMode override). + // A value assigned through the setter is remembered here for potential future use only. + private VisualStylesMode _requestedVisualStylesMode = VisualStylesMode.Inherit; + private AttributeCollection? _browsableAttributes; private SnappableControl? _targetMove; @@ -819,6 +823,39 @@ public Color LineColor remove => base.PaddingChanged -= value; } + /// + /// Gets or sets how the renders itself when visual styles are applied. + /// + /// + /// Always . Assigning a different value is remembered but does + /// not change how the grid or its hosted editing controls render. + /// + /// + /// + /// The pins its effective to + /// . The editing controls hosted inside the grid (the value + /// text boxes and drop-down editors) read the ambient from their + /// parent. They must render in the classic style so their frames and glyphs line up with the grid's + /// own fixed layout. Because this override always returns and + /// is a virtual ambient property, those children never switch to a + /// modern renderer, regardless of or the parent + /// form's setting. + /// + /// + /// A value assigned through the setter is stored for potential future use but currently has no + /// effect; the getter continues to report . This keeps the + /// surface forward compatible for a later release in which the grid may honor a modern mode. + /// + /// + [Browsable(false)] + [EditorBrowsable(EditorBrowsableState.Never)] + [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)] + public override VisualStylesMode VisualStylesMode + { + get => VisualStylesMode.Classic; + set => _requestedVisualStylesMode = value; + } + /// /// Sets or gets the current property sort type, which can be /// PropertySort.Categorized or PropertySort.Alphabetical. diff --git a/src/System.Windows.Forms/System/Windows/Forms/Controls/RichTextBox/RichTextBox.SuspendMutation.cs b/src/System.Windows.Forms/System/Windows/Forms/Controls/RichTextBox/RichTextBox.SuspendMutation.cs new file mode 100644 index 00000000000..f5d35fe41e3 --- /dev/null +++ b/src/System.Windows.Forms/System/Windows/Forms/Controls/RichTextBox/RichTextBox.SuspendMutation.cs @@ -0,0 +1,25 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. + +namespace System.Windows.Forms; + +public partial class RichTextBox +{ +#if NET11_0_OR_GREATER + /// + protected override void BeginSuspendPaintingCore() + { + BeginSuspendPaintingScope(); + BeginUpdateInternal(); + } + + /// + protected override void EndSuspendPaintingCore() + { + if (EndSuspendPaintingScope()) + { + EndUpdateInternal(invalidate: !IsRecursiveInvalidateAfterSuspendPaintingRequested); + } + } +#endif +} diff --git a/src/System.Windows.Forms/System/Windows/Forms/Controls/RichTextBox/RichTextBox.cs b/src/System.Windows.Forms/System/Windows/Forms/Controls/RichTextBox/RichTextBox.cs index 4a6c7972a34..3affbca00ed 100644 --- a/src/System.Windows.Forms/System/Windows/Forms/Controls/RichTextBox/RichTextBox.cs +++ b/src/System.Windows.Forms/System/Windows/Forms/Controls/RichTextBox/RichTextBox.cs @@ -309,7 +309,9 @@ protected override CreateParams CreateParams // Remove the WS_BORDER style from the control, if we're trying to set it, // to prevent the control from displaying the single point rectangle around the 3D border - if (BorderStyle == BorderStyle.FixedSingle && ((cp.Style & (int)WINDOW_STYLE.WS_BORDER) != 0)) + if (EffectiveVisualStylesMode < VisualStylesMode.Net11 + && BorderStyle == BorderStyle.FixedSingle + && ((cp.Style & (int)WINDOW_STYLE.WS_BORDER) != 0)) { cp.Style &= ~(int)WINDOW_STYLE.WS_BORDER; cp.ExStyle |= (int)WINDOW_EX_STYLE.WS_EX_CLIENTEDGE; @@ -319,7 +321,34 @@ protected override CreateParams CreateParams } } - // public bool CanUndo {}; <-- inherited from TextBoxBase + /// + /// RichEdit (MSFTEDIT) reserves its own border and scrollbar space during + /// WM_NCCALCSIZE, so the modern Visual Styles padding band is carved on top of the client + /// rectangle the native handler produces. + /// + private protected override bool ReservesNativeNonClientArea => true; + + /// + /// RichEdit reserves the scrollbar space itself while processing WM_NCCALCSIZE (see + /// ). Return the configured reservation for preferred-size + /// measurement; the native client-area calculation explicitly excludes it. + /// + private protected override Padding GetScrollBarPadding() + { + Padding padding = Padding.Empty; + + if (Multiline && !WordWrap && (ScrollBars & RichTextBoxScrollBars.Horizontal) != 0) + { + padding.Bottom = SystemInformation.GetHorizontalScrollBarHeightForDpi(DeviceDpiInternal); + } + + if (Multiline && (ScrollBars & RichTextBoxScrollBars.Vertical) != 0) + { + padding.Right = SystemInformation.GetVerticalScrollBarWidthForDpi(DeviceDpiInternal); + } + + return padding; + } /// /// Controls whether or not the rich edit control will automatically highlight URLs. @@ -425,6 +454,12 @@ public override Font Font internal override Size GetPreferredSizeCore(Size proposedConstraints) { + if (EffectiveVisualStylesMode >= VisualStylesMode.Net11) + { + // TextBoxBase owns the modern inset, user Padding, and scrollbar geometry. + return base.GetPreferredSizeCore(proposedConstraints); + } + Size scrollBarPadding = Size.Empty; // If the RTB is multiline, we won't have a horizontal scrollbar. @@ -645,6 +680,7 @@ public RichTextBoxScrollBars ScrollBars using (LayoutTransaction.CreateTransactionIf(AutoSize, ParentInternal, this, PropertyNames.ScrollBars)) { _richTextBoxFlags[s_scrollBarsSection] = (int)value; + CommonProperties.xClearPreferredSizeCache(this); RecreateHandle(); } } @@ -2246,6 +2282,68 @@ private static bool GetCharInCharSet(char c, char[] charSet, bool negate) public override int GetLineFromCharIndex(int index) => (int)PInvokeCore.SendMessage(this, PInvokeCore.EM_EXLINEFROMCHAR, 0, index); + /// + /// Scrolls the caret into view, showing as much of the surrounding text as possible. This overrides + /// the base edit-control behavior so the RichEdit-specific handling lives with the RichTextBox. + /// + private protected override unsafe void ScrollToCaretCore() + { + using ComScope richEdit = new(null); + + if (PInvokeCore.SendMessage( + hWnd: this, + Msg: PInvokeCore.EM_GETOLEINTERFACE, + wParam: 0, + lParam: (void**)richEdit) == 0) + { + // No OLE interface available; fall back to the plain edit behavior. + base.ScrollToCaretCore(); + return; + } + + using var textDocument = richEdit.TryQuery(out HRESULT hr); + + if (hr.Succeeded) + { + // When the user calls RichTextBox::ScrollToCaret we want the RichTextBox to show as much text as + // possible. Here is how we do that: + // + // 1. We scroll the RichTextBox all the way to the bottom so the last line of text is the last visible line. + // 2. We get the first visible line. + // 3. If the first visible line is smaller than the start of the selection, then we are done: + // The selection fits inside the RichTextBox display rectangle. + // 4. Otherwise, scroll the selection to the top of the RichTextBox. + + GetSelectionStartAndLength(out int selStart, out int selLength); + int selStartLine = GetLineFromCharIndex(selStart); + + using ComScope windowTextRange = new(null); + textDocument.Value->Range(WindowText.Length - 1, WindowText.Length - 1, windowTextRange).ThrowOnFailure(); + + // 1. Scroll the RichTextBox all the way to the bottom + windowTextRange.Value->ScrollIntoView((int)tomConstants.tomEnd).ThrowOnFailure(); + + // 2. Get the first visible line. + int firstVisibleLine = (int)PInvokeCore.SendMessage(this, PInvokeCore.EM_GETFIRSTVISIBLELINE); + + // 3. If the first visible line is smaller than the start of the selection, we are done. + if (firstVisibleLine <= selStartLine) + { + return; + } + else + { + // 4. Scroll the selection to the top of the RichTextBox. + using ComScope selectionTextRange = new(null); + textDocument.Value->Range(selStart, selStart + selLength, selectionTextRange).ThrowOnFailure(); + selectionTextRange.Value->ScrollIntoView((int)tomConstants.tomStart).ThrowOnFailure(); + return; + } + } + + base.ScrollToCaretCore(); + } + /// /// Returns the location of the character at the given index. /// @@ -2500,6 +2598,11 @@ protected override void OnHandleCreated(EventArgs e) SendZoomFactor(_zoomMultiplier); + // RichEdit does not send the WM_CTLCOLOR messages that a plain EDIT control uses to lazily trigger + // the modern Visual Styles client-area carve, so provoke it explicitly once the handle exists. + // This is a no-op unless EffectiveVisualStylesMode is Net11 or above. + RecalculateVisualStylesClientArea(); + SystemEvents.UserPreferenceChanged += UserPreferenceChangedHandler; } diff --git a/src/System.Windows.Forms/System/Windows/Forms/Controls/TextBox/MaskedTextBox.cs b/src/System.Windows.Forms/System/Windows/Forms/Controls/TextBox/MaskedTextBox.cs index d0670ce3e1f..08c4fd56f9a 100644 --- a/src/System.Windows.Forms/System/Windows/Forms/Controls/TextBox/MaskedTextBox.cs +++ b/src/System.Windows.Forms/System/Windows/Forms/Controls/TextBox/MaskedTextBox.cs @@ -2884,7 +2884,9 @@ private void WmPrint(ref Message m) { base.WndProc(ref m); if (((nint)m.LParamInternal & PInvoke.PRF_NONCLIENT) != 0 - && Application.RenderWithVisualStyles && BorderStyle == BorderStyle.Fixed3D) + && Application.RenderWithVisualStyles + && BorderStyle == BorderStyle.Fixed3D + && EffectiveVisualStylesMode < VisualStylesMode.Net11) { using Graphics g = Graphics.FromHdc((HDC)m.WParamInternal); Rectangle rect = new(0, 0, Size.Width - 1, Size.Height - 1); diff --git a/src/System.Windows.Forms/System/Windows/Forms/Controls/TextBox/TextBox.cs b/src/System.Windows.Forms/System/Windows/Forms/Controls/TextBox/TextBox.cs index e3fce96d521..5f29b798482 100644 --- a/src/System.Windows.Forms/System/Windows/Forms/Controls/TextBox/TextBox.cs +++ b/src/System.Windows.Forms/System/Windows/Forms/Controls/TextBox/TextBox.cs @@ -4,6 +4,7 @@ using System.ComponentModel; using System.Drawing; using System.Drawing.Design; +using System.Windows.Forms.Layout; using System.Windows.Forms.VisualStyles; using Windows.Win32.UI.Accessibility; @@ -383,6 +384,8 @@ public ScrollBars ScrollBars _scrollBars = value; RecreateHandle(); + CommonProperties.xClearPreferredSizeCache(this); + LayoutTransaction.DoLayoutIf(AutoSize, ParentInternal, this, PropertyNames.ScrollBars); } } } @@ -391,6 +394,13 @@ public ScrollBars ScrollBars internal override Size GetPreferredSizeCore(Size proposedConstraints) { + if (EffectiveVisualStylesMode >= VisualStylesMode.Net11) + { + // TextBoxBase already includes the native scrollbar reservation in the modern geometry. + // Applying the legacy adjustment here would count each scrollbar twice. + return base.GetPreferredSizeCore(proposedConstraints); + } + Size scrollBarPadding = Size.Empty; if (Multiline && !WordWrap && (ScrollBars & ScrollBars.Horizontal) != 0) @@ -411,6 +421,31 @@ internal override Size GetPreferredSizeCore(Size proposedConstraints) return prefSize + scrollBarPadding; } + private protected override Padding GetScrollBarPadding() + { + if (IsHandleCreated) + { + return base.GetScrollBarPadding(); + } + + Padding padding = Padding.Empty; + + if (Multiline + && !WordWrap + && _textAlign == HorizontalAlignment.Left + && (_scrollBars & ScrollBars.Horizontal) != 0) + { + padding.Bottom = SystemInformation.GetHorizontalScrollBarHeightForDpi(DeviceDpiInternal); + } + + if (Multiline && (_scrollBars & ScrollBars.Vertical) != 0) + { + padding.Right = SystemInformation.GetVerticalScrollBarWidthForDpi(DeviceDpiInternal); + } + + return padding; + } + /// /// Gets or sets the current text in the text box. /// @@ -800,8 +835,12 @@ private void ResetAutoCompleteCustomSource() private void WmPrint(ref Message m) { base.WndProc(ref m); + + // In modern Visual Styles mode the base TextBoxBase NC painting owns the border chrome, so we + // must not also draw the classic Fixed3D edge here - that would double-draw over the modern chrome. if (((nint)m.LParamInternal & PInvoke.PRF_NONCLIENT) != 0 && Application.RenderWithVisualStyles - && BorderStyle == BorderStyle.Fixed3D) + && BorderStyle == BorderStyle.Fixed3D + && EffectiveVisualStylesMode < VisualStylesMode.Net11) { using Graphics g = Graphics.FromHdc((HDC)m.WParamInternal); Rectangle rect = new(0, 0, Size.Width - 1, Size.Height - 1); diff --git a/src/System.Windows.Forms/System/Windows/Forms/Controls/TextBox/TextBoxBase.cs b/src/System.Windows.Forms/System/Windows/Forms/Controls/TextBox/TextBoxBase.cs index 0ec7ce5fdb5..91c9ddaa4bb 100644 --- a/src/System.Windows.Forms/System/Windows/Forms/Controls/TextBox/TextBoxBase.cs +++ b/src/System.Windows.Forms/System/Windows/Forms/Controls/TextBox/TextBoxBase.cs @@ -5,12 +5,13 @@ using System.ComponentModel; using System.Drawing; using System.Drawing.Design; +using System.Drawing.Drawing2D; using System.Runtime.InteropServices; using System.Text; using System.Windows.Forms.Layout; +using System.Windows.Forms.Rendering.Animation; using Windows.Win32.System.Variant; using Windows.Win32.UI.Accessibility; -using Windows.Win32.UI.Controls.RichEdit; namespace System.Windows.Forms; @@ -51,6 +52,26 @@ public abstract partial class TextBoxBase : Control /// The current border for this edit control. /// private BorderStyle _borderStyle = BorderStyle.Fixed3D; + private AnimatedFocusIndicatorRenderer? _focusIndicatorRenderer; + + // Device-independent padding (in logical units) carved from the non-client band for each + // border style when modern Visual Styles chrome is active. These are DPI-scaled at use time. + private const int VisualStylesFixed3DBorderPadding = 2; + private const int VisualStylesFixedSingleBorderPadding = 1; + private const int VisualStylesNoBorderPadding = 1; + internal const int VisualStylesInternalChromeInset = 2; + private const int VisualStylesCornerRadius = 15; + private const int VisualStylesFocusBandHeight = 4; + private const OBJECT_IDENTIFIER HorizontalScrollBarObjectId = (OBJECT_IDENTIFIER)(-6); + private const OBJECT_IDENTIFIER VerticalScrollBarObjectId = (OBJECT_IDENTIFIER)(-5); + private const uint StateSystemInvisible = 0x00008000; + private const int BorderThickness = 1; + + /// + /// One-shot latch that gates the single NC-calc round trip used to carve the modern Visual + /// Styles padding band. Set by and reset on handle recreation. + /// + private bool _triggerNewClientSizeRequest; /// /// Controls the maximum length of text in the edit control. @@ -83,8 +104,7 @@ public abstract partial class TextBoxBase : Control private BitVector32 _textBoxFlags; /// - /// Creates a new TextBox control. Uses the parent's current font and color - /// set. + /// Creates a new TextBox control. Uses the parent's current font and color set. /// internal TextBoxBase() : base() { @@ -116,10 +136,7 @@ internal TextBoxBase() : base() [SRDescription(nameof(SR.TextBoxAcceptsTabDescr))] public bool AcceptsTab { - get - { - return _textBoxFlags[s_acceptsTab]; - } + get => _textBoxFlags[s_acceptsTab]; set { if (_textBoxFlags[s_acceptsTab] != value) @@ -148,10 +165,8 @@ public event EventHandler? AcceptsTabChanged [SRDescription(nameof(SR.TextBoxShortcutsEnabledDescr))] public virtual bool ShortcutsEnabled { - get - { - return _textBoxFlags[s_shortcutsEnabled]; - } + get => _textBoxFlags[s_shortcutsEnabled]; + set { s_shortcutsToDisable ??= @@ -193,6 +208,7 @@ protected override bool ProcessCmdKey(ref Message msg, Keys keyData) if (_textBoxFlags[s_readOnly]) { int k = (int)keyData; + if (k is ((int)Shortcut.CtrlL) // align left or ((int)Shortcut.CtrlR) // align right or ((int)Shortcut.CtrlE) // align center @@ -202,27 +218,29 @@ protected override bool ProcessCmdKey(ref Message msg, Keys keyData) } } - if (!ReadOnly && (keyData == (Keys.Control | Keys.Back) || keyData == (Keys.Control | Keys.Shift | Keys.Back))) + if (ReadOnly + || (keyData != (Keys.Control | Keys.Back) + && keyData != (Keys.Control | Keys.Shift | Keys.Back))) { - if (SelectionLength != 0) - { - SetSelectedTextInternal(string.Empty, clearUndo: false); - } - else if (SelectionStart != 0) - { - int boundaryStart = ClientUtils.GetWordBoundaryStart(Text, SelectionStart); - int length = SelectionStart - boundaryStart; - BeginUpdateInternal(); - SelectionStart = boundaryStart; - SelectionLength = length; - EndUpdateInternal(); - SetSelectedTextInternal(string.Empty, clearUndo: false); - } + return returnedValue; + } - return true; + if (SelectionLength != 0) + { + SetSelectedTextInternal(string.Empty, clearUndo: false); + } + else if (SelectionStart != 0) + { + int boundaryStart = ClientUtils.GetWordBoundaryStart(Text, SelectionStart); + int length = SelectionStart - boundaryStart; + BeginUpdateInternal(); + SelectionStart = boundaryStart; + SelectionLength = length; + EndUpdateInternal(); + SetSelectedTextInternal(string.Empty, clearUndo: false); } - return returnedValue; + return true; } /// @@ -242,10 +260,8 @@ protected override bool ProcessCmdKey(ref Message msg, Keys keyData) [EditorBrowsable(EditorBrowsableState.Never)] public override bool AutoSize { - get - { - return _textBoxFlags[s_autoSize]; - } + get => _textBoxFlags[s_autoSize]; + set { // Note that we intentionally do not call base. TextBoxes size themselves by @@ -356,8 +372,24 @@ public BorderStyle BorderStyle SourceGenerated.EnumValidator.Validate(value); _borderStyle = value; + _focusIndicatorRenderer?.Synchronize(Focused, invalidate: false); + CommonProperties.xClearPreferredSizeCache(this); + + if (EffectiveVisualStylesMode >= VisualStylesMode.Net11) + { + AdjustHeight(false); + } + UpdateStyles(); - RecreateHandle(); + + if (EffectiveVisualStylesMode >= VisualStylesMode.Net11 && IsHandleCreated) + { + RecalculateVisualStylesClientArea(); + } + else + { + RecreateHandle(); + } // PreferredSize depends on BorderStyle : thru CreateParams.ExStyle in User32!AdjustRectEx. // So when the BorderStyle changes let the parent of this control know about it. @@ -377,9 +409,11 @@ public event EventHandler? BorderStyleChanged remove => Events.RemoveHandler(s_borderStyleChangedEvent, value); } - internal virtual bool CanRaiseTextChangedEvent => true; + internal virtual bool CanRaiseTextChangedEvent + => true; - protected override bool CanEnableIme => !(ReadOnly || PasswordProtect) && base.CanEnableIme; + protected override bool CanEnableIme + => !(ReadOnly || PasswordProtect) && base.CanEnableIme; /// /// Gets a value indicating whether the user can undo the previous operation in a text box control. @@ -388,7 +422,9 @@ public event EventHandler? BorderStyleChanged [Browsable(false)] [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)] [SRDescription(nameof(SR.TextBoxCanUndoDescr))] - public bool CanUndo => IsHandleCreated && (int)PInvokeCore.SendMessage(this, PInvokeCore.EM_CANUNDO) != 0; + public bool CanUndo + => IsHandleCreated + && (int)PInvokeCore.SendMessage(this, PInvokeCore.EM_CANUNDO) != 0; /// /// Returns the parameters needed to create the handle. Inheriting classes @@ -405,6 +441,7 @@ protected override CreateParams CreateParams CreateParams cp = base.CreateParams; cp.ClassName = PInvoke.WC_EDIT; cp.Style |= PInvoke.ES_AUTOHSCROLL | PInvoke.ES_AUTOVSCROLL; + if (!_textBoxFlags[s_hideSelection]) { cp.Style |= PInvoke.ES_NOHIDESEL; @@ -423,6 +460,7 @@ protected override CreateParams CreateParams case BorderStyle.Fixed3D: cp.ExStyle |= (int)WINDOW_EX_STYLE.WS_EX_CLIENTEDGE; break; + case BorderStyle.FixedSingle: cp.Style |= (int)WINDOW_STYLE.WS_BORDER; break; @@ -431,12 +469,23 @@ protected override CreateParams CreateParams if (_textBoxFlags[s_multiline]) { cp.Style |= PInvoke.ES_MULTILINE; + if (_textBoxFlags[s_wordWrap]) { cp.Style &= ~PInvoke.ES_AUTOHSCROLL; } } + if (EffectiveVisualStylesMode >= VisualStylesMode.Net11) + { + // Under a modern VisualStylesMode we custom-draw the frame in the non-client area (see OnNcPaint), + // so the natively drawn border must be suppressed to avoid a double frame. Only the effective + // Win32 styles are stripped here; the public BorderStyle property remains authoritative for what + // the custom non-client paint draws. + cp.Style &= ~(int)WINDOW_STYLE.WS_BORDER; + cp.ExStyle &= ~(int)WINDOW_EX_STYLE.WS_EX_CLIENTEDGE; + } + return cp; } } @@ -481,12 +530,7 @@ protected override Cursor DefaultCursor /// This is more efficient than setting the size in the control's constructor. /// protected override Size DefaultSize - { - get - { - return new Size(100, PreferredHeight); - } - } + => new Size(100, PreferredHeight); /// /// Gets or sets the foreground color of the control. @@ -496,17 +540,10 @@ protected override Size DefaultSize [SRDescription(nameof(SR.ControlForeColorDescr))] public override Color ForeColor { - get - { - if (ShouldSerializeForeColor()) - { - return base.ForeColor; - } - else - { - return SystemColors.WindowText; - } - } + get => ShouldSerializeForeColor() + ? base.ForeColor + : SystemColors.WindowText; + set => base.ForeColor = value; } @@ -519,10 +556,7 @@ public override Color ForeColor [SRDescription(nameof(SR.TextBoxHideSelectionDescr))] public bool HideSelection { - get - { - return _textBoxFlags[s_hideSelection]; - } + get => _textBoxFlags[s_hideSelection]; set { @@ -550,7 +584,10 @@ public event EventHandler? HideSelectionChanged /// protected override ImeMode ImeModeBase { - get => (DesignMode || CanEnableIme) ? base.ImeModeBase : ImeMode.Disable; + get => (DesignMode || CanEnableIme) + ? base.ImeModeBase + : ImeMode.Disable; + set => base.ImeModeBase = value; } @@ -576,9 +613,11 @@ public string[] Lines while (lineStart < text.Length) { int lineEnd = lineStart; + for (; lineEnd < text.Length; lineEnd++) { char c = text[lineEnd]; + if (c is '\r' or '\n') { break; @@ -610,18 +649,12 @@ public string[] Lines return [.. list]; } - set - { + + set => // unparse this string list... - if (value is not null && value.Length > 0) - { - Text = string.Join(Environment.NewLine, value); - } - else - { - Text = string.Empty; - } - } + Text = value is not null && value.Length > 0 + ? string.Join(Environment.NewLine, value) + : string.Empty; } /// @@ -634,10 +667,8 @@ public string[] Lines [SRDescription(nameof(SR.TextBoxMaxLengthDescr))] public virtual int MaxLength { - get - { - return _maxLength; - } + get => _maxLength; + set { ArgumentOutOfRangeException.ThrowIfNegative(value); @@ -662,22 +693,26 @@ public bool Modified { get { - if (IsHandleCreated) + if (!IsHandleCreated) + { + return _textBoxFlags[s_modified]; + } + else { - bool curState = (int)PInvokeCore.SendMessage(this, PInvokeCore.EM_GETMODIFY) != 0; + bool curState = (int)PInvokeCore.SendMessage( + this, + PInvokeCore.EM_GETMODIFY) != 0; + if (_textBoxFlags[s_modified] != curState) { - // Raise ModifiedChanged event. See WmReflectCommand for more info. + // Raise ModifiedChanged event. + // See WmReflectCommand for more info. _textBoxFlags[s_modified] = curState; OnModifiedChanged(EventArgs.Empty); } return curState; } - else - { - return _textBoxFlags[s_modified]; - } } set @@ -686,11 +721,14 @@ public bool Modified { if (IsHandleCreated) { - PInvokeCore.SendMessage(this, PInvokeCore.EM_SETMODIFY, (WPARAM)(BOOL)value); - // Must maintain this state always in order for the - // test in the Get method to work properly. + PInvokeCore.SendMessage( + this, + PInvokeCore.EM_SETMODIFY, + (WPARAM)(BOOL)value); } + // Must maintain this state always in order for the + // test in the Get method to work properly. _textBoxFlags[s_modified] = value; OnModifiedChanged(EventArgs.Empty); } @@ -716,33 +754,36 @@ public event EventHandler? ModifiedChanged [RefreshProperties(RefreshProperties.All)] public virtual bool Multiline { - get - { - return _textBoxFlags[s_multiline]; - } + get => _textBoxFlags[s_multiline]; set { - if (_textBoxFlags[s_multiline] != value) + if (_textBoxFlags[s_multiline] == value) { - using (LayoutTransaction.CreateTransactionIf(AutoSize, ParentInternal, this, PropertyNames.Multiline)) - { - _textBoxFlags[s_multiline] = value; + return; + } - if (value) - { - // Multi-line textboxes do not have fixed height - SetStyle(ControlStyles.FixedHeight, false); - } - else - { - // Single-line textboxes may have fixed height, depending on AutoSize - SetStyle(ControlStyles.FixedHeight, AutoSize); - } + using (LayoutTransaction.CreateTransactionIf( + condition: AutoSize, + controlToLayout: ParentInternal, + elementCausingLayout: this, + property: PropertyNames.Multiline)) + { + _textBoxFlags[s_multiline] = value; - RecreateHandle(); - AdjustHeight(false); - OnMultilineChanged(EventArgs.Empty); + if (value) + { + // Multi-line textboxes do not have fixed height + SetStyle(ControlStyles.FixedHeight, false); + } + else + { + // Single-line textboxes may have fixed height, depending on AutoSize + SetStyle(ControlStyles.FixedHeight, AutoSize); } + + RecreateHandle(); + AdjustHeight(false); + OnMultilineChanged(EventArgs.Empty); } } } @@ -755,15 +796,47 @@ public event EventHandler? MultilineChanged remove => Events.RemoveHandler(s_multilineChangedEvent, value); } - [Browsable(false)] - [EditorBrowsable(EditorBrowsableState.Never)] - [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)] + /// + /// Gets or sets the distance, in pixels, between the text displayed in a text box control and the top, bottom and side edges of the control. + /// + /// + /// + /// Note: While shadowing this property may seem redundant at first glance, it is necessary for binary compatibility. + /// Prior to .NET 11, this property was marked with Browsable(false) and + /// EditorBrowsable(EditorBrowsableState.Never), + /// and was never serialized. Effectively, did not work before .NET 11. While this changed in .NET 11, + /// removing the property shadowing would constitute a binary breaking change. Therefore, the shadowing is retained + /// with updated attributes to ensure Padding now functions as expected. + /// + /// + /// As long as the default value for the Padding property is not modified, -derived + /// controls (, ) will behave as they always have. However, if you + /// opt into modern styling by calling and set + /// to a value other than or + /// , the control will automatically apply styling that conforms to the + /// Windows 10+ Fluent Design Language. This provides both stylistic improvements (such as Dark Mode support) and + /// functional enhancements (including increased touch targets for easier mouse and touch interaction), thereby + /// meeting the accessibility requirements of modern high-resolution displays. + /// + /// + [Browsable(true)] + [EditorBrowsable(EditorBrowsableState.Always)] + [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)] public new Padding Padding { get => base.Padding; set => base.Padding = value; } + private new bool ShouldSerializePadding() + => Padding != DefaultPadding; + + private void ResetPadding() + => Padding = DefaultPadding; + + // Not new API and not a breaking change: TextBoxBase.PaddingChanged is already in + // PublicAPI.Shipped.txt. It is re-declared here with 'new' purely to apply the designer-hiding + // attributes below (Browsable(false) / EditorBrowsable(Never)) on top of Control.PaddingChanged. [Browsable(false)] [EditorBrowsable(EditorBrowsableState.Never)] [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)] @@ -791,54 +864,204 @@ public event EventHandler? MultilineChanged [EditorBrowsable(EditorBrowsableState.Advanced)] [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)] [SRDescription(nameof(SR.TextBoxPreferredHeightDescr))] - public int PreferredHeight + public int PreferredHeight => + EffectiveVisualStylesMode switch + { + VisualStylesMode.Disabled => PreferredHeightClassic, + VisualStylesMode.Classic => PreferredHeightClassic, + >= VisualStylesMode.Net11 => PreferredHeightCore, + + // We should never be here. + _ => throw new InvalidEnumArgumentException( + argumentName: nameof(VisualStylesMode), + invalidValue: (int)VisualStylesMode, + enumClass: typeof(VisualStylesMode)) + }; + + /// + /// Returns the preferred height for modern Visual Styles, taking the carved padding band + /// (including the live scrollbar allowance and the user ) into account. + /// + private protected virtual int PreferredHeightCore + { + get + { + int preferredHeight = FontHeight + GetVisualStylesPadding(includeScrollbars: true).Vertical; + + if (AutoSize && !Multiline && BorderStyle == BorderStyle.Fixed3D) + { + // A naturally sized single-line control must leave enough room for the complete + // rounded chrome. Explicitly fixed controls are not forced through this floor. + int roundedChromeMinimumHeight = ScaleVisualStylesMetric(VisualStylesCornerRadius) + + ScaleVisualStylesMetric(BorderThickness) + + ScaleVisualStylesMetric(VisualStylesInternalChromeInset); + + preferredHeight = Math.Max(preferredHeight, roundedChromeMinimumHeight); + } + + return preferredHeight; + } + } + + /// + /// Returns the classic (Everett-compatible) preferred height for a single-line text box. + /// + /// + /// + /// COMPAT: we must return the same busted height we did in Everett, even if it does not take + /// multiline and word wrap into account. For better accuracy and/or wrapping use + /// instead. + /// + /// + private int PreferredHeightClassic { get { - // COMPAT we must return the same busted height we did in Everett, even - // if it doesn't take multiline and word wrap into account. For better accuracy and/or wrapping use - // GetPreferredSize instead. int height = FontHeight; + if (_borderStyle != BorderStyle.None) { - height += SystemInformation.GetBorderSizeForDpi(DeviceDpiInternal).Height * 4 + 3; + height += SystemInformation.GetBorderSizeForDpi(DeviceDpiInternal).Height + * 4 + 3; } return height; } } - // GetPreferredSizeCore - // This method can return a different value than PreferredHeight! It properly handles - // border style + multiline and wordwrap. + /// + /// Defines the visible part of the padding band carved for modern Visual Styles chrome. + /// + /// + /// to add the live scrollbar allowance (see ) + /// on top of the border padding; otherwise . + /// + /// The visible padding dimensions. + /// + /// + /// The visible part of the padding is the area by which we extend the real-estate of the control + /// with its back color. Border/corner reservation, the DPI-scaled internal chrome inset, the + /// user-provided , and scrollbar reservation are each added once. + /// + /// + private protected Padding GetVisualStylesPadding(bool includeScrollbars) + { + int offset = ScaleVisualStylesMetric(BorderThickness); - internal override Size GetPreferredSizeCore(Size proposedConstraints) + // The visible padding is selected per BorderStyle (not per VisualStylesMode): each border look + // reserves a differently sized band around the native edit's client area for the modern chrome + // we paint in the non-client region. Fixed3D reserves the widest uniform band for the rounded + // lozenge and its focus line; FixedSingle reserves a slightly smaller uniform band for the flat + // single-line border; None reserves only a minimal band, plus extra room on the right and bottom + // for the scrollbars and the focus line. BorderThickness (offset) is added so the drawn border + // line sits inside the reserved band rather than on its outer edge. + Padding borderPadding = BorderStyle switch + { + BorderStyle.Fixed3D => new Padding( + left: ScaleVisualStylesMetric(VisualStylesFixed3DBorderPadding) + offset, + top: ScaleVisualStylesMetric(VisualStylesFixed3DBorderPadding) + offset, + right: ScaleVisualStylesMetric(VisualStylesFixed3DBorderPadding) + offset, + bottom: ScaleVisualStylesMetric(VisualStylesFixed3DBorderPadding) + offset), + + BorderStyle.FixedSingle => new Padding( + left: ScaleVisualStylesMetric(VisualStylesFixedSingleBorderPadding) + offset, + top: ScaleVisualStylesMetric(VisualStylesFixedSingleBorderPadding) + offset, + right: ScaleVisualStylesMetric(VisualStylesFixedSingleBorderPadding) + offset, + bottom: ScaleVisualStylesMetric(VisualStylesFixedSingleBorderPadding) + offset), + + BorderStyle.None => new Padding( + left: ScaleVisualStylesMetric(VisualStylesNoBorderPadding), + top: ScaleVisualStylesMetric(VisualStylesNoBorderPadding), + right: ScaleVisualStylesMetric(VisualStylesNoBorderPadding) + offset, + // We still need some extra space for the focus indication. + bottom: ScaleVisualStylesMetric(VisualStylesNoBorderPadding) + offset), + + _ => Padding.Empty, + }; + + Padding padding = borderPadding + new Padding(ScaleVisualStylesMetric(VisualStylesInternalChromeInset)); + + if (includeScrollbars) + { + padding += GetScrollBarPadding(); + } + + padding += Padding; + + return padding; + } + + private int ScaleVisualStylesMetric(int logicalValue) + => ScaleHelper.ScaleToDpi(logicalValue, DeviceDpiInternal); + + internal static bool CanRenderVisualStylesRoundedChrome( + Rectangle bounds, + int cornerSize, + int borderThickness) { - // 3px vertical space is required between the text and the border to keep the last - // line from being clipped. - // This 3 pixel size was added in everett and we do this to maintain compat. - // old everett behavior was FontHeight + [SystemInformation.BorderSize.Height * 4 + 3] - // however the [ ] was only added if borderstyle was not none. - Size bordersAndPadding = SizeFromClientSize(Size.Empty) + Padding.Size; + int minimumDimension = cornerSize + borderThickness; + return bounds.Width >= minimumDimension && bounds.Height >= minimumDimension; + } - if (BorderStyle != BorderStyle.None) + internal static Color GetVisualStylesFocusColor(bool highContrast) + => highContrast + ? SystemColors.Highlight + : Application.GetWindowsAccentColor(); + + /// + /// Returns the additional padding required to clear the live scrollbars, + /// if any are currently shown. + /// + private protected virtual Padding GetScrollBarPadding() + { + Padding padding = Padding.Empty; + + // Are the scrollbars visible? + WINDOW_STYLE style = (WINDOW_STYLE)PInvokeCore.GetWindowLong( + this, + WINDOW_LONG_PTR_INDEX.GWL_STYLE); + + bool hasHScroll = (style & WINDOW_STYLE.WS_HSCROLL) != 0; + bool hasVScroll = (style & WINDOW_STYLE.WS_VSCROLL) != 0; + + if (hasHScroll) { - bordersAndPadding += new Size(0, 3); + padding.Bottom += SystemInformation.GetHorizontalScrollBarHeightForDpi(DeviceDpiInternal); } - if (BorderStyle == BorderStyle.FixedSingle) + if (hasVScroll) { - // Bump these by 2px to match BorderStyle.Fixed3D - they'll be omitted from the SizeFromClientSize call. - bordersAndPadding.Width += 2; - bordersAndPadding.Height += 2; + padding.Right += SystemInformation.GetVerticalScrollBarWidthForDpi(DeviceDpiInternal); } - // Reduce constraints by border/padding size - proposedConstraints -= bordersAndPadding; + return padding; + } + + /// + /// Computes the preferred size of the control, honoring border style, multiline, and word wrap. + /// + /// + /// + /// This can return a different value than . + /// is a single-line, border-only height kept for backward compatibility, whereas this method measures + /// the actual text against the available width and additionally accounts for multiline and word wrap, + /// and - for modern Visual Styles - the carved adorner band and the user . + /// + /// + internal override Size GetPreferredSizeCore(Size proposedConstraints) + { + // 3px vertical space is required between the text and the border to keep the last + // line from being clipped. + + // This 3 pixel size was added in Everett, and we do this to maintain compat. + // Old Everett behavior was FontHeight + [SystemInformation.BorderSize.Height * 4 + 3], + // however the [ ] was only added if BorderStyle was not None. + Padding padding = default; // Fit the text to the remaining space. - // Fixed for .NET Framework 4.0 + // Originally fixed for .NET Framework 4.0 TextFormatFlags format = TextFormatFlags.NoPrefix; + if (!Multiline) { format |= TextFormatFlags.SingleLine; @@ -848,11 +1071,50 @@ internal override Size GetPreferredSizeCore(Size proposedConstraints) format |= TextFormatFlags.WordBreak; } + if (EffectiveVisualStylesMode >= VisualStylesMode.Net11) + { + // For modern Visual Styles we take the carved adorner band (including scrollbars + // and the user Padding) into account when measuring. + padding = GetVisualStylesPadding(includeScrollbars: true); + proposedConstraints -= padding.Size; + } + else + { + // Keep the pre-.NET 11 layout contract for classic or disabled visual styles. In these + // modes we retain the historic border sizing, but user Padding does not participate in + // preferred-size calculations until a modern visual styles mode is selected explicitly. + Size borders = SizeFromClientSize(Size.Empty); + + if (BorderStyle != BorderStyle.None) + { + borders += new Size(0, 3); + } + + if (BorderStyle == BorderStyle.FixedSingle) + { + // Bump these by 2px to match BorderStyle.Fixed3D - they'll be omitted from the SizeFromClientSize call. + borders.Width += 2; + borders.Height += 2; + } + + // Preserve the classic border-only measurement contract until a modern + // visual styles mode is selected. + padding.Left = 0; + padding.Top = 0; + padding.Right = borders.Width; + padding.Bottom = borders.Height; + + // Reduce constraints by the classic border size. + proposedConstraints -= borders; + } + Size textSize = TextRenderer.MeasureText(Text, Font, proposedConstraints, format); // We use this old computation as a lower bound to ensure backwards compatibility. textSize.Height = Math.Max(textSize.Height, FontHeight); - Size preferredSize = textSize + bordersAndPadding; + + Size preferredSize = textSize + new Size(padding.Horizontal, padding.Vertical); + return preferredSize; } @@ -885,9 +1147,9 @@ internal unsafe void GetSelectionStartAndLength(out int start, out int length) // the windows call. This eliminates a problem on nt4 where // a huge negative # is being returned. start = Math.Max(0, start); + // ditto for end end = Math.Max(0, end); - length = end - start; } @@ -898,14 +1160,9 @@ internal unsafe void GetSelectionStartAndLength(out int start, out int length) end = start + length - 1; - if (t is null) - { - len = 0; - } - else - { - len = t.Length; - } + len = t is null + ? 0 + : t.Length; Debug.Assert(end <= len, $"SelectionEnd is outside the set of valid caret positions for the current WindowText (end ={end}, WindowText.Length ={len})"); @@ -922,26 +1179,25 @@ internal unsafe void GetSelectionStartAndLength(out int start, out int length) [SRDescription(nameof(SR.TextBoxReadOnlyDescr))] public bool ReadOnly { - get - { - return _textBoxFlags[s_readOnly]; - } + get => _textBoxFlags[s_readOnly]; + set { - if (_textBoxFlags[s_readOnly] != value) + if (_textBoxFlags[s_readOnly] == value) { - _textBoxFlags[s_readOnly] = value; - - if (IsHandleCreated) - { - PInvokeCore.SendMessage(this, PInvokeCore.EM_SETREADONLY, (WPARAM)(BOOL)value); - EnsureReadonlyBackgroundColor(value); - } + return; + } - OnReadOnlyChanged(EventArgs.Empty); + _textBoxFlags[s_readOnly] = value; - VerifyImeRestrictedModeChanged(); + if (IsHandleCreated) + { + PInvokeCore.SendMessage(this, PInvokeCore.EM_SETREADONLY, (WPARAM)(BOOL)value); + EnsureReadonlyBackgroundColor(value); } + + OnReadOnlyChanged(EventArgs.Empty); + VerifyImeRestrictedModeChanged(); } } @@ -952,7 +1208,10 @@ private void EnsureReadonlyBackgroundColor(bool value) && DarkModeRequestState is true && !ShouldSerializeBackColor()) { - base.BackColor = value ? SystemColors.ControlLight : SystemColors.Window; + base.BackColor = value + ? SystemColors.ControlLight + : SystemColors.Window; + Invalidate(); } } @@ -978,12 +1237,11 @@ public virtual string SelectedText get { GetSelectionStartAndLength(out int selStart, out int selLength); + return Text.Substring(selStart, selLength); } - set - { - SetSelectedTextInternal(value, true); - } + + set => SetSelectedTextInternal(value, true); } /// @@ -1077,11 +1335,13 @@ public int SelectionStart public override string Text { get => base.Text; + set { if (value != base.Text) { base.Text = value; + if (IsHandleCreated) { // clear the modified flag @@ -1098,9 +1358,16 @@ public virtual int TextLength => IsHandleCreated ? PInvokeCore.GetWindowTextLength(this) : Text.Length; + /// + /// Gets or sets the underlying native window text (the edit control's caption). This is overridden + /// so that setting the text from code raises a "code update" flag for the duration of the assignment: + /// updating the text on a created handle produces a WM_COMMAND / EN_CHANGE notification, + /// and the flag lets us swallow it so we do not raise a duplicate event. + /// internal override string WindowText { get => base.WindowText; + set { value ??= string.Empty; @@ -1111,6 +1378,7 @@ internal override string WindowText if (!WindowText.Equals(value)) { _textBoxFlags[s_codeUpdateText] = true; + try { base.WindowText = value; @@ -1133,6 +1401,7 @@ internal void ForceWindowText(string? value) value ??= string.Empty; _textBoxFlags[s_codeUpdateText] = true; + try { if (IsHandleCreated) @@ -1160,13 +1429,15 @@ internal void ForceWindowText(string? value) [SRDescription(nameof(SR.TextBoxWordWrapDescr))] public bool WordWrap { - get - { - return _textBoxFlags[s_wordWrap]; - } + get => _textBoxFlags[s_wordWrap]; + set { - using (LayoutTransaction.CreateTransactionIf(AutoSize, ParentInternal, this, PropertyNames.WordWrap)) + using (LayoutTransaction.CreateTransactionIf( + condition: AutoSize, + controlToLayout: ParentInternal, + elementCausingLayout: this, + property: PropertyNames.WordWrap)) { if (_textBoxFlags[s_wordWrap] != value) { @@ -1185,12 +1456,15 @@ private void AdjustHeight(bool returnIfAnchored) { // If we're anchored to two opposite sides of the form, don't adjust the size because // we'll lose our anchored size by resetting to the requested width. - if (returnIfAnchored && (Anchor & (AnchorStyles.Top | AnchorStyles.Bottom)) == (AnchorStyles.Top | AnchorStyles.Bottom)) + if (returnIfAnchored + && (Anchor & (AnchorStyles.Top | AnchorStyles.Bottom)) + == (AnchorStyles.Top | AnchorStyles.Bottom)) { return; } int saveHeight = _requestedHeight; + try { if (_textBoxFlags[s_autoSize] && !_textBoxFlags[s_multiline]) @@ -1210,6 +1484,7 @@ private void AdjustHeight(bool returnIfAnchored) } _integralHeightAdjust = true; + try { Height = saveHeight; @@ -1281,22 +1556,30 @@ public void ClearUndo() protected bool ContainsNavigationKeyCode(Keys keyCode) => keyCode switch { - Keys.Up or Keys.Down or Keys.PageUp or Keys.PageDown or Keys.Home or Keys.End or Keys.Left or Keys.Right => true, + Keys.Up or Keys.Down or Keys.PageUp or Keys.PageDown + or Keys.Home or Keys.End or Keys.Left or Keys.Right => true, _ => false, }; /// /// Copies the current selection in the text box to the Clipboard. /// - public void Copy() => PInvokeCore.SendMessage(this, PInvokeCore.WM_COPY); + public void Copy() + => PInvokeCore.SendMessage(this, PInvokeCore.WM_COPY); - protected override AccessibleObject CreateAccessibilityInstance() => new TextBoxBaseAccessibleObject(this); + protected override AccessibleObject CreateAccessibilityInstance() + => new TextBoxBaseAccessibleObject(this); protected override void CreateHandle() { + // Should the handle be (re)created at this point, we need to reset the latch so the + // modern Visual Styles client area is re-carved against the new window. + _triggerNewClientSizeRequest = false; + // This "creatingHandle" stuff is to avoid property change events // when we set the Text property. _textBoxFlags[s_creatingHandle] = true; + try { base.CreateHandle(); @@ -1313,52 +1596,58 @@ protected override void CreateHandle() /// /// Moves the current selection in the text box to the Clipboard. /// - public void Cut() => PInvokeCore.SendMessage(this, PInvokeCore.WM_CUT); + public void Cut() + => PInvokeCore.SendMessage(this, PInvokeCore.WM_CUT); /// /// Returns the text end position (one past the last input character). This property is virtual to allow MaskedTextBox /// to set the last input char position as opposed to the last char position which may be a mask character. /// - internal virtual int GetEndPosition() - { + internal virtual int GetEndPosition() => // +1 because RichTextBox has this funny EOF pseudo-character after all the text. - return IsHandleCreated ? TextLength + 1 : TextLength; - } + IsHandleCreated + ? TextLength + 1 + : TextLength; /// /// Overridden to handle TAB key. /// protected override bool IsInputKey(Keys keyData) { - if ((keyData & Keys.Alt) != Keys.Alt) + if ((keyData & Keys.Alt) == Keys.Alt) { - switch (keyData & Keys.KeyCode) - { - case Keys.Tab: - // Single-line RichEd's want tab characters (see WM_GETDLGCODE), - // so we don't ask it - return Multiline && _textBoxFlags[s_acceptsTab] && ((keyData & Keys.Control) == 0); - case Keys.Escape: - if (Multiline) - { - return false; - } + return base.IsInputKey(keyData); + } - break; - case Keys.Back: - if (!ReadOnly) - { - return true; - } + switch (keyData & Keys.KeyCode) + { + case Keys.Tab: + // Single-line RichEd's want tab characters (see WM_GETDLGCODE), + // so we don't ask it + return Multiline && _textBoxFlags[s_acceptsTab] && ((keyData & Keys.Control) == 0); - break; - case Keys.PageUp: - case Keys.PageDown: - case Keys.Home: - case Keys.End: + case Keys.Escape: + if (Multiline) + { + return false; + } + + break; + + case Keys.Back: + if (!ReadOnly) + { return true; - // else fall through to base - } + } + + break; + + case Keys.PageUp: + case Keys.PageDown: + case Keys.Home: + case Keys.End: + return true; + // else fall through to base } return base.IsInputKey(keyData); @@ -1371,6 +1660,7 @@ protected override bool IsInputKey(Keys keyData) protected override void OnHandleCreated(EventArgs e) { base.OnHandleCreated(e); + if (!IsHandleCreated) { return; @@ -1380,8 +1670,8 @@ protected override void OnHandleCreated(EventArgs e) // the border size/etc. CommonProperties.xClearPreferredSizeCache(this); AdjustHeight(true); - UpdateMaxLength(); + if (_textBoxFlags[s_modified]) { PInvokeCore.SendMessage(this, PInvokeCore.EM_SETMODIFY, (WPARAM)(BOOL)true); @@ -1394,10 +1684,14 @@ protected override void OnHandleCreated(EventArgs e) ScrollToCaret(); _textBoxFlags[s_scrollToCaretOnHandleCreated] = false; } + + RecalculateVisualStylesClientArea(); } protected override void OnHandleDestroyed(EventArgs e) { + _focusIndicatorRenderer?.Dispose(); + _focusIndicatorRenderer = null; _textBoxFlags[s_modified] = Modified; _textBoxFlags[s_setSelectionOnHandleCreated] = true; // Update text selection cached values to be restored when recreating the handle. @@ -1405,10 +1699,33 @@ protected override void OnHandleDestroyed(EventArgs e) base.OnHandleDestroyed(e); } - /// - /// Replaces the current selection in the text box with the contents of the Clipboard. - /// - public void Paste() => PInvokeCore.SendMessage(this, PInvokeCore.WM_PASTE); + /// + protected override void OnVisualStylesModeChanged(EventArgs e) + { + base.OnVisualStylesModeChanged(e); + _focusIndicatorRenderer?.Synchronize(Focused, invalidate: false); + + CommonProperties.xClearPreferredSizeCache(this); + LayoutTransaction.DoLayoutIf(AutoSize, ParentInternal, this, PropertyNames.VisualStylesMode); + AdjustHeight(false); + + if (!IsHandleCreated) + { + return; + } + + // Update the native styles without recreating the handle so text, selection, and scroll state + // remain untouched. The client-area latch must be reset before requesting a new frame. + _triggerNewClientSizeRequest = false; + UpdateStyles(); + RecalculateVisualStylesClientArea(); + } + + /// + /// Replaces the current selection in the text box with the contents of the Clipboard. + /// + public void Paste() + => PInvokeCore.SendMessage(this, PInvokeCore.WM_PASTE); protected override bool ProcessDialogKey(Keys keyData) { @@ -1451,12 +1768,81 @@ protected virtual void OnBorderStyleChanged(EventArgs e) } } + protected override unsafe void OnGotFocus(EventArgs e) + { + if (EffectiveVisualStylesMode >= VisualStylesMode.Net11) + { + if (BorderStyle == BorderStyle.Fixed3D) + { + FocusIndicatorRenderer.SetFocused( + focused: true, + animate: SystemInformation.UIEffectsEnabled && !SystemInformation.HighContrast); + } + else + { + InvalidateVisualStylesFrame(); + } + } + + base.OnGotFocus(e); + } + + protected override unsafe void OnLostFocus(EventArgs e) + { + if (EffectiveVisualStylesMode >= VisualStylesMode.Net11) + { + if (BorderStyle == BorderStyle.Fixed3D) + { + FocusIndicatorRenderer.SetFocused( + focused: false, + animate: SystemInformation.UIEffectsEnabled && !SystemInformation.HighContrast); + } + else + { + InvalidateVisualStylesFrame(); + } + } + + base.OnLostFocus(e); + } + + protected override unsafe void OnSizeChanged(EventArgs e) + { + if (EffectiveVisualStylesMode >= VisualStylesMode.Net11) + { + // Invalidate the non-client area to ensure the border chrome is drawn correctly after a resize. + PInvoke.RedrawWindow( + hWnd: this, + lprcUpdate: null, + hrgnUpdate: HRGN.Null, + flags: REDRAW_WINDOW_FLAGS.RDW_FRAME | REDRAW_WINDOW_FLAGS.RDW_INVALIDATE); + } + + base.OnSizeChanged(e); + } + protected override void OnFontChanged(EventArgs e) { base.OnFontChanged(e); + CommonProperties.xClearPreferredSizeCache(this); + LayoutTransaction.DoLayoutIf(AutoSize, ParentInternal, this, PropertyNames.Font); AdjustHeight(false); } + protected override void OnDpiChangedAfterParent(EventArgs e) + { + base.OnDpiChangedAfterParent(e); + + CommonProperties.xClearPreferredSizeCache(this); + LayoutTransaction.DoLayoutIf(AutoSize, ParentInternal, this, PropertyNames.Bounds); + AdjustHeight(false); + + if (EffectiveVisualStylesMode >= VisualStylesMode.Net11) + { + RecalculateVisualStylesClientArea(); + } + } + protected virtual void OnHideSelectionChanged(EventArgs e) { if (Events[s_hideSelectionChangedEvent] is EventHandler eh) @@ -1513,7 +1899,16 @@ protected virtual void OnMultilineChanged(EventArgs e) protected override void OnPaddingChanged(EventArgs e) { base.OnPaddingChanged(e); + CommonProperties.xClearPreferredSizeCache(this); + LayoutTransaction.DoLayoutIf(AutoSize, ParentInternal, this, PropertyNames.Padding); AdjustHeight(false); + + // The carved modern Visual Styles padding band includes the user Padding, so a runtime change + // has to re-provoke the non-client calculation for it to be reflected in the client rectangle. + if (EffectiveVisualStylesMode >= VisualStylesMode.Net11) + { + RecalculateVisualStylesClientArea(); + } } protected virtual void OnReadOnlyChanged(EventArgs e) @@ -1594,7 +1989,8 @@ public virtual int GetCharIndexFromPosition(Point pt) /// you pass the index of a overflowed character, GetLineFromCharIndex would /// return 1 and not 0. /// - public virtual int GetLineFromCharIndex(int index) => (int)PInvokeCore.SendMessage(this, PInvokeCore.EM_LINEFROMCHAR, (WPARAM)index); + public virtual int GetLineFromCharIndex(int index) + => (int)PInvokeCore.SendMessage(this, PInvokeCore.EM_LINEFROMCHAR, (WPARAM)index); /// /// Returns the location of the character at the given index. @@ -1606,7 +2002,11 @@ public virtual Point GetPositionFromCharIndex(int index) return Point.Empty; } - int i = (int)PInvokeCore.SendMessage(this, PInvokeCore.EM_POSFROMCHAR, (WPARAM)index); + int i = (int)PInvokeCore.SendMessage( + hWnd: this, + Msg: PInvokeCore.EM_POSFROMCHAR, + wParam: (WPARAM)index); + return new Point(PARAM.SignedLOWORD(i), PARAM.SignedHIWORD(i)); } @@ -1617,19 +2017,26 @@ public int GetFirstCharIndexFromLine(int lineNumber) { ArgumentOutOfRangeException.ThrowIfNegative(lineNumber); - return (int)PInvokeCore.SendMessage(this, PInvokeCore.EM_LINEINDEX, (WPARAM)lineNumber); + return (int)PInvokeCore.SendMessage( + hWnd: this, + Msg: PInvokeCore.EM_LINEINDEX, + wParam: (WPARAM)lineNumber); } /// /// Returns the index of the first character of the line where the caret is. /// - public int GetFirstCharIndexOfCurrentLine() => (int)PInvokeCore.SendMessage(this, PInvokeCore.EM_LINEINDEX, (WPARAM)(-1)); + public int GetFirstCharIndexOfCurrentLine() + => (int)PInvokeCore.SendMessage( + hWnd: this, + Msg: PInvokeCore.EM_LINEINDEX, + wParam: (WPARAM)(-1)); /// /// Ensures that the caret is visible in the TextBox window, by scrolling the /// TextBox control surface if necessary. /// - public unsafe void ScrollToCaret() + public void ScrollToCaret() { if (!IsHandleCreated) { @@ -1643,57 +2050,23 @@ public unsafe void ScrollToCaret() return; } - using ComScope richEdit = new(null); - - if (PInvokeCore.SendMessage(this, PInvokeCore.EM_GETOLEINTERFACE, 0, (void**)richEdit) == 0) - { - PInvokeCore.SendMessage(this, PInvokeCore.EM_SCROLLCARET); - return; - } - - using var textDocument = richEdit.TryQuery(out HRESULT hr); - - if (hr.Succeeded) - { - // When the user calls RichTextBox::ScrollToCaret we want the RichTextBox to show as much text as - // possible. Here is how we do that: - // - // 1. We scroll the RichTextBox all the way to the bottom so the last line of text is the last visible line. - // 2. We get the first visible line. - // 3. If the first visible line is smaller than the start of the selection, then we are done: - // The selection fits inside the RichTextBox display rectangle. - // 4. Otherwise, scroll the selection to the top of the RichTextBox. - - GetSelectionStartAndLength(out int selStart, out int selLength); - int selStartLine = GetLineFromCharIndex(selStart); - - using ComScope windowTextRange = new(null); - textDocument.Value->Range(WindowText.Length - 1, WindowText.Length - 1, windowTextRange).ThrowOnFailure(); - - // 1. Scroll the RichTextBox all the way to the bottom - windowTextRange.Value->ScrollIntoView((int)tomConstants.tomEnd).ThrowOnFailure(); - - // 2. Get the first visible line. - int firstVisibleLine = (int)PInvokeCore.SendMessage(this, PInvokeCore.EM_GETFIRSTVISIBLELINE); - - // 3. If the first visible line is smaller than the start of the selection, we are done. - if (firstVisibleLine <= selStartLine) - { - return; - } - else - { - // 4. Scroll the selection to the top of the RichTextBox. - using ComScope selectionTextRange = new(null); - textDocument.Value->Range(selStart, selStart + selLength, selectionTextRange).ThrowOnFailure(); - selectionTextRange.Value->ScrollIntoView((int)tomConstants.tomStart).ThrowOnFailure(); - return; - } - } - - PInvokeCore.SendMessage(this, PInvokeCore.EM_SCROLLCARET); + ScrollToCaretCore(); } + /// + /// Performs the control-specific work of scrolling the caret into view. The base implementation + /// asks the native edit control to scroll the caret into view; overrides + /// this to additionally show as much of the surrounding text as possible. + /// + /// + /// + /// Invoked by only once the handle exists and the control has text, so + /// overrides do not need to re-check those conditions. + /// + /// + private protected virtual void ScrollToCaretCore() + => PInvokeCore.SendMessage(this, PInvokeCore.EM_SCROLLCARET); + /// /// Sets the SelectionLength to 0. /// @@ -1716,14 +2089,10 @@ public void Select(int start, int length) // We shouldn't allow positive length if you're starting at the end, but // should allow negative length. long longLength = Math.Min(0, (long)length + start - textLen); - if (longLength < int.MinValue) - { - length = int.MinValue; - } - else - { - length = (int)longLength; - } + + length = longLength < int.MinValue + ? int.MinValue + : (int)longLength; start = textLen; } @@ -1742,9 +2111,22 @@ public void Select(int start, int length) private protected virtual void SelectInternal(int selectionStart, int selectionLength, int textLength) { // if our handle is created - send message... - if (IsHandleCreated) + if (!IsHandleCreated) + { + // otherwise, wait until handle is created to send this message. + // Store the indices until then... + _selectionStart = selectionStart; + _selectionLength = selectionLength; + _textBoxFlags[s_setSelectionOnHandleCreated] = true; + } + else { - AdjustSelectionStartAndEnd(selectionStart, selectionLength, out int start, out int end, textLength); + AdjustSelectionStartAndEnd( + selectionStart, + selectionLength, + out int start, + out int end, + textLength); PInvokeCore.SendMessage(this, PInvokeCore.EM_SETSEL, (WPARAM)start, (LPARAM)end); @@ -1755,14 +2137,6 @@ private protected virtual void SelectInternal(int selectionStart, int selectionL : UIA_EVENT_ID.UIA_Text_TextSelectionChangedEventId); } } - else - { - // otherwise, wait until handle is created to send this message. - // Store the indices until then... - _selectionStart = selectionStart; - _selectionLength = selectionLength; - _textBoxFlags[s_setSelectionOnHandleCreated] = true; - } } /// @@ -1792,7 +2166,8 @@ protected override void SetBoundsCore(int x, int y, int width, int height, Bound base.SetBoundsCore(x, y, width, height, specified); } - private static void Swap(ref int n1, ref int n2) => (n1, n2) = (n2, n1); + private static void Swap(ref int n1, ref int n2) + => (n1, n2) = (n2, n1); // Send in -1 if you don't have the text length cached // when calling this method. It will be computed. If not, @@ -1810,16 +2185,9 @@ internal void AdjustSelectionStartAndEnd(int selStart, int selLength, out int st } else { - int textLength; - - if (textLen >= 0) - { - textLength = textLen; - } - else - { - textLength = TextLength; - } + int textLength = textLen >= 0 + ? textLen + : TextLength; if (start > textLength) { @@ -1855,6 +2223,7 @@ internal void AdjustSelectionStartAndEnd(int selStart, int selLength, out int st internal void SetSelectionOnHandle() { Debug.Assert(IsHandleCreated, "Don't call this method until the handle is created."); + if (_textBoxFlags[s_setSelectionOnHandleCreated]) { _textBoxFlags[s_setSelectionOnHandleCreated] = false; @@ -1876,6 +2245,7 @@ private static void ToUnicodeOffsets(string str, ref int start, ref int end) byte[] bytes = e.GetBytes(str); bool swap = start > end; + if (swap) { Swap(ref start, ref end); @@ -1922,6 +2292,7 @@ internal static void ToDbcsOffsets(string str, ref int start, ref int end) Encoding e = Encoding.Default; bool swap = start > end; + if (swap) { Swap(ref start, ref end); @@ -1971,6 +2342,7 @@ public override string ToString() string s = base.ToString(); string txt = Text; + if (txt.Length > 40) { txt = $"{txt.AsSpan(0, 40)}..."; @@ -1982,7 +2354,8 @@ public override string ToString() /// /// Undoes the last edit operation in the text box. /// - public void Undo() => PInvokeCore.SendMessage(this, PInvokeCore.EM_UNDO); + public void Undo() + => PInvokeCore.SendMessage(this, PInvokeCore.EM_UNDO); internal virtual void UpdateMaxLength() { @@ -1994,6 +2367,8 @@ internal virtual void UpdateMaxLength() internal override HBRUSH InitializeDCForWmCtlColor(HDC dc, MessageId msg) { + InitializeClientArea(dc, (HWND)Handle); + if (msg == PInvokeCore.WM_CTLCOLORSTATIC && !ShouldSerializeBackColor()) { // Let the Win32 Edit control handle background colors itself. @@ -2007,26 +2382,579 @@ internal override HBRUSH InitializeDCForWmCtlColor(HDC dc, MessageId msg) } } - private void WmReflectCommand(ref Message m) + /// + /// Provokes the single non-client calc round trip that carves the modern Visual Styles padding + /// band from the client area. + /// + /// + /// + /// This runs only for values of + /// and above, and only once per handle. The latch () is + /// reset on handle recreation in . + /// + /// + private protected virtual unsafe void InitializeClientArea(HDC hDC, HWND hwnd) + { + if (EffectiveVisualStylesMode < VisualStylesMode.Net11 + || _triggerNewClientSizeRequest) + { + return; + } + + RecalculateVisualStylesClientArea(); + } + + /// + /// Sets the latch and provokes the WM_NCCALCSIZE round trip that carves the modern Visual + /// Styles padding band from the client area. Safe to call repeatedly; it re-carves against the + /// current and scrollbar state. + /// + private protected void RecalculateVisualStylesClientArea() + { + if (!IsHandleCreated || EffectiveVisualStylesMode < VisualStylesMode.Net11) + { + return; + } + + _triggerNewClientSizeRequest = true; + + // Call SetWindowPos with the current bounds and the SWP_FRAMECHANGED flag. We do not change the + // window position/size, but this provokes the WM_NCCALCSIZE message we need to carve the client area. + PInvoke.SetWindowPos( + hWnd: this, + hWndInsertAfter: HWND.HWND_TOP, + X: 0, + Y: 0, + cx: 0, + cy: 0, + uFlags: SET_WINDOW_POS_FLAGS.SWP_FRAMECHANGED + | SET_WINDOW_POS_FLAGS.SWP_NOACTIVATE + | SET_WINDOW_POS_FLAGS.SWP_NOMOVE + | SET_WINDOW_POS_FLAGS.SWP_NOSIZE + | SET_WINDOW_POS_FLAGS.SWP_NOZORDER); + + Invalidate(true); + } + + /// + /// When , the native window reserves its own non-client metrics (border and + /// scrollbars) during WM_NCCALCSIZE, so the modern Visual Styles padding band is carved from + /// the client rectangle the default handler produces rather than from the raw proposed window + /// rectangle. The default is : the managed carve is authoritative and the + /// default handler is not invoked (a plain EDIT control keeps its scrollbars inside the + /// managed allowance). + /// + private protected virtual bool ReservesNativeNonClientArea => false; + + /// + /// Handles WM_NCCALCSIZE by carving the modern Visual Styles padding band from the client + /// rectangle. The carve is floored so the client rectangle can never invert. + /// + private unsafe void WmNcCalcSize(ref Message m) { - if (!_textBoxFlags[s_codeUpdateText] && !_textBoxFlags[s_creatingHandle]) + // Make sure we actually kicked this off. + if (_triggerNewClientSizeRequest) { - uint hiword = m.WParamInternal.HIWORD; - if (hiword == PInvoke.EN_CHANGE && CanRaiseTextChangedEvent) + NCCALCSIZE_PARAMS* ncCalcSizeParams = (NCCALCSIZE_PARAMS*)(void*)m.LParamInternal; + + if (ncCalcSizeParams is not null) { - OnTextChanged(EventArgs.Empty); + // Controls whose native window reserves its own non-client metrics (RichEdit reserves its + // border and scrollbars) must run the default handler first, so we carve the modern padding + // band from the already-adjusted client rectangle rather than the raw proposed window + // rectangle. Their managed scrollbar allowance is omitted from this carve because the + // native handler already reserved it. + if (ReservesNativeNonClientArea) + { + base.WndProc(ref m); + } + + Padding padding = GetVisualStylesPadding(includeScrollbars: !ReservesNativeNonClientArea); + + ref RECT clientRect = ref ncCalcSizeParams->rgrc._0; + + // Never-invert clamp: a large Padding plus the live scrollbar allowance can drive the + // carved client rect to zero or inverted. A 0-1px client area is acceptable and intended + // (shipping multiline TextBox already collapses this way); we only prevent underflow past + // zero. This is deliberately NOT a MinimumSize and does not vary by VisualStylesMode. + int newTop = clientRect.top + padding.Top; + int newBottom = clientRect.bottom - padding.Bottom; + int newLeft = clientRect.left + padding.Left; + int newRight = clientRect.right - padding.Right; + + clientRect.top = newTop; + clientRect.bottom = Math.Max(newTop, newBottom); + clientRect.left = newLeft; + clientRect.right = Math.Max(newLeft, newRight); + + m.ResultInternal = (LRESULT)0; + + return; } - else if (hiword == PInvoke.EN_UPDATE) + } + + base.WndProc(ref m); + } + + /// + /// Handles WM_NCPAINT for modern Visual Styles by painting the custom chrome into the + /// window DC. The wParam clip region is intentionally ignored for our own paint and the + /// custom frame is repainted to avoid the offscreen-restore "dirty corners" artifact. + /// + /// + /// + /// The default (themed) non-client paint for a scrollbar-bearing edit control fills the client + /// rectangle, which erases the control's content on every non-client repaint - for example when + /// the mouse hovers over the frame. To prevent that, the default handler is invoked with an update + /// region that spans the whole window but excludes the live client rectangle, so the border and + /// scrollbars still paint while the client area is left untouched. + /// + /// + /// The custom chrome blit also excludes native scrollbars that are currently visible. This keeps + /// the native scrollbar pixels painted by the default handler from being covered by the frame. + /// + /// + private void WmNcPaint(ref Message m) + { + if (EffectiveVisualStylesMode < VisualStylesMode.Net11) + { + base.WndProc(ref m); + return; + } + + HWND hwnd = (HWND)m.HWnd; + + // A non-client update region (in screen coordinates, as WM_NCPAINT requires) that excludes the + // client rectangle. Handing this to the default handler keeps it from overpainting the client. + using RegionScope nonClientRegion = CreateNonClientClipRegion(); + WPARAM originalWParam = m.WParamInternal; + + HDC hdc = PInvokeCore.GetWindowDC(hwnd); + + // Intentional: Graphics.FromHdc does NOT own the DC, so we release the DC ourselves in the + // finally. Do not "tidy" the DC into a single using - that would be a regression. + using Graphics graphics = Graphics.FromHdc(hdc); + + try + { + if (!nonClientRegion.IsNull) { - // Force update to the Modified property, which will trigger ModifiedChanged event handlers - _ = Modified; + m.WParamInternal = (WPARAM)(nuint)(nint)nonClientRegion.Region; } + + base.WndProc(ref m); + + // Restore the original wParam so nothing downstream observes our temporary clip region. + m.WParamInternal = originalWParam; + + OnNcPaint(graphics, hdc); + } + finally + { + int result = PInvokeCore.ReleaseDC(hwnd, hdc); + Debug.Assert(result != 0); + } + } + + private void WmPrint(ref Message m) + { + base.WndProc(ref m); + + if (EffectiveVisualStylesMode < VisualStylesMode.Net11 + || ((nint)m.LParamInternal & PInvoke.PRF_NONCLIENT) == 0) + { + return; + } + + HDC hdc = (HDC)m.WParamInternal; + + if (hdc.IsNull) + { + return; + } + + using Graphics graphics = Graphics.FromHdc(hdc); + OnNcPaint(graphics, hdc); + } + + /// + /// Builds a non-client update region, in screen coordinates, that spans the whole window but + /// excludes the live client rectangle. This is handed to the default WM_NCPAINT handler so + /// it cannot overpaint (erase) the client area of scrollbar-bearing edit controls. + /// + /// + /// The non-client region scope, or a null scope when the window rectangle cannot be retrieved. + /// + private RegionScope CreateNonClientClipRegion() + { + if (!PInvokeCore.GetWindowRect(this, out RECT windowRect)) + { + return new RegionScope(HRGN.Null); + } + + PInvokeCore.GetClientRect(this, out RECT clientRect); + + Point clientTopLeft = default; + PInvoke.ClientToScreen(this, ref clientTopLeft); + + RegionScope nonClientRegion = new(windowRect.left, windowRect.top, windowRect.right, windowRect.bottom); + + using RegionScope clientRegion = new( + clientTopLeft.X, + clientTopLeft.Y, + clientTopLeft.X + clientRect.Width, + clientTopLeft.Y + clientRect.Height); + + if (PInvokeCore.CombineRgn(nonClientRegion.Region, nonClientRegion.Region, clientRegion.Region, RGN_COMBINE_MODE.RGN_DIFF) + == GDI_REGION_TYPE.RGN_ERROR) + { + nonClientRegion.Dispose(); + return new RegionScope(HRGN.Null); + } + + return nonClientRegion; + } + + /// + /// Paints the modern Visual Styles non-client chrome (border, rounded + /// lozenge, focus indicator) into a shared offscreen buffer and blits it to the supplied window DC. + /// + private protected virtual void OnNcPaint(Graphics graphics, HDC windowHdc) + { + int cornerRadius = ScaleVisualStylesMetric(VisualStylesCornerRadius); + int borderThickness = ScaleVisualStylesMetric(BorderThickness); + + Color adornerColor = ForeColor; + + Color clientBackColor = BackColor; + Color parentBackColor = Parent?.BackColor ?? BackColor; + + using var clientBackgroundBrush = clientBackColor.GetCachedSolidBrushScope(); + using var adornerBrush = adornerColor.GetCachedSolidBrushScope(); + using var adornerPen = adornerColor.GetCachedPenScope(borderThickness); + + Rectangle bounds = new( + x: 0, + y: 0, + width: Bounds.Width, + height: Bounds.Height); + + // Repaint the outermost client pixel with the chrome background so no residual native edge can + // remain between the managed frame and edit surface. The rest of the live client stays protected. + Rectangle nativeClientBounds = Rectangle.Intersect(bounds, GetNativeClientRectangle()); + Rectangle protectedClientBounds = GetProtectedClientBounds(nativeClientBounds); + Rectangle[] scrollBarBounds = GetVisibleScrollBarRectangles(bounds); + + Rectangle deflatedBounds = bounds; + + // Making sure we never color outside the lines. + deflatedBounds.Width -= 1; + deflatedBounds.Height -= 1; + + // Keep the target clip excluded from the GDI+ drawing as well as from the explicit blits below. + using Region region = new(bounds); + graphics.Clip = region; + graphics.ExcludeClip(protectedClientBounds); + + // WinForms paints NC serially (one HWND at a time on the UI thread), so a single shared buffer + // suffices for any number of controls. The buffer is reused when the size fits; steady-state + // allocation is zero. + BufferedGraphicsContext context = BufferedGraphicsManager.Current; + using BufferedGraphics buffer = context.Allocate(graphics, bounds); + + // Intentional: the buffer owns this Graphics - do NOT dispose buffer.Graphics separately. + Graphics offscreenGraphics = buffer.Graphics; + Rectangle bufferBounds = bounds; + + // We need anti-aliasing for the rounded chrome. + offscreenGraphics.SmoothingMode = SmoothingMode.AntiAlias; + + // AddRoundedRectangle receives the bounding size of each corner arc, so one corner size plus + // the border thickness is the minimum height that avoids overlapping curves. + bool canRenderRoundedChrome = CanRenderVisualStylesRoundedChrome( + deflatedBounds, + cornerRadius, + borderThickness); + + if (BorderStyle == BorderStyle.Fixed3D && canRenderRoundedChrome) + { + ParentBackgroundRenderer.Paint(this, offscreenGraphics, bufferBounds, parentBackColor); + } + else + { + using var parentBackgroundBrush = parentBackColor.GetCachedSolidBrushScope(); + offscreenGraphics.FillRectangle(parentBackgroundBrush, bounds); + } + + switch (BorderStyle) + { + case BorderStyle.None: + + // Just fill a rectangle. + offscreenGraphics.FillRectangle(clientBackgroundBrush, deflatedBounds); + break; + + case BorderStyle.FixedSingle: + + offscreenGraphics.FillRectangle(clientBackgroundBrush, deflatedBounds); + offscreenGraphics.DrawRectangle(adornerPen, deflatedBounds); + break; + + case BorderStyle.Fixed3D: + + if (canRenderRoundedChrome) + { + using GraphicsPath roundedBodyPath = new(); + roundedBodyPath.AddRoundedRectangle(deflatedBounds, new Size(cornerRadius, cornerRadius)); + offscreenGraphics.FillPath(clientBackgroundBrush, roundedBodyPath); + offscreenGraphics.DrawPath(adornerPen, roundedBodyPath); + } + else + { + // Chrome degradation fallback - flat render in place of the broken lozenge. + offscreenGraphics.FillRectangle(clientBackgroundBrush, deflatedBounds); + offscreenGraphics.DrawRectangle(adornerPen, deflatedBounds); + } + + break; + } + + if (BorderStyle == BorderStyle.Fixed3D && canRenderRoundedChrome) + { + Color focusColor = GetVisualStylesFocusColor(SystemInformation.HighContrast); + FocusIndicatorRenderer.DrawRoundedFocusIndicator( + offscreenGraphics, + deflatedBounds, + cornerRadius, + borderThickness, + ScaleVisualStylesMetric(VisualStylesFocusBandHeight), + adornerColor, + focusColor); + } + else if (Focused) + { + Color focusColor = GetVisualStylesFocusColor(SystemInformation.HighContrast); + using var focusPen = focusColor.GetCachedPenScope(borderThickness); + offscreenGraphics.DrawLine( + focusPen, + deflatedBounds.Left, + deflatedBounds.Bottom, + deflatedBounds.Right, + deflatedBounds.Bottom); + offscreenGraphics.DrawLine( + focusPen, + deflatedBounds.Left, + deflatedBounds.Bottom - 1, + deflatedBounds.Right, + deflatedBounds.Bottom - 1); + } + + Rectangle[] nonClientBands = GetNonClientPaintBands( + bufferBounds, + protectedClientBounds, + scrollBarBounds); + IntPtr bufferHdc = offscreenGraphics.GetHdc(); + + try + { + foreach (Rectangle band in nonClientBands) + { + if (band.Width > 0 && band.Height > 0) + { + PInvokeCore.BitBlt( + hdc: windowHdc, + x: band.X, + y: band.Y, + cx: band.Width, + cy: band.Height, + hdcSrc: (HDC)bufferHdc, + x1: band.X, + y1: band.Y, + rop: ROP_CODE.SRCCOPY); + } + } + } + finally + { + offscreenGraphics.ReleaseHdcInternal(bufferHdc); + } + } + + private static Rectangle[] GetNonClientPaintBands(Rectangle bounds, Rectangle clientBounds) + => GetNonClientPaintBands(bounds, clientBounds, []); + + private static Rectangle GetProtectedClientBounds(Rectangle clientBounds) + => clientBounds.Width > 2 && clientBounds.Height > 2 + ? Rectangle.Inflate(clientBounds, -1, -1) + : clientBounds; + + private static Rectangle[] GetNonClientPaintBands( + Rectangle bounds, + Rectangle clientBounds, + Rectangle[] additionalProtectedBounds) + { + if (bounds.Width <= 0 || bounds.Height <= 0) + { + return [Rectangle.Empty, Rectangle.Empty, Rectangle.Empty, Rectangle.Empty]; + } + + Rectangle protectedBounds = Rectangle.Intersect(bounds, clientBounds); + + Rectangle[] initialBands = protectedBounds.Width <= 0 || protectedBounds.Height <= 0 + ? [bounds, Rectangle.Empty, Rectangle.Empty, Rectangle.Empty] + : [ + Rectangle.FromLTRB(bounds.Left, bounds.Top, bounds.Right, protectedBounds.Top), + Rectangle.FromLTRB(bounds.Left, protectedBounds.Bottom, bounds.Right, bounds.Bottom), + Rectangle.FromLTRB(bounds.Left, protectedBounds.Top, protectedBounds.Left, protectedBounds.Bottom), + Rectangle.FromLTRB(protectedBounds.Right, protectedBounds.Top, bounds.Right, protectedBounds.Bottom) + ]; + + if (additionalProtectedBounds.Length == 0) + { + return initialBands; + } + + List paintBands = [.. initialBands]; + + foreach (Rectangle additionalProtectedBoundsItem in additionalProtectedBounds) + { + Rectangle clippedProtectedBounds = Rectangle.Intersect(bounds, additionalProtectedBoundsItem); + + if (clippedProtectedBounds.Width <= 0 || clippedProtectedBounds.Height <= 0) + { + continue; + } + + for (int i = paintBands.Count - 1; i >= 0; i--) + { + Rectangle band = paintBands[i]; + Rectangle intersection = Rectangle.Intersect(band, clippedProtectedBounds); + + if (intersection.Width <= 0 || intersection.Height <= 0) + { + continue; + } + + paintBands.RemoveAt(i); + AddPaintBand(Rectangle.FromLTRB(band.Left, band.Top, band.Right, intersection.Top)); + AddPaintBand(Rectangle.FromLTRB(band.Left, intersection.Bottom, band.Right, band.Bottom)); + AddPaintBand(Rectangle.FromLTRB(band.Left, intersection.Top, intersection.Left, intersection.Bottom)); + AddPaintBand(Rectangle.FromLTRB(intersection.Right, intersection.Top, band.Right, intersection.Bottom)); + } + } + + return [.. paintBands]; + + void AddPaintBand(Rectangle band) + { + if (band.Width > 0 && band.Height > 0) + { + paintBands.Add(band); + } + } + } + + private unsafe Rectangle[] GetVisibleScrollBarRectangles(Rectangle bounds) + { + if (!IsHandleCreated + || !PInvokeCore.GetWindowRect(this, out RECT windowRect)) + { + return []; + } + + List scrollBarBounds = []; + AddVisibleScrollBar(HorizontalScrollBarObjectId); + AddVisibleScrollBar(VerticalScrollBarObjectId); + + return [.. scrollBarBounds]; + + void AddVisibleScrollBar(OBJECT_IDENTIFIER objectId) + { + SCROLLBARINFO scrollBarInfo = new() + { + cbSize = (uint)sizeof(SCROLLBARINFO) + }; + + if (!PInvoke.GetScrollBarInfo((HWND)Handle, objectId, ref scrollBarInfo) + || (scrollBarInfo.rgstate[0] & StateSystemInvisible) != 0) + { + return; + } + + Rectangle scrollBarRectangle = Rectangle.FromLTRB( + scrollBarInfo.rcScrollBar.left - windowRect.left, + scrollBarInfo.rcScrollBar.top - windowRect.top, + scrollBarInfo.rcScrollBar.right - windowRect.left, + scrollBarInfo.rcScrollBar.bottom - windowRect.top); + scrollBarRectangle.Intersect(bounds); + + if (scrollBarRectangle.Width > 0 && scrollBarRectangle.Height > 0) + { + scrollBarBounds.Add(scrollBarRectangle); + } + } + } + + private Rectangle GetNativeClientRectangle() + { + if (!IsHandleCreated + || !PInvokeCore.GetWindowRect(this, out RECT windowRect)) + { + return Rectangle.Empty; + } + + PInvokeCore.GetClientRect(this, out RECT clientRect); + Point clientTopLeft = default; + PInvoke.ClientToScreen(this, ref clientTopLeft); + + return new Rectangle( + clientTopLeft.X - windowRect.left, + clientTopLeft.Y - windowRect.top, + clientRect.Width, + clientRect.Height); + } + + private AnimatedFocusIndicatorRenderer FocusIndicatorRenderer + => _focusIndicatorRenderer ??= new(this, InvalidateVisualStylesFrame); + + private unsafe void InvalidateVisualStylesFrame() + { + if (!IsHandleCreated) + { + return; + } + + PInvoke.RedrawWindow( + hWnd: this, + lprcUpdate: null, + hrgnUpdate: HRGN.Null, + flags: REDRAW_WINDOW_FLAGS.RDW_FRAME | REDRAW_WINDOW_FLAGS.RDW_INVALIDATE); + } + + private void WmReflectCommand(ref Message m) + { + if (_textBoxFlags[s_codeUpdateText] || _textBoxFlags[s_creatingHandle]) + { + return; + } + + uint hiword = m.WParamInternal.HIWORD; + + if (hiword == PInvoke.EN_CHANGE && CanRaiseTextChangedEvent) + { + OnTextChanged(EventArgs.Empty); + } + else if (hiword == PInvoke.EN_UPDATE) + { + // Force update to the Modified property, which will trigger ModifiedChanged event handlers + _ = Modified; } } private void WmSetFont(ref Message m) { base.WndProc(ref m); + if (!_textBoxFlags[s_multiline]) { PInvokeCore.SendMessage(this, PInvokeCore.EM_SETMARGINS, (WPARAM)(PInvoke.EC_LEFTMARGIN | PInvoke.EC_RIGHTMARGIN)); @@ -2036,9 +2964,11 @@ private void WmSetFont(ref Message m) private void WmGetDlgCode(ref Message m) { base.WndProc(ref m); + m.ResultInternal = AcceptsTab - ? (LRESULT)(m.ResultInternal | (int)PInvoke.DLGC_WANTTAB) - : (LRESULT)(m.ResultInternal & ~(int)(PInvoke.DLGC_WANTTAB | PInvoke.DLGC_WANTALLKEYS)); + ? (LRESULT)(nint)(m.ResultInternal | (int)PInvoke.DLGC_WANTTAB) + : (LRESULT)(nint)(m.ResultInternal & ~(int)(PInvoke.DLGC_WANTTAB + | PInvoke.DLGC_WANTALLKEYS)); } /// @@ -2081,19 +3011,34 @@ protected override void WndProc(ref Message m) { switch (m.MsgInternal) { + case PInvokeCore.WM_NCCALCSIZE: + WmNcCalcSize(ref m); + break; + + case PInvokeCore.WM_NCPAINT: + WmNcPaint(ref m); + break; + case PInvokeCore.WM_PRINT: + WmPrint(ref m); + break; + case PInvokeCore.WM_LBUTTONDBLCLK: _doubleClickFired = true; base.WndProc(ref m); break; + case MessageId.WM_REFLECT_COMMAND: WmReflectCommand(ref m); break; + case PInvokeCore.WM_GETDLGCODE: WmGetDlgCode(ref m); break; + case PInvokeCore.WM_SETFONT: WmSetFont(ref m); break; + case PInvokeCore.WM_CONTEXTMENU: if (ShortcutsEnabled) { @@ -2109,6 +3054,7 @@ protected override void WndProc(ref Message m) } break; + case PInvokeCore.WM_DESTROY: if (TryGetAccessibilityObject(out AccessibleObject? @object) && @object is TextBoxBaseAccessibleObject accessibleObject && !RecreatingHandle) @@ -2119,6 +3065,7 @@ protected override void WndProc(ref Message m) base.WndProc(ref m); break; + default: base.WndProc(ref m); break; diff --git a/src/System.Windows.Forms/System/Windows/Forms/Controls/TreeView/TreeView.SuspendMutation.cs b/src/System.Windows.Forms/System/Windows/Forms/Controls/TreeView/TreeView.SuspendMutation.cs new file mode 100644 index 00000000000..1c738bb8360 --- /dev/null +++ b/src/System.Windows.Forms/System/Windows/Forms/Controls/TreeView/TreeView.SuspendMutation.cs @@ -0,0 +1,25 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. + +namespace System.Windows.Forms; + +public partial class TreeView +{ +#if NET11_0_OR_GREATER + /// + protected override void BeginSuspendPaintingCore() + { + BeginSuspendPaintingScope(); + BeginUpdate(); + } + + /// + protected override void EndSuspendPaintingCore() + { + if (EndSuspendPaintingScope()) + { + EndUpdate(invalidate: !IsRecursiveInvalidateAfterSuspendPaintingRequested); + } + } +#endif +} diff --git a/src/System.Windows.Forms/System/Windows/Forms/Controls/TreeView/TreeView.cs b/src/System.Windows.Forms/System/Windows/Forms/Controls/TreeView/TreeView.cs index 4d6a3c53602..90e74d24f1e 100644 --- a/src/System.Windows.Forms/System/Windows/Forms/Controls/TreeView/TreeView.cs +++ b/src/System.Windows.Forms/System/Windows/Forms/Controls/TreeView/TreeView.cs @@ -43,6 +43,7 @@ public partial class TreeView : Control private TreeViewEventHandler? _onAfterSelect; private ItemDragEventHandler? _onItemDrag; private TreeNodeMouseHoverEventHandler? _onNodeMouseHover; + private EventHandler? _onNodeLeadingChanged; private EventHandler? _onRightToLeftLayoutChanged; internal TreeNode? _selectedNode; @@ -78,6 +79,7 @@ public partial class TreeView : Control private Collections.Specialized.BitVector32 _treeViewState; // see TREEVIEWSTATE_ constants above private static bool s_isScalingInitialized; + private static readonly int s_nodeLeadingProperty = PropertyStore.CreateKey(); private static Size? s_stateImageSize; private static Size? StateImageSize { @@ -117,6 +119,30 @@ internal ImageList.Indexer SelectedImageIndexer } } + private bool RequiresNonEvenHeightStyle + { + get + { +#if NET11_0_OR_GREATER + return _setOddHeight || UsesNodeLeading; +#else + return _setOddHeight; +#endif + } + } + + private bool UsesNodeLeading + { + get + { +#if NET11_0_OR_GREATER + return NodeLeading != 0.0f; +#else + return false; +#endif + } + } + private ImageList? _imageList; private int _indent = -1; private int _itemHeight = -1; @@ -170,6 +196,11 @@ public TreeView() SetStyle(ControlStyles.UserPaint, false); SetStyle(ControlStyles.StandardClick, false); SetStyle(ControlStyles.UseTextForAccessibility, false); + +#if NET11_0_OR_GREATER + FontChanged += HandleNodeLeadingMetricChanged; + DpiChangedAfterParent += HandleNodeLeadingMetricChanged; +#endif } internal override void ReleaseUiaProvider(HWND handle) @@ -366,7 +397,7 @@ protected override CreateParams CreateParams cp.Style |= (int)PInvoke.TVS_FULLROWSELECT; } - if (_setOddHeight) + if (RequiresNonEvenHeightStyle) { cp.Style |= (int)PInvoke.TVS_NONEVENHEIGHT; } @@ -760,6 +791,79 @@ public int Indent } } +#if NET11_0_OR_GREATER + /// + /// Gets or sets the per-node vertical leading factor applied on top of the live height. + /// Default 0.0f selects legacy behavior. + /// + /// + /// A non-negative . 0.0f (default) selects legacy behavior. + /// 1.0f opts in to the honest row-height calculation. Values greater than 1.0f add extra leading. + /// + /// + /// + /// This property applies uniformly to all nodes in the control. It is not a per-node knob, and per-node row + /// heights are not supported by the native control. + /// + /// + /// 0.0f keeps the legacy behavior, including the managed FontHeight + 3 + /// estimate and the default even-height rounding. 1.0f opts in to a row height derived from the live + /// height with TVS_NONEVENHEIGHT enabled. Other positive values scale the + /// honest base for denser or airier rows. + /// + /// + /// The factor is dimensionless. The resulting pixels scale with , whose height already + /// carries the current DPI, so the factor itself does not scale with DPI. + /// + /// + /// "Leading" (pronounced "ledding") is a typesetting term from the strips of lead metal once placed between + /// lines of type to add vertical spacing; it is unrelated to leading or guiding. + /// + /// + /// + /// The value is negative or not finite. + /// + [SRCategory(nameof(SR.CatAppearance))] + [DefaultValue(0.0f)] + [SRDescription(nameof(SR.TreeViewNodeLeadingDescr))] + public float NodeLeading + { + get => Properties.GetValueOrDefault(s_nodeLeadingProperty, 0.0f); + set + { + if (!float.IsFinite(value)) + { + throw new ArgumentOutOfRangeException(nameof(value)); + } + + ArgumentOutOfRangeException.ThrowIfNegative(value); + + float oldValue = NodeLeading; + if (oldValue != value) + { + bool oldUsesNodeLeading = oldValue != 0.0f; + bool newUsesNodeLeading = value != 0.0f; + + Properties.AddOrRemoveValue(s_nodeLeadingProperty, value, defaultValue: 0.0f); + + if (IsHandleCreated) + { + if (oldUsesNodeLeading != newUsesNodeLeading) + { + RecreateHandle(); + } + else if (newUsesNodeLeading) + { + ApplyItemHeightFromCurrentSettings(); + } + } + + OnNodeLeadingChanged(EventArgs.Empty); + } + } + } +#endif + /// /// The height of every item in the tree view, in pixels. /// @@ -769,6 +873,18 @@ public int ItemHeight { get { +#if NET11_0_OR_GREATER + if (UsesNodeLeading) + { + if (IsHandleCreated) + { + return (int)PInvokeCore.SendMessage(this, PInvoke.TVM_GETITEMHEIGHT); + } + + return GetNodeLeadingItemHeight(); + } +#endif + if (_itemHeight != -1) { return _itemHeight; @@ -798,21 +914,28 @@ public int ItemHeight _itemHeight = value; if (IsHandleCreated) { - if (_itemHeight % 2 != 0) + if (UsesNodeLeading) { - _setOddHeight = true; - try - { - RecreateHandle(); - } - finally + ApplyItemHeightFromCurrentSettings(); + } + else + { + if (_itemHeight % 2 != 0) { - _setOddHeight = false; + _setOddHeight = true; + try + { + RecreateHandle(); + } + finally + { + _setOddHeight = false; + } } - } - PInvokeCore.SendMessage(this, PInvoke.TVM_SETITEMHEIGHT, (WPARAM)value); - _itemHeight = (int)PInvokeCore.SendMessage(this, PInvoke.TVM_GETITEMHEIGHT); + PInvokeCore.SendMessage(this, PInvoke.TVM_SETITEMHEIGHT, (WPARAM)value); + _itemHeight = (int)PInvokeCore.SendMessage(this, PInvoke.TVM_GETITEMHEIGHT); + } } } } @@ -1481,6 +1604,19 @@ public event EventHandler? RightToLeftLayoutChanged remove => _onRightToLeftLayoutChanged -= value; } +#if NET11_0_OR_GREATER + /// + /// Occurs when the value of changes. + /// + [SRCategory(nameof(SR.CatPropertyChanged))] + [SRDescription(nameof(SR.TreeViewOnNodeLeadingChangedDescr))] + public event EventHandler? NodeLeadingChanged + { + add => _onNodeLeadingChanged += value; + remove => _onNodeLeadingChanged -= value; + } +#endif + /// /// Disables redrawing of the tree view. A call to beginUpdate() must be /// balanced by a following call to endUpdate(). Following a call to @@ -1569,10 +1705,9 @@ protected override void Dispose(bool disposing) /// beginUpdate(), any redrawing caused by operations performed on the /// combo box is deferred until the call to endUpdate(). /// - public void EndUpdate() - { - EndUpdateInternal(); - } + public void EndUpdate() => EndUpdate(invalidate: true); + + private void EndUpdate(bool invalidate) => EndUpdateInternal(invalidate); /// /// Expands all nodes at the root level. @@ -1796,6 +1931,16 @@ private void StateImageListChangedHandle(object? sender, EventArgs e) } } +#if NET11_0_OR_GREATER + private void HandleNodeLeadingMetricChanged(object? sender, EventArgs e) + { + if (UsesNodeLeading) + { + ApplyItemHeightFromCurrentSettings(); + } + } +#endif + /// /// Overridden to handle RETURN key. /// @@ -1913,10 +2058,7 @@ protected override void OnHandleCreated(EventArgs e) PInvokeCore.SendMessage(this, PInvoke.TVM_SETINDENT, (WPARAM)_indent); } - if (_itemHeight != -1) - { - PInvokeCore.SendMessage(this, PInvoke.TVM_SETITEMHEIGHT, (WPARAM)ItemHeight); - } + ApplyItemHeightFromCurrentSettings(); // Essentially we are setting the width to be infinite so that the // TreeView never thinks it needs a scrollbar when the first node is created @@ -1954,6 +2096,7 @@ protected override void OnHandleCreated(EventArgs e) flags); } } + finally { _treeViewState[TREEVIEWSTATE_stopResizeWindowMsgs] = false; @@ -2319,6 +2462,23 @@ protected virtual void OnRightToLeftLayoutChanged(EventArgs e) _onRightToLeftLayoutChanged?.Invoke(this, e); } +#if NET11_0_OR_GREATER + /// + /// Raises the event. + /// + /// An that contains the event data. + [EditorBrowsable(EditorBrowsableState.Advanced)] + protected virtual void OnNodeLeadingChanged(EventArgs e) + { + if (GetAnyDisposingInHierarchy()) + { + return; + } + + _onNodeLeadingChanged?.Invoke(this, e); + } +#endif + // Refresh the nodes by clearing the tree and adding the nodes back again // private void RefreshNodes() @@ -2349,6 +2509,13 @@ private void ResetItemHeight() RecreateHandle(); } +#if NET11_0_OR_GREATER + /// + /// This resets the node leading factor to the legacy default. + /// + private void ResetNodeLeading() => NodeLeading = 0.0f; +#endif + /// /// Retrieves true if the indent should be persisted in code gen. /// @@ -2359,6 +2526,10 @@ private void ResetItemHeight() /// private bool ShouldSerializeItemHeight() => (_itemHeight != -1); +#if NET11_0_OR_GREATER + private bool ShouldSerializeNodeLeading() => Properties.ContainsKey(s_nodeLeadingProperty); +#endif + private bool ShouldSerializeSelectedImageIndex() { if (_imageList is not null) @@ -2379,6 +2550,51 @@ private bool ShouldSerializeImageIndex() return ImageIndex != ImageList.Indexer.DefaultIndex; } + private void ApplyItemHeightFromCurrentSettings() + { + if (!IsHandleCreated) + { + return; + } + +#if NET11_0_OR_GREATER + if (UsesNodeLeading) + { + int itemHeight = GetNodeLeadingItemHeight(); + if ((int)PInvokeCore.SendMessage(this, PInvoke.TVM_GETITEMHEIGHT) != itemHeight) + { + PInvokeCore.SendMessage(this, PInvoke.TVM_SETITEMHEIGHT, (WPARAM)itemHeight); + } + + return; + } +#endif + + if (_itemHeight != -1) + { + PInvokeCore.SendMessage(this, PInvoke.TVM_SETITEMHEIGHT, (WPARAM)ItemHeight); + } + } + +#if NET11_0_OR_GREATER + private int GetNodeLeadingItemHeight() + { + double itemHeight = Math.Ceiling(NodeLeading * Font.Height); + + if (itemHeight < 1) + { + return 1; + } + + if (itemHeight >= short.MaxValue) + { + return short.MaxValue - 1; + } + + return (int)itemHeight; + } +#endif + /// /// Updated the sorted order /// diff --git a/src/System.Windows.Forms/System/Windows/Forms/Controls/UpDown/DomainUpDown.cs b/src/System.Windows.Forms/System/Windows/Forms/Controls/UpDown/DomainUpDown.cs index 70f700fe3b7..2205ba42f9b 100644 --- a/src/System.Windows.Forms/System/Windows/Forms/Controls/UpDown/DomainUpDown.cs +++ b/src/System.Windows.Forms/System/Windows/Forms/Controls/UpDown/DomainUpDown.cs @@ -57,17 +57,23 @@ public DomainUpDown() : base() [Editor($"System.Windows.Forms.Design.StringCollectionEditor, {Assemblies.SystemDesign}", typeof(UITypeEditor))] public DomainUpDownItemCollection Items => _domainItems ??= new DomainUpDownItemCollection(this); - [Browsable(false)] - [EditorBrowsable(EditorBrowsableState.Never)] - [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)] + [Browsable(true)] + [EditorBrowsable(EditorBrowsableState.Always)] + [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)] public new Padding Padding { get => base.Padding; set => base.Padding = value; } - [Browsable(false)] - [EditorBrowsable(EditorBrowsableState.Never)] + private new bool ShouldSerializePadding() + => Padding != DefaultPadding; + + private void ResetPadding() + => Padding = DefaultPadding; + + [Browsable(true)] + [EditorBrowsable(EditorBrowsableState.Always)] public new event EventHandler? PaddingChanged { add => base.PaddingChanged += value; @@ -512,7 +518,7 @@ internal override Size GetPreferredSizeCore(Size proposedConstraints) int width = LayoutUtils.OldGetLargestStringSizeInCollection(Font, Items).Width; // AdjustWindowRect with our border, since textbox is borderless. - width = SizeFromClientSizeInternal(new(width, height)).Width + _upDownButtons.Width; + width = GetPreferredWidth(width, height); return new Size(width, height) + Padding.Size; } } diff --git a/src/System.Windows.Forms/System/Windows/Forms/Controls/UpDown/NumericUpDown.cs b/src/System.Windows.Forms/System/Windows/Forms/Controls/UpDown/NumericUpDown.cs index fd1695e47f6..5c717356efe 100644 --- a/src/System.Windows.Forms/System/Windows/Forms/Controls/UpDown/NumericUpDown.cs +++ b/src/System.Windows.Forms/System/Windows/Forms/Controls/UpDown/NumericUpDown.cs @@ -214,17 +214,23 @@ public decimal Minimum } } - [Browsable(false)] - [EditorBrowsable(EditorBrowsableState.Never)] - [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)] + [Browsable(true)] + [EditorBrowsable(EditorBrowsableState.Always)] + [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)] public new Padding Padding { get => base.Padding; set => base.Padding = value; } - [Browsable(false)] - [EditorBrowsable(EditorBrowsableState.Never)] + private new bool ShouldSerializePadding() + => Padding != DefaultPadding; + + private void ResetPadding() + => Padding = DefaultPadding; + + [Browsable(true)] + [EditorBrowsable(EditorBrowsableState.Always)] public new event EventHandler? PaddingChanged { add => base.PaddingChanged += value; @@ -822,7 +828,7 @@ internal override Size GetPreferredSizeCore(Size proposedConstraints) } // Call AdjustWindowRect to add space for the borders - int width = SizeFromClientSizeInternal(new(textWidth, height)).Width + _upDownButtons.Width; + int width = GetPreferredWidth(textWidth, height); return new Size(width, height) + Padding.Size; } diff --git a/src/System.Windows.Forms/System/Windows/Forms/Controls/UpDown/UpDownBase.UpDownButtons.UpDownButtonsAccessibleObject.DirectionButtonAccessibleObject.cs b/src/System.Windows.Forms/System/Windows/Forms/Controls/UpDown/UpDownBase.UpDownButtons.UpDownButtonsAccessibleObject.DirectionButtonAccessibleObject.cs index e6ca2322105..b037005a273 100644 --- a/src/System.Windows.Forms/System/Windows/Forms/Controls/UpDown/UpDownBase.UpDownButtons.UpDownButtonsAccessibleObject.DirectionButtonAccessibleObject.cs +++ b/src/System.Windows.Forms/System/Windows/Forms/Controls/UpDown/UpDownBase.UpDownButtons.UpDownButtonsAccessibleObject.DirectionButtonAccessibleObject.cs @@ -33,17 +33,11 @@ public override Rectangle Bounds return Rectangle.Empty; } - // Get button bounds - Rectangle bounds = owner.Bounds; - bounds.Height /= 2; + // Get button bounds (client-relative), honoring the stacked vs side-by-side layout. + Rectangle bounds = owner.GetButtonRectangle(_up ? ButtonID.Up : ButtonID.Down); - if (!_up) - { - bounds.Y += bounds.Height; - } - - // Convert to screen coords - return owner.ParentInternal?.RectangleToScreen(bounds) ?? Rectangle.Empty; + // Convert from the buttons' client coordinates to screen coordinates. + return owner.RectangleToScreen(bounds); } } diff --git a/src/System.Windows.Forms/System/Windows/Forms/Controls/UpDown/UpDownBase.UpDownButtons.cs b/src/System.Windows.Forms/System/Windows/Forms/Controls/UpDown/UpDownBase.UpDownButtons.cs index 57e8edf7ca4..726ae729253 100644 --- a/src/System.Windows.Forms/System/Windows/Forms/Controls/UpDown/UpDownBase.UpDownButtons.cs +++ b/src/System.Windows.Forms/System/Windows/Forms/Controls/UpDown/UpDownBase.UpDownButtons.cs @@ -54,27 +54,68 @@ public event UpDownEventHandler? UpDown remove => _upDownEventHandler -= value; } + /// + /// When the two buttons are laid out side by side (see + /// ); otherwise they are stacked vertically. + /// + internal bool UseSideBySideButtons => _parent.UseSideBySideButtons; + + /// + /// Returns the client-area rectangle occupied by the requested button. In the classic (stacked) + /// layout the up button is the top half and the down button the bottom half. In the modern + /// (side-by-side) layout the increment (up) button is on the trailing edge and the decrement + /// (down) button on the leading edge, mirrored under right-to-left. + /// + internal Rectangle GetButtonRectangle(ButtonID button) + { + Rectangle client = ClientRectangle; + + if (UseSideBySideButtons) + { + int spacing = Math.Min(_parent.ModernButtonGroupSpacing, client.Width); + int availableWidth = Math.Max(0, client.Width - spacing); + int leadingWidth = (availableWidth + 1) / 2; + Rectangle leadingRect = new(client.X, client.Y, leadingWidth, client.Height); + Rectangle trailingRect = new( + client.X + leadingWidth + spacing, + client.Y, + availableWidth - leadingWidth, + client.Height); + + bool rightToLeft = _parent.RightToLeft == RightToLeft.Yes; + Rectangle upRect = rightToLeft ? leadingRect : trailingRect; + Rectangle downRect = rightToLeft ? trailingRect : leadingRect; + + return button == ButtonID.Up ? upRect : downRect; + } + + int halfHeight = client.Height / 2; + Rectangle topRect = new(client.X, client.Y, client.Width, halfHeight); + Rectangle bottomRect = new(client.X, client.Y + halfHeight, client.Width, client.Height - halfHeight); + + return button == ButtonID.Up ? topRect : bottomRect; + } + /// /// Called when the mouse button is pressed - we need to start spinning the value of the up-down control. /// /// The mouse event arguments. private void BeginButtonPress(MouseEventArgs e) { - int half_height = Size.Height / 2; + ButtonID button = GetButtonRectangle(ButtonID.Up).Contains(e.Location) + ? ButtonID.Up + : GetButtonRectangle(ButtonID.Down).Contains(e.Location) + ? ButtonID.Down + : ButtonID.None; - if (e.Y < half_height) + if (button == ButtonID.None) { - // Up button - _pushed = _captured = ButtonID.Up; - Invalidate(); - } - else - { - // Down button - _pushed = _captured = ButtonID.Down; - Invalidate(); + return; } + _pushed = _captured = button; + Invalidate(); + // Capture the mouse Capture = true; @@ -148,14 +189,8 @@ protected override void OnMouseMove(MouseEventArgs e) if (Capture) { - // Determine button area - Rectangle rect = ClientRectangle; - rect.Height /= 2; - - if (_captured == ButtonID.Down) - { - rect.Y += rect.Height; - } + // Determine the captured button area + Rectangle rect = GetButtonRectangle(_captured); // Test if the mouse has moved outside the button area if (rect.Contains(e.X, e.Y)) @@ -188,19 +223,19 @@ protected override void OnMouseMove(MouseEventArgs e) } // Logic for seeing which button is Hot if any - Rectangle rectUp = ClientRectangle, rectDown = ClientRectangle; - rectUp.Height /= 2; - rectDown.Y += rectDown.Height / 2; + Rectangle rectUp = GetButtonRectangle(ButtonID.Up); + Rectangle rectDown = GetButtonRectangle(ButtonID.Down); // Check if the mouse is on the upper or lower button. Note that it could be in neither. - if (rectUp.Contains(e.X, e.Y)) - { - _mouseOver = ButtonID.Up; - Invalidate(); - } - else if (rectDown.Contains(e.X, e.Y)) + ButtonID mouseOver = rectUp.Contains(e.X, e.Y) + ? ButtonID.Up + : rectDown.Contains(e.X, e.Y) + ? ButtonID.Down + : ButtonID.None; + + if (_mouseOver != mouseOver) { - _mouseOver = ButtonID.Down; + _mouseOver = mouseOver; Invalidate(); } @@ -270,9 +305,48 @@ protected override void OnPaint(PaintEventArgs e) int half_height = ClientSize.Height / 2; // Draw the up and down buttons - if (Application.IsDarkModeEnabled) + if (UseSideBySideButtons) + { + // Modern side-by-side layout: decrement (down) on the leading edge, increment (up) on + // the trailing edge (mirrored under right-to-left via GetButtonRectangle). Rendered with + // the modern control-button renderer, which adapts to both light and dark modes. + bool isDarkMode = Application.IsDarkModeEnabled; + + using Graphics cachedGraphics = EnsureCachedBitmap(ClientSize.Width, ClientSize.Height); + + DrawModernControlButton( + cachedGraphics, + GetButtonRectangle(ButtonID.Down), + ModernControlButtonStyle.Down, + GetButtonState(ButtonID.Down), + isDarkMode); + + DrawModernControlButton( + cachedGraphics, + GetButtonRectangle(ButtonID.Up), + ModernControlButtonStyle.Up, + GetButtonState(ButtonID.Up), + isDarkMode); + + e.GraphicsInternal.DrawImageUnscaled(_cachedBitmap, new Point(0, 0)); + + int spacing = _parent.ModernButtonGroupSpacing; + if (spacing > 0) + { + Rectangle upBounds = GetButtonRectangle(ButtonID.Up); + Rectangle downBounds = GetButtonRectangle(ButtonID.Down); + Rectangle gap = new( + Math.Min(upBounds.Right, downBounds.Right), + 0, + spacing, + ClientSize.Height); + using var gapBrush = _parent.BackColor.GetCachedSolidBrushScope(); + e.Graphics.FillRectangle(gapBrush, gap); + } + } + else if (Application.IsDarkModeEnabled) { - Graphics cachedGraphics = EnsureCachedBitmap( + using Graphics cachedGraphics = EnsureCachedBitmap( _parent._defaultButtonsWidth, ClientSize.Height); @@ -356,7 +430,7 @@ protected override void OnPaint(PaintEventArgs e) _pushed == ButtonID.Down ? ButtonState.Pushed : (Enabled ? ButtonState.Normal : ButtonState.Inactive)); } - if (half_height != (ClientSize.Height + 1) / 2) + if (!UseSideBySideButtons && half_height != (ClientSize.Height + 1) / 2) { // When control has odd height, a line needs to be drawn below the buttons with the BackColor. Color color = _parent.BackColor; @@ -374,6 +448,27 @@ protected override void OnPaint(PaintEventArgs e) base.OnPaint(e); } + /// + /// Computes the modern rendering state for the requested button from the current enabled, + /// pushed, and hot states. + /// + private ModernControlButtonState GetButtonState(ButtonID button) + { + if (!Enabled) + { + return ModernControlButtonState.Disabled; + } + + if (_pushed == button) + { + return ModernControlButtonState.Pressed; + } + + return _mouseOver == button + ? ModernControlButtonState.Hover + : ModernControlButtonState.Normal; + } + /// /// Ensures that the Bitmap has the correct size and returns the Graphics object from that Bitmap. /// diff --git a/src/System.Windows.Forms/System/Windows/Forms/Controls/UpDown/UpDownBase.UpDownEdit.cs b/src/System.Windows.Forms/System/Windows/Forms/Controls/UpDown/UpDownBase.UpDownEdit.cs index bfdb45c17ef..408617a9f65 100644 --- a/src/System.Windows.Forms/System/Windows/Forms/Controls/UpDown/UpDownBase.UpDownEdit.cs +++ b/src/System.Windows.Forms/System/Windows/Forms/Controls/UpDown/UpDownBase.UpDownEdit.cs @@ -1,6 +1,7 @@ // Licensed to the .NET Foundation under one or more agreements. // The .NET Foundation licenses this file to you under the MIT license. +using System.Drawing; using Windows.Win32.UI.Accessibility; namespace System.Windows.Forms; @@ -19,6 +20,18 @@ internal UpDownEdit(UpDownBase parent) _parent = parent; } + public override VisualStylesMode VisualStylesMode + { + get => VisualStylesMode.Classic; + set + { + } + } + + private protected override void OnNcPaint(Graphics graphics, HDC windowHdc) + { + } + [AllowNull] public override string Text { @@ -113,7 +126,9 @@ protected override void OnKeyUp(KeyEventArgs e) protected override void OnGotFocus(EventArgs e) { _parent.SetActiveControl(this); + _parent.SetModernFocusState(focused: true); _parent.InvokeGotFocus(_parent, e); + _parent.Invalidate(); if (IsAccessibilityObjectCreated) { @@ -122,6 +137,10 @@ protected override void OnGotFocus(EventArgs e) } protected override void OnLostFocus(EventArgs e) - => _parent.InvokeLostFocus(_parent, e); + { + _parent.SetModernFocusState(focused: false); + _parent.InvokeLostFocus(_parent, e); + _parent.Invalidate(); + } } } diff --git a/src/System.Windows.Forms/System/Windows/Forms/Controls/UpDown/UpDownBase.cs b/src/System.Windows.Forms/System/Windows/Forms/Controls/UpDown/UpDownBase.cs index 9508d76256a..008f69fda41 100644 --- a/src/System.Windows.Forms/System/Windows/Forms/Controls/UpDown/UpDownBase.cs +++ b/src/System.Windows.Forms/System/Windows/Forms/Controls/UpDown/UpDownBase.cs @@ -3,7 +3,10 @@ using System.ComponentModel; using System.Drawing; +using System.Drawing.Drawing2D; using System.Runtime.InteropServices; +using System.Windows.Forms.Layout; +using System.Windows.Forms.Rendering.Animation; using System.Windows.Forms.VisualStyles; using Microsoft.Win32; @@ -18,7 +21,16 @@ public abstract partial class UpDownBase : ContainerControl private const int DefaultWheelScrollLinesPerPage = 1; private const int DefaultButtonsWidth = 16; private const int DefaultControlWidth = 120; - private const int ThemedBorderWidth = 1; // width of custom border we draw when themed + + // width of custom border we draw when themed + private const int ThemedBorderWidth = 1; + + // Modern (Net11+) chrome geometry. The edit and button group share the border thickness and + // internal chrome inset used by TextBoxBase; only the gap between the two buttons is additional. + private const int ModernBorderThickness = 1; + private const int ModernButtonGroupSpacingLogical = 2; + private const int ModernCornerRadius = 14; + private const int ModernFocusBandHeight = 4; private const BorderStyle DefaultBorderStyle = BorderStyle.Fixed3D; private const LeftRightAlignment DefaultUpDownAlign = LeftRightAlignment.Right; private const int DefaultTimerInterval = 500; @@ -33,6 +45,7 @@ public abstract partial class UpDownBase : ContainerControl /// The current border for this edit control. /// private BorderStyle _borderStyle = DefaultBorderStyle; + private AnimatedFocusIndicatorRenderer? _focusIndicatorRenderer; // Mouse wheel movement private int _wheelDelta; @@ -47,6 +60,7 @@ public UpDownBase() _defaultButtonsWidth = LogicalToDeviceUnits(DefaultButtonsWidth); _upDownButtons = new UpDownButtons(this); + _upDownEdit = new UpDownEdit(this) { BorderStyle = BorderStyle.None, @@ -64,7 +78,10 @@ public UpDownBase() Controls.AddRange([_upDownButtons, _upDownEdit]); - SetStyle(ControlStyles.Opaque | ControlStyles.FixedHeight | ControlStyles.ResizeRedraw, true); + SetStyle(ControlStyles.Opaque + | ControlStyles.FixedHeight + | ControlStyles.ResizeRedraw, true); + SetStyle(ControlStyles.StandardClick, false); SetStyle(ControlStyles.UseTextForAccessibility, false); } @@ -220,6 +237,7 @@ protected override CreateParams CreateParams CreateParams cp = base.CreateParams; cp.Style &= ~(int)WINDOW_STYLE.WS_BORDER; + if (!Application.RenderWithVisualStyles) { switch (_borderStyle) @@ -237,6 +255,14 @@ protected override CreateParams CreateParams } } + /// + /// When , the up/down buttons are laid out side by side (decrement on the + /// leading edge, increment on the trailing edge) rather than stacked. This provides a larger, + /// more accessible click target and is used when a modern + /// ( or above) is in effect. + /// + internal bool UseSideBySideButtons => EffectiveVisualStylesMode >= VisualStylesMode.Net11; + /// /// Deriving classes can override this to configure a default size for their control. /// This is more efficient than setting the size in the control's constructor. @@ -334,19 +360,36 @@ public int PreferredHeight { get { - int height = FontHeight; - - // Adjust for the border style - if (_borderStyle != BorderStyle.None) + if (!UseSideBySideButtons) { - height += SystemInformation.BorderSize.Height * 4 + 3; + int height = FontHeight; + + // Adjust for the border style + if (_borderStyle != BorderStyle.None) + { + height += SystemInformation.BorderSize.Height * 4 + 3; + } + else + { + height += 3; + } + + return height; } - else + + int contentInset = ModernContentInset; + int preferredHeight = FontHeight + (contentInset * 2); + + if (_borderStyle == BorderStyle.Fixed3D) { - height += 3; + int roundedChromeMinimumHeight = LogicalToDeviceUnits(ModernCornerRadius) + + LogicalToDeviceUnits(ModernBorderThickness) + + LogicalToDeviceUnits(TextBoxBase.VisualStylesInternalChromeInset); + + preferredHeight = Math.Max(preferredHeight, roundedChromeMinimumHeight); } - return height; + return preferredHeight; } } @@ -450,7 +493,13 @@ public LeftRightAlignment UpDownAlign internal override Rectangle ApplyBoundsConstraints(int suggestedX, int suggestedY, int proposedWidth, int proposedHeight) { - return base.ApplyBoundsConstraints(suggestedX, suggestedY, proposedWidth, PreferredHeight); + int height = AutoSize + ? PreferredHeight + Padding.Vertical + : UseSideBySideButtons + ? proposedHeight + : PreferredHeight; + + return base.ApplyBoundsConstraints(suggestedX, suggestedY, proposedWidth, height); } internal override void ReleaseUiaProvider(HWND handle) @@ -478,6 +527,14 @@ protected override void RescaleConstantsForDpi(int deviceDpiOld, int deviceDpiNe base.RescaleConstantsForDpi(deviceDpiOld, deviceDpiNew); _defaultButtonsWidth = LogicalToDeviceUnits(DefaultButtonsWidth); _upDownButtons.Width = _defaultButtonsWidth; + CommonProperties.xClearPreferredSizeCache(this); + + if (AutoSize) + { + Height = PreferredHeight; + } + + PositionControls(); } /// @@ -503,9 +560,31 @@ protected override void OnHandleCreated(EventArgs e) protected override void OnHandleDestroyed(EventArgs e) { SystemEvents.UserPreferenceChanged -= UserPreferenceChanged; + _focusIndicatorRenderer?.Dispose(); + _focusIndicatorRenderer = null; base.OnHandleDestroyed(e); } + /// + protected override void OnVisualStylesModeChanged(EventArgs e) + { + base.OnVisualStylesModeChanged(e); + _focusIndicatorRenderer?.Synchronize(Focused, invalidate: false); + CommonProperties.xClearPreferredSizeCache(this); + + if (AutoSize) + { + Height = PreferredHeight; + } + + PositionControls(); + + if (IsHandleCreated) + { + Invalidate(true); + } + } + /// /// Handles painting the buttons on the control. /// @@ -513,6 +592,14 @@ protected override void OnPaint(PaintEventArgs e) { base.OnPaint(e); + if (UseSideBySideButtons) + { + // In modern mode we draw a single frame around the whole control (edit and buttons); the + // themed/classic single-textbox border below does not apply. + DrawModernBorder(e); + return; + } + Rectangle editBounds = _upDownEdit.Bounds; Color backColor = BackColor; @@ -636,7 +723,11 @@ protected virtual void OnTextBoxLostFocus(object? source, EventArgs e) /// protected virtual void OnTextBoxResize(object? source, EventArgs e) { - Height = PreferredHeight; + if (!UseSideBySideButtons || AutoSize) + { + Height = PreferredHeight; + } + PositionControls(); } @@ -755,6 +846,7 @@ protected override void OnMouseWheel(MouseEventArgs e) // Evaluate number of bands to scroll int scrollBands = (int)(wheelScrollLines * partialNotches); + if (scrollBands != 0) { int absScrollBands; @@ -801,7 +893,11 @@ protected override void OnFontChanged(EventArgs e) // Clear the font height cache FontHeight = -1; - Height = PreferredHeight; + if (!UseSideBySideButtons || AutoSize) + { + Height = PreferredHeight; + } + PositionControls(); base.OnFontChanged(e); @@ -829,11 +925,19 @@ private void OnUpDown(object? source, UpDownEventArgs e) /// private void PositionControls() { + if (UseSideBySideButtons) + { + PositionControlsModern(); + return; + } + Rectangle upDownEditBounds = Rectangle.Empty; Rectangle upDownButtonsBounds = Rectangle.Empty; - Rectangle clientArea = new(Point.Empty, ClientSize); - int totalClientWidth = clientArea.Width; + Rectangle clientArea = LayoutUtils.DeflateRect( + new Rectangle(Point.Empty, ClientSize), + Padding); + bool themed = Application.RenderWithVisualStyles; BorderStyle borderStyle = BorderStyle; @@ -852,6 +956,7 @@ private void PositionControls() if (_upDownButtons is not null) { int borderFixup = (themed) ? 1 : 2; + if (borderStyle == BorderStyle.None) { borderFixup = 0; @@ -872,8 +977,8 @@ private void PositionControls() if (updownAlign == LeftRightAlignment.Left) { // If the buttons are aligned to the left, swap position of text box/buttons - upDownButtonsBounds.X = totalClientWidth - upDownButtonsBounds.Right; - upDownEditBounds.X = totalClientWidth - upDownEditBounds.Right; + upDownButtonsBounds.X = clientArea.Left + (clientArea.Right - upDownButtonsBounds.Right); + upDownEditBounds.X = clientArea.Left + (clientArea.Right - upDownEditBounds.Right); } // Apply locations @@ -886,10 +991,209 @@ private void PositionControls() } } + private int ModernContentInset + => LogicalToDeviceUnits( + (_borderStyle == BorderStyle.None + ? 0 + : ModernBorderThickness) + + TextBoxBase.VisualStylesInternalChromeInset); + + internal int ModernButtonGroupSpacing + => LogicalToDeviceUnits(ModernButtonGroupSpacingLogical); + + internal int GetModernButtonGroupWidth() + => (_defaultButtonsWidth * 2) + ModernButtonGroupSpacing; + + internal int GetPreferredWidth(int textWidth, int height) + => UseSideBySideButtons + ? textWidth + (ModernContentInset * 2) + GetModernButtonGroupWidth() + : SizeFromClientSizeInternal(new(textWidth, height)).Width + _upDownButtons.Width; + + /// + /// Calculates the size and position of the upDownEdit control and the side-by-side updown buttons + /// when a modern is in effect. Both the edit and the buttons are + /// inset by so they sit inside the frame drawn by + /// and clear its rounded corners. + /// + private void PositionControlsModern() + { + Rectangle clientArea = LayoutUtils.DeflateRect( + new Rectangle(Point.Empty, ClientSize), + Padding); + + int pad = ModernContentInset; + int buttonsWidth = Math.Min(GetModernButtonGroupWidth(), Math.Max(0, clientArea.Width - (pad * 2))); + + Rectangle inner = clientArea; + inner.Inflate(-pad, -pad); + + if (inner.Width < 0 || inner.Height < 0) + { + inner = new Rectangle( + x: Math.Min(pad, clientArea.Width), + y: Math.Min(pad, clientArea.Height), + width: 0, + height: 0); + } + + Rectangle upDownEditBounds = inner; + upDownEditBounds.Width = Math.Max(0, inner.Width - buttonsWidth); + + Rectangle upDownButtonsBounds = new( + x: inner.Right - buttonsWidth, + y: inner.Top, + width: buttonsWidth, + height: inner.Height); + + // Left/right updown align translation (also honors RTL). + if (RtlTranslateLeftRight(UpDownAlign) == LeftRightAlignment.Left) + { + upDownButtonsBounds.X = clientArea.Left + (clientArea.Right - upDownButtonsBounds.Right); + upDownEditBounds.X = clientArea.Left + (clientArea.Right - upDownEditBounds.Right); + } + + _upDownEdit?.Bounds = upDownEditBounds; + + if (_upDownButtons is not null) + { + _upDownButtons.Bounds = upDownButtonsBounds; + _upDownButtons.Invalidate(); + } + } + + /// + /// Draws the modern frame around the whole control (edit and side-by-side buttons) using the same + /// rounded/flat chrome a stand-alone modern TextBox paints in its non-client area. The corners are + /// filled with the parent's back color so the rounded frame blends against it. + /// + private void DrawModernBorder(PaintEventArgs e) + { + Rectangle bounds = ClientRectangle; + + if (bounds.Width <= 0 || bounds.Height <= 0) + { + return; + } + + int cornerRadius = LogicalToDeviceUnits(ModernCornerRadius); + int borderThickness = LogicalToDeviceUnits(ModernBorderThickness); + + // The adorner (border) color matches the modern TextBox chrome, which uses the fore color. + Color adornerColor = ForeColor; + Color parentBackColor = Parent?.BackColor ?? BackColor; + Color clientBackColor = BackColor; + + using var clientBackgroundBrush = clientBackColor.GetCachedSolidBrushScope(); + using var adornerPen = adornerColor.GetCachedPenScope(borderThickness); + + Rectangle deflatedBounds = bounds; + deflatedBounds.Width -= 1; + deflatedBounds.Height -= 1; + + Graphics graphics = e.Graphics; + using GraphicsStateScope graphicsState = new(graphics); + graphics.SmoothingMode = SmoothingMode.AntiAlias; + + // AddRoundedRectangle receives the bounding size of each corner arc, so one corner size plus + // the border thickness is the minimum height that avoids overlapping curves. + bool canRenderRoundedChrome = TextBoxBase.CanRenderVisualStylesRoundedChrome( + deflatedBounds, + cornerRadius, + borderThickness); + + ParentBackgroundRenderer.Paint(this, graphics, bounds, parentBackColor); + + switch (_borderStyle) + { + case BorderStyle.None: + graphics.FillRectangle(clientBackgroundBrush, deflatedBounds); + break; + + case BorderStyle.FixedSingle: + graphics.FillRectangle(clientBackgroundBrush, deflatedBounds); + graphics.DrawRectangle(adornerPen, deflatedBounds); + break; + + case BorderStyle.Fixed3D: + if (canRenderRoundedChrome) + { + using GraphicsPath bodyPath = new(); + bodyPath.AddRoundedRectangle(deflatedBounds, new Size(cornerRadius, cornerRadius)); + graphics.FillPath(clientBackgroundBrush, bodyPath); + graphics.DrawPath(adornerPen, bodyPath); + } + else + { + graphics.FillRectangle(clientBackgroundBrush, deflatedBounds); + graphics.DrawRectangle(adornerPen, deflatedBounds); + } + + break; + } + + if (_borderStyle == BorderStyle.Fixed3D && canRenderRoundedChrome) + { + Color focusColor = ModernFocusColor; + FocusIndicatorRenderer.DrawRoundedFocusIndicator( + graphics, + deflatedBounds, + cornerRadius, + borderThickness, + LogicalToDeviceUnits(ModernFocusBandHeight), + adornerColor, + focusColor); + } + else if (Focused && _borderStyle == BorderStyle.Fixed3D) + { + Color focusColor = ModernFocusColor; + using var focusPen = focusColor.GetCachedPenScope(borderThickness); + graphics.DrawLine( + focusPen, + deflatedBounds.Left, + deflatedBounds.Bottom, + deflatedBounds.Right, + deflatedBounds.Bottom); + graphics.DrawLine( + focusPen, + deflatedBounds.Left, + deflatedBounds.Bottom - 1, + deflatedBounds.Right, + deflatedBounds.Bottom - 1); + } + } + + private AnimatedFocusIndicatorRenderer FocusIndicatorRenderer + => _focusIndicatorRenderer ??= new(this, InvalidateModernFocusIndicator); + + private static Color ModernFocusColor + => TextBoxBase.GetVisualStylesFocusColor(SystemInformation.HighContrast); + + private void SetModernFocusState(bool focused) + { + if (!UseSideBySideButtons || _borderStyle != BorderStyle.Fixed3D) + { + return; + } + + FocusIndicatorRenderer.SetFocused( + focused, + animate: SystemInformation.UIEffectsEnabled && !SystemInformation.HighContrast); + } + + private void InvalidateModernFocusIndicator() + { + int focusBandHeight = LogicalToDeviceUnits(ModernFocusBandHeight); + Rectangle focusBounds = ClientRectangle; + focusBounds.Y = Math.Max(focusBounds.Top, focusBounds.Bottom - focusBandHeight); + focusBounds.Height = Math.Min(focusBandHeight, focusBounds.Height); + Invalidate(focusBounds); + } + /// /// Selects a range of text in the up-down control. /// - public void Select(int start, int length) => _upDownEdit.Select(start, length); + public void Select(int start, int length) + => _upDownEdit.Select(start, length); /// /// Create a new with the points translated from the diff --git a/src/System.Windows.Forms/System/Windows/Forms/Form.RevealMode.cs b/src/System.Windows.Forms/System/Windows/Forms/Form.RevealMode.cs new file mode 100644 index 00000000000..d3eee07dbd0 --- /dev/null +++ b/src/System.Windows.Forms/System/Windows/Forms/Form.RevealMode.cs @@ -0,0 +1,124 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. + +using System.ComponentModel; +using Windows.Win32.Graphics.Dwm; + +namespace System.Windows.Forms; + +public partial class Form +{ +#if NET11_0_OR_GREATER + private static readonly int s_propFormRevealMode = PropertyStore.CreateKey(); + + /// + /// Gets or sets how this form is presented while its initial appearance is prepared. + /// + /// + /// A value. When not explicitly set, the effective value is + /// or , resolved from + /// . + /// + /// + /// + /// As an ambient property, a form that does not have this value set explicitly resolves it from + /// . Unlike a control-level ambient property that + /// chains through a parent hierarchy, this property does not chain through a parent hierarchy: + /// deferred reveal is a top-level-window-only concept (see remarks on + /// and the DWM cloaking mechanism it relies on), so only + /// itself, and the process-wide default, participate. + /// + /// + /// A splash screen or other form that should always appear instantly, even while the rest of the + /// application defers by default, can set this property on itself to + /// without affecting the process-wide default. + /// + /// + [SRCategory(nameof(SR.CatWindowStyle))] + [AmbientValue(FormRevealMode.Inherit)] + [SRDescription(nameof(SR.FormFormRevealModeDescr))] + public virtual FormRevealMode FormRevealMode + { + get + { + if (!Properties.TryGetValue(s_propFormRevealMode, out FormRevealMode value) + || value == FormRevealMode.Inherit) + { + value = Application.IsFormRevealDeferred ? FormRevealMode.Deferred : FormRevealMode.Classic; + } + + return value; + } + set + { + SourceGenerated.EnumValidator.Validate(value, nameof(value)); + + if (value == FormRevealMode.Inherit) + { + Properties.RemoveValue(s_propFormRevealMode); + } + else + { + Properties.AddValue(s_propFormRevealMode, value); + } + } + } + + private bool ShouldSerializeFormRevealMode() => Properties.ContainsKey(s_propFormRevealMode); + + private void ResetFormRevealMode() => Properties.RemoveValue(s_propFormRevealMode); + + private bool DeferredAppearanceCloaked + { + get => Properties.GetValueOrDefault(s_propFormAppearanceCloaked, false); + set => Properties.AddOrRemoveValue(s_propFormAppearanceCloaked, value, defaultValue: false); + } + + private void CloakForDeferredAppearanceIfNeeded() + { + if (!ShouldUseDeferredAppearanceCloak()) + { + return; + } + + if (SetDwmCloak(cloaked: true)) + { + DeferredAppearanceCloaked = true; + } + } + + private void UncloakDeferredAppearanceIfNeeded() + { + if (!DeferredAppearanceCloaked) + { + return; + } + + if (SetDwmCloak(cloaked: false)) + { + DeferredAppearanceCloaked = false; + } + } + + private void ClearDeferredAppearanceCloakState() => DeferredAppearanceCloaked = false; + + private bool ShouldUseDeferredAppearanceCloak() + => FormRevealMode == FormRevealMode.Deferred + && TopLevel + && !IsMdiChild + && Visible + && IsHandleCreated; + + private unsafe bool SetDwmCloak(bool cloaked) + { + BOOL cloak = cloaked; + HRESULT result = PInvoke.DwmSetWindowAttribute( + HWND, + DWMWINDOWATTRIBUTE.DWMWA_CLOAK, + &cloak, + (uint)sizeof(BOOL)); + + return result.Succeeded; + } +#endif +} diff --git a/src/System.Windows.Forms/System/Windows/Forms/Form.SystemTextSize.cs b/src/System.Windows.Forms/System/Windows/Forms/Form.SystemTextSize.cs new file mode 100644 index 00000000000..266766186d1 --- /dev/null +++ b/src/System.Windows.Forms/System/Windows/Forms/Form.SystemTextSize.cs @@ -0,0 +1,72 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. + +using System.ComponentModel; + +namespace System.Windows.Forms; + +public partial class Form +{ +#if NET11_0_OR_GREATER + private static readonly object s_systemTextSizeChangedEvent = new(); + + private double _lastSystemTextSize = ScaleHelper.GetSystemTextScaleFactor(); + + /// + /// Occurs on this top-level when the Windows Accessibility text-scale setting changes. + /// + /// + /// + /// On operating systems earlier than Windows 10 version 1507, this event is not raised. + /// + /// + [SRCategory(nameof(SR.CatLayout))] + [SRDescription(nameof(SR.FormOnSystemTextSizeChangedDescr))] + public event EventHandler? SystemTextSizeChanged + { + add => Events.AddHandler(s_systemTextSizeChangedEvent, value); + remove => Events.RemoveHandler(s_systemTextSizeChangedEvent, value); + } + + /// + /// Raises the event. + /// + /// An that contains the event data. + /// + /// + /// WinForms raises this event only after re-reading the current Windows Accessibility text-scale factor and + /// confirming that the underlying value actually changed. There is no dedicated Windows message for text-scale + /// changes. + /// + /// + [EditorBrowsable(EditorBrowsableState.Advanced)] + protected virtual void OnSystemTextSizeChanged(EventArgs e) + { + if (Events[s_systemTextSizeChangedEvent] is EventHandler handler) + { + handler(this, e); + } + } + + /// + /// Handles the WM_SETTINGCHANGE message. + /// + private void WmSettingChange(ref Message m) + { + base.WndProc(ref m); + + if (!GetTopLevel() || !ScaleHelper.TryGetSystemTextScaleFactor(out double systemTextSize)) + { + return; + } + + if (systemTextSize == _lastSystemTextSize) + { + return; + } + + _lastSystemTextSize = systemTextSize; + OnSystemTextSizeChanged(EventArgs.Empty); + } +#endif +} diff --git a/src/System.Windows.Forms/System/Windows/Forms/Form.cs b/src/System.Windows.Forms/System/Windows/Forms/Form.cs index c3900d09c6b..a4eb041efb0 100644 --- a/src/System.Windows.Forms/System/Windows/Forms/Form.cs +++ b/src/System.Windows.Forms/System/Windows/Forms/Form.cs @@ -137,6 +137,9 @@ public partial class Form : ContainerControl private static readonly int s_propFormCaptionTextColor = PropertyStore.CreateKey(); private static readonly int s_propFormCaptionBackColor = PropertyStore.CreateKey(); private static readonly int s_propFormScreenCaptureMode = PropertyStore.CreateKey(); +#if NET11_0_OR_GREATER + private static readonly int s_propFormAppearanceCloaked = PropertyStore.CreateKey(); +#endif // Form per instance members // Note: Do not add anything to this list unless absolutely necessary. @@ -4209,6 +4212,10 @@ protected override void OnHandleCreated(EventArgs e) { SetScreenCaptureModeInternal(FormScreenCaptureMode); } + +#if NET11_0_OR_GREATER + CloakForDeferredAppearanceIfNeeded(); +#endif } /// @@ -4219,6 +4226,9 @@ protected override void OnHandleCreated(EventArgs e) [EditorBrowsable(EditorBrowsableState.Advanced)] protected override void OnHandleDestroyed(EventArgs e) { +#if NET11_0_OR_GREATER + ClearDeferredAppearanceCloakState(); +#endif base.OnHandleDestroyed(e); _formStateEx[s_formStateExUseMdiChildProc] = 0; @@ -4230,6 +4240,35 @@ protected override void OnHandleDestroyed(EventArgs e) } } + // Snapshot of the Windows High Contrast state, used to detect a transition in OnSystemColorsChanged. + private bool _lastHighContrast = SystemInformation.HighContrast; + + /// + protected override void OnSystemColorsChanged(EventArgs e) + { + base.OnSystemColorsChanged(e); + + // Windows High Contrast forces Classic rendering (see Control.EffectiveVisualStylesMode), which changes + // CreateParams for this form and its children. When the High Contrast state actually toggles we must + // rebuild the handle so those CreateParams are re-read. Recreating the top-level Form destroys and + // rebuilds the entire child HWND tree, so the rebuild itself propagates the new mode to every child - + // no per-child notification is needed. Doing this only on Form (not Control) avoids recreating each + // child a second time as Control.OnSystemColorsChanged walks down the tree. + bool highContrast = SystemInformation.HighContrast; + + if (highContrast != _lastHighContrast) + { + _lastHighContrast = highContrast; + + // If visual styles are explicitly disabled they stay disabled regardless of High Contrast, so + // nothing about CreateParams changes and there is no need to recreate. + if (VisualStylesMode is not VisualStylesMode.Disabled) + { + RecreateHandle(); + } + } + } + /// /// Handles the event that a helpButton is clicked /// @@ -7175,6 +7214,12 @@ protected override void WndProc(ref Message m) case PInvokeCore.WM_ERASEBKGND: WmEraseBkgnd(ref m); break; + case PInvokeCore.WM_PAINT: + base.WndProc(ref m); +#if NET11_0_OR_GREATER + UncloakDeferredAppearanceIfNeeded(); +#endif + break; case PInvokeCore.WM_NCDESTROY: WmNCDestroy(ref m); @@ -7225,6 +7270,11 @@ protected override void WndProc(ref Message m) case PInvokeCore.WM_DPICHANGED: WmDpiChanged(ref m); break; +#if NET11_0_OR_GREATER + case PInvokeCore.WM_SETTINGCHANGE: + WmSettingChange(ref m); + break; +#endif default: base.WndProc(ref m); break; diff --git a/src/System.Windows.Forms/System/Windows/Forms/FormRevealMode.cs b/src/System.Windows.Forms/System/Windows/Forms/FormRevealMode.cs new file mode 100644 index 00000000000..076b339a558 --- /dev/null +++ b/src/System.Windows.Forms/System/Windows/Forms/FormRevealMode.cs @@ -0,0 +1,42 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. + +namespace System.Windows.Forms; + +#if NET11_0_OR_GREATER +/// +/// Specifies how WinForms presents a top-level while its initial appearance is +/// prepared. +/// +public enum FormRevealMode +{ + /// + /// The form inherits its effective reveal behavior from . + /// This is the ambient default and is never returned by after + /// resolution. + /// + /// + /// + /// This value is the ambient sentinel: assigning it to clears any + /// local override so the value is inherited from again. + /// + /// + Inherit = -1, + + /// + /// Uses the classic WinForms form presentation behavior. The form is never cloaked. + /// + Classic = 0, + + /// + /// Defers the initial top-level form presentation to help prevent default-background flash. + /// + /// + /// + /// Deferral applies to the form background. Deep child-control trees can still produce visible + /// updates after the form is shown. + /// + /// + Deferred = 1 +} +#endif diff --git a/src/System.Windows.Forms/System/Windows/Forms/ISupportSuspendPainting.cs b/src/System.Windows.Forms/System/Windows/Forms/ISupportSuspendPainting.cs new file mode 100644 index 00000000000..77c3ea39ee5 --- /dev/null +++ b/src/System.Windows.Forms/System/Windows/Forms/ISupportSuspendPainting.cs @@ -0,0 +1,22 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. + +namespace System.Windows.Forms; + +#if NET11_0_OR_GREATER +/// +/// Provides methods for temporarily suspending and resuming painting. +/// +public interface ISupportSuspendPainting +{ + /// + /// Begins a painting suspension region. + /// + void BeginSuspendPainting(); + + /// + /// Ends a painting suspension region. + /// + void EndSuspendPainting(); +} +#endif diff --git a/src/System.Windows.Forms/System/Windows/Forms/Layout/PropertyNames.cs b/src/System.Windows.Forms/System/Windows/Forms/Layout/PropertyNames.cs index 98779fafb7c..8c5f5165d51 100644 --- a/src/System.Windows.Forms/System/Windows/Forms/Layout/PropertyNames.cs +++ b/src/System.Windows.Forms/System/Windows/Forms/Layout/PropertyNames.cs @@ -79,6 +79,7 @@ internal static class PropertyNames public const string TextImageRelation = "TextImageRelation"; public const string UseCompatibleTextRendering = "UseCompatibleTextRendering"; public const string Visible = "Visible"; + public const string VisualStylesMode = "VisualStylesMode"; public const string WordWrap = "WordWrap"; public const string WrapContents = "WrapContents"; } diff --git a/src/System.Windows.Forms/System/Windows/Forms/LayoutSuspendTraversal.cs b/src/System.Windows.Forms/System/Windows/Forms/LayoutSuspendTraversal.cs new file mode 100644 index 00000000000..119c35d6cb4 --- /dev/null +++ b/src/System.Windows.Forms/System/Windows/Forms/LayoutSuspendTraversal.cs @@ -0,0 +1,27 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. + +namespace System.Windows.Forms; + +#if NET11_0_OR_GREATER +/// +/// Specifies how layout suspension traverses a control tree while painting is suspended. +/// +public enum LayoutSuspendTraversal +{ + /// + /// Does not suspend layout. + /// + None = 0, + + /// + /// Suspends layout only for the target control. + /// + TopLevelOnly = 1, + + /// + /// Suspends layout for the target control and all its descendants. + /// + Traverse = 2, +} +#endif diff --git a/src/System.Windows.Forms/System/Windows/Forms/ParentBackgroundRenderer.cs b/src/System.Windows.Forms/System/Windows/Forms/ParentBackgroundRenderer.cs new file mode 100644 index 00000000000..36957259b8e --- /dev/null +++ b/src/System.Windows.Forms/System/Windows/Forms/ParentBackgroundRenderer.cs @@ -0,0 +1,38 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. + +using System.Drawing; + +namespace System.Windows.Forms; + +internal static class ParentBackgroundRenderer +{ + internal static void Paint( + Control control, + Graphics graphics, + Rectangle bounds, + Color fallbackColor) + { + ArgumentNullException.ThrowIfNull(control); + ArgumentNullException.ThrowIfNull(graphics); + + using GraphicsStateScope state = new(graphics); + using Region paintRegion = new(bounds); + + // Keep an existing clip (for example, the native TextBox client area) in effect while the + // parent background is painted beneath the complete antialiased control body. + using Region currentClip = graphics.Clip; + paintRegion.Intersect(currentClip); + + Control? parent = control.ParentInternal; + if (parent is null || parent.IsDisposed) + { + using var fallbackBrush = fallbackColor.GetCachedSolidBrushScope(); + graphics.FillRegion(fallbackBrush, paintRegion); + return; + } + + using PaintEventArgs paintEventArgs = new(graphics, bounds); + control.PaintTransparentBackground(paintEventArgs, bounds, paintRegion); + } +} diff --git a/src/System.Windows.Forms/System/Windows/Forms/Rendering/Animation/AnimatedControlRenderer.cs b/src/System.Windows.Forms/System/Windows/Forms/Rendering/Animation/AnimatedControlRenderer.cs new file mode 100644 index 00000000000..7c2f754c514 --- /dev/null +++ b/src/System.Windows.Forms/System/Windows/Forms/Rendering/Animation/AnimatedControlRenderer.cs @@ -0,0 +1,177 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. + +using System.Drawing; +using System.Windows.Forms.Rendering.Button; + +namespace System.Windows.Forms.Rendering.Animation; + +/// +/// Represents an abstract base class for animated control renderers. +/// +/// The control associated with the renderer. +internal abstract class AnimatedControlRenderer(Control control) : IDisposable +{ + private const float InteractionShadeAmount = 0.08f; + + private Color _accentColor; + private bool _accentColorInitialized; + private bool _disposedValue; + protected float AnimationProgress = 1; + + /// + /// Callback for the animation progress. This method is called by the animation manager on each + /// frame tick delivered by HighPrecisionTimer. + /// + /// A fraction between 0 and 1 representing the animation progress. + public virtual void AnimationProc(float animationProgress) + { + AnimationProgress = animationProgress; + } + + /// + /// Called when the control needs to be painted. + /// + /// The to paint the control. + public abstract void RenderControl(Graphics graphics); + + /// + /// Invalidates the control, causing it to be redrawn, which in turns triggers + /// . + /// + public void Invalidate() => control.Invalidate(); + + /// + /// Starts the animation and gets the animation parameters. + /// + public void StartAnimation() + { + if (IsRunning) + { + return; + } + + // Get the animation parameters. + (int animationDuration, AnimationCycle animationCycle) = OnAnimationStarted(); + + // Register the renderer with the animation manager. + AnimationManager.RegisterOrUpdateAnimationRenderer( + this, + animationDuration, + animationCycle); + + IsRunning = true; + } + + internal void StopAnimationInternal() => IsRunning = false; + + public void RestartAnimation() + { + if (IsRunning) + { + StopAnimation(); + } + + StartAnimation(); + } + + /// + /// Called in a derived class when the animation starts. The derived class returns the animation duration and cycle type. + /// + /// + /// Tuple containing the animation duration and cycle type. + /// + protected abstract (int animationDuration, AnimationCycle animationCycle) OnAnimationStarted(); + + /// + /// Called by the animation manager when the animation ends. + /// + internal void EndAnimation() + { + OnAnimationEnded(); + } + + /// + /// Called in a derived class when the animation ends. + /// The derived class can perform any cleanup or state change operations. + /// + protected abstract void OnAnimationEnded(); + + /// + /// Can be called by an implementing control, when the animation needs to be stopped or restarted. + /// + public void StopAnimation() + { + AnimationManager.Suspend(this); + OnAnimationStopped(); + } + + /// + /// Called in the derived class when the animation is stopped. + /// The derived class can perform any cleanup or state change operations. + /// + protected abstract void OnAnimationStopped(); + + /// + /// Gets a value indicating whether the animation is running. + /// + public bool IsRunning { get; private set; } + + /// + /// Gets the control associated with the renderer. + /// + protected Control Control => control; + + protected Color WindowsAccentColor + { + get + { + if (!_accentColorInitialized) + { + _accentColor = Application.GetWindowsAccentColor(); + _accentColorInitialized = true; + } + + return _accentColor; + } + } + + internal bool IsAccentColorCached => _accentColorInitialized; + + internal void InvalidateAccentColor() + => _accentColorInitialized = false; + + internal static Color ApplyInteractionShade(Color color, float progress) + { + Color interactionColor = PopupButtonColorMath.TowardsContrast(color, InteractionShadeAmount); + return PopupButtonColorMath.Blend(color, interactionColor, progress); + } + + /// + /// Releases the unmanaged resources used by the and optionally releases the managed resources. + /// + /// to release both managed and unmanaged resources; to release only unmanaged resources. + protected virtual void Dispose(bool disposing) + { + if (!_disposedValue) + { + if (disposing) + { + // Remove the renderer from the animation manager. + AnimationManager.UnregisterAnimationRenderer(this); + } + + _disposedValue = true; + } + } + + /// + /// Releases all resources used by the . + /// + public void Dispose() + { + // Do not change this code. Put cleanup code in 'Dispose(bool disposing)' method. + Dispose(disposing: true); + GC.SuppressFinalize(this); + } +} diff --git a/src/System.Windows.Forms/System/Windows/Forms/Rendering/Animation/AnimatedFocusIndicatorRenderer.cs b/src/System.Windows.Forms/System/Windows/Forms/Rendering/Animation/AnimatedFocusIndicatorRenderer.cs new file mode 100644 index 00000000000..0ef0c90c736 --- /dev/null +++ b/src/System.Windows.Forms/System/Windows/Forms/Rendering/Animation/AnimatedFocusIndicatorRenderer.cs @@ -0,0 +1,171 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. + +using System.Drawing; +using System.Drawing.Drawing2D; +using System.Windows.Forms.Rendering.Button; + +namespace System.Windows.Forms.Rendering.Animation; + +/// +/// Animates a rounded focus indicator between a control's border and focus colors. +/// +internal sealed class AnimatedFocusIndicatorRenderer : AnimatedControlRenderer +{ + private const int AnimationDurationMilliseconds = 200; + + private readonly Action _invalidate; + private float _focusCurrent; + private float _focusStart; + private float _focusTarget; + private bool _initialized; + + public AnimatedFocusIndicatorRenderer(Control control, Action invalidate) + : base(control) + { + ArgumentNullException.ThrowIfNull(invalidate); + _invalidate = invalidate; + } + + internal float FocusAmount + { + get + { + EnsureInitialized(Control.Focused ? 1f : 0f); + return _focusCurrent; + } + } + + internal Color GetCurrentColor(Color borderColor, Color focusColor) + => PopupButtonColorMath.Blend(borderColor, focusColor, FocusAmount); + + internal void SetFocused(bool focused, bool animate) + { + float target = focused ? 1f : 0f; + EnsureInitialized(1f - target); + + if (_focusTarget == target && (!IsRunning || animate)) + { + return; + } + + _focusStart = _focusCurrent; + _focusTarget = target; + + if (!animate) + { + Synchronize(focused, invalidate: true); + return; + } + + RestartAnimation(); + _invalidate(); + } + + internal void Synchronize(bool focused, bool invalidate) + { + if (IsRunning) + { + StopAnimation(); + } + + _focusCurrent = focused ? 1f : 0f; + _focusStart = _focusCurrent; + _focusTarget = _focusCurrent; + _initialized = true; + AnimationProgress = 1f; + + if (invalidate) + { + _invalidate(); + } + } + + internal void DrawRoundedFocusIndicator( + Graphics graphics, + Rectangle bounds, + int cornerSize, + int borderThickness, + int focusBandHeight, + Color borderColor, + Color focusColor) + { + if (bounds.Width <= 0 + || bounds.Height <= 0 + || cornerSize <= 0 + || borderThickness <= 0 + || focusBandHeight <= 0 + || FocusAmount <= 0f) + { + return; + } + + int focusBandTop = Math.Max(bounds.Top, bounds.Bottom - focusBandHeight + 1); + Rectangle focusClip = Rectangle.FromLTRB( + bounds.Left, + focusBandTop, + bounds.Right + 1, + bounds.Bottom + 1); + + using GraphicsStateScope state = new(graphics); + graphics.SetClip(focusClip, CombineMode.Intersect); + graphics.SmoothingMode = SmoothingMode.AntiAlias; + + using GraphicsPath focusPath = new(); + focusPath.AddRoundedRectangle(bounds, new Size(cornerSize, cornerSize)); + + Color color = GetCurrentColor(borderColor, focusColor); + using var focusPen = color.GetCachedPenScope(borderThickness); + graphics.DrawPath(focusPen, focusPath); + } + + public override void AnimationProc(float animationProgress) + { + base.AnimationProc(animationProgress); + float easedProgress = EaseOut(animationProgress); + _focusCurrent = Lerp(_focusStart, _focusTarget, easedProgress); + _invalidate(); + } + + public override void RenderControl(Graphics graphics) + { + } + + protected override (int animationDuration, AnimationCycle animationCycle) OnAnimationStarted() + { + AnimationProgress = 0f; + return (AnimationDurationMilliseconds, AnimationCycle.Once); + } + + protected override void OnAnimationStopped() + { + } + + protected override void OnAnimationEnded() + { + StopAnimation(); + _focusCurrent = _focusTarget; + _focusStart = _focusCurrent; + AnimationProgress = 1f; + _invalidate(); + } + + private void EnsureInitialized(float initialValue) + { + if (_initialized) + { + return; + } + + _focusCurrent = initialValue; + _focusStart = initialValue; + _focusTarget = initialValue; + _initialized = true; + } + + private static float EaseOut(float progress) + => 1f - ((1f - progress) * (1f - progress)); + + private static float Lerp(float start, float end, float amount) + => start + ((end - start) * Math.Clamp(amount, 0f, 1f)); +} diff --git a/src/System.Windows.Forms/System/Windows/Forms/Rendering/Animation/AnimationCycle.cs b/src/System.Windows.Forms/System/Windows/Forms/Rendering/Animation/AnimationCycle.cs new file mode 100644 index 00000000000..0dfac5f12f6 --- /dev/null +++ b/src/System.Windows.Forms/System/Windows/Forms/Rendering/Animation/AnimationCycle.cs @@ -0,0 +1,11 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. + +namespace System.Windows.Forms.Rendering.Animation; + +internal enum AnimationCycle +{ + Once, + Loop, + Bounce +} diff --git a/src/System.Windows.Forms/System/Windows/Forms/Rendering/Animation/AnimationManager.AnimationRendererItem.cs b/src/System.Windows.Forms/System/Windows/Forms/Rendering/Animation/AnimationManager.AnimationRendererItem.cs new file mode 100644 index 00000000000..dae4505a21b --- /dev/null +++ b/src/System.Windows.Forms/System/Windows/Forms/Rendering/Animation/AnimationManager.AnimationRendererItem.cs @@ -0,0 +1,25 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. + +namespace System.Windows.Forms.Rendering.Animation; + +internal partial class AnimationManager +{ + private class AnimationRendererItem + { + public long StopwatchTarget; + + public AnimationRendererItem(AnimatedControlRenderer renderer, int animationDuration, AnimationCycle animationCycle) + { + Renderer = renderer; + AnimationDuration = animationDuration; + AnimationCycle = animationCycle; + } + + public AnimatedControlRenderer Renderer { get; } + public int AnimationDuration { get; set; } + public int FrameCount { get; set; } + public AnimationCycle AnimationCycle { get; set; } + public int FrameOffset { get; set; } = 1; + } +} diff --git a/src/System.Windows.Forms/System/Windows/Forms/Rendering/Animation/AnimationManager.cs b/src/System.Windows.Forms/System/Windows/Forms/Rendering/Animation/AnimationManager.cs new file mode 100644 index 00000000000..9cfc01ce919 --- /dev/null +++ b/src/System.Windows.Forms/System/Windows/Forms/Rendering/Animation/AnimationManager.cs @@ -0,0 +1,154 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. + +using System.Collections.Concurrent; +using System.Windows.Forms.Animation; + +namespace System.Windows.Forms.Rendering.Animation; + +/// +/// Process-wide dispatcher that drives all instances from a single +/// HighPrecisionTimer registration. +/// +/// +/// +/// The frame cadence is provided by HighPrecisionTimer (60 Hz where supported, otherwise 30 Hz). +/// Because that timer marshals its callback back to the captured when this +/// manager was constructed, the per-frame work runs on the UI thread and can invalidate controls directly. +/// +/// +internal partial class AnimationManager +{ + private readonly Stopwatch _stopwatch; + private readonly HighPrecisionTimer.TimerRegistration _timerRegistration; + + private readonly ConcurrentDictionary _renderer = []; + + private static AnimationManager? s_instance; + + private static AnimationManager Instance + => s_instance ??= new AnimationManager(); + + private AnimationManager() + { + _stopwatch = Stopwatch.StartNew(); + + // HighPrecisionTimer captures the current SynchronizationContext and posts each tick back to it, + // so OnFrameTickAsync runs on the UI thread. A SynchronizationContext is expected here because the + // first animation is always started from the UI thread. + _timerRegistration = HighPrecisionTimer.Register(OnFrameTickAsync); + + Application.ApplicationExit += (sender, e) => DisposeRenderer(); + } + + /// + /// Disposes the animation renderers and releases the timer registration. + /// + private void DisposeRenderer() + { + // Stop the timer. + _timerRegistration.Dispose(); + + foreach (AnimatedControlRenderer renderer in _renderer.Keys) + { + renderer.Dispose(); + } + } + + /// + /// Registers an animation renderer. + /// + /// The animation renderer to register. + /// The duration of the animation. + /// The animation cycle. + public static void RegisterOrUpdateAnimationRenderer( + AnimatedControlRenderer animationRenderer, + int animationDuration, + AnimationCycle animationCycle) + { + // If the renderer is already registered, update the animation parameters. + if (Instance._renderer.TryGetValue(animationRenderer, out AnimationRendererItem? renderItem)) + { + renderItem.StopwatchTarget = Instance._stopwatch.ElapsedMilliseconds + animationDuration; + renderItem.AnimationDuration = animationDuration; + renderItem.AnimationCycle = animationCycle; + + return; + } + + renderItem = new AnimationRendererItem(animationRenderer, animationDuration, animationCycle) + { + StopwatchTarget = Instance._stopwatch.ElapsedMilliseconds + animationDuration, + }; + + _ = Instance._renderer.TryAdd(animationRenderer, renderItem); + } + + /// + /// Unregisters an animation renderer. + /// + /// The animation renderer to unregister. + internal static void UnregisterAnimationRenderer(AnimatedControlRenderer animationRenderer) + { + _ = Instance._renderer.TryRemove(animationRenderer, out _); + } + + internal static void Suspend(AnimatedControlRenderer animatedControlRenderer) + { + if (Instance._renderer.TryGetValue(animatedControlRenderer, out AnimationRendererItem? renderItem)) + { + renderItem.Renderer.StopAnimationInternal(); + } + } + + /// + /// Handles a single frame tick delivered by HighPrecisionTimer (on the UI thread). + /// + private ValueTask OnFrameTickAsync(HighPrecisionTimerTick tick, CancellationToken cancellationToken) + { + long elapsedStopwatchMilliseconds = _stopwatch.ElapsedMilliseconds; + + foreach (AnimationRendererItem item in _renderer.Values) + { + if (!item.Renderer.IsRunning) + { + continue; + } + + long remainingAnimationMilliseconds = item.StopwatchTarget - elapsedStopwatchMilliseconds; + + item.FrameCount += item.FrameOffset; + + if (elapsedStopwatchMilliseconds >= item.StopwatchTarget) + { + switch (item.AnimationCycle) + { + case AnimationCycle.Once: + item.Renderer.EndAnimation(); + break; + + case AnimationCycle.Loop: + item.FrameCount = 0; + item.StopwatchTarget = elapsedStopwatchMilliseconds + item.AnimationDuration; + item.Renderer.RestartAnimation(); + break; + + case AnimationCycle.Bounce: + item.FrameOffset = -item.FrameOffset; + item.StopwatchTarget = elapsedStopwatchMilliseconds + item.AnimationDuration; + item.Renderer.RestartAnimation(); + break; + } + + continue; + } + + float progress = 1 - (remainingAnimationMilliseconds / (float)item.AnimationDuration); + + // We are already on the UI thread (HighPrecisionTimer marshalled us here), so invoke directly. + item.Renderer.AnimationProc(progress); + } + + return ValueTask.CompletedTask; + } +} diff --git a/src/System.Windows.Forms/System/Windows/Forms/Rendering/Button/AnimatedPopupButtonRenderer.cs b/src/System.Windows.Forms/System/Windows/Forms/Rendering/Button/AnimatedPopupButtonRenderer.cs new file mode 100644 index 00000000000..36a914140de --- /dev/null +++ b/src/System.Windows.Forms/System/Windows/Forms/Rendering/Button/AnimatedPopupButtonRenderer.cs @@ -0,0 +1,249 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. + +using System.Drawing; +using System.Windows.Forms.ButtonInternal; +using System.Windows.Forms.Rendering.Animation; +using PushButtonState = System.Windows.Forms.VisualStyles.PushButtonState; + +namespace System.Windows.Forms.Rendering.Button; + +/// +/// Drives and renders a whose is +/// when modern visual styles or dark mode are active, using the concave key-cap +/// look of . +/// +/// +/// +/// The renderer owns two interpolated animation channels (hover and press) that are advanced by the shared +/// on each high-precision timer tick. When settled it simply renders the +/// current channel values, so the same path serves both the animating and the resting button. +/// +/// +internal sealed class AnimatedPopupButtonRenderer : AnimatedControlRenderer +{ + private const int AnimationDurationMilliseconds = 160; + private const float CustomHoverShadeAmount = 0.05f; + private const float CustomPressedShadeAmount = 0.12f; + + private float _hoverCurrent; + private float _hoverStart; + private float _hoverTarget; + + private float _pressCurrent; + private float _pressStart; + private float _pressTarget; + private readonly ModernButtonDarkModeRenderer _baseColorRenderer = new(); + private readonly ButtonDarkModeAdapter _layoutAdapter; + + public AnimatedPopupButtonRenderer(Forms.ButtonBase button) + : base(button) + { + _layoutAdapter = new ButtonDarkModeAdapter(button); + } + + private Forms.ButtonBase Button => (Forms.ButtonBase)Control; + + /// + /// Updates the hover and press targets from the current interaction state and, if anything changed, + /// (re)starts the interpolation from the current channel values towards the new targets. + /// + public void SetInteractionState(bool hovered, bool pressed, bool selected) + { + float hoverTarget = hovered ? 1f : 0f; + float pressTarget = pressed || selected ? 1f : 0f; + + if (hoverTarget == _hoverTarget && pressTarget == _pressTarget) + { + return; + } + + _hoverStart = _hoverCurrent; + _pressStart = _pressCurrent; + _hoverTarget = hoverTarget; + _pressTarget = pressTarget; + + RestartAnimation(); + } + + protected override (int animationDuration, AnimationCycle animationCycle) OnAnimationStarted() + => (AnimationDurationMilliseconds, AnimationCycle.Once); + + public override void AnimationProc(float animationProgress) + { + base.AnimationProc(animationProgress); + + _hoverCurrent = Lerp(_hoverStart, _hoverTarget, PopupButtonEasing.EaseOutCubic(animationProgress)); + _pressCurrent = Lerp(_pressStart, _pressTarget, PopupButtonEasing.EaseInOutQuad(animationProgress)); + + Invalidate(); + } + + protected override void OnAnimationEnded() + { + _hoverCurrent = _hoverTarget; + _pressCurrent = _pressTarget; + _hoverStart = _hoverCurrent; + _pressStart = _pressCurrent; + + // The animation has reached its target; stop ticking so the settled button is not repainted every frame. + // A later interaction change restarts a fresh interpolation via SetInteractionState. + StopAnimation(); + + Invalidate(); + } + + protected override void OnAnimationStopped() + { + // Keep the current channel values so a restart interpolates smoothly from where we are. + } + + /// + /// Called from the button's OnPaint. Works both while the animation is running (driven by + /// ) and when it is settled. + /// + public override void RenderControl(Graphics graphics) + { + Forms.ButtonBase button = Button; + + FlatButtonAppearance flatAppearance = button.FlatAppearance; + _baseColorRenderer.DeviceDpi = button.DeviceDpi; + _baseColorRenderer.FlatAppearance = flatAppearance; + + PushButtonState state = _pressTarget > 0f + ? PushButtonState.Pressed + : _hoverTarget > 0f + ? PushButtonState.Hot + : PushButtonState.Normal; + bool highContrast = SystemInformation.HighContrast; + Color faceColor; + Color foreColor; + Color borderColor; + + if (highContrast) + { + bool highlighted = button.Enabled + && (button.Focused || button.MouseIsOver || button.IsDefault); + faceColor = highlighted ? SystemColors.Highlight : SystemColors.Control; + foreColor = !button.Enabled + ? SystemColors.GrayText + : highlighted + ? SystemColors.HighlightText + : SystemColors.ControlText; + borderColor = button.Enabled ? SystemColors.ControlText : SystemColors.GrayText; + } + else + { + (Color baseColor, Color hoverColor, Color pressedColor) = GetStateColors(); + + faceColor = PopupButtonColorMath.Blend(baseColor, hoverColor, _hoverCurrent); + faceColor = PopupButtonColorMath.Blend(faceColor, pressedColor, _pressCurrent); + bool useAutomaticForeColor = button.EffectiveVisualStylesModeInternal >= VisualStylesMode.Net11 + ? !button.ShouldSerializeForeColor() + : button.ForeColor == Forms.Control.DefaultForeColor; + foreColor = !useAutomaticForeColor + ? button.ForeColor + : _baseColorRenderer.GetTextColor(state, button.IsDefault, faceColor); + borderColor = flatAppearance.BorderColor.IsEmpty + ? PopupButtonColorMath.TowardsContrast(faceColor, 0.35f) + : flatAppearance.BorderColor; + } + + PopupButtonRenderContext context = new() + { + Bounds = button.ClientRectangle, + Text = button.Text, + Font = button.Font, + BackColor = faceColor, + ForeColor = foreColor, + SurfaceColor = button.Parent?.BackColor ?? button.BackColor, + UseAutomaticForeColor = button.EffectiveVisualStylesModeInternal >= VisualStylesMode.Net11 + ? !button.ShouldSerializeForeColor() + : button.ForeColor == Forms.Control.DefaultForeColor, + BorderColor = borderColor, + BorderWidth = flatAppearance.BorderSize, + Enabled = button.Enabled, + Focused = button.Focused && button.ShowFocusCues, + Pressed = button.MouseIsDown || _pressTarget > 0f, + IsDefault = button.IsDefault, + IsDarkMode = Application.IsDarkModeEnabled, + AnimationState = new PopupButtonAnimationState(_hoverCurrent, _pressCurrent), + TextAlign = button.TextAlign, + ImageSize = button.Image?.Size ?? Size.Empty, + ImageAlign = button.ImageAlign, + TextImageRelation = button.TextImageRelation, + RightToLeft = button.RightToLeft, + Padding = button.Padding, + DeviceDpi = button.DeviceDpi, + ShowKeyboardCues = button.ShowKeyboardCues, + HighContrast = highContrast + }; + + if (context.HighContrast || context.Bounds.Width < 8 || context.Bounds.Height < 8) + { + using PaintEventArgs paintEventArgs = new(graphics, button.ClientRectangle); + button.PaintBackground(paintEventArgs, button.ClientRectangle); + } + else + { + ParentBackgroundRenderer.Paint( + button, + graphics, + button.ClientRectangle, + button.Parent?.BackColor ?? button.BackColor); + } + + if (context.Bounds.Width < 8 || context.Bounds.Height < 8) + { + PopupButtonKeyCapRenderer.Render(graphics, context); + return; + } + + Image? image = button.Image; + Rectangle contentBounds = PopupButtonKeyCapRenderer.GetContentBounds(context); + ButtonBaseAdapter.LayoutData layout = _layoutAdapter.GetLayoutData(contentBounds); + Action? paintImage = null; + + if (image is not null) + { + paintImage = _ => + { + using PaintEventArgs paintEventArgs = new(graphics, button.ClientRectangle); + _layoutAdapter.PaintImage(paintEventArgs, layout); + }; + } + + PopupButtonKeyCapRenderer.Render( + graphics, + context, + paintImage, + (layout.TextBounds, layout.ImageBounds)); + } + + internal (Color BaseColor, Color HoverColor, Color PressedColor) GetStateColors() + { + Forms.ButtonBase button = Button; + FlatButtonAppearance flatAppearance = button.FlatAppearance; + _baseColorRenderer.DeviceDpi = button.DeviceDpi; + _baseColorRenderer.FlatAppearance = flatAppearance; + + bool hasCustomBackColor = button.BackColor != Forms.Control.DefaultBackColor; + Color baseColor = hasCustomBackColor + ? button.BackColor + : _baseColorRenderer.GetBackgroundColor(PushButtonState.Normal, isDefault: false); + Color hoverColor = !flatAppearance.MouseOverBackColorCore.IsEmpty + ? flatAppearance.MouseOverBackColorCore + : hasCustomBackColor + ? PopupButtonColorMath.TowardsContrast(baseColor, CustomHoverShadeAmount) + : _baseColorRenderer.GetBackgroundColor(PushButtonState.Hot, isDefault: false); + Color pressedColor = !flatAppearance.MouseDownBackColorCore.IsEmpty + ? flatAppearance.MouseDownBackColorCore + : hasCustomBackColor + ? PopupButtonColorMath.TowardsContrast(baseColor, CustomPressedShadeAmount) + : _baseColorRenderer.GetBackgroundColor(PushButtonState.Pressed, isDefault: false); + + return (baseColor, hoverColor, pressedColor); + } + + private static float Lerp(float start, float end, float amount) => start + ((end - start) * amount); +} diff --git a/src/System.Windows.Forms/System/Windows/Forms/Rendering/Button/PopupButtonAnimationState.cs b/src/System.Windows.Forms/System/Windows/Forms/Rendering/Button/PopupButtonAnimationState.cs new file mode 100644 index 00000000000..f59ab86609a --- /dev/null +++ b/src/System.Windows.Forms/System/Windows/Forms/Rendering/Button/PopupButtonAnimationState.cs @@ -0,0 +1,35 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. + +namespace System.Windows.Forms.Rendering.Button; + +/// +/// Immutable snapshot of the animation progress of a key-cap button. +/// +/// +/// +/// The control owns the animation timing (driven by the shared animation manager and its +/// high-precision timer); the renderer only consumes the resulting progress values. This keeps the +/// renderer usable outside a live control (designer surfaces, preview bitmaps) where no timer exists — +/// simply pass fixed progress values. +/// +/// +/// Hover progress, 0 (not hovered) to 1 (fully hovered). +/// Press progress, 0 (released) to 1 (fully pressed). +internal readonly struct PopupButtonAnimationState(float hoverProgress, float pressProgress) +{ + /// + /// Gets a state with no hover and no press applied. + /// + public static PopupButtonAnimationState None => default; + + /// + /// Gets the hover progress in the range 0-1. + /// + public float HoverProgress { get; } = Math.Clamp(hoverProgress, 0f, 1f); + + /// + /// Gets the press progress in the range 0-1. + /// + public float PressProgress { get; } = Math.Clamp(pressProgress, 0f, 1f); +} diff --git a/src/System.Windows.Forms/System/Windows/Forms/Rendering/Button/PopupButtonColorMath.cs b/src/System.Windows.Forms/System/Windows/Forms/Rendering/Button/PopupButtonColorMath.cs new file mode 100644 index 00000000000..88ce9322ed3 --- /dev/null +++ b/src/System.Windows.Forms/System/Windows/Forms/Rendering/Button/PopupButtonColorMath.cs @@ -0,0 +1,202 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. + +namespace System.Windows.Forms.Rendering.Button; + +/// +/// Color utilities used by the key-cap renderer to derive material shading +/// colors from arbitrary effective colors. +/// +/// +/// +/// Nothing in here assumes a light or dark base color; every derivation is relative to the input, which is +/// what makes the material effect work with saturated, neutral, light, dark and (dark-mode remapped) system +/// colors alike. +/// +/// +internal static class PopupButtonColorMath +{ + public const float MinimumReadableContrastRatio = 4.5f; + + /// + /// Gets the relative luminance of a color in the range 0-1. + /// + public static float GetLuminance(Color color) + => ((0.299f * color.R) + (0.587f * color.G) + (0.114f * color.B)) / 255f; + + /// + /// Gets the WCAG relative luminance of a color, including the required sRGB linearization. + /// + public static float GetRelativeLuminance(Color color) + => (0.2126f * Linearize(color.R)) + + (0.7152f * Linearize(color.G)) + + (0.0722f * Linearize(color.B)); + + /// + /// Gets the WCAG contrast ratio between two colors. + /// + public static float GetContrastRatio(Color first, Color second) + { + float firstLuminance = GetRelativeLuminance(first); + float secondLuminance = GetRelativeLuminance(second); + float lighter = Math.Max(firstLuminance, secondLuminance); + float darker = Math.Min(firstLuminance, secondLuminance); + + return (lighter + 0.05f) / (darker + 0.05f); + } + + /// + /// Chooses black or white, whichever has the greater WCAG contrast ratio against the background. + /// + public static Color GetReadableForeColor(Color backColor) + => GetContrastRatio(Color.Black, backColor) >= GetContrastRatio(Color.White, backColor) + ? Color.Black + : Color.White; + + /// + /// Chooses black or white by its worst contrast ratio across the darkest and lightest rendered surfaces. + /// + public static Color GetReadableForeColor(Color darkestBackColor, Color lightestBackColor) + { + float blackContrast = Math.Min( + GetContrastRatio(Color.Black, darkestBackColor), + GetContrastRatio(Color.Black, lightestBackColor)); + float whiteContrast = Math.Min( + GetContrastRatio(Color.White, darkestBackColor), + GetContrastRatio(Color.White, lightestBackColor)); + + return blackContrast >= whiteContrast ? Color.Black : Color.White; + } + + /// + /// Composites a foreground color over a background color. + /// + public static Color Composite(Color foreground, Color background) + { + float foregroundAlpha = foreground.A / 255f; + float backgroundAlpha = background.A / 255f; + float alpha = foregroundAlpha + (backgroundAlpha * (1f - foregroundAlpha)); + + if (alpha <= 0f) + { + return Color.Transparent; + } + + return Color.FromArgb( + (int)MathF.Round(alpha * 255f), + CompositeChannel(foreground.R, background.R), + CompositeChannel(foreground.G, background.G), + CompositeChannel(foreground.B, background.B)); + + int CompositeChannel(byte foregroundChannel, byte backgroundChannel) + => (int)MathF.Round( + ((foregroundChannel * foregroundAlpha) + + (backgroundChannel * backgroundAlpha * (1f - foregroundAlpha))) + / alpha); + } + + /// + /// Linearly blends towards . + /// + /// The starting color. + /// The color to blend towards. + /// Blend amount, 0 (base) to 1 (target). + public static Color Blend(Color baseColor, Color target, float amount) + { + amount = Math.Clamp(amount, 0f, 1f); + + int r = (int)MathF.Round(baseColor.R + ((target.R - baseColor.R) * amount)); + int g = (int)MathF.Round(baseColor.G + ((target.G - baseColor.G) * amount)); + int b = (int)MathF.Round(baseColor.B + ((target.B - baseColor.B) * amount)); + + return Color.FromArgb(baseColor.A, r, g, b); + } + + /// + /// Blends the color towards white by the given amount. + /// + public static Color Lighten(Color color, float amount) + => Blend(color, Color.White, amount); + + /// + /// Blends the color towards black by the given amount. + /// + public static Color Darken(Color color, float amount) + => Blend(color, Color.Black, amount); + + /// + /// Blends the color towards the pole (black or white) that increases contrast against itself - lightens + /// dark colors, darkens light colors. + /// + /// + /// + /// Used for borders and cues that must remain visible on both light and dark surfaces. + /// + /// + public static Color TowardsContrast(Color color, float amount) + => GetLuminance(color) > 0.5f + ? Darken(color, amount) + : Lighten(color, amount); + + /// + /// Desaturates the color towards a gray of the same luminance and slightly pulls it towards a mid tone. + /// Used for disabled surfaces. + /// + public static Color Mute(Color color, float amount) + { + int gray = (int)MathF.Round(GetLuminance(color) * 255f); + Color desaturated = Blend(color, Color.FromArgb(color.A, gray, gray, gray), amount); + + return Blend(desaturated, Color.FromArgb(color.A, 128, 128, 128), amount * 0.35f); + } + + /// + /// Ensures a minimum luminance difference between and + /// by pushing away if needed. + /// + /// The color to adjust. + /// The surface it must stay readable on. + /// Minimum luminance delta, 0-1. + public static Color EnsureContrast(Color color, Color against, float minDelta) + { + float delta = MathF.Abs(GetLuminance(color) - GetLuminance(against)); + + if (delta >= minDelta) + { + return color; + } + + float push = minDelta - delta; + + return GetLuminance(against) > 0.5f + ? Darken(color, push) + : Lighten(color, push); + } + + /// + /// Derives an opaque highlight color for text relief on the given surface. + /// + /// + /// + /// Opaque blended colors are used instead of alpha because GDI text rendering via + /// ignores the alpha channel. + /// + /// + public static Color GetTextHighlight(Color surface, float strength) + => Lighten(surface, 0.28f * strength); + + /// + /// Derives an opaque shadow color for text relief on the given surface. + /// + public static Color GetTextShadow(Color surface, float strength) + => Darken(surface, 0.34f * strength); + + private static float Linearize(byte channel) + { + float value = channel / 255f; + + return value <= 0.04045f + ? value / 12.92f + : MathF.Pow((value + 0.055f) / 1.055f, 2.4f); + } +} diff --git a/src/System.Windows.Forms/System/Windows/Forms/Rendering/Button/PopupButtonEasing.cs b/src/System.Windows.Forms/System/Windows/Forms/Rendering/Button/PopupButtonEasing.cs new file mode 100644 index 00000000000..d574992ace9 --- /dev/null +++ b/src/System.Windows.Forms/System/Windows/Forms/Rendering/Button/PopupButtonEasing.cs @@ -0,0 +1,33 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. + +namespace System.Windows.Forms.Rendering.Button; + +/// +/// Easing functions for the key-cap state-change animations. +/// +internal static class PopupButtonEasing +{ + /// + /// Cubic ease-out. Natural for hover enter/leave - fast start, gentle settle. + /// + public static float EaseOutCubic(float t) + { + t = Math.Clamp(t, 0f, 1f); + float inv = 1f - t; + + return 1f - (inv * inv * inv); + } + + /// + /// Quadratic ease-in/ease-out. Snappy for press and release. + /// + public static float EaseInOutQuad(float t) + { + t = Math.Clamp(t, 0f, 1f); + + return t < 0.5f + ? 2f * t * t + : 1f - (MathF.Pow((-2f * t) + 2f, 2f) / 2f); + } +} diff --git a/src/System.Windows.Forms/System/Windows/Forms/Rendering/Button/PopupButtonKeyCapRenderer.cs b/src/System.Windows.Forms/System/Windows/Forms/Rendering/Button/PopupButtonKeyCapRenderer.cs new file mode 100644 index 00000000000..f8d8602ccf0 --- /dev/null +++ b/src/System.Windows.Forms/System/Windows/Forms/Rendering/Button/PopupButtonKeyCapRenderer.cs @@ -0,0 +1,902 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. + +using System.Drawing; +using System.Drawing.Drawing2D; +using System.Windows.Forms.Layout; + +namespace System.Windows.Forms.Rendering.Button; + +/// +/// Renders a concave mechanical key top - the classic table-calculator/cash-register key with a raised rim +/// and a shallow bowl - used for the button visual style for arbitrary colors, +/// DPI values and interaction states. +/// +/// +/// +/// The renderer is completely stateless with respect to any control: every input arrives via +/// . It can therefore render previews, designer adornments or key +/// visuals inside other controls without a live control instance. +/// +/// +/// Visual layers, back to front: ambient drop shadow, key body (rim surface), concave bowl (path gradient +/// plus inner top-shadow and bottom-light overlays), bowl lip stroke, border, default/focus cues, the +/// optional image, and finally the caption with a raised or engraved relief. +/// +/// +/// In high-contrast accessibility modes the renderer falls back to a flat, high-contrast style without any +/// material emulation. +/// +/// +internal static class PopupButtonKeyCapRenderer +{ + /// + /// Renders the key into the given . + /// + /// The target graphics. + /// The complete render context. + /// + /// Optional callback used to paint an image onto the key surface. It is invoked after the key chrome and + /// before the caption, and receives the bowl (content) rectangle. + /// + public static void Render( + Graphics graphics, + PopupButtonRenderContext context, + Action? paintImage = null, + (Rectangle TextBounds, Rectangle ImageBounds)? contentLayout = null) + { + ArgumentNullException.ThrowIfNull(graphics); + ArgumentNullException.ThrowIfNull(context); + + Rectangle bounds = context.Bounds; + Color surfaceBackColor = GetSurfaceBackColor(context); + + if (bounds.Width <= 0 || bounds.Height <= 0) + { + return; + } + + // Degenerate bounds: just fill, never throw. + if (bounds.Width < 8 || bounds.Height < 8) + { + using SolidBrush tinyBrush = new(surfaceBackColor); + graphics.FillRectangle(tinyBrush, bounds); + + return; + } + + if (context.HighContrast) + { + RenderHighContrast(graphics, context, surfaceBackColor, paintImage, contentLayout); + + return; + } + + Metrics metrics = Metrics.Create(context); + Palette palette = Palette.Create(context, metrics, surfaceBackColor); + (Rectangle textBounds, Rectangle imageBounds) = contentLayout + ?? CreateContentLayout( + context, + metrics.BowlRect, + applySurfaceInset: false); + + GraphicsState state = graphics.Save(); + + try + { + graphics.SmoothingMode = SmoothingMode.AntiAlias; + graphics.PixelOffsetMode = PixelOffsetMode.HighQuality; + + DrawAmbientShadow(graphics, metrics, palette); + DrawKeyBody(graphics, metrics, palette); + DrawBowl(graphics, metrics, palette); + DrawBorder(graphics, metrics, palette); + DrawStateCues(graphics, context, metrics, palette); + + if (imageBounds.Width > 0 && imageBounds.Height > 0) + { + paintImage?.Invoke(imageBounds); + } + } + finally + { + graphics.Restore(state); + } + + DrawText(graphics, context, metrics, palette, textBounds); + } + + internal static Rectangle GetContentBounds(PopupButtonRenderContext context) + { + if (!context.HighContrast) + { + return Metrics.Create(context).BowlRect; + } + + float scale = context.DeviceDpi / 96f; + Rectangle contentBounds = Rectangle.Inflate( + context.Bounds, + -(int)(4 * scale), + -(int)(4 * scale)); + + if (context.Pressed) + { + contentBounds.Offset((int)scale, (int)scale); + } + + return contentBounds; + } + + internal static Size GetPreferredSizeChrome(int deviceDpi, int borderWidth) + { + float scale = Math.Max(0.5f, deviceDpi / 96f); + int sideClearance = Math.Max(1, (int)MathF.Round(Metrics.SideClearanceDip * scale)); + int pressTravel = Math.Max(1, (int)MathF.Round(Metrics.PressTravelDip * scale)); + int defaultBorderIncrease = Math.Max(1, (int)MathF.Round(scale)); + int stableBorderWidth = Math.Max(0, borderWidth + defaultBorderIncrease); + int rim = Math.Max(2, (int)MathF.Round(3f * scale)); + int bowlInset = stableBorderWidth + rim; + + return new Size( + 2 * (sideClearance + bowlInset), + (2 * sideClearance) + pressTravel + (2 * bowlInset)); + } + + private static void DrawAmbientShadow(Graphics graphics, Metrics metrics, Palette palette) + { + // The key visually lifts off the surface; pressing it reduces the drop shadow. + int drop = Math.Max(1, metrics.Ambient - metrics.PressOffset); + + Rectangle outer = metrics.KeyRect; + outer.Offset(drop / 2, drop); + outer.Inflate(1, 1); + + Rectangle inner = metrics.KeyRect; + inner.Offset(drop / 2, drop); + + using (GraphicsPath outerPath = CreateRoundedPath(outer, metrics.CornerRadius + 1f)) + using (SolidBrush softBrush = new(Color.FromArgb(palette.AmbientAlpha / 3, Color.Black))) + { + graphics.FillPath(softBrush, outerPath); + } + + using GraphicsPath innerPath = CreateRoundedPath(inner, metrics.CornerRadius); + using SolidBrush coreBrush = new(Color.FromArgb(palette.AmbientAlpha, Color.Black)); + graphics.FillPath(coreBrush, innerPath); + } + + private static void DrawKeyBody(Graphics graphics, Metrics metrics, Palette palette) + { + // The rim surface: lit from the upper left, falling into shadow at the lower right. + using GraphicsPath bodyPath = CreateRoundedPath(metrics.KeyRect, metrics.CornerRadius); + using LinearGradientBrush bodyBrush = new( + InflateForGradient(metrics.KeyRect), + palette.BodyLight, + palette.BodyDark, + LinearGradientMode.ForwardDiagonal); + + graphics.FillPath(bodyBrush, bodyPath); + } + + private static void DrawBowl(Graphics graphics, Metrics metrics, Palette palette) + { + Rectangle bowl = metrics.BowlRect; + + if (bowl.Width < 2 || bowl.Height < 2) + { + return; + } + + using GraphicsPath bowlPath = CreateRoundedPath(bowl, metrics.BowlRadius); + + // 1. Radial shading: edges catch light, the center sits lower and darker - the signature concave read. + // The dark center is biased towards the upper left, where a top-left light source cannot reach into + // a bowl. + using (PathGradientBrush bowlBrush = new(bowlPath)) + { + bowlBrush.CenterColor = palette.BowlCenter; + bowlBrush.SurroundColors = [palette.BowlEdge]; + bowlBrush.CenterPoint = new PointF( + bowl.Left + (bowl.Width * 0.40f), + bowl.Top + (bowl.Height * 0.36f)); + bowlBrush.FocusScales = new PointF(0.28f, 0.22f); + + graphics.FillPath(bowlBrush, bowlPath); + } + + // 2. Inner top shadow and inner bottom light, clipped to the bowl. + GraphicsState clipState = graphics.Save(); + + try + { + graphics.SetClip(bowlPath, CombineMode.Intersect); + + int topHeight = Math.Max(2, (int)(bowl.Height * 0.42f)); + Rectangle topRect = bowl with { Height = topHeight }; + + using (LinearGradientBrush topShadow = new( + InflateForGradient(topRect), + Color.FromArgb(palette.InnerShadowAlpha, Color.Black), + Color.FromArgb(0, Color.Black), + LinearGradientMode.Vertical)) + { + graphics.FillRectangle(topShadow, topRect); + } + + int bottomHeight = Math.Max(2, (int)(bowl.Height * 0.30f)); + Rectangle bottomRect = new(bowl.Left, bowl.Bottom - bottomHeight, bowl.Width, bottomHeight); + + using LinearGradientBrush bottomLight = new( + InflateForGradient(bottomRect), + Color.FromArgb(0, Color.White), + Color.FromArgb(palette.InnerLightAlpha, Color.White), + LinearGradientMode.Vertical); + + graphics.FillRectangle(bottomLight, bottomRect); + } + finally + { + graphics.Restore(clipState); + } + + // 3. The lip where rim and bowl meet: light on the upper left, shadow lower right - the raised edge of + // the surrounding rim. + using LinearGradientBrush lipBrush = new( + InflateForGradient(Rectangle.Inflate(bowl, 1, 1)), + palette.LipLight, + palette.LipDark, + LinearGradientMode.ForwardDiagonal); + using Pen lipPen = new(lipBrush, Math.Max(1f, metrics.Scale)); + + graphics.DrawPath(lipPen, bowlPath); + } + + private static void DrawBorder(Graphics graphics, Metrics metrics, Palette palette) + { + if (metrics.BorderWidth <= 0) + { + return; + } + + float half = metrics.BorderWidth / 2f; + RectangleF borderRect = metrics.KeyRect; + borderRect.Inflate(-half, -half); + + if (borderRect.Width < 1f || borderRect.Height < 1f) + { + return; + } + + using GraphicsPath borderPath = CreateRoundedPath( + Rectangle.Round(borderRect), + Math.Max(1f, metrics.CornerRadius - half)); + using Pen borderPen = new(palette.Border, metrics.BorderWidth); + + graphics.DrawPath(borderPen, borderPath); + } + + private static void DrawStateCues( + Graphics graphics, + PopupButtonRenderContext context, + Metrics metrics, + Palette palette) + { + if (context.Focused) + { + int inset = metrics.BorderWidth + Math.Max(2, (int)MathF.Round(2f * metrics.Scale)); + Rectangle focusRect = Rectangle.Inflate(metrics.KeyRect, -inset, -inset); + + if (focusRect.Width > 4 && focusRect.Height > 4) + { + using GraphicsPath focusPath = CreateRoundedPath( + focusRect, + Math.Max(1f, metrics.CornerRadius - inset)); + using Pen focusPen = new(palette.Focus, Math.Max(1f, metrics.Scale * 0.75f)) + { + DashStyle = DashStyle.Dot + }; + + graphics.DrawPath(focusPen, focusPath); + } + } + } + + private static void DrawText( + Graphics graphics, + PopupButtonRenderContext context, + Metrics metrics, + Palette palette, + Rectangle textRect) + { + string? text = context.Text; + + if (string.IsNullOrEmpty(text)) + { + return; + } + + if (textRect.Width <= 0 || textRect.Height <= 0) + { + return; + } + + TextFormatFlags flags = GetTextFormatFlags(context); + int reliefOffset = metrics.TextReliefOffset; + + PopupButtonTextEffect effect = context.TextEffect; + + if (!palette.TextOutline.IsEmpty) + { + for (int y = -reliefOffset; y <= reliefOffset; y += reliefOffset) + { + for (int x = -reliefOffset; x <= reliefOffset; x += reliefOffset) + { + if (x == 0 && y == 0) + { + continue; + } + + Rectangle outlineRect = textRect; + outlineRect.Offset(x, y); + TextRenderer.DrawText( + graphics, + text, + context.Font, + outlineRect, + palette.TextOutline, + flags); + } + } + } + + if (effect is not PopupButtonTextEffect.Flat) + { + // GDI text ignores alpha, so the relief colors are opaque blends against the bowl center - see + // PopupButtonColorMath.GetTextHighlight/GetTextShadow. + (Color reliefColor, Point reliefShift) = effect switch + { + // Raised: light from the upper left casts the glyph's shadow downwards. + PopupButtonTextEffect.Raised => (palette.TextShadow, new Point(0, reliefOffset)), + + // Engraved (letterpress): the recess's lower edge catches the light. + _ => (palette.TextHighlight, new Point(0, reliefOffset)) + }; + + Rectangle reliefRect = textRect; + reliefRect.Offset(reliefShift); + TextRenderer.DrawText(graphics, text, context.Font, reliefRect, reliefColor, flags); + + if (effect is PopupButtonTextEffect.Engraved) + { + // A faint dark edge above completes the engraving. + Rectangle upperRect = textRect; + upperRect.Offset(0, -reliefOffset); + Color upperShadow = PopupButtonColorMath.Blend(palette.TextShadow, palette.BowlCenter, 0.55f); + TextRenderer.DrawText(graphics, text, context.Font, upperRect, upperShadow, flags); + } + } + + TextRenderer.DrawText(graphics, text, context.Font, textRect, palette.Text, flags); + } + + private static void RenderHighContrast( + Graphics graphics, + PopupButtonRenderContext context, + Color surfaceBackColor, + Action? paintImage, + (Rectangle TextBounds, Rectangle ImageBounds)? contentLayout) + { + Rectangle bounds = context.Bounds; + bool pressed = context.Pressed; + float scale = context.DeviceDpi / 96f; + + Color back = surfaceBackColor; + Color fore = context.Enabled ? context.ForeColor : SystemColors.GrayText; + Color border = context.Enabled ? context.ForeColor : SystemColors.GrayText; + + using (SolidBrush backBrush = new(back)) + { + graphics.FillRectangle(backBrush, bounds); + } + + int defaultBorderIncrease = context.IsDefault && context.Enabled + ? Math.Max(1, (int)MathF.Round(scale)) + : 0; + int borderWidth = Math.Max( + Math.Max(1, context.BorderWidth + defaultBorderIncrease), + pressed ? (int)(2 * scale) : 1); + Rectangle borderRect = bounds; + borderRect.Width -= 1; + borderRect.Height -= 1; + + using (Pen borderPen = new(border, borderWidth) { Alignment = PenAlignment.Inset }) + { + graphics.DrawRectangle(borderPen, borderRect); + } + + Rectangle contentRect = GetContentBounds(context); + (Rectangle textRect, Rectangle imageRect) = contentLayout + ?? CreateContentLayout( + context, + contentRect, + applySurfaceInset: false); + if (imageRect.Width > 0 && imageRect.Height > 0) + { + paintImage?.Invoke(imageRect); + } + + if (textRect.Width > 0 && textRect.Height > 0) + { + TextRenderer.DrawText(graphics, context.Text, context.Font, textRect, fore, GetTextFormatFlags(context)); + } + + if (context.Focused) + { + Rectangle focusRect = Rectangle.Inflate(bounds, -(int)(3 * scale), -(int)(3 * scale)); + ControlPaint.DrawFocusRectangle(graphics, focusRect, fore, back); + } + } + + private static Color GetSurfaceBackColor(PopupButtonRenderContext context) + { + if (context.HighContrast || !context.IsDefault || !context.Enabled) + { + return context.BackColor; + } + + return context.IsDarkMode + ? PopupButtonColorMath.Lighten(context.BackColor, 0.1f) + : PopupButtonColorMath.Darken(context.BackColor, 0.1f); + } + + private static (Rectangle TextBounds, Rectangle ImageBounds) CreateContentLayout( + PopupButtonRenderContext context, + Rectangle contentBounds, + bool applySurfaceInset) + { + if (applySurfaceInset) + { + int inset = Math.Max(1, (int)MathF.Round(1.5f * context.DeviceDpi / 96f)); + contentBounds = Rectangle.Inflate(contentBounds, -inset, -inset); + } + + contentBounds = ApplyPadding(contentBounds, context.Padding); + bool hasText = !string.IsNullOrEmpty(context.Text); + bool hasImage = !context.ImageSize.IsEmpty; + ContentAlignment imageAlign = context.RightToLeft == RightToLeft.Yes + ? MirrorAlignment(context.ImageAlign) + : context.ImageAlign; + + if (!hasImage) + { + return (hasText ? contentBounds : Rectangle.Empty, Rectangle.Empty); + } + + Size imageSize = new( + Math.Min(contentBounds.Width, context.ImageSize.Width), + Math.Min(contentBounds.Height, context.ImageSize.Height)); + + if (!hasText || context.TextImageRelation == TextImageRelation.Overlay) + { + return ( + hasText ? contentBounds : Rectangle.Empty, + AlignInRectangle(contentBounds, imageSize, imageAlign)); + } + + TextImageRelation relation = context.RightToLeft == RightToLeft.Yes + ? LayoutUtils.GetOppositeTextImageRelation(context.TextImageRelation) + : context.TextImageRelation; + int gap = Math.Max(2, (int)MathF.Round(4f * context.DeviceDpi / 96f)); + + return relation switch + { + TextImageRelation.ImageBeforeText => CreateHorizontalLayout(imageFirst: true), + TextImageRelation.TextBeforeImage => CreateHorizontalLayout(imageFirst: false), + TextImageRelation.ImageAboveText => CreateVerticalLayout(imageFirst: true), + TextImageRelation.TextAboveImage => CreateVerticalLayout(imageFirst: false), + _ => (contentBounds, AlignInRectangle(contentBounds, imageSize, imageAlign)) + }; + + (Rectangle TextBounds, Rectangle ImageBounds) CreateHorizontalLayout(bool imageFirst) + { + int imageWidth = Math.Min(imageSize.Width, Math.Max(0, contentBounds.Width - gap)); + Rectangle imageSlot = imageFirst + ? new Rectangle(contentBounds.Left, contentBounds.Top, imageWidth, contentBounds.Height) + : new Rectangle(contentBounds.Right - imageWidth, contentBounds.Top, imageWidth, contentBounds.Height); + Rectangle textBounds = imageFirst + ? Rectangle.FromLTRB(imageSlot.Right + gap, contentBounds.Top, contentBounds.Right, contentBounds.Bottom) + : Rectangle.FromLTRB(contentBounds.Left, contentBounds.Top, imageSlot.Left - gap, contentBounds.Bottom); + + return ( + textBounds, + AlignInRectangle(imageSlot, imageSize with { Width = imageWidth }, imageAlign)); + } + + (Rectangle TextBounds, Rectangle ImageBounds) CreateVerticalLayout(bool imageFirst) + { + int imageHeight = Math.Min(imageSize.Height, Math.Max(0, contentBounds.Height - gap)); + Rectangle imageSlot = imageFirst + ? new Rectangle(contentBounds.Left, contentBounds.Top, contentBounds.Width, imageHeight) + : new Rectangle(contentBounds.Left, contentBounds.Bottom - imageHeight, contentBounds.Width, imageHeight); + Rectangle textBounds = imageFirst + ? Rectangle.FromLTRB(contentBounds.Left, imageSlot.Bottom + gap, contentBounds.Right, contentBounds.Bottom) + : Rectangle.FromLTRB(contentBounds.Left, contentBounds.Top, contentBounds.Right, imageSlot.Top - gap); + + return ( + textBounds, + AlignInRectangle(imageSlot, imageSize with { Height = imageHeight }, imageAlign)); + } + } + + private static Rectangle AlignInRectangle( + Rectangle container, + Size size, + ContentAlignment alignment) + { + int x = alignment switch + { + ContentAlignment.TopLeft or ContentAlignment.MiddleLeft or ContentAlignment.BottomLeft => container.Left, + ContentAlignment.TopRight or ContentAlignment.MiddleRight or ContentAlignment.BottomRight + => container.Right - size.Width, + _ => container.Left + ((container.Width - size.Width) / 2) + }; + + int y = alignment switch + { + ContentAlignment.TopLeft or ContentAlignment.TopCenter or ContentAlignment.TopRight => container.Top, + ContentAlignment.BottomLeft or ContentAlignment.BottomCenter or ContentAlignment.BottomRight + => container.Bottom - size.Height, + _ => container.Top + ((container.Height - size.Height) / 2) + }; + + return new Rectangle(x, y, size.Width, size.Height); + } + + /// + /// Creates a rounded-rectangle path; degrades to a plain rectangle for tiny radii and clamps the radius so + /// it never exceeds half of the smaller side. + /// + private static GraphicsPath CreateRoundedPath(Rectangle rect, float radius) + { + GraphicsPath path = new(); + + radius = Math.Min(radius, Math.Min(rect.Width, rect.Height) / 2f); + + if (radius < 1f || rect.Width < 2 || rect.Height < 2) + { + path.AddRectangle(rect); + + return path; + } + + float diameter = radius * 2f; + RectangleF arc = new(rect.X, rect.Y, diameter, diameter); + + path.AddArc(arc, 180f, 90f); + arc.X = rect.Right - diameter; + path.AddArc(arc, 270f, 90f); + arc.Y = rect.Bottom - diameter; + path.AddArc(arc, 0f, 90f); + arc.X = rect.X; + path.AddArc(arc, 90f, 90f); + path.CloseFigure(); + + return path; + } + + private static Rectangle ApplyPadding(Rectangle rect, Padding padding) + => new( + rect.X + padding.Left, + rect.Y + padding.Top, + Math.Max(0, rect.Width - padding.Horizontal), + Math.Max(0, rect.Height - padding.Vertical)); + + /// + /// Grows a gradient rectangle by one pixel to avoid GDI+ edge-seam artifacts and to guarantee non-zero + /// dimensions. + /// + private static Rectangle InflateForGradient(Rectangle rect) + { + Rectangle result = Rectangle.Inflate(rect, 1, 1); + + if (result.Width < 2) + { + result.Width = 2; + } + + if (result.Height < 2) + { + result.Height = 2; + } + + return result; + } + + private static TextFormatFlags GetTextFormatFlags(PopupButtonRenderContext context) + { + bool rtl = context.RightToLeft == RightToLeft.Yes; + ContentAlignment align = rtl ? MirrorAlignment(context.TextAlign) : context.TextAlign; + + TextFormatFlags flags = align switch + { + ContentAlignment.TopLeft => TextFormatFlags.Top | TextFormatFlags.Left, + ContentAlignment.TopCenter => TextFormatFlags.Top | TextFormatFlags.HorizontalCenter, + ContentAlignment.TopRight => TextFormatFlags.Top | TextFormatFlags.Right, + ContentAlignment.MiddleLeft => TextFormatFlags.VerticalCenter | TextFormatFlags.Left, + ContentAlignment.MiddleRight => TextFormatFlags.VerticalCenter | TextFormatFlags.Right, + ContentAlignment.BottomLeft => TextFormatFlags.Bottom | TextFormatFlags.Left, + ContentAlignment.BottomCenter => TextFormatFlags.Bottom | TextFormatFlags.HorizontalCenter, + ContentAlignment.BottomRight => TextFormatFlags.Bottom | TextFormatFlags.Right, + _ => TextFormatFlags.VerticalCenter | TextFormatFlags.HorizontalCenter + }; + + flags |= TextFormatFlags.EndEllipsis; + + if (context.Text is not null && !context.Text.Contains('\n')) + { + flags |= TextFormatFlags.SingleLine; + } + + if (rtl) + { + flags |= TextFormatFlags.RightToLeft; + } + + if (!context.ShowKeyboardCues) + { + flags |= TextFormatFlags.HidePrefix; + } + + return flags; + } + + private static ContentAlignment MirrorAlignment(ContentAlignment alignment) + => alignment switch + { + ContentAlignment.TopLeft => ContentAlignment.TopRight, + ContentAlignment.TopRight => ContentAlignment.TopLeft, + ContentAlignment.MiddleLeft => ContentAlignment.MiddleRight, + ContentAlignment.MiddleRight => ContentAlignment.MiddleLeft, + ContentAlignment.BottomLeft => ContentAlignment.BottomRight, + ContentAlignment.BottomRight => ContentAlignment.BottomLeft, + _ => alignment + }; + + /// + /// Device-resolved geometry and animation-modulated shading amounts for one render pass. + /// + private readonly struct Metrics + { + internal const float SideClearanceDip = 1f; + internal const float PressTravelDip = 1.5f; + + public float Scale { get; init; } + public int Ambient { get; init; } + public int BorderWidth { get; init; } + public int Rim { get; init; } + public float CornerRadius { get; init; } + public Rectangle KeyRect { get; init; } + public Rectangle BowlRect { get; init; } + public float BowlRadius { get; init; } + public int PressOffset { get; init; } + public float Hover { get; init; } + public float Press { get; init; } + public float HighlightAmount { get; init; } + public float ShadowAmount { get; init; } + public int TextReliefOffset { get; init; } + + public static Metrics Create(PopupButtonRenderContext context) + { + PopupButtonRenderOptions options = context.Options; + Rectangle bounds = context.Bounds; + + float scale = Math.Max(0.5f, context.DeviceDpi / 96f); + float hover = context.AnimationState.HoverProgress; + float press = context.AnimationState.PressProgress; + + int sideClearance = Math.Max(1, (int)MathF.Round(SideClearanceDip * scale)); + int pressTravel = Math.Max(1, (int)MathF.Round(PressTravelDip * scale)); + int ambient = sideClearance + pressTravel; + int maxBorder = Math.Max(0, (Math.Min(bounds.Width, bounds.Height) / 4) - 1); + int defaultBorderIncrease = context.IsDefault && context.Enabled + ? Math.Max(1, (int)MathF.Round(scale)) + : 0; + int borderWidth = Math.Clamp(context.BorderWidth + defaultBorderIncrease, 0, maxBorder); + + Rectangle keyRect = new( + bounds.X + sideClearance, + bounds.Y + sideClearance, + Math.Max(1, bounds.Width - (2 * sideClearance)), + Math.Max(1, bounds.Height - sideClearance - ambient)); + + // Pressing translates the complete key top into the space released by its shortening shadow. + // Keeping the key height constant avoids moving the bowl, border, and content independently. + int pressOffset = Math.Min( + (int)MathF.Round(press * PressTravelDip * scale), + Math.Max(0, bounds.Bottom - sideClearance - keyRect.Bottom)); + keyRect.Offset(0, pressOffset); + + int rim = Math.Max(2, (int)MathF.Round(3f * scale)); + int bowlInset = borderWidth + rim; + + if (keyRect.Width - (2 * bowlInset) < 8 || keyRect.Height - (2 * bowlInset) < 8) + { + rim = Math.Max(1, rim / 2); + bowlInset = borderWidth + rim; + } + + Rectangle bowlRect = Rectangle.Inflate(keyRect, -bowlInset, -bowlInset); + + if (bowlRect.Width < 2 || bowlRect.Height < 2) + { + bowlRect = Rectangle.Inflate(keyRect, -1, -1); + } + + float cornerRadius = Math.Clamp( + options.GetCornerRadiusDip() * scale, + 1f, + Math.Min(keyRect.Width, keyRect.Height) / 2f); + float bowlRadius = Math.Max(1f, cornerRadius - (rim * 0.6f)); + + // Pressing deepens the bowl; hovering flattens it a touch, as if the key rises to meet the finger. + // Highlights brighten on hover, shadows deepen on press. + float depth = options.GetConcavityDepth() * (1f + (press * 0.7f) - (hover * 0.12f)); + float highlight = Math.Clamp( + depth * options.GetHighlightMultiplier() * (1f + (hover * 0.55f)), + 0.02f, + 0.6f); + float shadow = Math.Clamp( + depth * options.GetShadowMultiplier() * (1f + (press * 0.35f)), + 0.02f, + 0.6f); + + return new Metrics + { + Scale = scale, + Ambient = ambient, + BorderWidth = borderWidth, + Rim = rim, + CornerRadius = cornerRadius, + KeyRect = keyRect, + BowlRect = bowlRect, + BowlRadius = bowlRadius, + PressOffset = pressOffset, + Hover = hover, + Press = press, + HighlightAmount = highlight, + ShadowAmount = shadow, + TextReliefOffset = Math.Max(1, (int)MathF.Round(0.8f * scale)) + }; + } + } + + /// + /// All colors of one render pass, derived from the effective context colors so the material effect adapts + /// to any BackColor/ForeColor combination. + /// + private readonly struct Palette + { + public Color BodyLight { get; init; } + public Color BodyDark { get; init; } + public Color BowlEdge { get; init; } + public Color BowlCenter { get; init; } + public Color DarkestTextBackground { get; init; } + public Color LightestTextBackground { get; init; } + public Color LipLight { get; init; } + public Color LipDark { get; init; } + public Color Border { get; init; } + public Color Text { get; init; } + public Color TextOutline { get; init; } + public Color TextHighlight { get; init; } + public Color TextShadow { get; init; } + public Color Focus { get; init; } + public int AmbientAlpha { get; init; } + public int InnerShadowAlpha { get; init; } + public int InnerLightAlpha { get; init; } + + public static Palette Create(PopupButtonRenderContext context, Metrics metrics, Color surfaceBackColor) + { + bool enabled = context.Enabled; + + // Disabled keys mute the material but keep the concave form readable - reduced contrast rather than + // flat gray. + Color back = enabled ? surfaceBackColor : PopupButtonColorMath.Mute(surfaceBackColor, 0.55f); + float contrast = enabled ? 1f : 0.35f; + float luminance = PopupButtonColorMath.GetLuminance(back); + + Color border = enabled + ? context.BorderColor + : PopupButtonColorMath.Blend(context.BorderColor, back, 0.45f); + + // Keep the border visible even if the user picked one too close to the face color. + border = PopupButtonColorMath.EnsureContrast(border, back, 0.08f); + + float reliefStrength = (enabled ? 1f : 0.4f) + (metrics.Press * 0.25f); + Color bodyLight = PopupButtonColorMath.Lighten(back, metrics.HighlightAmount * 0.9f * contrast); + Color bodyDark = PopupButtonColorMath.Darken(back, metrics.ShadowAmount * 0.9f * contrast); + Color bowlCenter = PopupButtonColorMath.Darken(back, (metrics.ShadowAmount * 0.75f * contrast) + (enabled ? 0f : 0.02f)); + Color bowlEdge = PopupButtonColorMath.Lighten(back, metrics.HighlightAmount * 0.55f * contrast); + int innerShadowAlpha = Math.Clamp( + (int)((30f + (metrics.ShadowAmount * 380f)) * contrast), 0, 120); + int innerLightAlpha = Math.Clamp( + (int)((20f + (metrics.HighlightAmount * 300f)) * contrast), 0, 100); + Color darkestBowl = Color.White; + Color lightestBowl = Color.Black; + float darkestLuminance = 1f; + float lightestLuminance = 0f; + EvaluateBowlExtremes(bodyLight, bowlCenter); + EvaluateBowlExtremes(bodyLight, bowlEdge); + EvaluateBowlExtremes(bodyDark, bowlCenter); + EvaluateBowlExtremes(bodyDark, bowlEdge); + Color text = enabled + ? context.UseAutomaticForeColor + ? PopupButtonColorMath.GetReadableForeColor(darkestBowl, lightestBowl) + : context.ForeColor + : PopupButtonColorMath.EnsureContrast(PopupButtonColorMath.Blend(context.ForeColor, back, 0.45f), back, 0.18f); + float textContrast = Math.Min( + PopupButtonColorMath.GetContrastRatio(text, darkestBowl), + PopupButtonColorMath.GetContrastRatio(text, lightestBowl)); + Color textOutline = context.UseAutomaticForeColor + && textContrast < PopupButtonColorMath.MinimumReadableContrastRatio + ? text == Color.Black ? Color.White : Color.Black + : Color.Empty; + + return new Palette + { + BodyLight = bodyLight, + BodyDark = bodyDark, + BowlEdge = bowlEdge, + BowlCenter = bowlCenter, + DarkestTextBackground = darkestBowl, + LightestTextBackground = lightestBowl, + LipLight = PopupButtonColorMath.Lighten(back, metrics.HighlightAmount * 1.3f * contrast), + LipDark = PopupButtonColorMath.Darken(back, metrics.ShadowAmount * 1.2f * contrast), + Border = border, + Text = text, + TextOutline = textOutline, + TextHighlight = PopupButtonColorMath.GetTextHighlight(bowlCenter, reliefStrength), + TextShadow = PopupButtonColorMath.GetTextShadow(bowlCenter, reliefStrength), + Focus = PopupButtonColorMath.EnsureContrast( + PopupButtonColorMath.TowardsContrast(back, 0.55f), + back, + 0.45f), + AmbientAlpha = (int)((luminance > 0.5f ? 55f : 85f) + * (1f - (metrics.Press * 0.6f)) + * (enabled ? 1f : 0.5f)), + InnerShadowAlpha = innerShadowAlpha, + InnerLightAlpha = innerLightAlpha + }; + + void EvaluateBowlExtremes(Color bodyColor, Color bowlColor) + { + Color renderedBody = PopupButtonColorMath.Composite(bodyColor, context.SurfaceColor); + Color renderedBowl = PopupButtonColorMath.Composite(bowlColor, renderedBody); + EvaluateLuminance(renderedBowl); + EvaluateLuminance( + PopupButtonColorMath.Composite( + Color.FromArgb(innerShadowAlpha, Color.Black), + renderedBowl)); + EvaluateLuminance( + PopupButtonColorMath.Composite( + Color.FromArgb(innerLightAlpha, Color.White), + renderedBowl)); + } + + void EvaluateLuminance(Color color) + { + float luminance = PopupButtonColorMath.GetRelativeLuminance(color); + if (luminance < darkestLuminance) + { + darkestBowl = color; + darkestLuminance = luminance; + } + + if (luminance > lightestLuminance) + { + lightestBowl = color; + lightestLuminance = luminance; + } + } + } + } +} diff --git a/src/System.Windows.Forms/System/Windows/Forms/Rendering/Button/PopupButtonMetric.cs b/src/System.Windows.Forms/System/Windows/Forms/Rendering/Button/PopupButtonMetric.cs new file mode 100644 index 00000000000..443770afb7c --- /dev/null +++ b/src/System.Windows.Forms/System/Windows/Forms/Rendering/Button/PopupButtonMetric.cs @@ -0,0 +1,32 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. + +namespace System.Windows.Forms.Rendering.Button; + +/// +/// A DPI-neutral, generic magnitude used to tune the key-cap renderer +/// (corner radius, concavity depth, highlight/shadow strength). +/// +/// +/// +/// Using generic magnitudes instead of pixel values allows the renderer to resolve the actual device +/// values per call, which keeps high-DPI scenarios trivially correct. +/// +/// +internal enum PopupButtonMetric +{ + /// + /// A small magnitude. + /// + Small = 0, + + /// + /// A medium magnitude. This is the default. + /// + Medium = 1, + + /// + /// A large magnitude. + /// + Large = 2 +} diff --git a/src/System.Windows.Forms/System/Windows/Forms/Rendering/Button/PopupButtonRenderContext.cs b/src/System.Windows.Forms/System/Windows/Forms/Rendering/Button/PopupButtonRenderContext.cs new file mode 100644 index 00000000000..eadb9f557fc --- /dev/null +++ b/src/System.Windows.Forms/System/Windows/Forms/Rendering/Button/PopupButtonRenderContext.cs @@ -0,0 +1,156 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. + +using System.Drawing; + +namespace System.Windows.Forms.Rendering.Button; + +/// +/// Carries every piece of information the key-cap renderer needs, without a +/// dependency on a live control instance. +/// +/// +/// +/// Because the renderer receives all state through this context, it can be driven from a control, a designer +/// surface or a preview-image generator. A caller only needs a target and this context. +/// +/// +internal sealed class PopupButtonRenderContext +{ + /// + /// Gets the bounds to render into, in device pixels of the target . + /// + public required Rectangle Bounds { get; init; } + + /// + /// Gets the caption text. May be or empty. + /// + public string? Text { get; init; } + + /// + /// Gets the font used for the caption. + /// + public required Font Font { get; init; } + + /// + /// Gets the effective key face color. + /// + public Color BackColor { get; init; } = SystemColors.Control; + + /// + /// Gets the effective caption color. + /// + public Color ForeColor { get; init; } = SystemColors.ControlText; + + /// + /// Gets the color behind the rendered key, used to resolve translucent automatic-color surfaces. + /// + public Color SurfaceColor { get; init; } = SystemColors.Control; + + /// + /// Gets a value indicating whether the renderer should select a readable caption color from the final bowl color. + /// + public bool UseAutomaticForeColor { get; init; } + + /// + /// Gets the border color of the key body. + /// + public Color BorderColor { get; init; } = SystemColors.ControlDark; + + /// + /// Gets the border width in device pixels. 0 renders no border. + /// + public int BorderWidth { get; init; } = 1; + + /// + /// Gets a value indicating whether the key is enabled. + /// + public bool Enabled { get; init; } = true; + + /// + /// Gets a value indicating whether the key has keyboard focus and should show a focus cue. + /// + public bool Focused { get; init; } + + /// + /// Gets a value indicating whether the key is currently pressed. Used for the high-contrast fallback, + /// where continuous animation is not applied. + /// + public bool Pressed { get; init; } + + /// + /// Gets a value indicating whether the key is the default button of its dialog. + /// + public bool IsDefault { get; init; } + + /// + /// Gets a value indicating whether the application is using its dark color scheme. + /// + public bool IsDarkMode { get; init; } + + /// + /// Gets the animation progress snapshot. + /// + public PopupButtonAnimationState AnimationState { get; init; } + + /// + /// Gets the caption alignment within the key top. + /// + public ContentAlignment TextAlign { get; init; } = ContentAlignment.MiddleCenter; + + /// + /// Gets the size of the image rendered with the caption. + /// + public Size ImageSize { get; init; } + + /// + /// Gets the alignment of the image within the key surface. + /// + public ContentAlignment ImageAlign { get; init; } = ContentAlignment.MiddleCenter; + + /// + /// Gets the positional relationship between the image and caption. + /// + public TextImageRelation TextImageRelation { get; init; } = TextImageRelation.Overlay; + + /// + /// Gets the right-to-left setting for text rendering. + /// + public RightToLeft RightToLeft { get; init; } = RightToLeft.No; + + /// + /// Gets the padding applied around the caption inside the bowl. + /// + public Padding Padding { get; init; } + + /// + /// Gets the DPI of the target device. Used to scale all chrome metrics. + /// + public int DeviceDpi { get; init; } = 96; + + /// + /// Gets the caption relief effect. + /// + public PopupButtonTextEffect TextEffect { get; init; } = PopupButtonTextEffect.Raised; + + /// + /// Gets a value indicating whether keyboard cues (mnemonic underlines) should be shown. + /// + public bool ShowKeyboardCues { get; init; } = true; + + /// + /// Gets a value indicating whether a high-contrast accessibility theme is active. + /// + /// + /// + /// When , the renderer falls back to a flat, higher-contrast style without material + /// emulation. + /// + /// + public bool HighContrast { get; init; } = SystemInformation.HighContrast; + + /// + /// Gets the rendering options to use. Defaults to the process-wide shared options. + /// + public PopupButtonRenderOptions Options { get; init; } = PopupButtonRenderOptions.Shared; +} diff --git a/src/System.Windows.Forms/System/Windows/Forms/Rendering/Button/PopupButtonRenderOptions.cs b/src/System.Windows.Forms/System/Windows/Forms/Rendering/Button/PopupButtonRenderOptions.cs new file mode 100644 index 00000000000..c1864d18ad2 --- /dev/null +++ b/src/System.Windows.Forms/System/Windows/Forms/Rendering/Button/PopupButtonRenderOptions.cs @@ -0,0 +1,92 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. + +namespace System.Windows.Forms.Rendering.Button; + +/// +/// Tunable rendering options for the key-cap renderer. +/// +/// +/// +/// All geometric magnitudes are expressed as values and only resolved to +/// device pixels inside the renderer, based on the DPI carried by the render context. +/// is the process-wide instance used to tune the best all-purpose defaults. Border width and color are not +/// part of these options; they are read from the button's . +/// +/// +internal sealed class PopupButtonRenderOptions +{ + /// + /// Gets the process-wide shared options instance. + /// + public static PopupButtonRenderOptions Shared { get; } = new(); + + /// + /// Gets or sets the corner radius magnitude of the key body. + /// + public PopupButtonMetric CornerRadius { get; set; } = PopupButtonMetric.Medium; + + /// + /// Gets or sets how deep the concave bowl of the key top appears. + /// + public PopupButtonMetric ConcavityDepth { get; set; } = PopupButtonMetric.Medium; + + /// + /// Gets or sets the strength of edge highlights. + /// + public PopupButtonMetric HighlightStrength { get; set; } = PopupButtonMetric.Medium; + + /// + /// Gets or sets the strength of edge and bowl shadows. + /// + public PopupButtonMetric ShadowStrength { get; set; } = PopupButtonMetric.Medium; + + /// + /// Gets or sets the base duration of state-change animations. + /// + public TimeSpan AnimationDuration { get; set; } = TimeSpan.FromMilliseconds(160); + + /// + /// Resolves to device-independent pixels (96 DPI). + /// + internal float GetCornerRadiusDip() + => CornerRadius switch + { + PopupButtonMetric.Small => 4f, + PopupButtonMetric.Large => 9f, + _ => 6f + }; + + /// + /// Resolves to a fractional shading depth. + /// + internal float GetConcavityDepth() + => ConcavityDepth switch + { + PopupButtonMetric.Small => 0.07f, + PopupButtonMetric.Large => 0.17f, + _ => 0.11f + }; + + /// + /// Resolves to a multiplier. + /// + internal float GetHighlightMultiplier() + => HighlightStrength switch + { + PopupButtonMetric.Small => 0.6f, + PopupButtonMetric.Large => 1.5f, + _ => 1f + }; + + /// + /// Resolves to a multiplier. + /// + internal float GetShadowMultiplier() + => ShadowStrength switch + { + PopupButtonMetric.Small => 0.6f, + PopupButtonMetric.Large => 1.5f, + _ => 1f + }; +} diff --git a/src/System.Windows.Forms/System/Windows/Forms/Rendering/Button/PopupButtonTextEffect.cs b/src/System.Windows.Forms/System/Windows/Forms/Rendering/Button/PopupButtonTextEffect.cs new file mode 100644 index 00000000000..42d10cd8d5f --- /dev/null +++ b/src/System.Windows.Forms/System/Windows/Forms/Rendering/Button/PopupButtonTextEffect.cs @@ -0,0 +1,26 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. + +namespace System.Windows.Forms.Rendering.Button; + +/// +/// Determines how the caption of a key-cap button is physically integrated +/// into the key surface. +/// +internal enum PopupButtonTextEffect +{ + /// + /// The caption appears slightly raised above the key surface (embossed). + /// + Raised = 0, + + /// + /// The caption appears slightly recessed into the key surface (engraved/letterpress). + /// + Engraved = 1, + + /// + /// The caption is drawn flat, without any relief effect. + /// + Flat = 2 +} diff --git a/src/System.Windows.Forms/System/Windows/Forms/Rendering/CheckBox/AnimatedCheckGlyphRenderer.cs b/src/System.Windows.Forms/System/Windows/Forms/Rendering/CheckBox/AnimatedCheckGlyphRenderer.cs new file mode 100644 index 00000000000..0abf778390f --- /dev/null +++ b/src/System.Windows.Forms/System/Windows/Forms/Rendering/CheckBox/AnimatedCheckGlyphRenderer.cs @@ -0,0 +1,294 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. + +using System.Drawing; +using System.Drawing.Drawing2D; +using System.Windows.Forms.Rendering.Animation; + +namespace System.Windows.Forms.Rendering.CheckBox; + +/// +/// Animates and draws the modern normal-appearance CheckBox glyph. +/// +internal sealed class AnimatedCheckGlyphRenderer : AnimatedControlRenderer +{ + private const int AnimationDuration = 220; + + private float _checkAlphaCurrent; + private float _checkAlphaStart; + private float _checkAlphaTarget; + private float _dashAlphaCurrent; + private float _dashAlphaStart; + private float _dashAlphaTarget; + private float _fillCurrent; + private float _fillStart; + private float _fillTarget; + private float _interactionCurrent; + private float _interactionStart; + private float _interactionTarget; + private bool _interactionInitialized; + private bool _stateInitialized; + + public AnimatedCheckGlyphRenderer(Control control) : base(control) + { + } + + internal void NotifyCheckStateChanged(CheckState newState) + { + (float fill, float checkAlpha, float dashAlpha) = GetStateTargets(newState); + if (!_stateInitialized) + { + _stateInitialized = true; + _fillCurrent = _fillStart = _fillTarget = fill; + _checkAlphaCurrent = _checkAlphaStart = _checkAlphaTarget = checkAlpha; + _dashAlphaCurrent = _dashAlphaStart = _dashAlphaTarget = dashAlpha; + return; + } + + if (_fillTarget == fill + && _checkAlphaTarget == checkAlpha + && _dashAlphaTarget == dashAlpha) + { + return; + } + + _fillTarget = fill; + _checkAlphaTarget = checkAlpha; + _dashAlphaTarget = dashAlpha; + RestartAnimation(); + } + + internal void SetInteractionState(bool hovered, bool focused) + { + float interactionTarget = hovered || focused ? 1f : 0f; + if (!_interactionInitialized) + { + _interactionInitialized = true; + _interactionCurrent = _interactionStart = _interactionTarget = interactionTarget; + return; + } + + if (_interactionTarget == interactionTarget) + { + return; + } + + _interactionTarget = interactionTarget; + RestartAnimation(); + } + + internal void DrawGlyph( + Graphics graphics, + Rectangle bounds, + FlatStyle flatStyle, + bool enabled, + bool hovered, + bool focused, + Color? customOnColor, + Color? customBorderColor) + { + SetInteractionState(hovered, focused); + + bool isDark = Application.IsDarkModeEnabled; + bool highContrast = SystemInformation.HighContrast; + Color onColor = highContrast + ? SystemColors.Highlight + : customOnColor ?? WindowsAccentColor; + + Color offBorderColor = highContrast + ? SystemColors.WindowText + : customBorderColor + ?? (isDark + ? Color.FromArgb(0x9B, 0x9B, 0x9B) + : SystemColors.ControlDark); + + Color offBackColor = highContrast + ? SystemColors.Window + : isDark + ? Color.FromArgb(0x2D, 0x2D, 0x2D) + : Color.White; + + if (!enabled) + { + onColor = highContrast + ? SystemColors.GrayText + : isDark + ? Color.FromArgb(0x55, 0x55, 0x55) + : Color.FromArgb(0xC0, 0xC0, 0xC0); + offBorderColor = highContrast + ? SystemColors.GrayText + : isDark + ? Color.FromArgb(0x45, 0x45, 0x45) + : Color.FromArgb(0xD0, 0xD0, 0xD0); + } + + Color backColor = LerpColor(offBackColor, onColor, _fillCurrent); + Color borderColor = LerpColor(offBorderColor, onColor, _fillCurrent); + float interaction = enabled && !highContrast ? _interactionCurrent : 0f; + backColor = ApplyInteractionShade(backColor, interaction); + borderColor = ApplyInteractionShade(borderColor, interaction); + + GraphicsState? saved = graphics.Save(); + try + { + graphics.SmoothingMode = SmoothingMode.AntiAlias; + using GraphicsPath path = CreateBoxPath(bounds, flatStyle); + + using (var brush = backColor.GetCachedSolidBrushScope()) + { + graphics.FillPath(brush, path); + } + + int borderThickness = Math.Max( + 1, + Control.LogicalToDeviceUnits(flatStyle == FlatStyle.Popup ? 2 : 1)); + + using (var pen = new Pen(borderColor, borderThickness) { Alignment = PenAlignment.Inset }) + { + graphics.DrawPath(pen, path); + } + + Color glyphColor = enabled + ? highContrast + ? SystemColors.HighlightText + : ModernButtonColorMath.GetReadableForeColor(onColor) + : offBackColor; + + if (_checkAlphaCurrent > 0) + { + DrawCheckmark(graphics, bounds, glyphColor, _checkAlphaCurrent); + } + + if (_dashAlphaCurrent > 0) + { + DrawDash(graphics, bounds, glyphColor, _dashAlphaCurrent); + } + } + finally + { + if (saved is not null) + { + graphics.Restore(saved); + } + } + } + + public override void AnimationProc(float animationProgress) + { + base.AnimationProc(animationProgress); + float easedProgress = EaseOut(animationProgress); + _fillCurrent = Lerp(_fillStart, _fillTarget, easedProgress); + _checkAlphaCurrent = Lerp(_checkAlphaStart, _checkAlphaTarget, easedProgress); + _dashAlphaCurrent = Lerp(_dashAlphaStart, _dashAlphaTarget, easedProgress); + _interactionCurrent = Lerp(_interactionStart, _interactionTarget, easedProgress); + Invalidate(); + } + + public override void RenderControl(Graphics graphics) + { + } + + protected override (int animationDuration, AnimationCycle animationCycle) OnAnimationStarted() + { + _fillStart = _fillCurrent; + _checkAlphaStart = _checkAlphaCurrent; + _dashAlphaStart = _dashAlphaCurrent; + _interactionStart = _interactionCurrent; + AnimationProgress = 0; + return (AnimationDuration, AnimationCycle.Once); + } + + protected override void OnAnimationStopped() + { + } + + protected override void OnAnimationEnded() + { + StopAnimation(); + _fillCurrent = _fillTarget; + _checkAlphaCurrent = _checkAlphaTarget; + _dashAlphaCurrent = _dashAlphaTarget; + _interactionCurrent = _interactionTarget; + AnimationProgress = 1; + Invalidate(); + } + + private static (float Fill, float CheckAlpha, float DashAlpha) GetStateTargets(CheckState state) + => state switch + { + CheckState.Checked => (1f, 1f, 0f), + CheckState.Indeterminate => (1f, 0f, 1f), + _ => (0f, 0f, 0f) + }; + + private static float Lerp(float from, float to, float progress) + => from + ((to - from) * Math.Clamp(progress, 0f, 1f)); + + private static Color LerpColor(Color from, Color to, float progress) + { + progress = Math.Clamp(progress, 0f, 1f); + + return Color.FromArgb( + LerpChannel(from.A, to.A, progress), + LerpChannel(from.R, to.R, progress), + LerpChannel(from.G, to.G, progress), + LerpChannel(from.B, to.B, progress)); + + static int LerpChannel(int from, int to, float progress) + => from + (int)((to - from) * progress); + } + + private static float EaseOut(float progress) + => 1 - ((1 - progress) * (1 - progress)); + + private static GraphicsPath CreateBoxPath(Rectangle bounds, FlatStyle flatStyle) + { + GraphicsPath path = new(); + if (flatStyle == FlatStyle.Flat) + { + path.AddRectangle(bounds); + return path; + } + + double radiusFactor = flatStyle == FlatStyle.Popup ? 0.3 : 0.2; + int radius = Math.Max(1, (int)(Math.Min(bounds.Width, bounds.Height) * radiusFactor)); + path.AddRoundedRectangle(bounds, new Size(radius, radius)); + return path; + } + + private static void DrawCheckmark(Graphics graphics, Rectangle bounds, Color color, float alpha) + { + using var pen = new Pen( + Color.FromArgb((int)(alpha * 255), color), + Math.Max(1.5f, bounds.Width * 0.12f)) + { + StartCap = LineCap.Round, + EndCap = LineCap.Round, + LineJoin = LineJoin.Round + }; + + PointF first = new(bounds.Left + (bounds.Width * 0.20f), bounds.Top + (bounds.Height * 0.52f)); + PointF second = new(bounds.Left + (bounds.Width * 0.42f), bounds.Top + (bounds.Height * 0.74f)); + PointF third = new(bounds.Left + (bounds.Width * 0.82f), bounds.Top + (bounds.Height * 0.28f)); + graphics.DrawLines(pen, [first, second, third]); + } + + private static void DrawDash(Graphics graphics, Rectangle bounds, Color color, float alpha) + { + using var pen = new Pen( + Color.FromArgb((int)(alpha * 255), color), + Math.Max(1.5f, bounds.Height * 0.14f)) + { + StartCap = LineCap.Round, + EndCap = LineCap.Round + }; + + float y = bounds.Top + (bounds.Height * 0.5f); + graphics.DrawLine( + pen, + bounds.Left + (bounds.Width * 0.2f), + y, + bounds.Right - (bounds.Width * 0.2f), + y); + } +} diff --git a/src/System.Windows.Forms/System/Windows/Forms/Rendering/CheckBox/AnimatedToggleSwitchRenderer.cs b/src/System.Windows.Forms/System/Windows/Forms/Rendering/CheckBox/AnimatedToggleSwitchRenderer.cs new file mode 100644 index 00000000000..444c7c0d0f0 --- /dev/null +++ b/src/System.Windows.Forms/System/Windows/Forms/Rendering/CheckBox/AnimatedToggleSwitchRenderer.cs @@ -0,0 +1,435 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. + +using System.Drawing; +using System.Drawing.Drawing2D; +using System.Windows.Forms.Rendering.Animation; +using System.Windows.Forms.Rendering.Button; + +namespace System.Windows.Forms.Rendering.CheckBox; + +/// +/// Renders and animates a or +/// in mode. Used when +/// is or later. +/// +internal sealed class AnimatedToggleSwitchRenderer : AnimatedControlRenderer +{ + private const int AnimationDuration = 300; + + private readonly ModernCheckBoxStyle _switchStyle; + private float _focusCurrent; + private float _focusStart; + private float _focusTarget; + private float _hoverCurrent; + private float _hoverStart; + private float _hoverTarget; + private bool _interactionInitialized; + private float _onAmountCurrent; + private float _onAmountStart; + private float _onAmountTarget; + private bool _positionInitialized; + private float _positionCurrent; + private float _positionStart; + private float _positionTarget; + + public AnimatedToggleSwitchRenderer(Control control, ModernCheckBoxStyle switchStyle) + : base(control) + { + _switchStyle = switchStyle; + } + + public override void AnimationProc(float animationProgress) + { + base.AnimationProc(animationProgress); + float easedProgress = EaseOut(animationProgress); + _positionCurrent = Lerp(_positionStart, _positionTarget, easedProgress); + _onAmountCurrent = Lerp(_onAmountStart, _onAmountTarget, easedProgress); + _focusCurrent = Lerp(_focusStart, _focusTarget, easedProgress); + _hoverCurrent = Lerp(_hoverStart, _hoverTarget, easedProgress); + Invalidate(); + } + + protected override (int animationDuration, AnimationCycle animationCycle) OnAnimationStarted() + { + EnsurePositionInitialized(); + _positionStart = _positionCurrent; + _positionTarget = IsChecked ? 1f : 0f; + _onAmountStart = _onAmountCurrent; + _onAmountTarget = IsChecked ? 1f : 0f; + _focusStart = _focusCurrent; + _hoverStart = _hoverCurrent; + AnimationProgress = 0; + + return (AnimationDuration, AnimationCycle.Once); + } + + /// + /// Called from the control's OnPaint. Works both while the animation is running (driven by + /// ) and when it is settled (progress is 1). + /// + /// The graphics object to render into. + public override void RenderControl(Graphics graphics) + { + EnsurePositionInitialized(); + SetInteractionState( + hovered: MouseIsOver, + focused: Control.Focused && ShowFocusCues); + + ToggleSwitchMetrics metrics = ToggleSwitchMetrics.Create(Control); + Size textSize = TextRenderer.MeasureText(Control.Text, Control.Font); + Rectangle contentBounds = ToggleSwitchMetrics.GetContentBounds(Control); + int totalHeight = Math.Max(textSize.Height, metrics.SwitchHeight); + int contentTop = contentBounds.Top + Math.Max(0, (contentBounds.Height - totalHeight) / 2); + int textY = contentTop + ((totalHeight - textSize.Height) / 2); + Rectangle switchBounds = GetSwitchBounds( + Control, + RtlTranslatedCheckAlign, + metrics, + textSize); + + graphics.Clear(Control.BackColor); + + if (contentBounds.Width <= 0 || contentBounds.Height <= 0) + { + return; + } + + if (IsSwitchOnRight(RtlTranslatedCheckAlign)) + { + int textX = Math.Max(contentBounds.Left, switchBounds.Left - metrics.TextGap - textSize.Width); + RenderSwitch(graphics, switchBounds, metrics); + RenderText(graphics, new Point(textX, textY)); + } + else + { + RenderSwitch(graphics, switchBounds, metrics); + RenderText(graphics, new Point(contentBounds.Left + metrics.SwitchWidth + metrics.TextGap, textY)); + } + + if (Control.Focused && ShowFocusCues) + { + Rectangle focusBounds = Rectangle.Inflate(Control.ClientRectangle, -1, -1); + ControlPaint.DrawFocusRectangle( + graphics, + focusBounds, + Control.ForeColor, + Control.BackColor); + } + } + + internal void SetInteractionState(bool hovered, bool focused) + { + float hoverTarget = hovered ? 1f : 0f; + float focusTarget = focused ? 1f : 0f; + if (!_interactionInitialized) + { + _interactionInitialized = true; + _hoverCurrent = _hoverStart = _hoverTarget = hoverTarget; + _focusCurrent = _focusStart = _focusTarget = focusTarget; + return; + } + + if (_hoverTarget == hoverTarget && _focusTarget == focusTarget) + { + return; + } + + _hoverTarget = hoverTarget; + _focusTarget = focusTarget; + RestartAnimation(); + } + + private static bool IsSwitchOnRight(ContentAlignment checkAlign) => checkAlign is + ContentAlignment.TopRight or + ContentAlignment.MiddleRight or + ContentAlignment.BottomRight; + + internal static Rectangle GetSwitchBounds( + Control control, + ContentAlignment checkAlign, + ToggleSwitchMetrics metrics) + => GetSwitchBounds( + control, + checkAlign, + metrics, + TextRenderer.MeasureText(control.Text, control.Font)); + + private static Rectangle GetSwitchBounds( + Control control, + ContentAlignment checkAlign, + ToggleSwitchMetrics metrics, + Size textSize) + { + Rectangle contentBounds = ToggleSwitchMetrics.GetContentBounds(control); + int totalHeight = Math.Max(textSize.Height, metrics.SwitchHeight); + int contentTop = contentBounds.Top + Math.Max(0, (contentBounds.Height - totalHeight) / 2); + int switchY = contentTop + ((totalHeight - metrics.SwitchHeight) / 2); + int switchX = IsSwitchOnRight(checkAlign) + ? Math.Max(contentBounds.Left, contentBounds.Right - metrics.SwitchWidth) + : contentBounds.Left; + + return new Rectangle(switchX, switchY, metrics.SwitchWidth, metrics.SwitchHeight); + } + + private void RenderText(Graphics graphics, Point position) + => TextRenderer.DrawText( + graphics, + Control.Text, + Control.Font, + position, + Control.Enabled ? Control.ForeColor : SystemColors.GrayText); + + private void RenderSwitch(Graphics graphics, Rectangle rect, ToggleSwitchMetrics metrics) + { + if (rect.Width <= 0 || rect.Height <= 0) + { + return; + } + + bool highContrast = SystemInformation.HighContrast; + Color onColor = highContrast + ? SystemColors.Highlight + : WindowsAccentColor; + Color offColor = highContrast ? SystemColors.Window : SystemColors.ControlDark; + Color backgroundColor = Control.Enabled + ? PopupButtonColorMath.Blend(offColor, onColor, _onAmountCurrent) + : SystemColors.Control; + Color highContrastForeground = PopupButtonColorMath.Blend( + SystemColors.WindowText, + SystemColors.HighlightText, + _onAmountCurrent); + Color borderColor = Control.Enabled + ? highContrast + ? highContrastForeground + : SystemColors.WindowFrame + : SystemColors.GrayText; + float focus = Control.Enabled && !highContrast ? _focusCurrent : 0f; + backgroundColor = ApplyInteractionShade(backgroundColor, focus); + Color circleColor = Control.Enabled + ? highContrast + ? highContrastForeground + : PopupButtonColorMath.GetReadableForeColor(offColor, onColor) + : SystemColors.GrayText; + circleColor = ApplyInteractionShade(circleColor, focus); + borderColor = ApplyInteractionShade(borderColor, focus); + + float thumbDiameter = Lerp( + metrics.ThumbDiameter, + metrics.HoverThumbDiameter, + Control.Enabled ? _hoverCurrent : 0f); + float circlePosition = (rect.Width - thumbDiameter) * _positionCurrent; + + using var backgroundBrush = backgroundColor.GetCachedSolidBrushScope(); + using var circleBrush = circleColor.GetCachedSolidBrushScope(); + using var backgroundPen = borderColor.GetCachedPenScope(metrics.BorderThickness); + + SmoothingMode previousSmoothingMode = graphics.SmoothingMode; + try + { + graphics.SmoothingMode = SmoothingMode.AntiAlias; + + if (_switchStyle == ModernCheckBoxStyle.Rounded) + { + float radius = Math.Min(rect.Width, rect.Height) / 2f; + + using GraphicsPath path = new(); + path.AddArc(rect.X, rect.Y, radius * 2, radius * 2, 180, 90); + path.AddArc(rect.Right - radius * 2, rect.Y, radius * 2, radius * 2, 270, 90); + path.AddArc(rect.Right - radius * 2, rect.Bottom - radius * 2, radius * 2, radius * 2, 0, 90); + path.AddArc(rect.X, rect.Bottom - radius * 2, radius * 2, radius * 2, 90, 90); + path.CloseFigure(); + + graphics.FillPath(backgroundBrush, path); + graphics.DrawPath(backgroundPen, path); + } + else + { + graphics.FillRectangle(backgroundBrush, rect); + graphics.DrawRectangle(backgroundPen, rect); + } + + float circleTop = rect.Y + ((rect.Height - thumbDiameter) / 2f); + graphics.FillEllipse( + circleBrush, + rect.X + circlePosition, + circleTop, + thumbDiameter, + thumbDiameter); + } + finally + { + graphics.SmoothingMode = previousSmoothingMode; + } + } + + internal void PrepareStateChange() + { + EnsurePositionInitialized(); + StopAnimation(); + } + + internal void SynchronizeState() + { + StopAnimation(); + _positionCurrent = IsChecked ? 1f : 0f; + _positionStart = _positionCurrent; + _positionTarget = _positionCurrent; + _onAmountCurrent = IsChecked ? 1f : 0f; + _onAmountStart = _onAmountCurrent; + _onAmountTarget = _onAmountCurrent; + _focusCurrent = _focusStart = _focusTarget; + _hoverCurrent = _hoverStart = _hoverTarget; + _interactionInitialized = true; + _positionInitialized = true; + AnimationProgress = 1; + } + + protected override void OnAnimationStopped() + { + } + + protected override void OnAnimationEnded() + { + StopAnimation(); + _positionCurrent = _positionTarget; + _positionStart = _positionCurrent; + _onAmountCurrent = _onAmountTarget; + _onAmountStart = _onAmountCurrent; + _focusCurrent = _focusTarget; + _focusStart = _focusCurrent; + _hoverCurrent = _hoverTarget; + _hoverStart = _hoverCurrent; + AnimationProgress = 1; + Invalidate(); + } + + private void EnsurePositionInitialized() + { + if (_positionInitialized) + { + return; + } + + _positionCurrent = IsChecked ? 1f : 0f; + _positionStart = _positionCurrent; + _positionTarget = _positionCurrent; + _onAmountCurrent = IsChecked ? 1f : 0f; + _onAmountStart = _onAmountCurrent; + _onAmountTarget = _onAmountCurrent; + _positionInitialized = true; + } + + private static float EaseOut(float value) + => 1 - ((1 - value) * (1 - value)); + + private static float Lerp(float start, float end, float amount) + => start + ((end - start) * amount); + + private bool IsChecked => Control switch + { + Forms.CheckBox checkBox => checkBox.Checked, + Forms.RadioButton radioButton => radioButton.Checked, + _ => false + }; + + private ContentAlignment RtlTranslatedCheckAlign => Control switch + { + Forms.CheckBox checkBox => checkBox.RtlTranslatedCheckAlign, + Forms.RadioButton radioButton => radioButton.RtlTranslatedCheckAlign, + _ => ContentAlignment.MiddleLeft + }; + + private bool ShowFocusCues => Control switch + { + Forms.CheckBox checkBox => checkBox.ShowFocusCuesInternal, + Forms.RadioButton radioButton => radioButton.ShowFocusCuesInternal, + _ => false + }; + + private bool MouseIsOver => Control switch + { + Forms.CheckBox checkBox => checkBox.MouseIsOver, + Forms.RadioButton radioButton => radioButton.MouseIsOver, + _ => false + }; +} + +/// +/// Provides the geometry shared by toggle-switch rendering and preferred-size calculations. +/// +internal readonly struct ToggleSwitchMetrics +{ + private ToggleSwitchMetrics( + int switchWidth, + int switchHeight, + int thumbDiameter, + int hoverThumbDiameter, + int borderThickness, + int textGap) + { + SwitchWidth = switchWidth; + SwitchHeight = switchHeight; + ThumbDiameter = thumbDiameter; + HoverThumbDiameter = hoverThumbDiameter; + BorderThickness = borderThickness; + TextGap = textGap; + } + + internal int SwitchWidth { get; } + + internal int SwitchHeight { get; } + + internal int ThumbDiameter { get; } + + internal int HoverThumbDiameter { get; } + + internal int BorderThickness { get; } + + internal int TextGap { get; } + + internal static ToggleSwitchMetrics Create(Control control) + { + int switchHeight = Math.Max( + control.LogicalToDeviceUnits(13), + (int)(control.Font.Height * 0.9f)); + int minimumMetric = Math.Max(1, control.LogicalToDeviceUnits(1)); + int borderThickness = Math.Max(minimumMetric, switchHeight / 12); + int maximumThumbDiameter = Math.Max(1, switchHeight - (2 * borderThickness)); + int thumbDiameter = Math.Max( + 1, + Math.Min( + switchHeight - (2 * (borderThickness + minimumMetric)), + (int)Math.Floor(maximumThumbDiameter / 1.1f))); + int hoverThumbDiameter = Math.Min( + maximumThumbDiameter, + Math.Max(thumbDiameter, (int)Math.Ceiling(thumbDiameter * 1.1f))); + int textGap = Math.Max(2 * minimumMetric, switchHeight / 3); + + return new( + switchWidth: 2 * switchHeight, + switchHeight: switchHeight, + thumbDiameter: thumbDiameter, + hoverThumbDiameter: hoverThumbDiameter, + borderThickness: borderThickness, + textGap: textGap); + } + + internal static Rectangle GetContentBounds(Control control) + { + Padding padding = control.Padding; + return new Rectangle( + padding.Left, + padding.Top, + Math.Max(0, control.ClientSize.Width - padding.Horizontal), + Math.Max(0, control.ClientSize.Height - padding.Vertical)); + } + + internal Size GetPreferredSize(Control control) + { + Size textSize = TextRenderer.MeasureText(control.Text, control.Font); + return new Size( + SwitchWidth + TextGap + textSize.Width + control.Padding.Horizontal, + Math.Max(SwitchHeight, textSize.Height) + control.Padding.Vertical); + } +} diff --git a/src/System.Windows.Forms/System/Windows/Forms/Rendering/CheckBox/ModernCheckBoxStyle.cs b/src/System.Windows.Forms/System/Windows/Forms/Rendering/CheckBox/ModernCheckBoxStyle.cs new file mode 100644 index 00000000000..32f35518ed3 --- /dev/null +++ b/src/System.Windows.Forms/System/Windows/Forms/Rendering/CheckBox/ModernCheckBoxStyle.cs @@ -0,0 +1,10 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. + +namespace System.Windows.Forms.Rendering.CheckBox; + +internal enum ModernCheckBoxStyle +{ + Rectangular, + Rounded +} diff --git a/src/System.Windows.Forms/System/Windows/Forms/Rendering/RadioButton/AnimatedRadioGlyphRenderer.cs b/src/System.Windows.Forms/System/Windows/Forms/Rendering/RadioButton/AnimatedRadioGlyphRenderer.cs new file mode 100644 index 00000000000..2582400678c --- /dev/null +++ b/src/System.Windows.Forms/System/Windows/Forms/Rendering/RadioButton/AnimatedRadioGlyphRenderer.cs @@ -0,0 +1,240 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. + +using System.Drawing; +using System.Drawing.Drawing2D; +using System.Windows.Forms.Rendering.Animation; +using System.Windows.Forms.Rendering.Button; + +namespace System.Windows.Forms.Rendering.RadioButton; + +/// +/// Animates and draws the modern normal-appearance RadioButton glyph. +/// +internal sealed class AnimatedRadioGlyphRenderer : AnimatedControlRenderer +{ + private const int AnimationDuration = 200; + private const float HoverGrowth = 1.1f; + + private float _dotScaleCurrent; + private float _dotScaleStart; + private float _dotScaleTarget; + private float _focusCurrent; + private float _focusStart; + private float _focusTarget; + private float _hoverCurrent; + private float _hoverStart; + private float _hoverTarget; + private bool _interactionInitialized; + private bool _stateInitialized; + + public AnimatedRadioGlyphRenderer(Control control) : base(control) + { + } + + internal void NotifyCheckedChanged(bool newChecked) + { + float dotScaleTarget = newChecked ? 1f : 0f; + if (!_stateInitialized) + { + _stateInitialized = true; + _dotScaleCurrent = _dotScaleStart = _dotScaleTarget = dotScaleTarget; + return; + } + + if (_dotScaleTarget == dotScaleTarget) + { + return; + } + + _dotScaleTarget = dotScaleTarget; + RestartAnimation(); + } + + internal void SetInteractionState(bool hovered, bool focused) + { + float hoverTarget = hovered ? 1f : 0f; + float focusTarget = focused ? 1f : 0f; + if (!_interactionInitialized) + { + _interactionInitialized = true; + _hoverCurrent = _hoverStart = _hoverTarget = hoverTarget; + _focusCurrent = _focusStart = _focusTarget = focusTarget; + return; + } + + if (_hoverTarget == hoverTarget && _focusTarget == focusTarget) + { + return; + } + + _hoverTarget = hoverTarget; + _focusTarget = focusTarget; + RestartAnimation(); + } + + internal void DrawGlyph( + Graphics graphics, + Rectangle bounds, + FlatStyle flatStyle, + bool enabled, + bool hovered, + bool focused, + Color? customOnColor, + Color? customBorderColor) + { + SetInteractionState(hovered, focused); + + bool isDark = Application.IsDarkModeEnabled; + bool highContrast = SystemInformation.HighContrast; + Color onColor = highContrast + ? SystemColors.Highlight + : customOnColor ?? WindowsAccentColor; + + Color borderColor = highContrast + ? SystemColors.WindowText + : customBorderColor + ?? (isDark + ? Color.FromArgb(0x9B, 0x9B, 0x9B) + : SystemColors.ControlDark); + + Color backColor = highContrast + ? SystemColors.Window + : isDark + ? Color.FromArgb(0x2D, 0x2D, 0x2D) + : Color.White; + + if (!enabled) + { + onColor = highContrast + ? SystemColors.GrayText + : isDark + ? Color.FromArgb(0x55, 0x55, 0x55) + : Color.FromArgb(0xC0, 0xC0, 0xC0); + borderColor = highContrast + ? SystemColors.GrayText + : isDark + ? Color.FromArgb(0x45, 0x45, 0x45) + : Color.FromArgb(0xD0, 0xD0, 0xD0); + } + + float focus = enabled && !highContrast ? _focusCurrent : 0f; + onColor = ApplyInteractionShade(onColor, focus); + borderColor = ApplyInteractionShade(borderColor, focus); + backColor = ApplyInteractionShade(backColor, focus); + + float normalOuterScale = 1f / HoverGrowth; + float outerScale = Lerp(normalOuterScale, 1f, enabled ? _hoverCurrent : 0f); + RectangleF outerBounds = ScaleFromCenter(bounds, outerScale); + RectangleF normalBounds = ScaleFromCenter(bounds, normalOuterScale); + + GraphicsState? saved = graphics.Save(); + try + { + graphics.SmoothingMode = SmoothingMode.AntiAlias; + + using (var brush = backColor.GetCachedSolidBrushScope()) + { + graphics.FillEllipse(brush, outerBounds); + } + + int borderThickness = Math.Max( + 1, + Control.LogicalToDeviceUnits(flatStyle == FlatStyle.Popup ? 2 : 1)); + + using (var pen = new Pen(borderColor, borderThickness)) + { + graphics.DrawEllipse(pen, outerBounds); + } + + if (_dotScaleCurrent > 0.001f) + { + float dotDiameter = normalBounds.Width * 0.5f * _dotScaleCurrent; + RectangleF dotRectangle = new( + normalBounds.X + ((normalBounds.Width - dotDiameter) / 2f), + normalBounds.Y + ((normalBounds.Height - dotDiameter) / 2f), + dotDiameter, + dotDiameter); + + Color dotOutlineColor = highContrast + ? SystemColors.HighlightText + : PopupButtonColorMath.GetReadableForeColor(onColor, backColor); + int outlineThickness = Math.Max(1, Control.LogicalToDeviceUnits(1)); + using var outlineBrush = dotOutlineColor.GetCachedSolidBrushScope(); + graphics.FillEllipse(outlineBrush, dotRectangle); + + RectangleF accentRectangle = RectangleF.Inflate( + dotRectangle, + -outlineThickness, + -outlineThickness); + if (accentRectangle.Width > 0 && accentRectangle.Height > 0) + { + using var dotBrush = onColor.GetCachedSolidBrushScope(); + graphics.FillEllipse(dotBrush, accentRectangle); + } + } + } + finally + { + if (saved is not null) + { + graphics.Restore(saved); + } + } + } + + public override void AnimationProc(float animationProgress) + { + base.AnimationProc(animationProgress); + float easedProgress = EaseOut(animationProgress); + _dotScaleCurrent = Lerp(_dotScaleStart, _dotScaleTarget, easedProgress); + _focusCurrent = Lerp(_focusStart, _focusTarget, easedProgress); + _hoverCurrent = Lerp(_hoverStart, _hoverTarget, easedProgress); + Invalidate(); + } + + public override void RenderControl(Graphics graphics) + { + } + + protected override (int animationDuration, AnimationCycle animationCycle) OnAnimationStarted() + { + _dotScaleStart = _dotScaleCurrent; + _focusStart = _focusCurrent; + _hoverStart = _hoverCurrent; + AnimationProgress = 0; + return (AnimationDuration, AnimationCycle.Once); + } + + protected override void OnAnimationStopped() + { + } + + protected override void OnAnimationEnded() + { + StopAnimation(); + _dotScaleCurrent = _dotScaleTarget; + _focusCurrent = _focusTarget; + _hoverCurrent = _hoverTarget; + AnimationProgress = 1; + Invalidate(); + } + + private static float EaseOut(float progress) + => 1 - ((1 - progress) * (1 - progress)); + + private static float Lerp(float from, float to, float progress) + => from + ((to - from) * Math.Clamp(progress, 0f, 1f)); + + private static RectangleF ScaleFromCenter(Rectangle bounds, float scale) + { + float width = bounds.Width * scale; + float height = bounds.Height * scale; + + return new( + bounds.X + ((bounds.Width - width) / 2f), + bounds.Y + ((bounds.Height - height) / 2f), + width, + height); + } +} diff --git a/src/System.Windows.Forms/System/Windows/Forms/SuspendPaintingScope.cs b/src/System.Windows.Forms/System/Windows/Forms/SuspendPaintingScope.cs new file mode 100644 index 00000000000..e65deea5d20 --- /dev/null +++ b/src/System.Windows.Forms/System/Windows/Forms/SuspendPaintingScope.cs @@ -0,0 +1,269 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. + +namespace System.Windows.Forms; + +using System.ComponentModel; +using System.Runtime.ExceptionServices; + +#if NET11_0_OR_GREATER +/// +/// Suspends painting for a target until the scope is disposed. +/// +/// +/// +/// This is a sealed class rather than a ref struct so the scope can span an +/// in an asynchronous UI event handler (for example, suspending painting for +/// the duration of an async data reload). is idempotent: disposing the scope more +/// than once only resumes painting once. +/// +/// +public sealed class SuspendPaintingScope : IDisposable +{ + private ISupportSuspendPainting? _target; + private Control[]? _layoutControls; + + /// + /// Initializes a new instance of the class. + /// + /// The target whose painting should be suspended. + public SuspendPaintingScope(ISupportSuspendPainting? target) + { + _target = target; + + if (target is null) + { + return; + } + + int initialPaintingCount = target is Control control + ? control.SuspendPaintingCount + : 0; + + try + { + target.BeginSuspendPainting(); + } + catch (Exception exception) + { + _target = null; + List exceptions = [exception]; + UnwindFailedPaintingAcquisition(target, initialPaintingCount, exceptions); + ThrowExceptions(exceptions); + } + } + + internal SuspendPaintingScope( + ISupportSuspendPainting target, + LayoutSuspendTraversal layoutSuspendTraversal) + : this(target, GetLayoutControls(target, layoutSuspendTraversal)) + { + } + + internal SuspendPaintingScope( + ISupportSuspendPainting target, + Func suspendLayoutContainerFilter) + : this(target, GetLayoutControls(target, suspendLayoutContainerFilter)) + { + } + + private SuspendPaintingScope( + ISupportSuspendPainting target, + Control[] layoutControls) + { + _target = target; + _layoutControls = layoutControls; + int initialPaintingCount = ((Control)target).SuspendPaintingCount; + int suspendedLayoutCount = 0; + + try + { + target.BeginSuspendPainting(); + + foreach (Control control in layoutControls) + { + control.SuspendLayout(); + suspendedLayoutCount++; + } + } + catch (Exception exception) + { + List exceptions = [exception]; + ResumeLayouts(layoutControls, suspendedLayoutCount, exceptions); + UnwindFailedPaintingAcquisition(target, initialPaintingCount, exceptions); + + _target = null; + _layoutControls = null; + ThrowExceptions(exceptions); + } + } + + /// + /// Resumes layout and painting for the target associated with this scope. + /// + public void Dispose() + { + ISupportSuspendPainting? target = _target; + Control[] layoutControls = _layoutControls ?? []; + _target = null; + _layoutControls = null; + + if (target is null) + { + return; + } + + List exceptions = []; + ResumeLayouts(layoutControls, layoutControls.Length, exceptions); + EndPainting(target, exceptions, requestRecursiveInvalidate: target is Control); + + ThrowExceptions(exceptions); + } + + private static Control[] GetLayoutControls( + ISupportSuspendPainting target, + LayoutSuspendTraversal layoutSuspendTraversal) + { + Control control = GetControl(target); + + return layoutSuspendTraversal switch + { + LayoutSuspendTraversal.None => [], + LayoutSuspendTraversal.TopLevelOnly => [control], + LayoutSuspendTraversal.Traverse => GetLayoutControls(control, static _ => true), + _ => throw new InvalidEnumArgumentException( + nameof(layoutSuspendTraversal), + (int)layoutSuspendTraversal, + typeof(LayoutSuspendTraversal)) + }; + } + + private static Control[] GetLayoutControls( + ISupportSuspendPainting target, + Func suspendLayoutContainerFilter) + { + ArgumentNullException.ThrowIfNull(suspendLayoutContainerFilter); + + return GetLayoutControls(GetControl(target), suspendLayoutContainerFilter); + } + + private static Control GetControl(ISupportSuspendPainting target) + { + ArgumentNullException.ThrowIfNull(target); + + return target as Control + ?? throw new InvalidOperationException(SR.ControlMutationExtensionsLayoutSuspensionRequiresControl); + } + + private static Control[] GetLayoutControls( + Control target, + Func suspendLayoutContainerFilter) + { + List layoutControls = []; + Stack controlsToVisit = new(); + controlsToVisit.Push(target); + + while (controlsToVisit.TryPop(out Control? control)) + { + if (suspendLayoutContainerFilter(control)) + { + layoutControls.Add(control); + } + + if (control.ChildControls is not { } children) + { + continue; + } + + for (int i = children.Count - 1; i >= 0; i--) + { + controlsToVisit.Push(children[i]); + } + } + + return [.. layoutControls]; + } + + private static void ResumeLayouts( + Control[] layoutControls, + int suspendedLayoutCount, + List exceptions) + { + for (int i = suspendedLayoutCount - 1; i >= 0; i--) + { + try + { + layoutControls[i].ResumeLayoutAfterSuspendPainting(); + } + catch (Exception exception) + { + exceptions.Add(exception); + } + } + } + + private static void EndPainting( + ISupportSuspendPainting target, + List exceptions, + bool requestRecursiveInvalidate = false) + { + try + { + if (requestRecursiveInvalidate) + { + ((Control)target).RequestRecursiveInvalidateAfterSuspendPainting(); + } + + target.EndSuspendPainting(); + } + catch (Exception exception) + { + exceptions.Add(exception); + } + } + + private static void UnwindFailedPaintingAcquisition( + ISupportSuspendPainting target, + int initialPaintingCount, + List exceptions) + { + if (target is not Control control) + { + return; + } + + while (control.SuspendPaintingCount > initialPaintingCount) + { + int paintingCount = control.SuspendPaintingCount; + + try + { + target.EndSuspendPainting(); + } + catch (Exception exception) + { + exceptions.Add(exception); + return; + } + + if (control.SuspendPaintingCount >= paintingCount) + { + return; + } + } + } + + private static void ThrowExceptions(List exceptions) + { + if (exceptions.Count == 1) + { + ExceptionDispatchInfo.Throw(exceptions[0]); + } + + if (exceptions.Count > 1) + { + throw new AggregateException(exceptions); + } + } +} +#endif diff --git a/src/System.Windows.Forms/System/Windows/Forms/VisualStylesMode.cs b/src/System.Windows.Forms/System/Windows/Forms/VisualStylesMode.cs new file mode 100644 index 00000000000..f1879068e59 --- /dev/null +++ b/src/System.Windows.Forms/System/Windows/Forms/VisualStylesMode.cs @@ -0,0 +1,55 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. + +namespace System.Windows.Forms; + +/// +/// Represents the version of the visual renderer that a control or the application uses. +/// +/// +/// +/// The visual styles version controls how a control renders its adorners, borders, and layout. +/// Newer versions can adjust minimum sizes, padding, and margins to satisfy current accessibility +/// requirements without changing the behavior of applications that target an earlier version. +/// +/// +public enum VisualStylesMode : short +{ + /// + /// The control inherits its from its parent, or, for a + /// top-level control, from . This is the ambient + /// default and is never returned by after resolution. + /// + /// + /// + /// This value is the ambient sentinel: assigning it to + /// clears any local override so the value is inherited again. It is not valid as the application + /// default and is rejected by . + /// + /// + Inherit = -1, + + /// + /// The classic version of the visual renderer (.NET 8 and earlier), based on version 6 of the + /// common controls library. + /// + Classic = 0, + + /// + /// Visual renderers are not in use - see . + /// Controls are based on version 5 of the common controls library. + /// + Disabled = 1, + + /// + /// The .NET 11 version of the visual renderer. Controls are rendered using the latest version + /// of the common controls library, and the adorner rendering or the layout of specific controls + /// has been improved based on the latest accessibility requirements. + /// + Net11 = 2, + + /// + /// The latest stable version of the visual renderer available in the running framework. + /// + Latest = short.MaxValue +} diff --git a/src/test/integration/UIIntegrationTests/NumericUpDownTests.cs b/src/test/integration/UIIntegrationTests/NumericUpDownTests.cs index f835831c3f0..1d0fac899fa 100644 --- a/src/test/integration/UIIntegrationTests/NumericUpDownTests.cs +++ b/src/test/integration/UIIntegrationTests/NumericUpDownTests.cs @@ -27,4 +27,25 @@ await RunSingleControlTestAsync(async (form, control) => Assert.NotNull(focused); }); } + + [WinFormsFact] + public async Task NumericUpDown_ModernChrome_UsesInsetEditAndSideBySideButtonsAsync() + { + await RunSingleControlTestAsync(async (form, control) => + { + control.VisualStylesMode = VisualStylesMode.Net11; + control.AutoSize = true; + form.PerformLayout(); + + if (!control.UseSideBySideButtons) + { + return; + } + + Assert.True(control.Height >= control.LogicalToDeviceUnits(15) * 2); + Assert.Equal(control.LogicalToDeviceUnits(3), control.TextBox.Left); + Assert.Equal(control.LogicalToDeviceUnits(3), control.UpDownButtonsInternal.Top); + Assert.True(control.UpDownButtonsInternal.Bounds.Left >= control.TextBox.Bounds.Right); + }); + } } diff --git a/src/test/integration/UIIntegrationTests/VisualStylesCrossFeatureTests.cs b/src/test/integration/UIIntegrationTests/VisualStylesCrossFeatureTests.cs new file mode 100644 index 00000000000..df29d20c6e5 --- /dev/null +++ b/src/test/integration/UIIntegrationTests/VisualStylesCrossFeatureTests.cs @@ -0,0 +1,226 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. + +using System.Drawing; +using System.Drawing.Drawing2D; + +namespace System.Windows.Forms.UITests; + +public class VisualStylesCrossFeatureTests : ControlTestBase +{ + public VisualStylesCrossFeatureTests(ITestOutputHelper testOutputHelper) + : base(testOutputHelper) + { + } + + [WinFormsFact] + public async Task VisualStyles_InheritedControls_RenderAgainstPatternedParentAcrossModesAndDpiAsync() + { + List samples = []; + + await RunFormWithoutControlAsync( + () => + { + Form form = new() + { + AutoScaleMode = AutoScaleMode.Dpi, + RightToLeft = RightToLeft.Yes, + RightToLeftLayout = true, + Size = new Size(900, 600) + }; + + PatternedGradientPanel parent = new() + { + Dock = DockStyle.Fill, + Padding = new Padding(8), + RightToLeft = RightToLeft.Yes, + VisualStylesMode = VisualStylesMode.Classic + }; + form.Controls.Add(parent); + + TableLayoutPanel table = new() + { + AutoScroll = true, + BackColor = Color.Transparent, + ColumnCount = 3, + Dock = DockStyle.Fill, + RowCount = 11 + }; + table.ColumnStyles.Add(new ColumnStyle(SizeType.Percent, 32)); + table.ColumnStyles.Add(new ColumnStyle(SizeType.Percent, 34)); + table.ColumnStyles.Add(new ColumnStyle(SizeType.Percent, 34)); + parent.Controls.Add(table); + + table.Controls.Add(new Label { Text = "Control / font", AutoSize = true }, 0, 0); + table.Controls.Add(new Label { Text = "AutoSize", AutoSize = true }, 1, 0); + table.Controls.Add(new Label { Text = "Fixed small", AutoSize = true }, 2, 0); + + string[] controlKinds = ["TextBox", "MaskedTextBox", "RichTextBox", "NumericUpDown", "DomainUpDown"]; + int row = 1; + foreach (float fontSize in new[] { 9f, 11f }) + { + foreach (string controlKind in controlKinds) + { + table.Controls.Add(new Label { Text = $"{controlKind} ({fontSize:0} pt)", AutoSize = true }, 0, row); + Control autoSized = CreateSampleControl(controlKind, fontSize, autoSize: true); + Control fixedSmall = CreateSampleControl(controlKind, fontSize, autoSize: false); + samples.Add(autoSized); + samples.Add(fixedSmall); + table.Controls.Add(autoSized, 1, row); + table.Controls.Add(fixedSmall, 2, row); + row++; + } + } + + return form; + }, + async form => + { + Panel parent = (Panel)form.Controls[0]; + Assert.Equal(form.DeviceDpi, parent.DeviceDpi); + Assert.All(samples, sample => Assert.Equal(RightToLeft.Yes, sample.RightToLeft)); + + foreach (Control sample in samples) + { + Assert.Equal(VisualStylesMode.Classic, sample.VisualStylesMode); + Assert.True(sample.Width > 0); + Assert.True(sample.Height > 0); + using Bitmap bitmap = new(sample.Width, sample.Height); + sample.DrawToBitmap(bitmap, new Rectangle(Point.Empty, sample.Size)); + } + + parent.VisualStylesMode = VisualStylesMode.Net11; + form.PerformLayout(); + Assert.All(samples, sample => Assert.Equal(VisualStylesMode.Net11, sample.VisualStylesMode)); + + foreach (Control sample in samples) + { + using Bitmap bitmap = new(sample.Width, sample.Height); + sample.DrawToBitmap(bitmap, new Rectangle(Point.Empty, sample.Size)); + } + + parent.VisualStylesMode = VisualStylesMode.Classic; + await Task.Yield(); + Assert.All(samples, sample => Assert.Equal(VisualStylesMode.Classic, sample.VisualStylesMode)); + }); + } + + [WinFormsFact] + public async Task VisualStyles_StandardSystemAndPopup_RenderFocusedDefaultAndPressedStatesAsync() + { + await RunFormWithoutControlAsync( + () => + { + Form form = new() { Size = new Size(500, 220) }; + FlowLayoutPanel panel = new() + { + Dock = DockStyle.Fill, + RightToLeft = RightToLeft.Yes, + FlowDirection = FlowDirection.LeftToRight + }; + form.Controls.Add(panel); + form.RightToLeft = RightToLeft.Yes; + form.RightToLeftLayout = true; + form.AutoScaleMode = AutoScaleMode.Dpi; + + Button standard = CreateButton(FlatStyle.Standard, "Standard"); + standard.NotifyDefault(true); + form.AcceptButton = standard; + panel.Controls.Add(standard); + foreach (FlatStyle style in new[] { FlatStyle.System, FlatStyle.Popup }) + { + Button button = CreateButton(style, style.ToString()); + button.NotifyDefault(true); + panel.Controls.Add(button); + } + + return form; + }, + async form => + { + FlowLayoutPanel panel = (FlowLayoutPanel)form.Controls[0]; + foreach (Button button in panel.Controls.OfType