SetCursor_() - Pippet in paint??

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

Post by netmaestro »

Very helpful, thanks very much! :D :D
BERESHEIT
User avatar
Shardik
Addict
Addict
Posts: 2058
Joined: Thu Apr 21, 2005 2:38 pm
Location: Germany

Post by Shardik »

netmaestro,

I am very proud of having been able to help you, because your (and srod's) uncountable and very profound code examples have made my programming life a lot easier... :wink:
srod
PureBasic Expert
PureBasic Expert
Posts: 10589
Joined: Wed Oct 29, 2003 4:35 pm
Location: Beyond the pale...

Post by srod »

Nice one Shardik.

I always thought that ExtractIcon_() rummaged through the resource section looking for icons.

Any idea why / how ExtractIcon_() differs from LoadImage_() in this respect?
I may look like a mule, but I'm not a complete ass.
User avatar
Shardik
Addict
Addict
Posts: 2058
Joined: Thu Apr 21, 2005 2:38 pm
Location: Germany

Post by Shardik »

srod,

unfortunately I am no expert in working with resources and therefore can't answer your question about the details in the difference between the use of ExtractIcon_() and LoadImage_(). But if you want to know how to scan for resources in DLL- and Exe-Files, take a look into the nice code from VoSs2o0o in the German forum:
http://www.purebasic.fr/german/archive/ ... 0&start=10

VoSs2o0o demonstrates how to extract and save an icon from the Shell32.DLL without knowing the resource name by using the following WinAPI and PB functions:
- LoadLibraryEx_()
- FindResource_()
- LoadResource_()
- LockResource_()
- CopyMemory()
- Analyze the icon information (number of icons, color, etc.) and save it

The analysis of other resources like cursors, AVI, etc. should be similar...

Some time ago I wrote a scanner to analyze Windows resource files for AVI animations for use in my own programs, but I had to freeze this project because of missing time. The scanning part was ready (using EnumResourceTypes_()) so that I was informed whether a resource file (for example Shell32.dll) contains AVI animations but the read out of the resources is still unfinished. The display of the animations is no problem... As soon as this project is finished I will post it in this forum.
srod
PureBasic Expert
PureBasic Expert
Posts: 10589
Joined: Wed Oct 29, 2003 4:35 pm
Location: Beyond the pale...

Post by srod »

Thanks, I'll have a look at that post. :)
I may look like a mule, but I'm not a complete ass.
User avatar
Shardik
Addict
Addict
Posts: 2058
Joined: Thu Apr 21, 2005 2:38 pm
Location: Germany

Post by Shardik »

If you want to see which cursors are contained as resources in a library (like .Exe, .DLL, .OCX or .Scr), try the following code example which displays all cursors of MSPaint.Exe:

Code: Select all

#RT_GROUP_CURSOR = 12

NewList ResNameList.S()

Procedure.l EnumResNameProc(ResHandle.L, ResType.L, ResName.L, AppParam.L)
  Shared ResNameList.S()

  AddElement(ResNameList())
  ResNameList() = "#" + Str(ResName)

  ProcedureReturn #True
EndProcedure


Procedure.L EnumResTypeProc(ResHandle.L, ResType.L, AppParam.L)
  If ResType = #RT_GROUP_CURSOR
    EnumResourceNames_(ResHandle, ResType, @EnumResNameProc(), 0)
  EndIf

  ProcedureReturn #True 
EndProcedure 


LibName.S = "MSPaint.Exe"
LibHandle.L = LoadLibrary_(@LibName)

If LibHandle = 0
  MessageRequester("Error", "Loading of " + LibName + " failed!", #MB_ICONERROR)
  End
EndIf

EnumResourceTypes_(LibHandle, @EnumResTypeProc(), 0) 

CreateImage(0, 135, 57) 
StartDrawing(ImageOutput(0)) 
Box(0, 0, 135, 57, #White) 
StopDrawing() 

OpenWindow(0, 0, 0, 160, 100, "Display lib's cursors", #PB_Window_SystemMenu | #PB_Window_ScreenCentered) 
CreateGadgetList(WindowID(0)) 
ImageGadget(0, 10, 10, 135, 57, ImageID(0), #PB_Image_Border)
ButtonGadget(1, 10, 75, 140, 20, "Switch to another cursor")

ForEach ResNameList()
  CursorHandle = LoadImage_(LibHandle, ResNameList(), #IMAGE_CURSOR, 0, 0, #LR_DEFAULTCOLOR)

  If CursorHandle = 0
    MessageRequester("Error", "Loading of cursor " + ResNameList() + " from " + LibName + " failed!", #MB_ICONERROR)
    FreeLibrary_(LibHandle)
    End
  EndIf  

  SetClassLong_(GadgetID(0), #GCL_HCURSOR, CursorHandle) 

  Repeat
    Select WaitWindowEvent()
      Case #PB_Event_Gadget
        If EventGadget() = 1
          Break
        EndIf
      Case #PB_Event_CloseWindow
        DestroyIcon_(CursorHandle) 
        FreeLibrary_(LibHandle)      
        End
    EndSelect
  ForEver

  DestroyIcon_(CursorHandle)
Next

FreeLibrary_(LibHandle)
srod
PureBasic Expert
PureBasic Expert
Posts: 10589
Joined: Wed Oct 29, 2003 4:35 pm
Location: Beyond the pale...

Post by srod »

Great example thanks. I'll keep that one. :)

I answered my own question as to why ExtractIcon_() doesn't find the cursors. It extracts icons - not cursors! Doh! :oops:
I may look like a mule, but I'm not a complete ass.
User avatar
Kwai chang caine
Always Here
Always Here
Posts: 5494
Joined: Sun Nov 05, 2006 11:42 pm
Location: Lyon - France

Post by Kwai chang caine »

Yes the code of SHARDIK is great and so simple 8)
But KCC have again a fly in his milk :cry:

If i have insert the ressource in my exe with

Code: Select all

Import "Ressource.res"
EndImport
I can't use for obtain the handle :cry:

Code: Select all

LibHandle.L = LoadLibrary_(@LibName) 
And i want enumerate the ressource inside it
How can i obtain the handle of the exe who extract HIS ressources ????

In the GREAT code of DROOPY lib, he use, for extract HIS ressource:

Code: Select all

HandleResource= FindResource_(0,@Name,#RT_RCDATA)
But if i don't know the name of the ressource who is in my exe ???
I must enumerate all the ressource in my EXE, and choose the ressource i want to extract :roll:
How can i do ???
ImageThe happiness is a road...
Not a destination
cas
Enthusiast
Enthusiast
Posts: 597
Joined: Mon Nov 03, 2008 9:56 pm

Post by cas »

EnumResourceNames_() or EnumResourceNamesEx_() ?
User avatar
Kwai chang caine
Always Here
Always Here
Posts: 5494
Joined: Sun Nov 05, 2006 11:42 pm
Location: Lyon - France

Post by Kwai chang caine »

Hello CAS :D
Thanks to answer at my question 8)
EnumResourceNames_() or EnumResourceNamesEx_() ?
In fact i don't know :oops:
My problem is for understand how an EXE can scan itself for ENUMERATE and extracting ressources inside him ???? :roll:

I found some codes, who use for access at the ressource.

Code: Select all

LibHandle.L = LoadLibrary_(@LibName) 
But me...the ressources is in the exe who ENUMERATE and extract it
I have include it with

Code: Select all

Import "Ressources.res"
EndImport
I don't know how the EXE obtain the handle of himself ??? :cry:

In the great code of DROOPY, the EXE scan itself and save ressource in a picture file, but i don't understand how ENUMERATE the ressources in this code

Code: Select all

; PureBasic 3.94 25/11/05

; Create a File with RC Extension And include it as RC_DATA Resource Type 
; Path can be absolute Or relative
; Exemple :
            ; REG RCData "C:\WINDOWS\REGEDIT.EXE"
            ; SON RCData "son.wav"
            ; Image RCData "image.bmp"

;/ SaveResourceAs(Name.s,File.s) Save the resource as a file
;/ GetResourcePointer(Name.s) is usefull with PureBasic CatchXXX functions (CatchImage / CatchSound ... )

ProcedureDLL SaveResourceAs(Name.s,File.s)
  
  ; (Use GetModuleHandle_ if you want to specify another file)
  
  HandleResource= FindResource_(0,@Name,#RT_RCDATA)
  If HandleResource
    HandleGlobalMemoryBlock=LoadResource_(0,HandleResource) 
    PointerFirstByteOfTheResource=LockResource_(HandleGlobalMemoryBlock)
    
    ; Get size of the resource
    Size= SizeofResource_(Handle,HandleResource)
    
    ; Save the file
    FileId=OpenFile(#PB_Any,File)
    If FileId
      WriteData(HandleGlobalMemoryBlock,Size)
      CloseFile(FileId)
    EndIf
    
    ; Test if the file is written
    If FileSize(File)=Size 
      ProcedureReturn 1
    EndIf
  EndIf
  
EndProcedure

ProcedureDLL GetResourcePointer(Name.s)
  
  ; (Use GetModuleHandle_ if you want to specify another file)
  
  HandleResource= FindResource_(0,@Name,#RT_RCDATA)
  If HandleResource
    HandleGlobalMemoryBlock=LoadResource_(0,HandleResource) 
    PointerFirstByteOfTheResource=LockResource_(HandleGlobalMemoryBlock)
    
    ProcedureReturn HandleGlobalMemoryBlock
  EndIf
EndProcedure

ProcedureDLL GetResourceSize(Name.s)
  
  ; (Use GetModuleHandle_ if you want to specify another file)
  
  HandleResource= FindResource_(0,@Name,#RT_RCDATA)
  If HandleResource
    HandleGlobalMemoryBlock=LoadResource_(0,HandleResource) 
    PointerFirstByteOfTheResource=LockResource_(HandleGlobalMemoryBlock)
    
    ; Return the size of the resource
    Size= SizeofResource_(Handle,HandleResource)
    ProcedureReturn Size
  EndIf
  
EndProcedure

;/ Test
CatchImage(0,GetResourcePointer("IMAGE"))
OpenWindow(0,0,0,240,240,#PB_Window_SystemMenu|#PB_Window_ScreenCentered,"ImageGadget")
ImageGadget(0,20,20,200,200,UseImage(0),#PB_Image_Border)  
SaveResourceAs("REG","c:\Picture.jpg")
ImageThe happiness is a road...
Not a destination
User avatar
Kwai chang caine
Always Here
Always Here
Posts: 5494
Joined: Sun Nov 05, 2006 11:42 pm
Location: Lyon - France

Post by Kwai chang caine »

I have destroy the nice code of SHARDIK to made that...
That's works apparently :D

1/ I use "GetModuleHandle_(0)" but is it the good way ??? :roll:
2/ The line is it usefull ????

Code: Select all

FreeLibrary_(LibHandle)  
This is my cowpat ...

Code: Select all

Import "c:\Ressources.res"
EndImport
 
Procedure.l EnumResNameProc(ResHandle.L, ResType.L, ResName.L, AppParam.L) 
 Debug PeekS(ResName)
 ProcedureReturn #True 
EndProcedure 


Procedure.L EnumResTypeProc(ResHandle.L, ResType.L, AppParam.L)
  Debug ResType
  If ResType = #RT_RCDATA
    EnumResourceNames_(ResHandle, ResType, @EnumResNameProc(), 0) 
  EndIf 

  ProcedureReturn #True 
EndProcedure 

LibHandle.L = GetModuleHandle_(0)
EnumResourceTypes_(LibHandle, @EnumResTypeProc(), 0) 

Repeat 
 Delay(100)
ForEver

FreeLibrary_(LibHandle)  
ImageThe happiness is a road...
Not a destination
eesau
Enthusiast
Enthusiast
Posts: 589
Joined: Fri Apr 27, 2007 12:38 pm
Location: Finland

Post by eesau »

Kwaï chang caïne wrote:1/ I use "GetModuleHandle_(0)" but is it the good way ??? :roll:
I think you can just pass #Null to EnumResourceTypes_ to get the same effect. I.e.:

Code: Select all

EnumResourceTypes_(0, @EnumResTypeProc(), 0) 
I can't test it right now though, so no guarantees.
eesau
Enthusiast
Enthusiast
Posts: 589
Joined: Fri Apr 27, 2007 12:38 pm
Location: Finland

Post by eesau »

Kwaï chang caïne wrote:2/ The line is it usefull ????

Code: Select all

FreeLibrary_(LibHandle)  
You shouldn't do that, here's an explanation from msdn:
msdn wrote:Use caution when calling FreeLibrary with a handle returned by GetModuleHandle. The GetModuleHandle function does not increment a module's reference count, so passing this handle to FreeLibrary can cause a module to be unloaded prematurely.
User avatar
Kwai chang caine
Always Here
Always Here
Posts: 5494
Joined: Sun Nov 05, 2006 11:42 pm
Location: Lyon - France

Post by Kwai chang caine »

I think you can just pass #Null to EnumResourceTypes_ to get the same effect. I.e.:
Thanks a lot EESAU 8)
You're right, i just replace the handle by "0" and that's works better fine :D
Just a "0" is the fault of all my worries :shock:
You shouldn't do that, here's an explanation from msdn:
Ok thanks too...
If EESAU and Mister CROSOFT say KCC don't use FREELIBRARY :?

Then KCC don't use..... :oops:
I promise to you :D

Thanks a lot again for your precious help
I wish you a very good day 8)
ImageThe happiness is a road...
Not a destination
eesau
Enthusiast
Enthusiast
Posts: 589
Joined: Fri Apr 27, 2007 12:38 pm
Location: Finland

Post by eesau »

Kwaï chang caïne wrote:You're right, i just replace the handle by "0" and that's works better fine :D
Glad that it works :)
Post Reply