Sunday, February 15, 2009

Making your own Aero glass windows

In this post, I will share you how to make your forms in Windows Vista (or Windows 7) glassy style. Like in Windows media player.

The example will be written on Delphi, but it will look similar on other languages. It's quite easy, all you need to do is to import the dwmapi.dll in your app. You can find all useful information about Desktop Window Manager on MSDN (http://msdn.microsoft.com/en-us/library/aa969540(VS.85).aspx). Shortly, DWM is the new Windows desktop composition feature. When it is enabled, individual windows drawing is redirected to off-screen surfaces in video memory, which are then rendered into a desktop image and presented on the display.

So you import this library, like this:

procedure InitDWM();
var
  Handle: THandle;
begin
  Handle := LoadLibrary('dwmapi.dll');
  if Handle = 0 then
    raise Exception.Create('Can''t load dwmapi.dll');
  @MakeDWM := GetProcAddress(Handle,
    'DwmExtendFrameIntoClientArea');
  if not Assigned(MakeDWM) then
    raise Exception.Create(
      'Can''t get DwmExtendFrameIntoClientArea');
end;

As you see the only function you need to import from this library is DwmExtendFrameIntoClientArea. This function allows to extend window border areas to a suitable view. 

It takes two parameters. The first one is window handle and the second one is pointer to a Margins structure.

HRESULT DwmExtendFrameIntoClientArea(  
  HWND hWnd,
  const MARGINS *pMarInset
);

In Delphi this function and structure defines like this:
type
  TMargins = record
    LeftWidth: Integer;
    RightWidth: Integer;
    TopHeight: Integer;
    BottomHeight: Integer;
  end;

var
  MakeDWM: function (Handle: THandle;
      var
AMargins: TMargins): Integer; stdcall;



No comments: