You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何检测Windows 10/11版本以适配窗口圆角代码

Windows 11窗口圆角适配与系统版本检测

一、判断系统是否为Windows 11及更高版本

Windows 11的系统构建号起始为22000,可通过VerifyVersionInfo函数准确判断,规避传统版本检测API的兼容性问题:

function IsWindows11OrGreater: Boolean;
var
  vi: OSVERSIONINFOEXW;
  cond: DWORDLONG;
begin
  ZeroMemory(@vi, SizeOf(vi));
  vi.dwOSVersionInfoSize := SizeOf(vi);
  vi.dwBuildNumber := 22000;
  vi.wServicePackMajor := 0;
  vi.wServicePackMinor := 0;

  cond := VerSetConditionMask(0, VER_BUILDNUMBER, VER_GREATER_EQUAL);
  Result := VerifyVersionInfoW(vi, VER_BUILDNUMBER, cond);
end;

二、调整窗口圆角实现方式

  • Windows 11及以上:直接调用系统原生API实现圆角(即你提供的原有逻辑)
  • Windows 10:系统无原生窗口圆角支持,需自行绘制圆角或选择不启用该效果

整合版本判断后的SetRoundedCorners函数:

procedure SetRoundedCorners(const TheHandle: HWND; const CornerType: TRoundedWindowCornerType);
var
  DWM_WINDOW_CORNER_PREFERENCE: DWORD;
begin
  if not IsWindows11OrGreater then
  begin
    // Win10处理逻辑:可自行实现圆角绘制,或直接跳过
    // 示例:调用自定义圆角绘制函数
    // CustomDrawRoundedCorners(TheHandle, CornerType);
    Exit;
  end;

  case CornerType of
    RoundedCornerOff:     DWM_WINDOW_CORNER_PREFERENCE := DWMWCP_DONOTROUND;
    RoundedCornerOn:      DWM_WINDOW_CORNER_PREFERENCE := DWMWCP_ROUND;
    RoundedCornerSmall:   DWM_WINDOW_CORNER_PREFERENCE := DWMWCP_ROUNDSMALL;
  else
    DWM_WINDOW_CORNER_PREFERENCE := DWMWCP_DEFAULT;
  end;

  Dwmapi.DwmSetWindowAttribute(TheHandle, DWMWA_WINDOW_CORNER_PREFERENCE, 
    @DWM_WINDOW_CORNER_PREFERENCE, sizeof(DWM_WINDOW_CORNER_PREFERENCE));
end;

Win10自定义圆角绘制示例(需根据窗口尺寸适配):

procedure CustomDrawRoundedCorners(const TheHandle: HWND; const CornerType: TRoundedWindowCornerType);
var
  Rgn: HRGN;
  WindowRect: TRect;
  Radius: Integer;
begin
  if CornerType = RoundedCornerOff then
  begin
    SetWindowRgn(TheHandle, 0, True);
    Exit;
  end;

  // 根据圆角类型设置半径
  case CornerType of
    RoundedCornerSmall: Radius := 8;
    RoundedCornerOn: Radius := 16;
  else
    Radius := 12; // 默认半径
  end;

  GetWindowRect(TheHandle, WindowRect);
  OffsetRect(WindowRect, -WindowRect.Left, -WindowRect.Top);

  Rgn := CreateRoundRectRgn(WindowRect.Left, WindowRect.Top, 
    WindowRect.Right, WindowRect.Bottom, Radius, Radius);
  SetWindowRgn(TheHandle, Rgn, True);
  DeleteObject(Rgn);
end;

三、检查圆角是否成功实现

通过DwmGetWindowAttribute读取当前窗口的圆角偏好设置,验证配置是否生效:

function GetRoundedCornersPreference(const TheHandle: HWND; out CornerType: TRoundedWindowCornerType): Boolean;
var
  Pref: DWORD;
  Res: HRESULT;
begin
  Res := Dwmapi.DwmGetWindowAttribute(TheHandle, DWMWA_WINDOW_CORNER_PREFERENCE, 
    @Pref, sizeof(Pref));
  Result := Succeeded(Res);
  if Result then
  begin
    case Pref of
      DWMWCP_DONOTROUND: CornerType := RoundedCornerOff;
      DWMWCP_ROUND: CornerType := RoundedCornerOn;
      DWMWCP_ROUNDSMALL: CornerType := RoundedCornerSmall;
    else
      CornerType := RoundedCornerDefault;
    end;
  end;
end;

调用示例:

var
  CurrentType: TRoundedWindowCornerType;
begin
  if GetRoundedCornersPreference(Self.Handle, CurrentType) then
  begin
    // 根据CurrentType判断当前设置是否符合预期
  end;
end;

内容的提问来源于stack exchange,提问作者Tom

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.17 19:07:30