I've created a playing card descendent of TCustomControl and I want it to have rounded corners. The card is displayed in a Paint procedure where I determine the bitmap and then use a Canvas.Draw(0, 0, FCardBMP); This approach works well except - no rounded corners. Setting FCardBMP.Transparent := true; doesn't work when the card is dragged onto the table which has a different color background. It seems that "Transparent" just sets the corner, transparent pixels to the color of the initial background, but doesn't change pixels colors when the card is dragged.

With no success I've tried to use

Cnt := GenRegion(Par, FCardBMP,
(FCardBMP.Width - FCardBMP.Width+1) div 2,
(FCardBMP.Height - FCardBMP.Height+1) div 2);
hRegion := CreatePolygonRgn(Par, Cnt, ALTERNATE);
SetWindowRgn(FCardBMP.handle, hRegion, true);

I believe the secret is to useSetWindowRgn but I'm stymied. Any help would be appreciated. Thanks.

Try the function

RoundRect(DC: HDC; X1, Y1, X2, Y2, X3, Y3: Integer) : Bool;

I've never got around to using it, but it says in the book "Draws a rectangle with rounded corners using the selected pen and filled using the selected brush."

Regions are for windows. Not for painting.

You should be able to set the transparent and transparentColor properties of the bitmap before drawing it in your paint procedure.

Hope this helps.

As the card is dragged from the gray side to the green "table" the background will change. Depending on where the card is, some of the time 1 or 2 corners will have a green background and 3 or 2 will have gray corners. Sometimes even half and half as the corner is about 3 x 2 pixels.

When I set the transparent color (to the unique color of the corners) it seems that the color in each corner is static....at the gray color of the initial backgroubnd of the card. Dragging the card to the table doesn't change the corner colors to green. I thought that's what was suppose to happen.

This seems to work fairly well, although not perfectly, to set the corners transparent for each bitmap card. Notice that it seems to work better to have width and height PLUS ONE. This coding is executed whenever the dimension of a card changes...generallhy only once per card...when it is instantiated.

GetWindowRgn(Handle, FRoundedReg);
DeleteObject(FRoundedReg);
D := width div 15; // "15" determined by trial and error
FRoundedReg := CreateRoundRectRgn(0, 0, Width+1, Height+1, D, D);
SetWindowRgn(Handle, FRoundedReg, TRUE);

So your card is actually a separate window?

If it is just the problem of dragging the card across different components on a single form, you can use a TPaintBox (found in the System tab of the components palette) to draw on the form directly. Then you could avoid using a separate window and window regions...

This article has been dead for over six months. Start a new discussion instead.