Mega Code Archive

 
Categories / Delphi / Hardware
 

Highlight a component when mouse is above it

Title: Highlight a component when mouse is above it Question: The implementation-inheritance of Delphi makes it easy to add a feature like this to any control... Answer: This example shows how to highlight a label when the mouse moves over it. CM_MOUSEENTER and CM_MOUSELEAVE messages are used to trap the mouse movements and set a flag. TMyLabel = class(TLabel) private FMouseInPos : Boolean; procedure CMMouseEnter(var AMsg: TMessage); message CM_MOUSEENTER; procedure CMMouseLeave(var AMsg: TMessage); message CM_MOUSELEAVE; end; implementation procedure TMyLabel.CMMouseEnter(var AMsg: TMessage); begin FMouseInPos := True; Refresh; end; procedure TMyLabel.CMMouseLeave(var AMsg: TMessage); begin FMouseInPos := False; Refresh; end; TMyLabel.Paint; begin if FMouseInPos then Font.Color := clBlack else Font.Color := clRed; inherited; end;