How can I display the chevron for toolbar and open dropdowned popup menu with invisible buttons?

How can I display the chevron for toolbar and open dropdowned popup menu with invisible buttons?
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:

type
   TForm1 = class(TForm)
   private
     { Private declarations }
     procedure WMNotify(var Message: TMessage); message WM_NOTIFY;
   end;

And use the next code as body of handler:

procedure TForm1.WMNotify(var Message: TMessage);
type
    PNMREBARCHEVRON = ^TNMREBARCHEVRON;
    TNMREBARCHEVRON = packed record
     hdr: TNMHdr;
     uBand: Integer;
     wID: Integer;
     lParam: LPARAM;
     rc: TRect;
     lParamNM: LPARAM;
    end;

const
   RBN_CHEVRONPUSHED = RBN_FIRST - 10;

var
   i: Integer;
   j: Integer;
   R: TRect;
   P: TPoint;
   tb: TToolbar;
   tbtn: TToolButton;
   mi: TMenuItem;
begin
   inherited;

   case PNMHDR(Message.lParam)^.Code of
     RBN_CHEVRONPUSHED:
       begin
         with PNMREBARCHEVRON(Message.lParam)^ do
         begin
           tb := yourCoolBar.Bands[uBand].Control as TToolbar;
           R := tb.ClientRect;

           {find first invisible button in toolbar}
           j := 0;
           for i := 1 to tb.ButtonCount do
           begin
             tbtn := tb.Buttons[i-1];
             if (tbtn.Left + tbtn.Width) > R.Right then
             begin
               j := i;
               break;
             end
           end;

           if j <> 0 then
           begin
             {delete all existing items}
             for i := PopupMenuForToolbar.Items.Count-1 downto 0 do
             begin
               mi := PopupMenuForToolbar.Items[i];
               PopupMenuForToolbar.Items.Delete(i);
               mi.Free;
             end;

             {add all invisible buttons as menu items}
             for i := j to tb.ButtonCount do
             begin
               mi := TMenuItem.Create(Self);

               tbtn := tb.Buttons[i-1];
               if (tbtn.Style = tbsSeparator) then
                 mi.Caption := '-'
               else
               begin
                 mi.Caption := tbtn.Caption;
                 mi.Hint := tbtn.Hint;
                 mi.Tag := tbtn.Tag;
                 mi.OnClick := tbtn.OnClick;
               end;
               PopupMenuForToolbar.Items.Add(mi);
             end;

             {show popup menu}
             P := tb.ClientToScreen(Point(rc.Left, rc.Bottom));
             PopupMenuForToolbar.Popup(P.X+2, P.Y+2);
           end;
         end;
       end;
   end;
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:-)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值