Addadd chevron to TToolbar if some buttons are invisible (part 2)
Hello,

today I want to continue the previous tip and describe how to open the popup menu with invisible buttons of toolbar by click in chevron
button.

Drop the TPopupMenu component on your form and change the Name property to PopupMenuForToolbar.

Now we must add the handler for WM_NOTIFY message in declaration of our form:

  1. type
  2.    TForm1 = class(TForm)
  3.    private
  4.      { Private declarations }
  5.      procedure WMNotify(var Message: TMessage); message WM_NOTIFY;
  6.    end;
  7.  
  8. And use the next code as body of handler:
  9. procedure TForm1.WMNotify(var Message: TMessage);
  10. type
  11.     PNMREBARCHEVRON = ^TNMREBARCHEVRON;
  12.     TNMREBARCHEVRON = packed record
  13.      hdr: TNMHdr;
  14.      uBand: Integer;
  15.      wID: Integer;
  16.      lParam: LPARAM;
  17.      rc: TRect;
  18.      lParamNM: LPARAM;
  19.     end;
  20.  
  21. const
  22.    RBN_CHEVRONPUSHED = RBN_FIRST - 10;
  23.  
  24. var
  25.    i: Integer;
  26.    j: Integer;
  27.    R: TRect;
  28.    P: TPoint;
  29.    tb: TToolbar;
  30.    tbtn: TToolButton;
  31.    mi: TMenuItem;
  32. begin
  33.    inherited;
  34.  
  35.    case PNMHDR(Message.lParam)^.Code of
  36.      RBN_CHEVRONPUSHED:
  37.        begin
  38.          with PNMREBARCHEVRON(Message.lParam)^ do
  39.          begin
  40.            tb := yourCoolBar.Bands[uBand].Control as TToolbar;
  41.            R := tb.ClientRect;
  42.  
  43.            {find first invisible button in toolbar}
  44.            j := 0;
  45.            for i := 1 to tb.ButtonCount do
  46.            begin
  47.              tbtn := tb.Buttons[i-1];
  48.              if (tbtn.Left + tbtn.Width) > R.Right then
  49.              begin
  50.                j := i;
  51.                break;
  52.              end
  53.            end;
  54.  
  55.            if j  0 then
  56.            begin
  57.              {delete all existing items}
  58.              for i := PopupMenuForToolbar.Items.Count-1 downto 0 do
  59.              begin
  60.                mi := PopupMenuForToolbar.Items<i>;
  61.                PopupMenuForToolbar.Items.Delete(i);
  62.                mi.Free;
  63.              end;
  64.  
  65.              {add all invisible buttons as menu items}
  66.              for i := j to tb.ButtonCount do
  67.              begin
  68.                mi := TMenuItem.Create(Self);
  69.  
  70.                tbtn := tb.Buttons[i-1];
  71.                if (tbtn.Style = tbsSeparator) then
  72.                  mi.Caption := '-'
  73.                else
  74.                begin
  75.                  mi.Caption := tbtn.Caption;
  76.                  mi.Hint := tbtn.Hint;
  77.                  mi.Tag := tbtn.Tag;
  78.                  mi.OnClick := tbtn.OnClick;
  79.                end;
  80.                PopupMenuForToolbar.Items.Add(mi);
  81.              end;
  82.  
  83.              {show popup menu}
  84.              P := tb.ClientToScreen(Point(rc.Left, rc.Bottom));
  85.              PopupMenuForToolbar.Popup(P.X+2, P.Y+2);
  86.            end;
  87.          end;
  88.        end;
  89.    end;
  90. end;


As you see from comments in code, all what we need is to process the RBN_CHEVRONPUSHED code and add all invisible buttons from toolbar to
our popupmenu.

It's all - run the project and you'll get nice feature for standard controls with compartibility with future versions of MS Windows.

And you don't need pay for third-party components with same functionality. Buy the beer for saved money:-)

With best regards, Mike Shkolnik
EMail: mshkolnik@scalabium.com
http://www.scalabium.com
  • Author:
  • URL:
    http://www.scalabium.com
  • Date added:
    27 April, 2006
  • Views:
    1802
Latest News
Submit News Form Past News
Latest Forum Entries