7
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

[Delphi] Windows11 の角丸ウィンドウを抑止する

Last updated at Posted at 2022-02-13

Windows11 の角丸ウィンドウ

Windows 11 から下図の様にウィンドウが角丸になるようになりました。

でも!
それでは困る場合もあります。
ということで、Windows 11 で角丸ウィンドウを抑止する方法です。

角丸ウィンドウの抑止

DwmSetWindowAttribute API を使います。
DwmSetWindowAttribute の第二引数に属性を指定するのですが Windows 11 から DWMWA_WINDOW_CORNER_PREFERENCE という属性が増えています。
この属性と第三引数に以下の値を設定するとウィンドウの角丸を制御出来ます。

名前 説明
DWMWCP_DEFAULT 0 システムの設定に従う
DWMWCP_DONOTROUND 1 角を丸くしない
DWMWCP_ROUND 2 角丸
DWMWCP_ROUNDSMALL 3 小さな角丸

角を丸めないのであれば DWMWCP_DONOTROUND を指定すれば良い、ということですね。

ということで、これを実装したソースはこちら。
FireMonkey 用ですが、VCL 用も簡単に作れると思います。

(*
 * WindowCorner を変更出来る関数
 *
 * PLATFORMS
 *   Windows 11
 *
 * ENVIRONMENT
 *   Delphi 10.4.2, 11
 *
 * LICENSE
 *   Copyright (c) 2021 HOSOKAWA Jun
 *   Released under the MIT license
 *   http://opensource.org/licenses/mit-license.php
 *
 * HISTORY
 *   2021/09/24 Version 1.0.0
 *
 * Programmed by HOSOKAWA Jun (twitter: @pik)
 *)

unit PK.GUI.WindowCornerRound.Win;

interface

uses
  FMX.Forms;

type
  TCornerRound = (
    Default,    // DWMWCP_DEFAULT    = 0  システムの設定に従う
    DoNotRound, // DWMWCP_DONOTROUND = 1  角を丸くしない
    Round,      // DWMWCP_ROUND      = 2  角丸
    RoundSmall  // DWMWCP_ROUNDSMALL = 3  小さな角丸
  );

function SetWindowCornerRound(
  const AForm: TCommonCustomForm;
  const ACornerRound: TCornerRound): Boolean;

implementation

{$IFDEF MSWINDOWS}
uses
  Winapi.Windows
  , Winapi.DwmApi
  , FMX.Platform.Win
  ;

const
  DWMWA_WINDOW_CORNER_PREFERENCE = 33; // Windows 11 で追加された角丸属性

function SetWindowCornerRound(
  const AForm: TCommonCustomForm;
  const ACornerRound: TCornerRound): Boolean;
begin
  var CornerRound: DWORD := Ord(ACornerRound);

  // Windows 11 より前では常に False
  Result := Succeeded(
    DwmSetWindowAttribute(
      FormToHWND(AForm),
      DWMWA_WINDOW_CORNER_PREFERENCE,
      @CornerRound,
      SizeOf(CornerRound)
    )
  );
end;
{$ELSE}
function SetWindowCornerRound(
  const AForm: TCommonCustomForm;
  const ACornerRound: TCornerRound): Boolean;
begin
  Result := True;
end;
{$ENDIF}

end.

使い方は↓こんな感じで呼び出すだけ。

uses
  PK.GUI.WindowCornerRound.Win;

procedure TForm1.FormCreate(Sender: TObject);
begin
  SetWindowCornerRound(Self, TCornerRound.DoNotRound);
end;

おわりに

もうみんな知ってる事を今更書いた。
なお、詳しくは MSDN の下記の記事をどうぞ。

Windows 11 向けデスクトップ アプリケーションで角の丸めを適用する

7
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
7
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?