Stuart_Johnson
asked on
Drawing in a TCustomLabel
I have made a control which (amongst other things) changes colours when the mouse is moved over it. Is there anyway that I can draw a black border around the label and then remove it when the mouse leaves?? I have tried just drawing directly on the canvas but it doesnt work.
Hi,
Do You meen just Tlabel class or something else?
What about this principal:
Procedure TYourcomponent.WMMousemove (Var message:TWMMousemove);
Begin
//Draw the frame around the control
Canvas.Brush.Color:=clBlac k;
Canvas.FrameRect(clientRec t);
//run timer
Timer.Enabled:=True;
End;
Procedure TYourComponent.OnTimer(sen der:Tobjec t);
Begin
Var Pt:Tpoint;
begin
GetCursorPos(Pt);
//check if the cursor is outside of the component
If WindowFromPoint(pt)<>Handl e Then
Begin
//Hide the frame without destroying other
//information of it
canvas.Brush.Color:=Color;
Canvas.FrameRect(ClientRec t);
Timer1.Enabled:=False;
End;
End;
- Rauno
Do You meen just Tlabel class or something else?
What about this principal:
Procedure TYourcomponent.WMMousemove
Begin
//Draw the frame around the control
Canvas.Brush.Color:=clBlac
Canvas.FrameRect(clientRec
//run timer
Timer.Enabled:=True;
End;
Procedure TYourComponent.OnTimer(sen
Begin
Var Pt:Tpoint;
begin
GetCursorPos(Pt);
//check if the cursor is outside of the component
If WindowFromPoint(pt)<>Handl
Begin
//Hide the frame without destroying other
//information of it
canvas.Brush.Color:=Color;
Canvas.FrameRect(ClientRec
Timer1.Enabled:=False;
End;
End;
- Rauno
Raunol, I'm not sure how using the extra resources of a timer is more advantagous than using the messages CMMouseEnter and CMMouseLeave to check whether the cursor is over the label. I can't find an situation where my code fails to detect the presence of the cursor over the label.
Steven.
Steven.
Well. It should have to create first Timer for You
component Timer:=Ttimer.Create(self)
And then have to make Tmylabel.Ontimer(sender:To bject)
function and then assign: Timer.onTimer:=Ontimer
But You are right and Stuart's code is much better. I
didn't discovered those messages. They should work.
But in Stuart's code I think, it is not nesissary to
repaint all contents of the label during framing. Just putting framerect under the Message-procedures should effects
borders without loosing the label contents already present.
Procedure Tmylabel.CMMouseEnter..
//paint frame
canvas.Brush.Color:=clBlac k;
Canvas.FrameRect(ClientRec t);
Procedure Tmylabel.CMMouseLeave..
//hide frame
canvas.Brush.Color:=color; //color=background color
Canvas.FrameRect(ClientRec t);
- Rauno
component Timer:=Ttimer.Create(self)
And then have to make Tmylabel.Ontimer(sender:To
function and then assign: Timer.onTimer:=Ontimer
But You are right and Stuart's code is much better. I
didn't discovered those messages. They should work.
But in Stuart's code I think, it is not nesissary to
repaint all contents of the label during framing. Just putting framerect under the Message-procedures should effects
borders without loosing the label contents already present.
Procedure Tmylabel.CMMouseEnter..
//paint frame
canvas.Brush.Color:=clBlac
Canvas.FrameRect(ClientRec
Procedure Tmylabel.CMMouseLeave..
//hide frame
canvas.Brush.Color:=color;
Canvas.FrameRect(ClientRec
- Rauno
ASKER
Raunol,
The answer I was looking for was provided by SteveB. Sorry.
Steve, if you want to repost the comment as an answer, I'll award the points too you.
Thanks alL!
Stu
The answer I was looking for was provided by SteveB. Sorry.
Steve, if you want to repost the comment as an answer, I'll award the points too you.
Thanks alL!
Stu
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Sorry, my mistake. You are Stuart. I had to say: code of
SteweB is better than mine. ;-)
- Rauno
SteweB is better than mine. ;-)
- Rauno
Which bit exactly doesn't work? I have no problems drawing on the canvas of a TLabel. The following code shows how I would display a border round a label when the mouse is over it. Perhaps it would suffice for your needs.
Steven.
unit NewLabel;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls;
type
TNewLabel = class(TLabel)
private
MouseIn : Boolean;
Procedure Paint; Override;
protected
procedure CMMouseEnter(var msg:TMessage); message CM_MouseEnter;
procedure CMMouseLeave(var msg:TMessage); message CM_MouseLeave;
end;
procedure Register;
implementation
procedure Register;
begin
RegisterComponents('Steven
end;
Procedure TNewLabel.Paint;
begin
inherited;
If MouseIn then begin
Canvas.Pen.Color := clBlack;
Canvas.Brush.Style := bsClear;
Canvas.Rectangle(0,0,width
end;
end;
procedure TNewLabel.CMMouseEnter(var
begin
if Enabled then begin
MouseIn := True;
Repaint;
end;
end;
procedure TNewLabel.CMMouseLeave(var
begin
if Enabled then begin
MouseIn := False;
Repaint;
end;
end;
end.