delphi - How best to create a TPanel with a close 'cross' button in the top right? -


there several third-pary controls (such raize components) have close 'cross' button 'option' (eg page control). requirement simpler, i'd plonk cross 'button' aligned top right on tpanel , access clicked event. there either simple way of doint without creating tpanel descendent, or there paid or free library component can use?

i wrote control you.

unit closebutton;  interface  uses   windows, messages, sysutils, classes, controls, uxtheme;  type   tclosebutton = class(tcustomcontrol)   private     fmouseinside: boolean;     function mousebuttondown: boolean;   protected     procedure paint; override;     procedure mousemove(shift: tshiftstate; x: integer; y: integer); override;     procedure wndproc(var message: tmessage); override;     procedure mousedown(button: tmousebutton; shift: tshiftstate; x: integer;       y: integer); override;     procedure mouseup(button: tmousebutton; shift: tshiftstate; x: integer;       y: integer); override;   public     constructor create(aowner: tcomponent); override;   published     property align;     property anchors;     property enabled;     property onclick;     property onmouseup;     property onmousedown;   end;  procedure register;  implementation  procedure register; begin   registercomponents('rejbrand 2009', [tclosebutton]); end;  { tclosebutton }  constructor tclosebutton.create(aowner: tcomponent); begin   inherited;   width := 32;   height := 32; end;  function tclosebutton.mousebuttondown: boolean; begin   mousebuttondown := getkeystate(vk_lbutton) , $8000 <> 0; end;  procedure tclosebutton.mousedown(button: tmousebutton; shift: tshiftstate; x,   y: integer); begin   inherited;   invalidate; end;  procedure tclosebutton.mousemove(shift: tshiftstate; x, y: integer); begin   inherited;   if not fmouseinside   begin     fmouseinside := true;     invalidate;   end; end;  procedure tclosebutton.mouseup(button: tmousebutton; shift: tshiftstate; x,   y: integer); begin   inherited;   invalidate; end;  procedure tclosebutton.paint;    function getaerostate: cardinal;   begin     result := cbs_normal;     if not enabled       result := cbs_disabled     else       if fmouseinside         if mousebuttondown           result := cbs_pushed         else           result := cbs_hot;   end;    function getclassicstate: cardinal;   begin     result := 0;     if not enabled       result := dfcs_inactive     else       if fmouseinside         if mousebuttondown           result := dfcs_pushed         else           result := dfcs_hot;   end;  var   h: htheme; begin   inherited;   if usethemes   begin     h := openthemedata(handle, 'window');     if h <> 0       try         drawthemebackground(h,           canvas.handle,           wp_closebutton,           getaerostate,           clientrect,           nil);               closethemedata(h);       end;   end   else     drawframecontrol(canvas.handle,       clientrect,       dfc_caption,       dfcs_captionclose or getclassicstate) end;  procedure tclosebutton.wndproc(var message: tmessage); begin   inherited;   case message.msg of     wm_mouseleave:       begin         fmouseinside := false;         invalidate;       end;     cm_enabledchanged:       invalidate;   end; end;  end. 

sample (with , without themes enabled):

screenshot http://privat.rejbrand.se/closebuttonaero.png screenshot http://privat.rejbrand.se/closebuttonclassic.png

just put in tpanel @ top-right corner , set anchors top , right.


Comments

Popular posts from this blog

c# - SharpSVN - How to get the previous revision? -

c++ - Is it possible to compile a VST on linux? -

url - Querystring manipulation of email Address in PHP -