Panel - Component rewrite

I'm trying to learn about writing components. Just for the hell of it I decided to have a go at a panel that OnMouseEnter changed colour, and OnMouseLeave reverted back to original colour. What I can't work out is how to make what I call the OutColor to be the Color from the Object Inspector. I don't want options of Color, InColor, OutColor I just want Color, OutColor (or just InColor, OutColor).
Here is what I got so far.
By the way sorry for points but they are my last.

unit ExtraPanel;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  ExtCtrls;

type
  TOnMouseEnter = Procedure(Sender: TObject) of Object;
  TOnMouseLeave = Procedure(Sender: TObject) of Object;
type
  TExtraPanel = class(TPanel)

  private
    { Private declarations }
    FHighlight: boolean;
    FOutColor: TColor;
    FInColor: TColor;
    FOnMouseEnter: TOnMouseEnter;
    FOnMouseLeave: TOnMouseLeave;
    procedure SetHighlight(Value: boolean);
    procedure SetOutColor(Value: TColor);
    procedure SetInColor(Value: TColor);
    procedure CMMouseEnter(var msg:TMessage); message CM_MOUSEENTER;
    procedure CMMouseLeave(var msg: TMessage); message CM_MOUSELEAVE;
    protected
    { Protected declarations }
     procedure Paint; override;
  public
    { Public declarations }
  published
    { Published declarations }
    property OutColor: TColor read FOutColor write SetOutColor;
    property InColor: TColor read FInColor write SetInColor;
    property Highlight: boolean read FHighlight write SetHighlight;
    property OnMouseEnter: TOnMouseEnter read FOnMouseEnter write FOnMouseEnter;
    property OnMouseLeave: TOnMouseLeave read FOnMouseLeave write FOnMouseLeave;
  end;

procedure Register;

implementation

procedure TExtraPanel.SetInColor(Value: TColor);
begin
  if FInColor <> Value then
  begin
  FInColor := Value;
  Invalidate;
  end;
end;

procedure TExtraPanel.SetOutColor(Value: TColor);
begin
  if FHighlight then FOutColor := Color;
  if FOutColor <> Value then
  begin
  FOutColor := Value;
  Invalidate;
  end;
end;

procedure TExtraPanel.SetHighlight(Value: boolean);
begin
  if FHighlight <> Value then
  begin
  FHighlight := Value;
  Invalidate;
  end;
end;

procedure TExtraPanel.CMMouseEnter(var msg: TMessage);
begin
  if FHighlight then
  begin Color := FInColor; end;
  If Assigned(FOnMouseEnter) then
      FOnMouseEnter(Self);
end;

procedure TExtraPanel.CMMouseLeave(var msg: TMessage);
begin
  if FHighlight then
  begin Color := FOutColor; end;
  if Assigned(FOnMouseLeave) then
      FOnMouseLeave(Self);
end;

procedure TExtraPanel.Paint;
begin
  inherited Paint;
end;

procedure Register;
begin
  RegisterComponents('MrPip', [TExtraPanel]);
end;

end.

Cheers
Chris
LVL 1
xpherAsked:
Who is Participating?
 
viktornetConnect With a Mentor Commented:
simply do..

FOutColor := Color;

that's all you need...

If you need more help please let me know...

..-=ViKtOr=-..
0
 
xpherAuthor Commented:
Where would I put FOutColor := Color because once the Colour becomes InColor FOutColor becomes InColor?

Regards
Chris
0
 
xpherAuthor Commented:
Ignore the last comment. Thanks for the kick to make me think. Now I've got to work out how to make it moveable OnMouseDown etc.

Cheers
Chris :)
0
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
viktornetCommented:
Hello sorry it took so long.... ok, you've foudn your mistake with the color,,,, you now want to make it move if you're clicked on it and move the mouse?? if so that is pretty easy...

override the OnMouseMove() event and do this in there....

if ssLeft in Shift then begin
  ReleaseCapture;
  Perform(WM_SYSCOMMAND, $F012, 0);
end;

Hope this helps...

If you need more help please let me know..

..-=ViKtOr=-..
0
 
xpherAuthor Commented:
Hi Viktor

Many thanks for the extra snippet got it working. Originally I was trying to use OnMouseDown with
ReleaseCapture;
TWinControl(Sender).perform(WM_SYSCOMMAND, $F012, 0);
but kept getting snarled up with sender and violations.

I'm well pleased, learnt a lot today, good fun this component lark.

Regards
Chris :))
0
 
viktornetCommented:
Great! :)

btw, I'm not sure about D3, but in D4 you can replace the $F012 with SC_MOVE since it's been defined in D4 for sure... ;)

..-=ViKtOr=-..
0
 
viktornetCommented:
When writing a component there isn't any Sender, btw :)
0
 
xpherAuthor Commented:
Hi Viktor
Don't know whether you are interested or not (you probably know it any way), but I have just been looking back in some old PC magazines and found an article on moving buttons at runtime. The difference being that this article shows how to do it by moving the whole button with caption and not just the outline which happens with the panel example you have helped me with. If you are interested I can mail it to you. (Don't know about copyright but who cares magazines are expensive enough anyway).

Regards
Chris
0
 
viktornetCommented:
yeah, great... please send it to me, even thought I know how to do it.. I'm just curious if that guy/gal ;) who wrote is uses the same code or something similar :)

If it's too large to be pasted in here, please send it to viktor@megsinet.net

Thanks!!

Regards,
Viktor
0
 
xpherAuthor Commented:
Hi Viktor

Have mailed it as an attachment (3.84kb)

Regards
Chris
0
 
viktornetCommented:
Hey thanks I will have a look at it right away :)
0
 
viktornetCommented:
Yep, I use the same code :)

You mentined that you're trying to implement it into your panel.. If you need help with that please let me know :)

..-=ViKtOr=-..
0
 
xpherAuthor Commented:
Yes it's a problem with OnClick event all I seem to get are access violations followed by keyboard hang-ups. Any help would be appreciated.

Cheers
Chris
0
 
viktornetCommented:
OK, please let me see what you've got so far (i mean the code) and I will try to implement it for you or correct som minor mistakes in your code... :)

C'Ya

..-=ViKtOr=-..
0
 
xpherAuthor Commented:
Hi Viktor

many thanks for the offer but I've battled on and realised my mistake. The only annoyance with it is not being able to use the OnClick event in other situations without causing problems but I changed this so the Panel is moved by holding down the right mouse button. I know this isn't intuative to an end user but it seemed the easiest way.

Cheers
Chris
0
 
viktornetCommented:
actually you can, but you'd have to do some coding ;)) use a timer, if the user has been holding the key for less or equal to a second then that is a click other wise it;s a dragging, kinda :)
0
 
xpherAuthor Commented:
Gotcha!

Cheers
Chris
0
All Courses

From novice to tech pro — start learning today.