Search code examples
delphidelphi-2007

How to use Winapi.Dwmapi.DwmSetWindowAttribute API for rounded window corners with Delphi 2007


I found this excellent source code for Delphi XE to make form windows rounded. How can I convert this code to work with Delphi 2007 and older? It does not compile properly.

My executable also needs to remain compatible with Windows 7 and 8, do I need to call this DLL dynamically to avoid issues with Win7?

unit delphi_rounded_corners;

interface
uses
  Winapi.Windows;

type TRoundedWindowCornerType = (RoundedCornerDefault, RoundedCornerOff, RoundedCornerOn, RoundedCornerSmall);

////////////////////////////////////////////////////////////////////////////
//
// Originally written by Ian Barker
//            https://github.com/checkdigits
//            https://about.me/IanBarker
//            [email protected]
//
// Based on an example in an answer during the RAD Studio 11 Launch Q & A
//
//
// Free software - use for any purpose including commercial use.
//
////////////////////////////////////////////////////////////////////////////
//
// Set or prevent Windows 11 from rounding the corners or your application
//
// Usage:
//         SetRoundedCorners(Self.Handle, RoundedCornerSmall);
//
////////////////////////////////////////////////////////////////////////////
///
procedure SetRoundedCorners(const TheHandle: HWND; const CornerType: TRoundedWindowCornerType);


implementation
uses
  Winapi.Dwmapi;

const

  //
  // More information:
  //      https://docs.microsoft.com/en-us/windows/apps/desktop/modernize/apply-rounded-corners
  //      https://docs.microsoft.com/en-us/windows/win32/api/dwmapi/ne-dwmapi-dwmwindowattribute
  //      https://docs.microsoft.com/en-us/windows/win32/api/dwmapi/nf-dwmapi-dwmsetwindowattribute
  //

  DWMWCP_DEFAULT    = 0; // Let the system decide whether or not to round window corners
  DWMWCP_DONOTROUND = 1; // Never round window corners
  DWMWCP_ROUND      = 2; // Round the corners if appropriate
  DWMWCP_ROUNDSMALL = 3; // Round the corners if appropriate, with a small radius

  DWMWA_WINDOW_CORNER_PREFERENCE = 33; // [set] WINDOW_CORNER_PREFERENCE, Controls the policy that rounds top-level window corners

procedure SetRoundedCorners(const TheHandle: HWND; const CornerType: TRoundedWindowCornerType);
var
  DWM_WINDOW_CORNER_PREFERENCE: Cardinal;
begin
  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;
  Winapi.Dwmapi.DwmSetWindowAttribute(TheHandle, DWMWA_WINDOW_CORNER_PREFERENCE, @DWM_WINDOW_CORNER_PREFERENCE, sizeof(DWM_WINDOW_CORNER_PREFERENCE));
 end;
end.

Solution

  • How can I convert this code to work with Delphi 2007 and older? It does not compile properly.

    Unit Scope Names were introduced in XE2. In order versions, simply drop the Winapi. prefix. In fact, you can do anyway even in modern Delphi versions, as Winapi is typically configured in a project's Unit Scope Names list by default. And if it is not, you can easily add it.

    The DwmApi unit does exist in Delphi 2007.

    Also, you should be using Windows.DWORD instead of Cardinal.

    Try this:

    unit delphi_rounded_corners;
    
    interface
    
    uses
      Windows;
    
    type
      TRoundedWindowCornerType = (RoundedCornerDefault, RoundedCornerOff, RoundedCornerOn, RoundedCornerSmall);
    
    ////////////////////////////////////////////////////////////////////////////
    //
    // Originally written by Ian Barker
    //            https://github.com/checkdigits
    //            https://about.me/IanBarker
    //            [email protected]
    //
    // Based on an example in an answer during the RAD Studio 11 Launch Q & A
    //
    //
    // Free software - use for any purpose including commercial use.
    //
    ////////////////////////////////////////////////////////////////////////////
    //
    // Set or prevent Windows 11 from rounding the corners or your application
    //
    // Usage:
    //         SetRoundedCorners(Self.Handle, RoundedCornerSmall);
    //
    ////////////////////////////////////////////////////////////////////////////
    ///
    procedure SetRoundedCorners(const TheHandle: HWND; const CornerType: TRoundedWindowCornerType);
    
    implementation
    
    uses
      Dwmapi;
    
    const
    
      //
      // More information:
      //      https://docs.microsoft.com/en-us/windows/apps/desktop/modernize/apply-rounded-corners
      //      https://docs.microsoft.com/en-us/windows/win32/api/dwmapi/ne-dwmapi-dwmwindowattribute
      //      https://docs.microsoft.com/en-us/windows/win32/api/dwmapi/nf-dwmapi-dwmsetwindowattribute
      //
    
      DWMWCP_DEFAULT    = 0; // Let the system decide whether or not to round window corners
      DWMWCP_DONOTROUND = 1; // Never round window corners
      DWMWCP_ROUND      = 2; // Round the corners if appropriate
      DWMWCP_ROUNDSMALL = 3; // Round the corners if appropriate, with a small radius
    
      DWMWA_WINDOW_CORNER_PREFERENCE = 33; // [set] WINDOW_CORNER_PREFERENCE, Controls the policy that rounds top-level window corners
    
    procedure SetRoundedCorners(const TheHandle: HWND; const CornerType: TRoundedWindowCornerType);
    var
      DWM_WINDOW_CORNER_PREFERENCE: DWORD;
    begin
      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;
    
      // or simpler, since TRoundedWindowCornerType and
      // the DWM_WINDOW_CORNER_PREFERENCE enum have the
      // same numeric values:
      //
      // DWM_WINDOW_CORNER_PREFERENCE := Ord(CornerType);
    
      Dwmapi.DwmSetWindowAttribute(TheHandle, DWMWA_WINDOW_CORNER_PREFERENCE, @DWM_WINDOW_CORNER_PREFERENCE, sizeof(DWM_WINDOW_CORNER_PREFERENCE));
    end;
    
    end.
    

    My executable also needs to remain compatible with Windows 7 and 8, do I need to call this DLL dynamically to avoid issues with Win7?

    Yes. Fortunately, the DwmApi unit already handles that for you internally. The DwmApi.DwmSetWindowAttribute() function dynamically loads DWMAPI.DLL and accesses the Win32 DwmSetWindowAttribute function at runtime, and it will return E_NOTIMPL if the API is not available.