Solved

Drawing in a TCustomLabel

Posted on 1998-03-31
7
201 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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

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…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

757 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

21 Experts available now in Live!

Get 1:1 Help Now