Board index » delphi » CopyRect: Am I missing something here?

CopyRect: Am I missing something here?

I have been tearing my hair out over why I cannot assign a glyph to a
speedbutton using CopyRect. I have tried possibly every variation.
Someone help me out please

Situation: Delphi 1. TStyleSpeed is a decendant of TSpeedButton. This
part works:

procedure AddToolbarButton(command: TToolCommand; const HintStr: string;
  Group: Integer; ClickHandler: TNotifyEvent);
var
  button: TStyleSpeed;
begin
  button := TStyleSpeed.Create(self);
  with button do
  begin
    ..
    Glyph.Assign(RequiredBitmap(command));
    ..
  end;
  ..
end;

The culprit is in RequiredBitmap() below. This is the working version;
It uses LoadBitmap to load seperate resources for each button, and
returns the bitmap, which is copied to the glyph above.

function RequiredBitmap(command: TToolCommand): TBitmap;
begin
  ToolbarGlyphs[command] := TBitmap.Create;
  ToolbarGlyphs[command].Handle := LoadBitmap(HInstance,
    ToolbarResNames[command]);
  Result := ToolbarGlyphs[command];
end;

In the new version, I want to retrieve the bitmap using CopyRect from a
common bitmap holding all the Glyphs. However, I keep ending up with
empty buttons...

ToolRes = loaded bitmap holding glyphs
ToolRect = bounds(offset * 16, 0, 16, 16)

function RequiredBitmap(command: TToolCommand): TBitmap;
begin
  ToolbarGlyphs[command] := TBitmap.Create;
  ToolbarGlyphs[command].Canvas.CopyRect(Bounds(0, 0, 16, 16),
    ToolRes.Canvas, ToolRect);}
  Result := ToolbarGlyphs[command];
end;

And it doesn't work... Why not? What am I missing? Even providing direct
coordinates doesn't work. The only way to show *something* (the middle
of the common bitmap that is) is to assign ToolRes directly to
ToolbarGlyphs[] (duh). But anything using CopyRect (or even BitBlt)
results in an empty glyph!

Help...

-- Erwin Dokter
   mailto:edok...@home.nl
   http://members.home.nl/edokter

 

Re:CopyRect: Am I missing something here?


Okay, Erwin, I'm sort-of looking at this one from a distance, but I've
pulled my hair on enough problems like this one to suggest ...

"Okay, what if all the code you're writing here IS WORKING, only it's
either not being called as you expect, or the VCL is over-writing the
effect of what you're doing, or ... "what ELSE could be causing this?"

Or if that seems fruitless, what about this?  What if you have a bitmap
containing all the glyphs and you re-code the RequiredBitmap routine to
copy the appropriate slice out of the master bitmap into the [separate]
bitmap that it returns?  Sure, "it's not optimal," but, "it works and
you don't spend quite so much money this month on Rogaine."  ;-)

Quote
>Erwin Dokter wrote:

> I have been tearing my hair out over why I cannot assign a glyph to a
> speedbutton using CopyRect. I have tried possibly every variation.
> Someone help me out please

> Situation: Delphi 1. TStyleSpeed is a decendant of TSpeedButton. This
> part works:

> procedure AddToolbarButton(command: TToolCommand; const HintStr: string;
>   Group: Integer; ClickHandler: TNotifyEvent);
> var
>   button: TStyleSpeed;
> begin
>   button := TStyleSpeed.Create(self);
>   with button do
>   begin
>     ..
>     Glyph.Assign(RequiredBitmap(command));
>     ..
>   end;
>   ..
> end;

> The culprit is in RequiredBitmap() below. This is the working version;
> It uses LoadBitmap to load seperate resources for each button, and
> returns the bitmap, which is copied to the glyph above.

> function RequiredBitmap(command: TToolCommand): TBitmap;
> begin
>   ToolbarGlyphs[command] := TBitmap.Create;
>   ToolbarGlyphs[command].Handle := LoadBitmap(HInstance,
>     ToolbarResNames[command]);
>   Result := ToolbarGlyphs[command];
> end;

> In the new version, I want to retrieve the bitmap using CopyRect from a
> common bitmap holding all the Glyphs. However, I keep ending up with
> empty buttons...

> ToolRes = loaded bitmap holding glyphs
> ToolRect = bounds(offset * 16, 0, 16, 16)

> function RequiredBitmap(command: TToolCommand): TBitmap;
> begin
>   ToolbarGlyphs[command] := TBitmap.Create;
>   ToolbarGlyphs[command].Canvas.CopyRect(Bounds(0, 0, 16, 16),
>     ToolRes.Canvas, ToolRect);}
>   Result := ToolbarGlyphs[command];
> end;

> And it doesn't work... Why not? What am I missing? Even providing direct
> coordinates doesn't work. The only way to show *something* (the middle
> of the common bitmap that is) is to assign ToolRes directly to
> ToolbarGlyphs[] (duh). But anything using CopyRect (or even BitBlt)
> results in an empty glyph!

> Help...

> -- Erwin Dokter
>    mailto:edok...@home.nl
>    http://members.home.nl/edokter

--
------------------------------------------------------------------
Sundial Services :: Scottsdale, AZ (USA) :: (480) 946-8259
mailto:i...@sundialservices.com  (PGP public key available.)

- Show quoted text -

Quote
> Fast(!), automatic table-repair with two clicks of the mouse!
> ChimneySweep(R):  "Click click, it's fixed!" {tm}
> http://www.sundialservices.com/products/chimneysweep

Re:CopyRect: Am I missing something here?


Quote
In article <397A31F3.2...@home.nl>, Erwin Dokter <edok...@home.nl> writes:
>function RequiredBitmap(command: TToolCommand): TBitmap;
>begin
>  ToolbarGlyphs[command] := TBitmap.Create;

\\ Have you tried
ToolbarGlyphs[command].Height := 16;
ToolbarGlyphs[command].Width := 16;

Quote
>  ToolbarGlyphs[command].Canvas.CopyRect(Bounds(0, 0, 16, 16),
>    ToolRes.Canvas, ToolRect);}
>  Result := ToolbarGlyphs[command];
>end;

Drawing takes place on a Canvas only within the ClipRect (a read-only
property), setting the Width and Height sizes the ClipRect.

Alan Lloyd
alangll...@aol.com

Re:CopyRect: Am I missing something here?


Quote
AlanGLLoyd wrote:

> In article <397A31F3.2...@home.nl>, Erwin Dokter <edok...@home.nl> writes:

> \\ Have you tried
> ToolbarGlyphs[command].Height := 16;
> ToolbarGlyphs[command].Width := 16;

That did it :) In fact, I discovered it right after I posted my
question. For some reason it never showed on my own newsserver... so i
couldn't cancel or reply.

I even got rid of the intermediate bitmap and CopyRect-ed right to the
button's glyph... after setting the glyph's width and height of course
:)

-- Erwin Dokter

Re:CopyRect: Am I missing something here?


Im Artikel <397A31F3.2...@home.nl>, Erwin Dokter <edok...@home.nl> schreibt:

Quote
>  ToolbarGlyphs[command].Canvas.CopyRect(Bounds(0, 0, 16, 16),
>    ToolRes.Canvas, ToolRect);}

IMO the Canvas is not a persistent storage, but only usable to paint something,
what's stored elsewhere. You should store the glyph in the TSpeedButton.Glyph
property instead, which is a (persistent) TBitmap. But I couldn't find out how
to fill that bitmap, other than from a TImageList.GetBitmap...

DoDi

Other Threads