?
Solved

Panel - Component rewrite

Posted on 1999-07-24
17
Medium Priority
?
220 Views
Last Modified: 2010-04-04
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
0
Comment
Question by:xpher
[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
  • 9
  • 8
17 Comments
 
LVL 10

Accepted Solution

by:
viktornet earned 80 total points
ID: 1390900
simply do..

FOutColor := Color;

that's all you need...

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

..-=ViKtOr=-..
0
 
LVL 1

Author Comment

by:xpher
ID: 1390901
Where would I put FOutColor := Color because once the Colour becomes InColor FOutColor becomes InColor?

Regards
Chris
0
 
LVL 1

Author Comment

by:xpher
ID: 1390902
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
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!

 
LVL 10

Expert Comment

by:viktornet
ID: 1390903
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
 
LVL 1

Author Comment

by:xpher
ID: 1390904
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
 
LVL 10

Expert Comment

by:viktornet
ID: 1390905
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
 
LVL 10

Expert Comment

by:viktornet
ID: 1390906
When writing a component there isn't any Sender, btw :)
0
 
LVL 1

Author Comment

by:xpher
ID: 1390907
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
 
LVL 10

Expert Comment

by:viktornet
ID: 1390908
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
 
LVL 1

Author Comment

by:xpher
ID: 1390909
Hi Viktor

Have mailed it as an attachment (3.84kb)

Regards
Chris
0
 
LVL 10

Expert Comment

by:viktornet
ID: 1390910
Hey thanks I will have a look at it right away :)
0
 
LVL 10

Expert Comment

by:viktornet
ID: 1390911
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
 
LVL 1

Author Comment

by:xpher
ID: 1390912
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
 
LVL 10

Expert Comment

by:viktornet
ID: 1390913
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
 
LVL 1

Author Comment

by:xpher
ID: 1390914
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
 
LVL 10

Expert Comment

by:viktornet
ID: 1390915
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
 
LVL 1

Author Comment

by:xpher
ID: 1390916
Gotcha!

Cheers
Chris
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

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.

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…
In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
Suggested Courses

719 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