1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391
| unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, ExtCtrls, ComCtrls, XPMan, Buttons;
type
TForm1 = class(TForm)
RedBar: TTrackBar;
GreenBar: TTrackBar;
BlueBar: TTrackBar;
Shape1: TShape;
Label1: TLabel;
ExportColor: TSpeedButton;
PaintBox1: TPaintBox;
ScrollBar1: TScrollBar;
ColorMode: TSpeedButton;
procedure ColorBarChange(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure FormDblClick(Sender: TObject);
procedure FormMouseMove(Sender: TObject; Shift: TShiftState; X,
Y: Integer);
procedure FormMouseDown(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
procedure ExportColorClick(Sender: TObject);
procedure FormHide(Sender: TObject);
procedure PaintBox1Paint(Sender: TObject);
procedure PaintBox1MouseDown(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
procedure PaintBox1MouseMove(Sender: TObject; Shift: TShiftState; X,
Y: Integer);
procedure PaintBox1MouseUp(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
procedure ScrollBar1Change(Sender: TObject);
procedure ColorModeClick(Sender: TObject);
private
FDisableChange:boolean;
FHtmlColor: TColor;
FHDC: HDC;
FMoveFormPos :TPoint;
FPaletteHandle:HPalette;
procedure PutColor(Value :TColor);
procedure SetHtmlColor(const Value: TColor);
procedure RandomPal;
procedure DeLPal;
procedure ColorFromPixel;
protected
procedure CreateParams(var params : TCreateparams); override;
public
function ColorStr():string;
property HtmlColor :TColor read FHtmlColor write SetHtmlColor;
// property ColorStr :string read FHtmlColor write GetetHtmlColor;
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
uses ClipBrd;
type
ColorRec = record
case Integer of
0: (Color: TColor);
1: (R,G,B,A: byte);
end;
{DelphiDabbler.com}
procedure PostKeyEx32(const Key: Word; const Shift: Classes.TShiftState;
const SpecialKey: Boolean = False);
type
TShiftKeyInfo = record // information about a shoft key
Shift: Byte; // shift key
VKey: Byte; // virtual key code
end;
ByteSet = set of 0..7; // byte sized bitset
const
// map of shift state values to virtual keys
ShiftKeys: array[ 1..3 ] of TShiftKeyInfo = (
(Shift: Ord(Classes.ssCtrl); VKey: Windows.VK_CONTROL),
(Shift: Ord(Classes.ssShift); VKey: Windows.VK_SHIFT),
(Shift: Ord(Classes.ssAlt); VKey: Windows.VK_MENU)
);
var
Flag: DWORD; // flag passed to keybd_event
ShiftSet: ByteSet absolute Shift; // shift state as bit set
I: Integer; // loop variable
begin
// generate any shift key down events
for I := 1 to 3 do
if ShiftKeys[I].Shift in ShiftSet then
Windows.keybd_event(
ShiftKeys[I].VKey,
Windows.MapVirtualKey(ShiftKeys[I].VKey, 0),
0,
0
);
// generate key down then key up event for key
if SpecialKey then
Flag := Windows.KEYEVENTF_EXTENDEDKEY
else
Flag := 0;
Windows.keybd_event(Key, Windows.MapVirtualKey(Key, 0), Flag, 0);
Flag := Flag or Windows.KEYEVENTF_KEYUP;
Windows.keybd_event(Key, Windows.MapVirtualKey(Key, 0), Flag, 0);
// generate any shift key up events in reverse order of key down events
for I := 3 downto 1 do
if ShiftKeys[I].Shift in ShiftSet then
Windows.keybd_event(
ShiftKeys[I].VKey,
Windows.MapVirtualKey(ShiftKeys[I].VKey, 0),
Windows.KEYEVENTF_KEYUP,
0
);
end;
procedure TForm1.CreateParams(var Params: TCreateparams);
begin
inherited;
Params.ExStyle := Params.ExStyle or WS_EX_NOACTIVATE;
end;
function TForm1.ColorStr():string;
begin
with ColorRec(FHtmlColor) do
Result:=Format('#%.2x%.2x%.2x',[R,G,B]);
end;
procedure TForm1.PutColor(Value :TColor);
begin
if Value = FHtmlColor then Exit;
FHtmlColor :=Value;
Label1.Caption :=ColorStr();
Shape1.Brush.Color:= FHtmlColor;
Application.ProcessMessages
end;
procedure TForm1.SetHtmlColor(const Value: TColor);
begin
with ColorRec(Value) do
begin
FHtmlColor := Color;
RedBar.Position := R;
GreenBar.Position:= G;
BlueBar.Position := B;
end;
end;
procedure TForm1.ColorBarChange(Sender: TObject);
begin
if FDisableChange then Exit;
PutColor(RGB(RedBar.Position ,
GreenBar.Position,
BlueBar.Position));
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
DoubleBuffered:=True;
FormStyle :=fsStayOnTop;
ColorModeClick(nil);
Scrollbar1.Position:=128;
end;
procedure TForm1.FormDblClick(Sender: TObject);
begin
Close
end;
procedure TForm1.FormMouseMove(Sender: TObject; Shift: TShiftState; X,
Y: Integer);
begin
// ReleaseCapture();
// Perform(WM_SYSCOMMAND, $F012, 0);
if not( ssLeft in Shift) then
Exit;
Left := Left+ X - FMoveFormPos.X;
Top := Top + Y - FMoveFormPos.Y;
end;
procedure TForm1.FormMouseDown(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
begin
FMoveFormPos.X := X;
FMoveFormPos.Y := Y;
end;
procedure TForm1.ExportColorClick(Sender: TObject);
begin
Clipboard.AsText := ColorStr();
PostKeyEx32(Word('V'),[ssCtrl],True); //Ctrl+V
end;
procedure TForm1.FormHide(Sender: TObject);
begin
DeLPal() ;
if FHDC = 0 then Exit;
ReleaseDc(0,FHDC);
FHDC := 0;
FDisableChange := False;
end;
procedure ColorSet(var Start:integer;Canvas:TCanvas;Idx,C1,C2,C3:byte;inv:boolean);
var
I,RecDim,T,itt,B:integer;
R:TRect;
begin
with Longrec(RecDim)do
begin
Bytes[0]:=C1;
Bytes[1]:=C2;
Bytes[2]:=C3;
Bytes[3]:= 0;
end;
Itt :=10;
T:= 256 div Itt;
with Canvas do
begin
if inv then
for I := T-1 downto 0 do
begin
R:= Bounds(Start,5,1,50);
Brush.Color := RecDim or((I*itt) shl Idx);
FillRect(R);
inc(Start);
end else
for I := 0 to T-1 do
begin
R:= Bounds(Start,5,1,50);
Brush.Color := RecDim or((I*itt) shl Idx);
FillRect(R);
inc(Start);
end;
end;
end;
procedure TForm1.ColorFromPixel();
var
Pt:TPoint;
begin
GetCursorPos(Pt);
PutColor(GetPixel(FHDC,Pt.X,Pt.Y));
HtmlColor:=FHtmlColor;
end;
procedure TForm1.PaintBox1MouseDown(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
begin
if not(ssLeft in Shift)then Exit;
if FHDC = 0 then
FHDC :=GetDC(0);
FDisableChange := True;
SetCursor(windows.LoadCursor(0,IDC_HAND));
ColorFromPixel();
end;
procedure TForm1.PaintBox1MouseMove(Sender: TObject; Shift: TShiftState; X,
Y: Integer);
begin
if (ssLeft in Shift)then
ColorFromPixel();
end;
procedure TForm1.PaintBox1MouseUp(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
begin
FormHide(nil);
end;
procedure TForm1.DeLPal;
begin
if FPaletteHandle <>0 then
begin
DeleteObject(FPaletteHandle);
FPaletteHandle:=0;
end;
end;
function ShowPalette(Dc:Hdc;HPal:HPalette;var Dest:TRect):integer;
var
OldBrush : hBrush;
Brush : hBrush;
PalOld : HPalette;
I,Row,Col : integer;
RecDim : integer;
XLen,YLen : integer;
begin
Result := 0;
if HPal = 0 then
Exit;
Result := GetPaletteEntries(HPal, 0, 0, nil^);
if Result = 0 then
Exit;
RecDim := (Dest.Right - Dest.Left) div 16;
PalOld :=SelectPalette(Dc, HPal, false);
RealizePalette(Dc);
for I := 0 to Result-1 do
begin
Row := I div 16 ;
Col := I mod 16 ;
Brush := CreateSolidBrush($01000000 or I);
OldBrush := SelectObject(Dc, Brush);
Rectangle(Dc,
Dest.Left+ RecDim*Col-1, Dest.Top+ RecDim*Row-1,
Dest.Left+RecDim*(Col+1), Dest.Top+ RecDim*(Row+1));
SelectObject(Dc, OldBrush);
DeleteObject(Brush);
end;
SelectPalette(Dc, PalOld, false);
end;
procedure TForm1.RandomPal();
var
i : integer;
Pal: TMaxLogPalette;
Pos:integer;
Size:integer;
begin
Pos :=Scrollbar1.position;
Pal.palVersion := $0300;
Pal.palNumEntries := 256;
for i := 0 to 255 do
with Pal.palPalEntry[i] do
begin
peRed := 255-Random(pos);
peGreen := 255-Random(pos);
peBlue := 255-Random(pos);
peFlags := PC_RESERVED;
end;
DeLPal();
FPaletteHandle := CreatePalette(PLogPalette(@Pal)^);
end;
procedure TForm1.PaintBox1Paint(Sender: TObject);
var
R:Trect;
I:integer;
begin
if ColorMode.Tag = 1 then
begin
I:=0;
ColorSet(I,PaintBox1.Canvas,8,255,0,0,false);
ColorSet(I,PaintBox1.Canvas,0,0,255,0,true);
ColorSet(I,PaintBox1.Canvas,16,0,255,0,false);
ColorSet(I,PaintBox1.Canvas,8,0,0,255,true);
ColorSet(I,PaintBox1.Canvas,0,0,0,255,false);
ColorSet(I,PaintBox1.Canvas,16,255,0,0,true);
end else begin
R:=PaintBox1.ClientRect;
ShowPalette(PaintBox1.Canvas.Handle ,FPaletteHandle,R);
end;
end;
procedure TForm1.ScrollBar1Change(Sender: TObject);
begin
RandSeed:= ScrollBar1.Position;
RandomPal;
PaintBox1.Repaint
end;
procedure TForm1.ColorModeClick(Sender: TObject);
begin
if ColorMode.Tag = 0 then
begin
DeLPal();
PaintBox1.Height :=22;
ColorMode.Tag:=1;
Height:=PaintBox1.Top+22+10;
end else begin
PaintBox1.Height :=150;
ColorMode.Tag:=0;
Height:=(PaintBox1.Top+150+10)+(ScrollBar1.Height+10);
RandomPal
end;
end;
end. |
Partager