replace mouse cursor with a bitmap

Just starting out? Need help? Post your questions and find answers here.
User avatar
netmaestro
PureBasic Bullfrog
PureBasic Bullfrog
Posts: 8425
Joined: Wed Jul 06, 2005 5:42 am
Location: Fort Nelson, BC, Canada

replace mouse cursor with a bitmap

Post by netmaestro »

Is this possible? Can anyone point the way?
Last edited by netmaestro on Tue Feb 21, 2006 7:38 pm, edited 2 times in total.
BERESHEIT
coma
Enthusiast
Enthusiast
Posts: 164
Joined: Fri Aug 15, 2003 3:46 am
Location: Canada

Post by coma »

look at LoadCursorFromFile

Code: Select all

SetClassLong_(hWnd , #GCL_HCURSOR, LoadCursorFromFile_("cursor.cur"))
You can also change system cursors with SetSystemCursor
User avatar
netmaestro
PureBasic Bullfrog
PureBasic Bullfrog
Posts: 8425
Joined: Wed Jul 06, 2005 5:42 am
Location: Fort Nelson, BC, Canada

Post by netmaestro »

Thanks for replying but I don't think that will help, as that only deals with cursors and I want to use a bitmap.
Last edited by netmaestro on Tue Feb 21, 2006 7:39 pm, edited 2 times in total.
BERESHEIT
coma
Enthusiast
Enthusiast
Posts: 164
Joined: Fri Aug 15, 2003 3:46 am
Location: Canada

Post by coma »

ok I see.
Should be possible using icons.
this is working :

SetClassLong_(hWnd , #GCL_HCURSOR ,LoadIcon_(0, #IDI_ASTERISK) )

so, may be with CreateIconIndirect ?

http://msdn.microsoft.com/library/defau ... direct.asp
Sparkie
PureBatMan Forever
PureBatMan Forever
Posts: 2307
Joined: Tue Feb 10, 2004 3:07 am
Location: Ohio, USA

Post by Sparkie »

netmaestro wrote:I'm not real sure setting the cursor to a bitmap is doable, but it might be.
I am currently working on a bitmap > icon converter (which works for creating cursors as well). It's nowhere near being finished, but if you would like to give it a shot yourself...

1. Load your bitmap
LoadImage(0, "imagePath$")

2. Resize it to 32 x 32
resizedBitmapID = ResizeImage(0, 32, 32)

3. CreateImage(1, 32, 32) (this will be the AND mask)

4. Draw the bitmap onto Image#1
DrawImage(resizedBitmapID, 0, 0, 32, 32)

5. Get the mask color at Point(0, 0)

6. Replace mask color pixels with #WHITE and all other pixels with #BLACK

7. Fill in an ICONINFO structure. (change xHotspot and yHotspot as desired)
icoInf.ICONINFO
icoInf\fIcon = #False
icoInf\xHotspot = 15
icoInf\yHotspot = 0
icoInf\hbmMask = UseImage(1)
icoInf\hbmColor = resizedBitmapID

8. Create the cursor
customCursor = CreateIconIndirect_(icoInf)

9. Set the window cursor
SetClassLong_(WindowID(), #GCL_HCURSOR, customCursor)

10. Make sure to DestroyIcon_(customCursor) when it's no longer needed.

Here's what you should end up with. Image on left is the mask, middle image is the bitmap, and the image on the right is the cursor. The image I used was from PureBasic\Examples\Sources\Data\Geebee2.bmp

Image
What goes around comes around.

PB 5.21 LTS (x86) - Windows 8.1
User avatar
netmaestro
PureBasic Bullfrog
PureBasic Bullfrog
Posts: 8425
Joined: Wed Jul 06, 2005 5:42 am
Location: Fort Nelson, BC, Canada

Post by netmaestro »

Wow, Sparkie, you almost wrote the whole program yourself. Just a matter of following directions:

Code: Select all

;1. Load your bitmap 
LoadImage(0, "c:\program files\PureBasic\Examples\Sources\Data\Geebee2.bmp") 
;2. Resize it To 32 x 32 
ri=ResizeImage(0,32,32) 
;3.  (this will be the And mask) 
CreateImage(1, 32, 32) 

StartDrawing(ImageOutput(1)) 
 DrawImage(ImageID(0),0,0) ;4. Draw the bitmap onto Image#1 
 transcol=Point(0,0)        ;5. Get the mask color at 0,0 
  For i=0 To 31             ;6. Replace mask color pixels with #White And all other pixels with #Black 
    For j=0 To 31 
      If Point(i,j)=transcol 
        Plot(i,j,#White) 
      Else 
        Plot(i,j,#Black) 
      EndIf 
    Next 
  Next 
StopDrawing() 

OpenWindow(0,0,0,640,480,#PB_Window_SystemMenu,"") 
StartDrawing(WindowOutput(0)) 
  DrawImage(ImageID(0),0,0) 
  DrawImage(ImageID(1),40,0) 
StopDrawing() 

;7. Fill in an ICONINFO Structure. (change xHotspot And yHotspot as desired) 
icoInf.ICONINFO 
icoInf\fIcon = #False 
icoInf\xHotspot = 15 
icoInf\yHotspot = 0 
icoInf\hbmMask = ImageID(1) 
icoInf\hbmColor = ri 

;8. Create the cursor 
customCursor = CreateIconIndirect_(icoInf) 

;9. Set the window cursor 
SetClassLong_(WindowID(0), #GCL_HCURSOR, customCursor) 

Repeat 
    ev=WaitWindowEvent() 
  Until ev=#PB_Event_CloseWindow 
DestroyIcon_(customCursor) 
End 
Last edited by netmaestro on Tue Feb 21, 2006 7:44 pm, edited 4 times in total.
BERESHEIT
Sparkie
PureBatMan Forever
PureBatMan Forever
Posts: 2307
Joined: Tue Feb 10, 2004 3:07 am
Location: Ohio, USA

Post by Sparkie »

Hmmm...you're right. Looking back at my current project code I guess it would have been just as easy for me to pluck the code out rather that make a step by step list. I'll have to work on making my tutorials less wordy and more challenging in the future. :twisted:

Nice job piecing it together netmaestro :)
What goes around comes around.

PB 5.21 LTS (x86) - Windows 8.1
User avatar
blueznl
PureBasic Expert
PureBasic Expert
Posts: 6161
Joined: Sat May 17, 2003 11:31 am
Contact:

Post by blueznl »

two bitmaps to one icon, this can perhaps point you in the right direction:

Code: Select all

Procedure x_icon_createicon(width,height,image_h,mask_h)                                 ; create icon from two bitmaps
  Protected icon.ICONINFO
  ;
  ; *** turn any two images into an icon
  ;
  ; note: you have to delete this icon manually when exiting the program!
  ;
  ; how do icons behave?
  ;
  ; to replace a pixel: pixel in mask should be black, pixel in image should be whatever
  ; for no changes to a pixel: pixel in mask should be white, pixel in image should be black
  ;
  icon.ICONINFO
  icon\ficon = #True
  icon\hbmmask = mask_h
  icon\hbmcolor = image_h
  ProcedureReturn CreateIconIndirect_(@icon)
  ;
EndProcedure

Procedure x_icon_freeicon(icon_h)                                                        ; free icon object
  DeleteObject_(icon_h)
EndProcedure

Procedure x_icon_charactericon(width.l,height.l,text.s,r.l,g.l,b.l)                      ; create an icon from a single character
  ;
  ; *** create an icon using one or two characters as template
  ;
  ; note: you have to delete this icon manually when exiting the program!
  ;
  ; step 1: draw the image
  ;
  image_nr = CreateImage(#PB_Any,width,height)
  image_h = ImageID()
  StartDrawing(ImageOutput())
    Box(0,0,width,height,$000000)                   ; background should be black so xor has no effect
    d = width/2-TextLength(text)/2-1
    DrawingMode(1)
    FrontColor(r,g,b)                               ; then the character in given colour
    Locate(d,0)
    DrawText(text)
  StopDrawing()
  ;
  ; step 2: the mask
  ;
  mask_nr = CreateImage(#PB_Any,width,height)
  mask_h = ImageID()
  StartDrawing(ImageOutput())
    DrawingMode(1)
    Box(0,0,width,height,$FFFFFF)
    FrontColor(0,0,0)
    Locate(d,0)
    DrawText(text)
  StopDrawing()
  ;
  icon_h = x_icon_createicon(widht,height,image_h,mask_h)
  FreeImage(image_nr)
  FreeImage(mask_nr)
  ;
  ProcedureReturn icon_h
EndProcedure
( PB6.00 LTS Win11 x64 Asrock AB350 Pro4 Ryzen 5 3600 32GB GTX1060 6GB)
( The path to enlightenment and the PureBasic Survival Guide right here... )
SFSxOI
Addict
Addict
Posts: 2970
Joined: Sat Dec 31, 2005 5:24 pm
Location: Where ya would never look.....

Post by SFSxOI »

is it possible to do the same thing with .png .gif or .jpg?
User avatar
netmaestro
PureBasic Bullfrog
PureBasic Bullfrog
Posts: 8425
Joined: Wed Jul 06, 2005 5:42 am
Location: Fort Nelson, BC, Canada

Post by netmaestro »

You get into compression problems there, transparency doesn't show right.
Last edited by netmaestro on Tue Feb 21, 2006 7:49 pm, edited 2 times in total.
BERESHEIT
User avatar
blueznl
PureBasic Expert
PureBasic Expert
Posts: 6161
Joined: Sat May 17, 2003 11:31 am
Contact:

Post by blueznl »

yes, if you look at my sample, it's just two bitmaps, you could use a jpg, turn it into a b&w picture and use that one as a mask, it's what i do in x_icon_charactoricon() which is why i included it :-)
( PB6.00 LTS Win11 x64 Asrock AB350 Pro4 Ryzen 5 3600 32GB GTX1060 6GB)
( The path to enlightenment and the PureBasic Survival Guide right here... )
dagcrack
Addict
Addict
Posts: 1868
Joined: Sun Mar 07, 2004 8:47 am
Location: Argentina
Contact:

Post by dagcrack »

SFSxOI wrote:is it possible to do the same thing with .png .gif or .jpg?
netmaestro wrote:You get into compression problems there, transparency doesn't show right.
PNG is not lossy and does even hold alpha channel data.
In memory all images are bitmaps anyway... (Expect for formats like dds).
! Black holes are where God divided by zero !
My little blog!
(Not for the faint hearted!)
User avatar
netmaestro
PureBasic Bullfrog
PureBasic Bullfrog
Posts: 8425
Joined: Wed Jul 06, 2005 5:42 am
Location: Fort Nelson, BC, Canada

Post by netmaestro »

yes, I wasn't really referring to .png there, sorry. Just the other two. I've had major problems getting transparency with .gif and .jpg. Png is excellent.
BERESHEIT
Post Reply