?
Solved

Why is my rectangle not showing?

Posted on 2003-02-23
10
Medium Priority
?
357 Views
Last Modified: 2011-09-20
HI all,

Im writing an app which when a user mouses over a control (TLabel based) it draws a framerect around the control.  
  My simple code is:

procedure TDispText.paint;
var Rect : TRect;
begin

 if FIsMouseIn then  // triggered from mouse over
    begin
     Rect := self.BoundsRect;
     Canvas.Brush.Color := clWhite;
      Canvas.DrawFocusRect(rect);
    end;
   inherited paint;
end;

My rectangle isnt been drawn though.
Do I have to draw it in the form's paint event (there will be more than one TDispText tho) or have I done something wrong??

Any help appreciated

Al

0
Comment
Question by:TCS_AL
[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
10 Comments
 
LVL 34

Expert Comment

by:Slick812
ID: 8005115
hello TCS AL, the paint event is only called when the component needs to be painted (usually a WM_PAINT message from the system). So you willl need to call the paint method yourself in code. In the the event that sets the FIsMouseIn to true, you might try and add paint

begin
FIsMouseIn := True
paint;
end;


you may also need to call it if you set it to false in the mouse out event
0
 
LVL 17

Expert Comment

by:geobul
ID: 8006909
Hi,

In addition to Slick's comment try calling inherited first not last in your overrided Paint method:

procedure TDispText.paint;
var Rect : TRect;
begin
  inherited paint; // call inherited here not in the if..then
  if FIsMouseIn then  // triggered from mouse over
  begin
    Rect := self.BoundsRect;
    Canvas.Brush.Color := clWhite;
    Canvas.DrawFocusRect(rect);
  end;
end;

Regards, Geo
0
 
LVL 17

Expert Comment

by:geobul
ID: 8006929
One more thing:
If you want to change the color of the rectangle use Canvas.FrameRect(rect); instead of Canvas.DrawFocusRect(rect);

Regards, Geo
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

Expert Comment

by:pderuiter
ID: 8007581
try this procedure, call it in your onmousemove:

procedure drawHandle(Control: TControl; Visible: boolean);
var
  DrawRect: TRect;
  Width: integer;
  Height: integer;
  DC: THandle;
  container: TWinControl;

const
  handleSize: integer = 4;

  procedure DrawHandle (X, Y : Integer);
  var
    Rect : tRect;
  begin
    PatBlt (DC, DrawRect.Left + X, DrawRect.Top + Y,
            HandleSize, HandleSize, PATCOPY)
  end;
begin
  if not Control.Visible then exit;
  container := TWinControl(Control.Parent.Controls[0]);

  DrawRect    := Control.ClientRect;

  OffSetRect(DrawRect,Control.Left - 2,Control.Top - 2);

  Container.Update;

  Width  := DrawRect.Right - DrawRect.Left + 4;
  Height := DrawRect.Bottom - DrawRect.Top + 4;


  DC := GetDC(Container.Handle);
  SelectObject (DC, GetStockObject(BLACK_BRUSH));

  DrawHandle(0, 0);
  DrawHandle((Width - HandleSize) div 2, 0);
  DrawHandle( Width - HandleSize, 0);

  DrawHandle (0, (Height - HandleSize) div 2);
  DrawHandle (Width - HandleSize, (Height - HandleSize) div 2);

  DrawHandle (0, Height - HandleSize);
  DrawHandle ((Width - HandleSize) div 2, Height - HandleSize);
  DrawHandle (Width - HandleSize, Height - HandleSize);

  ReleaseDC (Container.Handle, DC);

  Container.Update;
end;
0
 

Author Comment

by:TCS_AL
ID: 8010951
pderuiter,

Unfortunately your code dosent work either.  No handle is drawn.
stepping through it, I noticed that this line
   DC := GetDC(Container.Handle);
returns a 0 value.
I did notice that you are casting the control to a TWinControl, whereas a label is decended from TGraphicControl.
Im not sure if this is important as Im still a relative beginner.

Also I forgot to mention, the TDispText is situated on top of a TShape object.  Could the shapes paint method be drawing over the rectangle??

Oh and putting the inherited paint before or after the if statement makes no difference either.

Regards

Al


0
 
LVL 17

Accepted Solution

by:
geobul earned 100 total points
ID: 8014379
Hi,

>Oh and putting the inherited paint before or after the if statement makes no difference either.

I'm pretty sure that it changes everything.
 
I suppose that your component's canvas is larger than the visual part of the component. Using a rectangle around the text (not around the control) for drawing resolves the problem. Here is an example which works fine for me.

  TDispText = class(TLabel)
  private
    FIsMouseIn: boolean;
    FOnMouseEnter, FOnMouseLeave: TNotifyEvent;
  protected
    procedure WMMouseEnter(var Msg: TWMMouse); message CM_MOUSEENTER;
    procedure WMMouseLeave(var Msg: TWMMouse); message CM_MOUSELEAVE;
  public
    procedure Paint; override;
  published
    property OnMouseEnter: TNotifyEvent read FOnMouseEnter write FOnMouseEnter;
    property OnMouseLeave: TNotifyEvent read FOnMouseLeave write FOnMouseLeave;
  end;

procedure TDispText.WMMouseEnter(var Msg: TWMMouse);
begin
  inherited;
  FIsMouseIn := true;
  Paint;
  if Assigned(FOnMouseEnter) then FOnMouseEnter(Self);
end;

procedure TDispText.WMMouseLeave(var Msg: TWMMouse);
begin
  inherited;
  FIsMouseIn := false;
  Paint;
  if Assigned(FOnMouseLeave) then FOnMouseLeave(Self);
end;

procedure TDispText.paint;
begin
 inherited paint;
 if FIsMouseIn then  // triggered from mouse over
 begin
   Canvas.Brush.Color := clWhite;
   Canvas.FrameRect(ClientRect);
 end;
end;

Regards, Geo
0
 

Author Comment

by:TCS_AL
ID: 8021016
Geo,

Thanks for the code, it does work, so points to u.
Interesting, u using the  mouse messages for your events.  I just overrode the labels MouseMove method.  Is that normal, or is your way the norm.

Thanks anyway.

Al
0
 

Author Comment

by:TCS_AL
ID: 8021066
oops, make that the properties onMouseEnter and OnMouseExit.

Al.


0
 
LVL 17

Expert Comment

by:geobul
ID: 8023145
Why 'C' grade? It means that my answer was the worst ever. Once you say that your questions is for 50 points your account will be reduced with exactly 50 points regardless of the grade you will give. But my expert points will be increased with more if you give a better grade. If you don't give 'A' it's usual to say why you are not satisfied by the answer.

>I just overrode the labels MouseMove method.

And your label repaints itself many times in a second on every mouse move? Never do that.

Regards, Geo
0
 

Author Comment

by:TCS_AL
ID: 8054444
Geo,

Sorry bout that.  Its my first time ive ever used this facility.  

So just imagine I gave you an A grade.

Al
0

Featured Post

Enroll in August's Course of the Month

August's CompTIA IT Fundamentals course includes 19 hours of basic computer principle modules and prepares you for the certification exam. It's free for Premium Members, Team Accounts, and Qualified Experts!

Question has a verified solution.

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

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
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…
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
Suggested Courses
Course of the Month8 days, 8 hours left to enroll

764 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