IconMaker Gadget
Posted: Thu Feb 14, 2008 7:25 pm
CODE EDITED AND UPDATED WITH SOME FIXES ECT....
Here is something I have put together because I needed it for my app, and I got Iconconstructor from GiveawayOfTheDay, which has a similar gadget.
This is a very alpha version, bit it essentially allows a user to select a rectangle of an image, and generates a 32, 48.64, or 128 pixel icon.
The gadget is layed out with the main image and selection box, and to the right there are 4 other image gadgets that grab the rectangle selection of the image. The rectangle is forced to be a square, although you could change the code if you wanted to do otherwise.
Moving the selection rectangle works perfectly, although i cant seem to figure out how to keep the entire selection square inside the image gadget.
Sizing the selection square is a bit more tricky, and can be buggy. If ANYONE can help with working out the bugs, id appreciate it.
The gadget works by proessing the #wm_mousemove commands and #wm_lbuttondown and up. After the selection rectangle is drawn, it sends a message to itself called #image_Rect_change. This message prompts grabbing of the selection rectangle and scaling it down for the other image gadgets.
In theory, you could load any image, get the selection you want, and save 32, 48, 64, and 128 pixel icons. I haven't got that far yet, but this code can be very adaptable for those that want it. Any hints or bug fixes are more than welcome, they are needed--lol.
Here is something I have put together because I needed it for my app, and I got Iconconstructor from GiveawayOfTheDay, which has a similar gadget.
This is a very alpha version, bit it essentially allows a user to select a rectangle of an image, and generates a 32, 48.64, or 128 pixel icon.
The gadget is layed out with the main image and selection box, and to the right there are 4 other image gadgets that grab the rectangle selection of the image. The rectangle is forced to be a square, although you could change the code if you wanted to do otherwise.
Moving the selection rectangle works perfectly, although i cant seem to figure out how to keep the entire selection square inside the image gadget.
Sizing the selection square is a bit more tricky, and can be buggy. If ANYONE can help with working out the bugs, id appreciate it.
The gadget works by proessing the #wm_mousemove commands and #wm_lbuttondown and up. After the selection rectangle is drawn, it sends a message to itself called #image_Rect_change. This message prompts grabbing of the selection rectangle and scaling it down for the other image gadgets.
In theory, you could load any image, get the selection you want, and save 32, 48, 64, and 128 pixel icons. I haven't got that far yet, but this code can be very adaptable for those that want it. Any hints or bug fixes are more than welcome, they are needed--lol.
Code: Select all
#WM_MOUSEHOVER = $2A1
#WM_MOUSELEAVE = $2A3
#TME_HOVER = 1
#TME_LEAVE = 2
#Icon_Rect_Change=$456
Structure iconbox
width.l
height.l
pbID.l ;PB gadget number
pbimage.l ; this is the REAL image that we load or create
TLrect.RECT ;location for the top left grab box
TRrect.RECT ;""
BLrect.RECT;""
BRrect.RECT;""
LocRect.RECT ;this is the actual rectangle of the selection box
tempimage.l ;we dont want to mess up the real image, so we ALWAYS draw on this one
image32.l ;32x32 image gadget
image48.l;""
image64.l;""
image128.l;""
PBimage32.l ;this is the image associated with the 32x32 - we just redraw over this to save memory and creating constant new images
PBimage48.l ;""
PBimage64.l;""
PBimage128.l;""
EndStructure
Global*box.iconbox=AllocateMemory(SizeOf(iconbox))
Global mousedown.l,MouseBtnDownX,MouseBtnDownY,mousedowntopleft.l, mousedowntopright.l, mousedownbotleft.l,mousedownbotright.l, drawrect.RECT
Procedure.w LOWORD(Value.l) ;windef.h macro
ProcedureReturn (Value & $FFFF)
EndProcedure
Procedure.w HIWORD(Value.l) ;windef.h macro
ProcedureReturn ((Value >> 16) & $FFFF)
EndProcedure
Procedure ScaleImage(hBitmap,width.l,height.l,color.l)
Define.l OriW, OriH, w, h, oriAR, newAR
Define.f fw, fh
GetObject_(hBitmap,SizeOf(BITMAP),bz.BITMAP)
OriW=bz\bmWidth
OriH=bz\bmHeight
tempimage=CreateImage(#PB_Any,bz\bmWidth,bz\bmHeight)
hdc=StartDrawing(ImageOutput(tempimage))
DrawImage(hBitmap,0,0)
StopDrawing()
If (OriH > OriW And height < width) Or (OriH < OriW And height > width)
Swap width, height
EndIf
; Calc Factor
fw = width/OriW
fh = height/OriH
; Calc AspectRatio
oriAR = Round((OriW / OriH) * 10,0)
newAR = Round((width / height) * 10,0)
; AspectRatio already correct?
If oriAR = newAR
w = width
h = height
ElseIf OriW * fh <= width
w = OriW * fh
h = OriH * fh
ElseIf OriH * fw <= height
w = OriW * fw
h = OriH * fw
EndIf
fixx=(width/2)-(w/2)
fixy=(height/2)-(h/2)
ResizeImage(tempimage,w,h,#PB_Image_Smooth)
newPhoto=CreateImage(#PB_Any,width,height)
hdc=StartDrawing(ImageOutput(newPhoto))
SelectObject_(newdc,hBitmap)
Box(0,0,width,height,color)
DrawImage(ImageID(tempimage), fixx,fixy)
StopDrawing()
FreeImage(tempimage)
ProcedureReturn newPhoto
EndProcedure
Procedure DrawSelectRect(*rect.RECT,gadget,numberimage)
*box.iconbox=GetWindowLong_(GadgetID(gadget),#GWL_USERDATA)
If *box\tempimage
FreeImage(*box\tempimage);get rid of it and start over
EndIf
tempimage=CreateImage(#PB_Any,*box\width,*box\height)
StartDrawing(ImageOutput(tempimage))
DrawImage(ImageID(numberimage),0,0) ;draw the REAL image associated with the gadget
DrawingMode(#PB_2DDrawing_Outlined)
width=*rect\right-*rect\left
height=*rect\bottom-*rect\top
Box(*rect\left,*rect\top,width,height, #Blue);actual selection box
DrawingMode(#PB_2DDrawing_Default)
LineXY(*rect\left+width/2,*rect\top,*rect\left+width/2,*rect\bottom,#Blue)
LineXY(*rect\left,*rect\top+height/2,*rect\right,*rect\top+height/2,#Blue)
Box(*rect\left-6,*rect\top-6,6,6,#Blue); Top Left
Box(*rect\right,*rect\top-6,6,6,#Blue) ;Top Right
Box(*rect\left-6,*rect\bottom,6,6,#Blue) ;Bottom Left
Box(*rect\right,*rect\bottom,6,6,#Blue);Bottom Left
StopDrawing()
SetGadgetState(gadget,ImageID(tempimage))
With *box
*box\TLrect\left=*rect\left-6
*box\TLrect\top=*rect\top-6
*box\TLrect\right=*rect\left
*box\TLrect\bottom=*rect\top
*box\TRrect\left=*rect\right
*box\TRrect\top=*rect\top-6
*box\TRrect\right=*rect\right+6
*box\TRrect\bottom =*rect\top
*box\BLrect\left=*rect\left-6
*box\BLrect\top=*rect\bottom
*box\BLrect\right=*rect\left
*box\BLrect\bottom=*rect\bottom+6
*box\BRrect\left=*rect\right
*box\BRrect\top=*rect\bottom
*box\BRrect\right=*rect\right+6
*box\BRrect\bottom=*rect\bottom+6
*box\LocRect\left=*rect\left
*box\LocRect\right=*rect\right
*box\LocRect\top=*rect\top
*box\LocRect\bottom=*rect\bottom
*box\tempimage=tempimage
EndWith
SetWindowLong_(GadgetID(gadget),#GWL_USERDATA,*box)
EndProcedure
Procedure DrawRect(*rect.RECT,gadget,numberimage)
*boxrect.iconbox=GetWindowLong_(GadgetID(gadget),#GWL_USERDATA)
If *boxrect\tempimage
FreeImage(*boxrect\tempimage)
EndIf
tempimage=CreateImage(#PB_Any,*boxrect\width,*boxrect\height)
StartDrawing(ImageOutput(tempimage))
DrawImage(ImageID(numberimage),0,0)
DrawingMode(#PB_2DDrawing_Outlined)
width=*rect\right-*rect\left
height=*rect\bottom-*rect\top
Box(*rect\left,*rect\top,width,height, #Blue)
StopDrawing()
SetGadgetState(gadget,ImageID(tempimage))
EndProcedure
Procedure IconProc(hwnd,msg,wParam,lParam)
Select msg
Case #WM_KEYDOWN
Select wParam
Case #VK_UP
Debug "up"
Case #VK_DOWN
EndSelect
Case #WM_PAINT
Case #WM_LBUTTONDOWN
*boxrect.iconbox=GetWindowLong_(hwnd,#GWL_USERDATA)
xPos = LOWORD(lParam)
yPos = HIWORD(lParam)
MouseBtnDownX=LOWORD(lParam)
MouseBtnDownY=HIWORD(lParam)
therect.RECT
With therect.RECT
\left=*boxrect\LocRect\left
\right=*boxrect\LocRect\right
\top=*boxrect\LocRect\top
\bottom=*boxrect\LocRect\bottom
EndWith
If ptinrect_(therect.RECT,xPos,yPos)
hCurHand = LoadCursor_(0,#IDC_HAND)
SetCursor_(hCurHand)
SetCapture_(hwnd)
getwindowrect_(hwnd,rect.RECT)
ClipCursor_(rect.RECT)
mousedown.l=1
ElseIf ptinrect_(*boxrect\TLrect,xPos,yPos)
hCurHand = LoadCursor_(0,#IDC_SIZENWSE)
SetCursor_(hCurHand)
mousedowntopleft.l=1
getwindowrect_(hwnd,rect.RECT)
ClipCursor_(rect.RECT)
ElseIf ptinrect_(*boxrect\TRrect,xPos,yPos)
hCurHand = LoadCursor_(0,#IDC_SIZENESW )
SetCursor_(hCurHand)
mousedowntopright=1
getwindowrect_(hwnd,rect.RECT)
ClipCursor_(rect.RECT)
ElseIf ptinrect_(*boxrect\BLrect,xPos,yPos)
hCurHand = LoadCursor_(0,#IDC_SIZENESW )
SetCursor_(hCurHand)
mousedownbotleft=1
getwindowrect_(hwnd,rect.RECT)
ClipCursor_(rect.RECT)
ElseIf ptinrect_(*boxrect\BRrect,xPos,yPos)
hCurHand = LoadCursor_(0,#IDC_SIZENWSE)
SetCursor_(hCurHand)
mousedownbotright=1
getwindowrect_(hwnd,rect.RECT)
ClipCursor_(rect.RECT)
EndIf
ProcedureReturn 0
Case #WM_LBUTTONUP
*boxrect.iconbox=GetWindowLong_(hwnd,#GWL_USERDATA)
ClipCursor_(#Null)
If mousedowntopleft=1 Or mousedowntopright=1 Or mousedownbotleft=1 Or mousedownbotright=1
DrawSelectRect(drawrect.RECT,*box\pbID,*box\pbimage)
SendMessage_(hwnd,#Icon_Rect_Change, drawrect.RECT,0)
EndIf
mousedown.l=0
mousedowntopleft=0
mousedowntopright=0
mousedownbotleft=0
mousedownbotright=0
ReleaseCapture_()
Case #WM_RBUTTONDOWN
Case #WM_RBUTTONUP
Case #WM_MOUSEMOVE
GetCursorPos_(point.POINT)
screentoclient_(hwnd,point.POINT)
*boxrect.iconbox=GetWindowLong_(hwnd,#GWL_USERDATA)
Structure myTRACKMOUSEEVENT
cbSize.l
dwFlags.l
hwndTrack.l
dwHoverTime.l
EndStructure
mte.myTRACKMOUSEEVENT
mte\cbSize = SizeOf(myTRACKMOUSEEVENT)
mte\dwFlags = #TME_LEAVE
mte\hwndTrack = hwnd
TrackMouseEvent_(mte)
xPos = LOWORD(lParam)
yPos = HIWORD(lParam)
therect.RECT
With therect.RECT
\left=*boxrect\LocRect\left
\right=*boxrect\LocRect\right
\top=*boxrect\LocRect\top
\bottom=*boxrect\LocRect\bottom
EndWith
If ptinrect_(therect.RECT,xPos,yPos) And mousedowntopleft=0 And mousedowntopright=0 And mousedownbotleft=0 And mousedownbotright=0
hCurHand = LoadCursor_(0,#IDC_HAND)
SetCursor_(hCurHand)
If mousedown=1
MouseOffsetX.w = lParam&$FFFF - MouseBtnDownX
MouseOffsetY.w = (lParam>>16) - MouseBtnDownY
drawrect.RECT
With drawrect
\left=*boxrect\LocRect\left+MouseOffsetX
\right=*boxrect\LocRect\right+MouseOffsetX
\top=*boxrect\LocRect\top+MouseOffsetY
\bottom=*boxrect\LocRect\bottom+MouseOffsetY
If \left < 0
\left = 0
\right = *boxrect\LocRect\right - *boxrect\LocRect\left
ElseIf \right >= ImageWidth(*boxrect\pbimage)
\right = ImageWidth(*boxrect\pbimage) - 1
\left = \right - (*boxrect\LocRect\right - *boxrect\LocRect\left)
EndIf
If \top < 0
\top = 0
\bottom = *boxrect\LocRect\bottom - *boxrect\LocRect\top
ElseIf \bottom >= ImageHeight(*boxrect\pbimage)
\bottom = ImageHeight(*boxrect\pbimage) - 1
\top = \bottom - (*boxrect\LocRect\bottom - *boxrect\LocRect\top)
EndIf
EndWith
DrawSelectRect(drawrect.RECT,*boxrect\pbID,*boxrect\pbimage)
SendMessage_(hwnd,#Icon_Rect_Change, drawrect.RECT,0)
MouseBtnDownX + MouseOffsetX
MouseBtnDownY + MouseOffsetY
EndIf
ElseIf ptinrect_(*boxrect\TLrect,xPos,yPos)And mousedowntopleft=0 And mousedowntopright=0 And mousedownbotleft=0 And mousedownbotright=0 And mousedown=0
hCurHand = LoadCursor_(0,#IDC_SIZENWSE)
SetCursor_(hCurHand)
ElseIf ptinrect_(*boxrect\TRrect,xPos,yPos)And mousedowntopleft=0 And mousedowntopright=0 And mousedownbotleft=0 And mousedownbotright=0 And mousedown=0
hCurHand = LoadCursor_(0,#IDC_SIZENESW )
SetCursor_(hCurHand)
ElseIf ptinrect_(*boxrect\BLrect,xPos,yPos)And mousedowntopleft=0 And mousedowntopright=0 And mousedownbotleft=0 And mousedownbotright=0 And mousedown=0
hCurHand = LoadCursor_(0,#IDC_SIZENESW )
SetCursor_(hCurHand)
ElseIf ptinrect_(*boxrect\BRrect,xPos,yPos)And mousedowntopleft=0 And mousedowntopright=0 And mousedownbotleft=0 And mousedownbotright=0 And mousedown=0
hCurHand = LoadCursor_(0,#IDC_SIZENWSE)
SetCursor_(hCurHand)
EndIf
If mousedowntopleft=1 ;Keep in mine here that we want a SQUARE, so the greatest change in movement is forced to be accepted for both X and Y
hCurHand = LoadCursor_(0,#IDC_SIZENWSE)
SetCursor_(hCurHand)
MouseOffsetX.w = lParam&$FFFF - MouseBtnDownX
MouseOffsetY.w = (lParam>>16) - MouseBtnDownY
drawrect.RECT
If Abs(MouseOffsetX)<Abs(MouseOffsetY)
Xaddition.l=MouseOffsetY
Yaddition=MouseOffsetY
ElseIf Abs(MouseOffsetX)>Abs(MouseOffsetY)
Xaddition.l=MouseOffsetX
Yaddition=MouseOffsetX
ElseIf Abs(MouseOffsetX)=Abs(MouseOffsetY)
Xaddition=MouseOffsetX
Yaddition=MouseOffsetY
EndIf
With drawrect
\left=*boxrect\LocRect\left+Xaddition
\right=*boxrect\LocRect\right
\top=*boxrect\LocRect\top+Yaddition
\bottom=*boxrect\LocRect\bottom
EndWith
If drawrect\right-drawrect\left >=32
DrawRect(drawrect.RECT,*boxrect\pbID,*boxrect\pbimage)
Else
With drawrect
\left=*boxrect\LocRect\right-32
\right=*boxrect\LocRect\right
\top=*boxrect\LocRect\bottom-32
\bottom=*boxrect\LocRect\bottom
EndWith
DrawRect(drawrect.RECT,*boxrect\pbID,*boxrect\pbimage)
EndIf
ElseIf mousedowntopright=1
hCurHand = LoadCursor_(0,#IDC_SIZENESW)
SetCursor_(hCurHand)
MouseOffsetX.w = lParam&$FFFF - MouseBtnDownX
MouseOffsetY.w = (lParam>>16) - MouseBtnDownY
drawrect.RECT
If Abs(MouseOffsetX)<Abs(MouseOffsetY) ; greater change in Y movement
If MouseOffsetY<0
Xaddition.l=Abs(MouseOffsetY)
Yaddition=MouseOffsetY
ElseIf MouseOffsetY>0
Xaddition.l=-MouseOffsetY
Yaddition=MouseOffsetY
EndIf
ElseIf Abs(MouseOffsetX)>Abs(MouseOffsetY) ;greater change in X movement
If MouseOffsetX<0
Xaddition.l=MouseOffsetX
Yaddition=-MouseOffsetX
ElseIf MouseOffsetX>0
Xaddition.l=MouseOffsetX
Yaddition=-MouseOffsetX
EndIf
ElseIf Abs(MouseOffsetX)=Abs(MouseOffsetY)
Xaddition=MouseOffsetX
Yaddition=MouseOffsetY
EndIf
With drawrect
\left=*boxrect\LocRect\left
\right=*boxrect\LocRect\right+Xaddition.l
\top=*boxrect\LocRect\top+Yaddition.l
\bottom=*boxrect\LocRect\bottom
EndWith
If drawrect\right-drawrect\left >=32
DrawRect(drawrect.RECT,*boxrect\pbID,*boxrect\pbimage)
Else
With drawrect
\left=*boxrect\LocRect\left
\right=*boxrect\LocRect\left+32
\top=*boxrect\LocRect\bottom-32
\bottom=*boxrect\LocRect\bottom
EndWith
DrawRect(drawrect.RECT,*boxrect\pbID,*boxrect\pbimage)
EndIf
ElseIf mousedownbotleft=1
hCurHand = LoadCursor_(0,#IDC_SIZENESW)
SetCursor_(hCurHand)
MouseOffsetX.w = lParam&$FFFF - MouseBtnDownX
MouseOffsetY.w = (lParam>>16) - MouseBtnDownY
drawrect.RECT
If Abs(MouseOffsetX)<Abs(MouseOffsetY) ; greater change in Y movement
If MouseOffsetY<0
Xaddition.l=-MouseOffsetY
Yaddition=MouseOffsetY
ElseIf MouseOffsetY>0
Xaddition.l=-MouseOffsetY
Yaddition=MouseOffsetY
EndIf
ElseIf Abs(MouseOffsetX)>Abs(MouseOffsetY) ;greater change in X movement
If MouseOffsetX<0
Xaddition.l=MouseOffsetX
Yaddition=-MouseOffsetX
ElseIf MouseOffsetX>0
Xaddition.l=MouseOffsetX
Yaddition=-MouseOffsetX
EndIf
ElseIf Abs(MouseOffsetX)=Abs(MouseOffsetY)
Xaddition=MouseOffsetX
Yaddition=MouseOffsetY
EndIf
With drawrect
\left=*boxrect\LocRect\left+Xaddition.l
\right=*boxrect\LocRect\right
\top=*boxrect\LocRect\top
\bottom=*boxrect\LocRect\bottom+Yaddition.l
EndWith
If drawrect\right-drawrect\left >=32
DrawRect(drawrect.RECT,*boxrect\pbID,*boxrect\pbimage)
Else
With drawrect
\left=*boxrect\LocRect\right-32
\right=*boxrect\LocRect\right
\top=*boxrect\LocRect\top
\bottom=*boxrect\LocRect\top+32
EndWith
DrawRect(drawrect.RECT,*boxrect\pbID,*boxrect\pbimage)
EndIf
ElseIf mousedownbotright=1
hCurHand = LoadCursor_(0,#IDC_SIZENWSE)
SetCursor_(hCurHand)
MouseOffsetX.w = lParam&$FFFF - MouseBtnDownX
MouseOffsetY.w = (lParam>>16) - MouseBtnDownY
drawrect.RECT
If Abs(MouseOffsetX)<Abs(MouseOffsetY) ; greater change in Y movement
If MouseOffsetY<0
Xaddition.l=MouseOffsetY
Yaddition=MouseOffsetY
ElseIf MouseOffsetY>0
Xaddition.l=MouseOffsetY
Yaddition=MouseOffsetY
EndIf
ElseIf Abs(MouseOffsetX)>Abs(MouseOffsetY) ;greater change in X movement
If MouseOffsetX<0
Xaddition.l=MouseOffsetX
Yaddition=MouseOffsetX
ElseIf MouseOffsetX>0
Xaddition.l=MouseOffsetX
Yaddition=MouseOffsetX
EndIf
ElseIf Abs(MouseOffsetX)=Abs(MouseOffsetY)
Xaddition=MouseOffsetX
Yaddition=MouseOffsetY
EndIf
With drawrect
\left=*boxrect\LocRect\left
\right=*boxrect\LocRect\right+Xaddition.l
\top=*boxrect\LocRect\top
\bottom=*boxrect\LocRect\bottom+Yaddition.l
EndWith
If drawrect\right-drawrect\left >=32
DrawRect(drawrect.RECT,*boxrect\pbID,*boxrect\pbimage)
Else
With drawrect
\left=*boxrect\LocRect\left
\right=*boxrect\LocRect\left+32
\top=*boxrect\LocRect\top
\bottom=*boxrect\LocRect\top+32
EndWith
DrawRect(drawrect.RECT,*boxrect\pbID,*boxrect\pbimage)
EndIf
EndIf
ProcedureReturn 0
Case #WM_MOUSEHOVER
Case #WM_MOUSELEAVE
Case #Icon_Rect_Change
*boxrect.iconbox=GetWindowLong_(hwnd,#GWL_USERDATA)
Resultimage = GrabImage(*boxrect\pbimage, #PB_Any, *boxrect\LocRect\left, *boxrect\LocRect\top, *boxrect\LocRect\right-*boxrect\LocRect\left, *boxrect\LocRect\bottom-*boxrect\LocRect\top)
StartDrawing(ImageOutput(*boxrect\PBimage32))
DrawImage(ImageID(Resultimage),0,0,32,32)
StopDrawing()
StartDrawing(ImageOutput(*boxrect\PBimage48))
DrawImage(ImageID(Resultimage),0,0,48,48)
StopDrawing()
StartDrawing(ImageOutput(*boxrect\PBimage64))
DrawImage(ImageID(Resultimage),0,0,64,64)
StopDrawing()
StartDrawing(ImageOutput(*boxrect\PBimage128))
DrawImage(ImageID(Resultimage),0,0,128,128)
StopDrawing()
SetGadgetState(*boxrect\image32,ImageID(*boxrect\PBimage32))
SetGadgetState(*boxrect\image48,ImageID(*boxrect\PBimage48))
SetGadgetState(*boxrect\image64,ImageID(*boxrect\PBimage64))
SetGadgetState(*boxrect\image128,ImageID(*boxrect\PBimage128))
FreeImage(Resultimage)
EndSelect
ProcedureReturn CallWindowProc_(GetProp_(hwnd,"oldproc"),hwnd,msg,wParam,lParam)
EndProcedure
Procedure IconGadget(id.l,x,y,width,height)
image=CreateImage(#PB_Any,width,height)
StartDrawing(ImageOutput(image))
Box(0,0,400,300,#White)
Circle(200,150,60,#Red)
StopDrawing()
container=ContainerGadget(#PB_Any,x,y,width+154,height+20,#PB_Container_Raised)
imagegad.l=ImageGadget(id,5,5,width,height,ImageID(image),#PB_Image_Border)
PBimage32.l=CreateImage(#PB_Any,32,32)
PBimage48.l=CreateImage(#PB_Any,48,48)
PBimage64.l=CreateImage(#PB_Any,64,64)
PBimage128.l=CreateImage(#PB_Any,128,128)
image32.l=ImageGadget(#PB_Any,width+64,10,32,32,ImageID(PBimage32),#PB_Image_Border)
image48.l=ImageGadget(#PB_Any,width+56,50,48,48,ImageID(PBimage48),#PB_Image_Border)
image64.l=ImageGadget(#PB_Any,width+46,107,64,64,ImageID(PBimage64),#PB_Image_Border)
image128.l=ImageGadget(#PB_Any,width+14,180,128,128,ImageID(PBimage128),#PB_Image_Border)
CloseGadgetList()
rect.RECT
rect\left=width/2-50
rect\right=width/2 +50
rect\top=height/2-50
rect\bottom=height/2+50
If id=#PB_Any
*box\pbID=imagegad
hwndreturn=imagegad
hwnd=GadgetID(imagegad)
Else
*box\pbID=id
hwndreturn=imagegad
hwnd=imagegad
EndIf
*box\width=width
*box\height=height
*box\pbimage=image
*box\image32=image32
*box\image48=image48
*box\image64=image64
*box\image128=image128
*box\PBimage32=PBimage32
*box\PBimage48=PBimage48
*box\PBimage64=PBimage64
*box\PBimage128=PBimage128
SetWindowLong_(hwnd,#GWL_USERDATA,*box)
SetProp_(hwnd,"oldproc",SetWindowLong_(hwnd,#GWL_WNDPROC,@IconProc()))
DrawSelectRect(rect.RECT,*box\pbID,image)
SendMessage_(hwnd,#Icon_Rect_Change, drawrect.RECT,0)
EndProcedure
Procedure SetIconGadgetImage(icongadget,PBimageNumber)
*boxrect.iconbox=GetWindowLong_(GadgetID(icongadget),#GWL_USERDATA)
Debug GadgetWidth(*boxrect\pbID)
newimage.l=ScaleImage(ImageID(PBimageNumber),*box\width,*box\height,#White)
ResizeImage(newimage,*box\width,*box\height)
tempimage=CopyImage(newimage,#PB_Any)
If IsImage(*boxrect\pbimage)
FreeImage(*boxrect\pbimage)
EndIf
If IsImage(*boxrect\tempimage)
FreeImage(*boxrect\tempimage)
EndIf
Debug GadgetWidth(*boxrect\pbID)
*boxrect\pbimage=newimage
*boxrect\tempimage=tempimage
SetGadgetState(icongadget,ImageID(newimage))
SetWindowLong_(GadgetID(icongadget),#GWL_USERDATA,*boxrect)
rect.RECT
rect\left=GadgetWidth(*boxrect\pbID)/2-50
rect\right=GadgetWidth(*boxrect\pbID)/2 +50
rect\top=GadgetHeight(*boxrect\pbID)/2-50
rect\bottom=GadgetHeight(*boxrect\pbID)/2+50
DrawSelectRect(rect.RECT,*boxrect\pbID,newimage)
Debug GadgetWidth(*boxrect\pbID)
FreeImage(PBimageNumber)
SendMessage_(GadgetID(*boxrect\pbID),#Icon_Rect_Change, drawrect.RECT,0)
Debug GadgetWidth(*boxrect\pbID)
EndProcedure
UseJPEGImageDecoder()
UsePNGImageDecoder()
If OpenWindow(0, 259, 217, 605, 388, "IconMaker Gadget", #PB_Window_SystemMenu | #PB_Window_TitleBar | #PB_Window_ScreenCentered )
If CreateGadgetList(WindowID(0))
IconGadget(10,20,40,400,300)
ButtonGadget(1,30,10,70,20,"Open Image")
EndIf
EndIf
;-Event Loop
Repeat
Select WaitWindowEvent()
Case #PB_Event_Gadget
Select EventGadget()
Case 1
imagefile$=OpenFileRequester("Open An Image","","Images|*.bmp;*.jpg;*.png",0)
If imagefile$
image=LoadImage(#PB_Any,imagefile$)
SetIconGadgetImage(10,image)
EndIf
EndSelect
Case #PB_Event_Menu
Select EventMenu()
EndSelect
Case #PB_Event_CloseWindow
Quit=1
EndSelect
Until Quit=1