?
Solved

Mouse Pointer

Posted on 2003-03-09
16
Medium Priority
?
379 Views
Last Modified: 2010-05-18
I want to capture the mouse pointer and save it to a bitmap(or other image format). Please, help me if you can.
0
Comment
Question by:MikeAllen3k
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 4
  • 3
  • +2
16 Comments
 
LVL 5

Expert Comment

by:msa2003
ID: 8097978
Please describe what you exactly need to do.
0
 

Expert Comment

by:JamesLondon
ID: 8098018
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
 

Author Comment

by:MikeAllen3k
ID: 8098183
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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Expert Comment

by:JamesLondon
ID: 8098286
Just change the following: -

hC:= Screen.Cursors[0];

To: -

hC:= GetCursor();

That will obtain the Current Screen Cursor.

Best Regards,
James
0
 

Author Comment

by:MikeAllen3k
ID: 8098740
I already did that, but it doesn't get the cursor if the pointer is outside of my application's form.
0
 

Expert Comment

by:JamesLondon
ID: 8098765
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
 

Author Comment

by:MikeAllen3k
ID: 8098787
Yes. I need to capture the mouse cursor(there can be custom made cursors).
0
 
LVL 34

Expert Comment

by:Slick812
ID: 8100629
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
 
LVL 34

Expert Comment

by:Slick812
ID: 8100644
sorry, I left something out. . . the function GetCursor, will get the handle of the current cursor


hCursor := GetCursor;
0
 
LVL 20

Expert Comment

by:Madshi
ID: 8101172
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
 

Author Comment

by:MikeAllen3k
ID: 8102452
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
 
LVL 20

Accepted Solution

by:
Madshi earned 80 total points
ID: 8102513
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
 

Author Comment

by:MikeAllen3k
ID: 8102633
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
 
LVL 20

Expert Comment

by:Madshi
ID: 8102690
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
 
LVL 20

Expert Comment

by:Madshi
ID: 8102711
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
 

Author Comment

by:MikeAllen3k
ID: 8102890
Thank you again.
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
Suggested Courses
Course of the Month8 days, 15 hours left to enroll

764 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question