Solved

Drawing in a TCustomLabel

Posted on 1998-03-31
7
206 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

749 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