• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 382
  • Last Modified:

Mouse Pointer

I want to capture the mouse pointer and save it to a bitmap(or other image format). Please, help me if you can.
0
MikeAllen3k
Asked:
MikeAllen3k
  • 6
  • 4
  • 3
  • +2
1 Solution
 
msa2003Commented:
Please describe what you exactly need to do.
0
 
JamesLondonCommented:
Hi,

Here is the code to extract and Icon and save it to file...

procedure TForm1.Button1Click(Sender: TObject);
Var
 hC: HCursor;
 Info: _ICONINFO;
begin

  hC:= Screen.Cursors[0];

  With Info
  Do Begin
    fIcon := FALSE;
    xHotspot:=0;
    yHotspot:=0;
    //HBITMAP hbmMask; // This is a result
    //HBITMAP hbmColor; // This is a result
  End;

  //LoadCursor
  GetIconInfo(hC, Info);
  Image1.Picture.Bitmap.Handle := Info.hbmMask;
  Image1.Picture.Bitmap.SaveToFile('C:\MyCursor.bmp');
end;

Alternatively...

1) Create a blank project in Delphi and Compile it.
2) Run the Resource Workshop program (it comes on your Delphi CD, or you can probably download it).
3) Open your project and inside your will find All the Cursors that Delphi by default adds to your exe.

Best Regards,
James
0
 
MikeAllen3kAuthor Commented:
I need to capture the current cursor from the screen. Example: My application is running and I am drawing something in Paint(the cursor looks like a pen). I want my application to capture the cursor from Paint(the pen looking one). This code doesn't work for this operation.
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
JamesLondonCommented:
Just change the following: -

hC:= Screen.Cursors[0];

To: -

hC:= GetCursor();

That will obtain the Current Screen Cursor.

Best Regards,
James
0
 
MikeAllen3kAuthor Commented:
I already did that, but it doesn't get the cursor if the pointer is outside of my application's form.
0
 
JamesLondonCommented:
Be more specific. Do you really need to capture the cursor or do you just require the same Pen cursor that Paint has but in your app?
0
 
MikeAllen3kAuthor Commented:
Yes. I need to capture the mouse cursor(there can be custom made cursors).
0
 
Slick812Commented:
hello MikeAllen3k, you can use the API DrawIconEx( ) to draw an Cursor to a bitmap

begin
hCursor := LoadCursor(0,IDC_ARROW);
CopyBmp := TBitmap.Create;
CopyBmp.Width := 32;
CopyBmp.Height := 32;
DrawIconEx(CopyBmp.Canvas.Handle,0,0, hCursor, 0,0, 0, 0, DI_DEFAULTSIZE or DI_NORMAL);
Canvas.Draw(20,20,CopyBmp);
CopyBmp.SavetoFile('C:\SomeBmp.bmp');
CopyBmp.Free;
end;


you can get the Handle for your Pen Cursor and then draw it any where you need it
0
 
Slick812Commented:
sorry, I left something out. . . the function GetCursor, will get the handle of the current cursor


hCursor := GetCursor;
0
 
MadshiCommented:
GetCursor works only for our own process. But you can use GetCursorInfo instead, here's the documentation:

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/winui/winui/windowsuserinterface/resources/cursors/cursorreference/cursorfunctions/getcursorinfo.asp

Unfortunately this only works with 98+, not with 95. Hopefully this is no problem for you.

Regards, Madshi.
0
 
MikeAllen3kAuthor Commented:
GetCursorInfo works fine. I need to capture only the actual mouse pointer(example: just the arrow - the area surrounding it should be transparent). How can I do that?
0
 
MadshiCommented:
I'd suggest this:

(1) Create a 32x32 imagelist.
(2) Add the cursor handle you got from GetCursorInfo to the image list by using ImageList_AddIcon. This function also accepts cursors.

That was it. If you urgently need an icon handle, call ImageList_GetIcon.

Regards, Madshi.
0
 
MikeAllen3kAuthor Commented:
I managed to do it in another way(without using ImageList). Thank you very much for "GetCursorInfo". If it is possible, please show me exactly(source code) how you can do the capture with ImageList. Also, if you know how to capture the pointer in Win95 send you answer please. The points are yours, Madshi.
0
 
MadshiCommented:
function CursorToIcon(cursor: dword) : dword;
var il : dword;
begin
  il := ImageList_Create(32, 32, ILC_COLOR32 or ILC_MASK, 1, 1);
  ImageList_AddIcon(il, cursor);
  result := ImageList_GetIcon(il, 0, ILD_NORMAL);
  DestroyCursor(cursor);
end;

procedure SaveCursorToIcoFile(cursor: dword; icoFile: string);
begin
  cursor := CursorToIcon(cursor);
  with TIcon.Create do begin
    Handle := cursor;
    SaveToFile(icoFile);
  end;
  DestroyIcon(cursor);
end;

This is just written from my mind, not tested in any way.

I'm not sure, perhaps you can simply leave that CursorToIcon stuff away. Perhaps TIcon accepts cursor handles, too. I'm not sure about that.
0
 
MadshiCommented:
About 95: The easiest way would be to call GetForegroundWindow + GetWindowThreadProcessID + AttachThreadInput + GetCursor. However, don't do that in a loop all the time, that sometimes kills the double click (according to my experience).
0
 
MikeAllen3kAuthor Commented:
Thank you again.
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 6
  • 4
  • 3
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now