Solved

Eyedropper color selector

Posted on 2002-04-17
21
322 Views
Last Modified: 2010-04-04
Hi all,

Can anybody explain the logistics of
implementing an eyedropper color selector
like the function in M$ Frontpage?

At the moment I have a ColorDialog to set
the color of  a control and the desired
color is selected manually... How, for
instance, can I detect the color beneath
the cursor at any point on the screen
(or background color of user desktop scheme
and click to set that color for the control
I want to change the color of.

Info please for me to work on :)

(If you think I;ve been a bit stingy on the
points award, I'm rounding off my points total,
The real points will come for code examples ;))

TIA.
tonga
0
Comment
Question by:tongalite
  • 7
  • 7
  • 6
  • +1
21 Comments
 
LVL 27

Expert Comment

by:kretzschmar
ID: 6947264
already coded,
but source not on hand yet,
i can post it in about ~7 hours,
if not solved until

meikl ;-)

0
 
LVL 3

Author Comment

by:tongalite
ID: 6947275
Meikl,
Was für eine überaschung! Sehr schnell Anwort!

Thank a lot... I'll look forward to that
and award you more points.

Cheers!
t.
0
 
LVL 27

Expert Comment

by:kretzschmar
ID: 6947303
well,
but even not with a solution.

its quiet simple,
i used a timer,
where in the timer event,
the color of the desktop-pixel
under the mouse is retrieved,
and then displayed
as delphi colorstring
in an editbox, so that it
could be simple copied
into a colorproperty or
into the sourcecode

additional i had there
appended the colorpicker
from mike-lischke,
where it is easy to
select another
nuance of the retrieved color

if you want, i can send you this little app,
which i've coded, to get some seen colors,
without to try and error with a colordialog
(my customer wanted special colors (corporate identity))

its a small simple app,
just leave your email here,
and i send it to you this evening

meikl ;-)
0
 
LVL 3

Author Comment

by:tongalite
ID: 6947320
OK Meikl...

Thanks a lot...

tongalite@supalife.com

t.
0
 
LVL 27

Expert Comment

by:kretzschmar
ID: 6948354
ooops,

don't find what i have provided,
was two years ago

the only thing i found was this prototype

unit color_pipette_u;

interface

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

type
  TForm1 = class(TForm)
    Timer1: TTimer;
    Edit1: TEdit;
    Panel1: TPanel;
    procedure Timer1Timer(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.DFM}

procedure TForm1.Timer1Timer(Sender: TObject);
var
  HDC : THandle;
  P : TPoint;
  C : TColor;
begin
  GetCursorPos(P);
  HDC := GetDC(0);
  C := GetPixel(HDC,P.x,P.y);
  ReleaseDC(HDC,0);
  edit1.Text := ColorToString(C);
  Panel1.Color := c;
end;

end.

i can send you the exe for this,
but its so simple, so i guess
you can implement it byself

hope this help

meikl ;-)
0
 
LVL 3

Author Comment

by:tongalite
ID: 6948536
Meikl,
That does a good job of selecting a color.
I modified it a little.
3 further questions.

How to....
(a)halt select color process onmousedown
to pass panel the selected color? i.e. click
to stop select/panel is then colored?

(b) Is it possible to have a cursor
active outside of form region which will
only select colors and not activate desktop
icons etc?

(c)To use a pipette/eyedropper cursor.. does this require a custom cursor loaded from resource?

Thanks
t.
0
 
LVL 6

Expert Comment

by:DrDelphi
ID: 6949023
Jumping in with my 2 cents worth... (actually 1 cent.. some of the code is meikl's)

How to....
(a)halt select color process onmousedown
to pass panel the selected color? i.e. click
to stop select/panel is then colored?

Check this out:
=================


unit Unit1;

interface

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

type
  TForm1 = class(TForm)
    Button1: TButton;
    Panel1: TPanel;
    procedure ColorthePanel;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.DFM}

procedure TForm1.ColorthePanel;
var
 HDC : THandle;
 P : TPoint;
 C : TColor;
begin
 GetCursorPos(P);
 HDC := GetDC(0);
 C := GetPixel(HDC,P.x,P.y);
 ReleaseDC(HDC,0);
 Panel1.Color := c;
end;



procedure TForm1.Button1Click(Sender: TObject);
var msg:Tagmsg;
begin
  SetCapture(self.handle);
   While true do
    if  PeekMessage(msg,0,WM_LBUTTONDOWN,WM_LBUTTONDOWN,PM_NOREMOVE) then
    begin
      ColorthePanel; ///look familiar? <G>
      TranslateMessage(msg);
      DispatchMessage(msg);
      ReleaseCapture;
      Break;
   end;
end;

end.
================================================
(b) Is it possible to have a cursor
active outside of form region which will
only select colors and not activate desktop
icons etc?


 Yes, but it wouldn't be the actual desktop. What you'd want to do it to create an invisible window that occupies the entire screen. Take a look at the CreateWindowEx API for guidance.

 



(c)To use a pipette/eyedropper cursor.. does this require a custom cursor loaded from resource?


Yes. (short and sweet)



Good luck and happy coding!!

0
 
LVL 9

Expert Comment

by:ginsonic
ID: 6949673
listening
0
 
LVL 27

Expert Comment

by:kretzschmar
ID: 6949721
nothing to append to drdelphi's comment ;-)
0
 
LVL 3

Author Comment

by:tongalite
ID: 6950309
Hi guys...
Thanks for the response.

Dr Delphi,
I tried your suggestion which is pretty much what Meikl gave me. Your other comments to my added questions are appreciated.
/ / / / / / / / / / / / /
Nick,
The demo you sent me used a custom trackbar
comp which I substituted with the delphi tbar
to get it running.Stripping away all but the
function I need, results in virtually the same
as Meikl's solution :)
/ / / / / / / / / / / / /

So, if I can get a bit more mileage out of this question....:-)

Doc,
Regarding your comments to my question (b) above...

Am I right in thinking this is a possibility:
OnBtnClick, take a screenshot of entire screen
and display it.
Move mouse pointer to any area of displayed
screenshot to select desired color.
Mouse L.Click to pass selected color to panel on
form and at same time release screenshot.
Click OK button to stop color selection function
and panel keeps assigned color

Would that work? (I hope you can make sense of that)
/ / / / / / / / / / / / / /

Later.
t.

0
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 
LVL 6

Expert Comment

by:DrDelphi
ID: 6950621
There's no reason to go through all that. The code I gave you under Form1.Button1Click is already doing what you're looking for.The buttonlick starts the "selection" and will only stop on a LeftClick, at which time it calls the code that Meikl gave you for getting the color that the mouse is over. Is there something here that I'm not getting?


0
 
LVL 6

Expert Comment

by:DrDelphi
ID: 6950633
It bears mentioning however that what you suggested is possible.
0
 
LVL 3

Author Comment

by:tongalite
ID: 6951120
DrDelphi

Just bear with me...
It's this bit I'm pondering over...

> and will only stop on a LeftClick, at
> which time it calls the code that Meikl
> gave you for getting the color that the
> mouse is over.

OK, supposing the mousepointer is over an
icon on the Taskbar and I want that color...
If I click to pass that color to the panel,
would that not activate the icon and fire up
its associated app! That's why I thought about
the screenshot... to simulate everything on
the desktop. If I'm talking BS here please put
me right... this is very new territory to me.

Here for clarity, is the exact (but simplified)
scenario:

//...........................
I have two forms, Mainform & Form2
Mainform has 2 buttons: Color & Show.

// Code for Color is a simple ColorDialog1 call

procedure TMainform.colorClick(Sender: TObject);
begin
colordialog1.Execute;
Form2.Color:=ColorDialog1.color;
end;
//.............................
procedure TMainform.ShowClick(Sender: TObject);
begin
Hide;
Form2.visible:=True;
end;
//............................

So, instead of using the standard colordialog
I want to implement the eyedropper option to get
the exact color of the desktop. (BTW when form2
is visible, I have a dblClick event set up to
restore Mainform and hide self/form2).

Simple as that...
I now have to work the code you supplied above
into my unit, if it will do what I want, as
you say it will.

Hope that makes things clearer.

Looks like I'll have to award points to you both....
Sorry Nick.... I'll get you next time :-)

t.
0
 
LVL 6

Expert Comment

by:DrDelphi
ID: 6951466

If I click to pass that color to the panel,
would that not activate the icon and fire up
its associated app!

-Nope.... The SetCapture call is passing the mouseclicks to your form. It is not released until AFTER the LBUTTON down, so at worst you'd get the effect of a LMouseUp, which to the best of my knowledge does nothing to to a shortcut.

Add this to my original example and you'll see what I mean.


procedure TForm1.FormMouseDown(Sender: TObject; Button: TMouseButton;
  Shift: TShiftState; X, Y: Integer);
begin
  showmessage('MouseDown')
end;



Good luck!!
 
0
 
LVL 3

Author Comment

by:tongalite
ID: 6951625
Hi again
dd.....
I just ran your color pickercode again.
I dropped a button and a panel on the form
and set up the button event, pasted your code
into my unit and hit the F9 key. Guess what? I
clicked the button, aimed the cursor at the IE
icon on my taskbar and gave a left mouse click...
The panel went a lovely shade of light blue.... and
IE6 fired up!!! I repeated the same on all the one
click icons on my Taskbar... each click passed the
color to the panel and fired up the associated app
like I was afraid it would..... Do we have something missing here? or am I implementing things wrong?.

BTW, if I double the points... how do I split them between you and M? I don't want to post a zero point 'question'
Thanks
T.
0
 
LVL 6

Accepted Solution

by:
DrDelphi earned 100 total points
ID: 6951973
Got it! Behold:

unit Eyedropper;
{this is a blank form with no controls at all on it}
interface

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

type
  TFrmEye = class(TForm)
    procedure FormCreate(Sender: TObject);
    procedure ColorthePanel;
    procedure FormMouseDown(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);
    procedure FormMouseUp(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);

  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  FrmEye: TFrmEye;

implementation
     uses main;
{$R *.DFM}

procedure TFrmEye.FormCreate(Sender: TObject);
begin
    self.Brush.Style:=BSClear;    //create a transparent form
    Self.BorderStyle:=BsNone;
    Self.WindowState:=WSMaximized;
end;

procedure TFrmEye.ColorthePanel;
var
  HDC : THandle;
  P : TPoint;
  C : TColor;
begin
  GetCursorPos(P);
  HDC := GetDC(0);
  C := GetPixel(HDC,P.x,P.y);
  ReleaseDC(HDC,0);
  form1.Panel1.Color := c;
 end;


procedure TFrmEye.FormMouseDown(Sender: TObject; Button: TMouseButton;
  Shift: TShiftState; X, Y: Integer);
begin
    ColorthePanel;  ///call the get color routine in the mouse down
end;

procedure TFrmEye.FormMouseUp(Sender: TObject; Button: TMouseButton;
  Shift: TShiftState; X, Y: Integer);
begin
  self.close;  //closes in the mouse up
end;

end.
==========================================================

Main.Pas:
{this is the form with the button and the panel}


procedure TForm1.Button1Click(Sender: TObject);
begin
   Application.CreateForm(TFrmeye,FrmEye);
   Frmeye.Show;
end;



Good luck!!

0
 
LVL 6

Expert Comment

by:DrDelphi
ID: 6951992
BTW,
 
Re:Points... I really don't know how you'd go about it, but I suspect that you could probably ask the moderators to split them up for you.


Happy coding!!

0
 
LVL 27

Expert Comment

by:kretzschmar
ID: 6951995
clever ;-)
0
 
LVL 27

Expert Comment

by:kretzschmar
ID: 6951999
by clever i meant your last code above,
and not your last comment, drdelphi
(saw it after post)
meikl ;-)
0
 
LVL 3

Author Comment

by:tongalite
ID: 6952164
Doc,
That is brilliant! :-)
I managed to get it to compile after a few botched runs and it's working great... Now I need to modify it to suit my purposes. It's like what I had in mind with the screen shot method but you used a transparent form...neat!
Thanks to all for the great help.

I'll try to split the points between you (drdelphi) and meikl.

Meikl... Do you know the point splitting procedure?
I'll top up the points with another 100
t.
0
 
LVL 6

Expert Comment

by:DrDelphi
ID: 6952267
Glad to hear it's working for you....


Good luck!!
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
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…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

744 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

12 Experts available now in Live!

Get 1:1 Help Now