Solved

Drawing in a TCustomLabel

Posted on 1998-03-31
7
203 Views
Last Modified: 2010-04-06
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.

0
Comment
Question by:Stuart_Johnson
  • 3
  • 3
7 Comments
 
LVL 4

Expert Comment

by:StevenB
ID: 1361149
 Stuart,

  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', [TNewLabel]);
end;

Procedure TNewLabel.Paint;
begin
  inherited;
  If MouseIn then begin
    Canvas.Pen.Color := clBlack;
    Canvas.Brush.Style := bsClear;
    Canvas.Rectangle(0,0,width,height);
  end;
end;

procedure TNewLabel.CMMouseEnter(var msg:TMessage);
begin
  if Enabled then begin
    MouseIn := True;
    Repaint;
  end;
end;

procedure TNewLabel.CMMouseLeave(var msg:TMessage);
begin
  if Enabled then begin
    MouseIn := False;
    Repaint;
  end;
end;

end.



0
 

Expert Comment

by:raunol
ID: 1361150
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:=clBlack;
     Canvas.FrameRect(clientRect);
     //run timer
     Timer.Enabled:=True;
End;

Procedure TYourComponent.OnTimer(sender:Tobject);
Begin
    Var Pt:Tpoint;
begin
     GetCursorPos(Pt);
     //check if the cursor is outside of the component
     If WindowFromPoint(pt)<>Handle Then
     Begin
          //Hide the frame without destroying other
          //information of it
          canvas.Brush.Color:=Color;
          Canvas.FrameRect(ClientRect);  
          Timer1.Enabled:=False;
     End;
End;

- Rauno



0
 
LVL 4

Expert Comment

by:StevenB
ID: 1361151
 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.
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Expert Comment

by:raunol
ID: 1361152
Well. It should have to create first Timer for You
component Timer:=Ttimer.Create(self)
And then have to make Tmylabel.Ontimer(sender:Tobject)
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:=clBlack;
Canvas.FrameRect(ClientRect);

Procedure Tmylabel.CMMouseLeave..
//hide frame
canvas.Brush.Color:=color; //color=background color
Canvas.FrameRect(ClientRect);
 
- Rauno
0
 
LVL 6

Author Comment

by:Stuart_Johnson
ID: 1361153
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
0
 
LVL 4

Accepted Solution

by:
StevenB earned 50 total points
ID: 1361154
 <<<See First Comment For Answer>>>

Raunol:

  I'd agree that in ithis example the use of a complete repaint to update the appearance of the components might be considered to be overkill, but there are reasons for it. If the component were to be obscured from view then the border would not reappear when it came back into view. By putting the commands in the Paint procedure, you can ensure that the border always displays correctly. Secondly, it is possible for a label to be transparent and for the background to be multicoloured (a bitmap for example) If you simply use Canvas.Brush.Color:=color;Canvas.FrameRect(ClientRect); to eliminate the border then the background will be corrupted.

  Steven.
       
0
 

Expert Comment

by:raunol
ID: 1361155
Sorry, my mistake. You are Stuart. I had to say: code of
SteweB is better than mine. ;-)

- Rauno
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…

895 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now