I'm using Delphi 2009, and I can't figure out how to preserve transparency when converting a TBitmap to a TIcon and assigned it as the cursor, its always square and has the white background. Can anyone tell me how I could draw to the bitmaps canvas, and convert the entire thing into a cursor for the mouse, with white being the transparent color?

Re: Bitmap to Cursor w/Transparency? 80 80

1.Make cursor in cursor editor (hand.cur).
2.Make text file "cursors.rc": HAND CURSOR HAND.CUR
3.Compile: "C:\Program Files\CodeGear\RAD Studio\6.0\bin\brcc32.exe" cursor.rc
4.In program:

  crHand  = 10;
{$R Cursor.res}
procedure TForm1.Create(Owner: TObject);
  Screen.Cursors[crHand]  := LoadCursor(hInstance,'HAND');
  Screen.Cursor := crHand;
Re: Bitmap to Cursor w/Transparency? 80 80

Bitmap to Icon:
We need to create two bitmap: bitmap-mask ("AND" bitmap) and bitmap-image (XOR bitmap). Then transmit descriptors "AND" and "XOR" bitmap-s API functions CreateIconIndirect ():

procedure TForm1.Button1Click(Sender: TObject);
  IconSizeX: integer;
  IconSizeY: integer;
  AndMask: TBitmap;
  XOrMask: TBitmap;
  IconInfo: TIconInfo;
  Icon: TIcon;
  {Get the icon size}
  IconSizeX := GetSystemMetrics(SM_CXICON);
  IconSizeY := GetSystemMetrics(SM_CYICON);
  {Create the "And" mask}
  AndMask := TBitmap.Create;
  AndMask.Monochrome := true;
  AndMask.Width := IconSizeX;
  AndMask.Height := IconSizeY;
  {Draw on the "And" mask}
  AndMask.Canvas.Brush.Color := clWhite;
  AndMask.Canvas.FillRect(Rect(0, 0, IconSizeX, IconSizeY));
  AndMask.Canvas.Brush.Color := clBlack;
  AndMask.Canvas.Ellipse(4, 4, IconSizeX - 4, IconSizeY - 4);
  {Draw as a test}
  Form1.Canvas.Draw(IconSizeX * 2, IconSizeY, AndMask);
  {Create the "XOr" mask}
  XOrMask := TBitmap.Create;
  XOrMask.Width := IconSizeX;
  XOrMask.Height := IconSizeY;
  {Draw on the "XOr" mask}
  XOrMask.Canvas.Brush.Color := ClBlack;
  XOrMask.Canvas.FillRect(Rect(0, 0, IconSizeX, IconSizeY));
  XOrMask.Canvas.Pen.Color := clRed;
  XOrMask.Canvas.Brush.Color := clRed;
  XOrMask.Canvas.Ellipse(4, 4, IconSizeX - 4, IconSizeY - 4);
  {Draw as a test}
  Form1.Canvas.Draw(IconSizeX * 4, IconSizeY, XOrMask);
  {Create a icon}
  Icon := TIcon.Create;
  IconInfo.fIcon := true;
  IconInfo.xHotspot := 0;
  IconInfo.yHotspot := 0;
  IconInfo.hbmMask := AndMask.Handle;
  IconInfo.hbmColor := XOrMask.Handle;
  Icon.Handle := CreateIconIndirect(IconInfo);
  {Destroy the temporary bitmaps}
  {Draw as a test}
  Form1.Canvas.Draw(IconSizeX * 6, IconSizeY, Icon);
  {Assign the application icon}
  Application.Icon := Icon;
  {Force a repaint}
  InvalidateRect(Application.Handle, nil, true);
  {Free the icon}
Re: Bitmap to Cursor w/Transparency? 80 80

Thanks, I have it down now :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.18 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.