Revisiting the VistaAltFixUnit.pas code

When Windows Vista was introduced, many moons ago now, many Delphi programs ran into a problem where buttons, checkboxes and the like would disappear when the user pressed the Alt key.  This was due to a change in how and when the underscores that displayed shortcuts on these buttons was displayed.

The problem was duly noted on Borland/CodeGear/Embarcadero’s QualityCentral forum, and several workarounds were documented.  The one that became most popular online seemed to be the one known as VistaAltFixUnit.pas.  Due to its popularity, we ended integrated that unit into our code, without spending a lot of time looking at it.  Surely many eyes had already reviewed it?

Now I don’t really want to be harsh but unfortunately there are a number of issues with the unit, ranging from performance to stability issues.  Here’s a rundown.

  1. The most egregious problem is the replacement of each TForm‘s WindowProc procedure.
    constructor TFormObj.Create(aForm: TForm; aRepaintAll: Boolean);
      inherited Create;
      Form := aForm;
      RepaintAll := aRepaintAll;
      Used := True;
      OrgProc := Form.WindowProc;
      Form.WindowProc := WndProc;
    procedure TFormObj.WndProc(var Message: TMessage);
      if (Message.Msg = WM_UPDATEUISTATE) then
        NeedRepaint := True;

    Like any naively chained procedure hook, if another component or other code also attempts to replace the WindowProc, the two will end up interfering with each other at some point, as neither component will be aware of the other, and destruction of either component, or attempts to unhook, are likely to end in access violations and tears shortly thereafter.

    Now, we also make use of the TNT Unicode components (as we have not yet moved to a fully Unicode version of Delphi — that’s a big job!), and it turns out that these components also use this approach for some of their own jiggery-pokery.  The TNT Unicode components are more important to us than this one…  guess which component suite got to keep their hook?

  2. Note that, possibly due to instabilities that became evident, the TFormObj class does not attempt to restore the original WindowProc when it is destroyed.
  3. Performance-wise, this component does a lot of work checking form status at application idle time.  As other users noted, the TApplicationEvents approach only works if no other code in the project has already assigned TApplication.OnIdle.

    In the end, we rewrote the component from scratch, and used a Windows CallWndProc hook as this is clean, simple and robust.  We also opted to live with a very minor flicker when the user first presses Alt, as this reduced the complexity of the code significantly, relying on Windows’ existing repaint infrastructure instead of reimplementing it ourselves.

    Any thoughts on the changes?  What have we got wrong?  Feel free to use this code without restrictions, for what it is worth — at your own risk of course!

    unit VistaAltFixUnit2;
      Windows, Classes;
      TVistaAltFix2 = class(TComponent)
        FInstalled: Boolean;
        function VistaWithTheme: Boolean;
        constructor Create(AOwner: TComponent); override;
        destructor Destroy; override;
    procedure Register;
      Messages, Themes;
    procedure Register;
      RegisterComponents('MEP', [TVistaAltFix2]);
      FInstallCount: Integer = 0;
      FCallWndProcHook: Cardinal = 0;
    { TVistaAltFix2 }
    function CallWndProcFunc(
      nCode: Integer;
      wParam: WPARAM;
      lParam: LPARAM): LRESULT; stdcall;
      p: PCWPSTRUCT;
      if nCode = HC_ACTION then
        p := PCWPSTRUCT(lParam);
        if p.message = WM_UPDATEUISTATE then
          InvalidateRect(p.hwnd, nil, False);
      Result := CallNextHookEx(FCallWndProcHook, nCode, wParam, lParam);
    constructor TVistaAltFix2.Create(AOwner: TComponent);
      if VistaWithTheme and not (csDesigning in ComponentState) then
        Inc(FInstallCount); // Allow more than 1 instance, assume single threaded as VCL is not thread safe anyway
        if FInstallCount = 1 then
          FCallWndProcHook := SetWindowsHookEx(WH_CALLWNDPROC, CallWndProcFunc, 0, GetCurrentThreadID);
        FInstalled := True;
    destructor TVistaAltFix2.Destroy;
      if FInstalled then
        if FInstallCount = 0 then
          FCallWndProcHook := 0;
      inherited Destroy;
    function TVistaAltFix2.VistaWithTheme: Boolean;
      OSVersionInfo: TOSVersionInfo;
      OSVersionInfo.dwOSVersionInfoSize := SizeOf(OSVersionInfo);
      if GetVersionEx(OSVersionInfo) and
         (OSVersionInfo.dwMajorVersion >= 6) and
         ThemeServices.ThemesEnabled then
        Result := True
        Result := False;

6 thoughts on “Revisiting the VistaAltFixUnit.pas code

  1. Thanks for this improved code, I’m using Delphi 7 and just came across this problem. Have you encountered any problems using this code or are there any changes to it ?

  2. Thank you – it givess such a good feeling to see that others come to the same conclusions as I do. Right now I am tracing AV bugs on shutdown in my application and VistaAltFixUnit really seems to mess things up. TFormObj object are not freed either, are they?

    1. I think TFormObj instances were held in a TObjectList, which frees its member items when they are removed from the list or the list itself is destroyed.

  3. Thanks for sharing, it seems a more clean method than the original indeed… however… Is there any special reason why you set:
    InvalidateRect(p.hwnd, nil, False);
    and not:
    InvalidateRect(p.hwnd, nil, True);
    just for performance? however I did found a small issue with that, if cleartype is enabled then text captions will become “blurry” after the code is fired… apparently setting it to True (to invalidate background) solves it. If you aware of any possible disadvantage please let me know.

    1. Yes, the reason it was set to false was primarily to reduce flicker. Note: you shouldn’t need this unit with any relatively recent version of Delphi.

Leave a Reply

Your email address will not be published. Required fields are marked *