Link to home
Start Free TrialLog in
Avatar of TCS_AL
TCS_AL

asked on

Why is my rectangle not showing?

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

Avatar of Member_2_248744
Member_2_248744
Flag of United States of America image

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
Avatar of geobul
geobul

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
One more thing:
If you want to change the color of the rectangle use Canvas.FrameRect(rect); instead of Canvas.DrawFocusRect(rect);

Regards, Geo
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;
Avatar of TCS_AL

ASKER

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


ASKER CERTIFIED SOLUTION
Avatar of geobul
geobul

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of TCS_AL

ASKER

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
Avatar of TCS_AL

ASKER

oops, make that the properties onMouseEnter and OnMouseExit.

Al.


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
Avatar of TCS_AL

ASKER

Geo,

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

So just imagine I gave you an A grade.

Al