Tile,List,Small Icon & Thumbnails [Windows]

Share your advanced PureBasic knowledge/code with the community.
RASHAD
PureBasic Expert
PureBasic Expert
Posts: 4663
Joined: Sun Apr 12, 2009 6:27 am

Tile,List,Small Icon & Thumbnails [Windows]

Post by RASHAD »

- Files
- Icons
- Movies
- Pictures
Not Fast as Windows file browser but Windows use indexing :)

Code: Select all

UseTGAImageDecoder()

#PBM_SETBARCOLOR = 1033
#PBM_SETBKCOLOR  = 8193

#TBS_TOOLTIPS = $100

Global Image ,ext$ ,hwndPB,oldpos
Global iSize,ILwnd

iSize = 16

Structure PPMColor
  r.a
  g.a
  b.a
EndStructure

Structure PGMColor
  c.a
EndStructure

Structure File
  FileName$
  Image.l
EndStructure

Macro _imgscale
  hScale.f = iSize/bm\bmWidth
  vScale.f = iSize/bm\bmHeight
  If hScale > vScale
    Scale.f = vScale*0.9
  Else
    Scale.f = hScale*0.9
  EndIf
  If Scale > 2
    Scale = 2
  EndIf
  x = (iSize-Scale*bm\bmWidth)/2
  y = (iSize-Scale*bm\bmHeight)/2
EndMacro

Procedure iItem(Gad,itText$,index)
  it.LVITEM
  it\Mask     = #LVIF_TEXT| #LVIF_IMAGE
  it\iItem    = 0
  it\iSubItem = 0
  it\iImage = index
  it\pszText  = @itText$
  SendMessage_(GadgetID(Gad), #LVM_INSERTITEM, 0, @it)
EndProcedure

Procedure LoadPPGM(Image,filename$)
  Result = ReadFile(#PB_Any, filename$,#PB_Ascii)
  If Result
    If CreateImage(Image, 1, 1)
      Format$ = ReadString(Result)
      Dimensions$ = ReadString(Result)
      If Val(StringField(Dimensions$, 1, " ")) = 0
        Dimensions$ = ReadString(Result)
      EndIf
      w  = Val(StringField(Dimensions$, 1, " "))
      h  = Val(StringField(Dimensions$, 2, " "))
      If w = 0 Or h = 0
        ProcedureReturn 1
      EndIf
      ResizeImage(Image, w, h)
      StartDrawing(ImageOutput(Image))
      max = Val(ReadString(Result))
      Select Format$
        Case "P2"
          Stringlen = Lof(Result) - Loc(Result)
          content$  = Space(Stringlen*SizeOf(Character)+1)
          Dim color.s(0)
          ReadData(Result, @content$, Stringlen)
          content$ = PeekS(@content$,-1,#PB_Ascii)
          CreateRegularExpression(1, "\d+")
          ExtractRegularExpression(1, content$, color())
          For y = 0 To h - 1
            For x = 0 To w - 1
              pos = (y*w + x)*1
              Plot(x, y,RGB(Val(color(pos)),Val(color(pos)),Val(color(pos))))
            Next
          Next
          
        Case "P3"
          Stringlen = Lof(Result) - Loc(Result)
          content$  = Space(Stringlen*SizeOf(Character)+1)
          Dim color.s(0)
          ReadData(Result, @content$, Stringlen)
          content$ = PeekS(@content$,-1,#PB_Ascii)
          CreateRegularExpression(1, "\d+")
          ExtractRegularExpression(1, content$, color())
          For y = 0 To h - 1
            For x = 0 To w - 1
              pos = (y*w + x)*3
              r=Val(color(pos))*255 / max
              g=Val(color(pos+1))*255 / max
              b=Val(color(pos+2))*255 / max
              Plot(x, y, RGB(r,g,b))
            Next
          Next
          
        Case "P5"
          Bufferlen = Lof(Result) - Loc(Result)
          *Buffer   = AllocateMemory(Bufferlen)
          ReadData(Result, *Buffer, Bufferlen)
          For y = 0 To h - 1
            For x = 0 To w - 1
              *gray.PGMColor = pos + *Buffer
              Plot(x, y,RGB(*gray\c,*gray\c,*gray\c))
              pos + 1
            Next
          Next
          
        Case "P6"
          Bufferlen = Lof(Result) - Loc(Result)
          *Buffer   = AllocateMemory(Bufferlen)
          ReadData(Result, *Buffer, Bufferlen)
          For y = 0 To h - 1
            For x = 0 To w - 1
              *color.PPMColor = pos + *Buffer
              Plot(x, y, RGB(*color\r*255 / max, *color\g*255 / max, *color\b*255 / max))
              pos + 3
            Next
          Next
      EndSelect
      StopDrawing()     
      CloseFile(Result)
      ProcedureReturn 1
    EndIf
  EndIf
EndProcedure

Procedure GetShellThumbnail(FileName$, Image, Width, Height, Depth = #PB_Image_DisplayFormat)
  Protected Result = 0, ImageResult
  Protected Desktop.IShellFolder, Folder.IShellFolder
  Protected Extract.IExtractImage
  Protected *pidlFolder.ITEMIDLIST, *pidlFile.ITEMIDLIST
  Protected Priority, Flags, Bitmap = 0, size.SIZE
  
  ext$ = GetExtensionPart(FileName$)
  
  If SHGetDesktopFolder_(@Desktop) >= 0
    If Desktop\ParseDisplayName(#Null, #Null, GetPathPart(FileName$), #Null, @*pidlFolder, #Null) = #S_OK
      If Desktop\BindToObject(*pidlFolder, #Null, ?IID_IShellFolder, @Folder) = #S_OK
        If Folder\ParseDisplayName(#Null, #Null, GetFilePart(FileName$) , #Null, @*pidlFile, #Null) = #S_OK
          If Folder\GetUIObjectOf(#Null, 1, @*pidlFile, ?IID_IExtractImage, 0, @Extract) = #S_OK
            
            ImageResult = CreateImage(Image, width,height, Depth)
            If ImageResult
              If Image = #PB_Any
                Image = ImageResult
              EndIf   
              If Depth = #PB_Image_DisplayFormat
                Depth = ImageDepth(Image)
              EndIf
              
              size\cx = Width
              size\cy = Height
              
              If Extract\GetLocation(Space(#MAX_PATH), #MAX_PATH, @Priority, @size, Depth, @Flags) >= 0               
                If Extract\Extract(@Bitmap) >= 0 And Bitmap
                  GetObject_(Bitmap, SizeOf(BITMAP), @bm.BITMAP)                  
                  If ext$ = "3gpp" Or ext$ = "mp4"; Or ext$ = "mkv"
                    If StartDrawing(ImageOutput(Image))
                      Box(0,0,iSize,iSize,$FFFFFF)
                      DrawImage(Bitmap,iSize/2-bm\bmWidth/2,iSize/2-bm\bmHeight/2-2)
                      StopDrawing()                   
                      Result = ImageResult
                    EndIf
                  Else
                    If StartVectorDrawing(ImageVectorOutput(Image))
                      ResetCoordinates()
                      VectorSourceColor($FFEBFEFF)
                      FillVectorOutput()
                      If OSVersion() >= #PB_OS_Windows_Vista
                        MovePathCursor(iSize/2-bm\bmWidth/2,iSize/2+bm\bmHeight/2-2)                         
                        FlipCoordinatesY(180)
                      EndIf
                      DrawVectorImage(Bitmap, 255)
                      StopVectorDrawing()                    
                      Result = ImageResult
                    EndIf
                  EndIf                 
                  DeleteObject_(Bitmap)
                EndIf
              EndIf               
              Extract\Release()
            EndIf
            
            If Result = 0
              FreeImage(Image)
            EndIf           
          EndIf
          
          CoTaskMemFree_(*pidlFile)
        EndIf                       
        Folder\Release()
      EndIf     
      CoTaskMemFree_(*pidlFolder)     
    EndIf   
    Desktop\Release()
  EndIf
  
  ProcedureReturn Result 
  
EndProcedure

Procedure Resizewindow_EX()
  ResizeGadget(5,#PB_Ignore,#PB_Ignore,WindowWidth(0)-40,WindowHeight(0)-55) 
  ResizeGadget(2,#PB_Ignore,#PB_Ignore,WindowWidth(0) - 52,WindowHeight(0)-68)
  MoveWindow_(hwndPB,WindowWidth(0)-24,10,18,WindowHeight(0)-55,1)
  ResizeGadget(10,10,WindowHeight(0) - 34,160,20)
  ResizeGadget(15,180,WindowHeight(0) - 34,40,20)
  ResizeGadget(20,230,WindowHeight(0) - 34,GadgetWidth(5)-220,20)
EndProcedure

NewList File.File()

LoadFont(0,"Consolas",14)

Flag = #PB_Window_SystemMenu|#PB_Window_ScreenCentered| #PB_Window_MinimizeGadget | #PB_Window_MaximizeGadget| #PB_Window_SizeGadget
If OpenWindow(0,0,0,820,600,"Thumbnails Viewer",Flag)
  SetWindowColor(0,$E9E9E9)
  WindowBounds(0,600,400,#PB_Default,#PB_Default)
  
  ContainerGadget(5,10,10,780,545,#PB_Container_Flat)
  SetGadgetColor(5,#PB_Gadget_BackColor,0)
  ListIconGadget(0,0,0,0,0,"",0,#PB_ListIcon_MultiSelect|#LVS_AUTOARRANGE |#WS_DLGFRAME )
  SendMessage_(GadgetID(0),#LVM_SETVIEW,#LV_VIEW_ICON,0)
  SendMessage_(GadgetID(0), #LVM_SETEXTENDEDLISTVIEWSTYLE,0,SendMessage_(GadgetID(0),#LVM_GETEXTENDEDLISTVIEWSTYLE,0,0)| #LVS_EX_TRACKSELECT)
  SendMessage_(GadgetID(0), #LVM_SETHOVERTIME, 0, 10)
  ExplorerTreeGadget(1, 0,0,0,0, "*",#PB_Explorer_NoFiles|#PB_Explorer_NoDriveRequester)
  SetGadgetFont(1,FontID(0))
  SetGadgetColor(1,#PB_Gadget_BackColor,$DBDBDB)
  SplitterGadget(2, 5, 5, 768, 532, 1,0, #PB_Splitter_Vertical|#PB_Splitter_Separator)
  SetGadgetState(2,250)
  CloseGadgetList()
  hwndPB = CreateWindowEx_(0, "msctls_progress32", 0, #WS_CHILD|#WS_VISIBLE|#PBS_VERTICAL , WindowWidth(0)-24,10,18,545, WindowID(0), 0, 0, 0)
  SetWindowTheme_(hwndPB, "", "")
  SendMessage_(hwndPB, #PBM_SETBKCOLOR ,0,$DBDBDB)
  SendMessage_(hwndPB, #PBM_SETBARCOLOR ,0,$FDAE4D)
  
  TrackBarGadget(10,10,568,160,24,1,12,#TBS_AUTOTICKS | #TBS_ENABLESELRANGE|#TBS_TOOLTIPS |#TBS_FIXEDLENGTH)
  SendMessage_(GadgetID(10), #TBM_SETTHUMBLENGTH,16,0)
  SendMessage_(GadgetID(10), #TBM_SETPAGESIZE ,0,1)
  
  SetGadgetState(10,3)
  
  TextGadget(15,180,568,40,20,"16",#WS_BORDER | #SS_CENTER|#SS_CENTERIMAGE)
  SetGadgetColor(15,#PB_Gadget_BackColor,$FFFFFF)
  
  TextGadget(20,230,568,582,20,"",#WS_BORDER | #SS_CENTER | #SS_CENTERIMAGE)
  SetGadgetFont(20,FontID(0))
  SetGadgetColor(20,#PB_Gadget_BackColor,$FFFFFF)
  SetGadgetColor(20,#PB_Gadget_FrontColor,$0000FF)
  
  BindEvent(#PB_Event_SizeWindow,@Resizewindow_EX())
  Repeat
    Select WaitWindowEvent()
      Case #PB_Event_CloseWindow
        Quit = 1
        
      Case #WM_MOUSEMOVE
        If GetActiveGadget() = 10
          If GetGadgetState(10) >= 4
            iSize = GetGadgetState(10)*16
            SetGadgetText(15,Str(iSize))
          Else
            SetGadgetText(15,Str(GetGadgetState(10)))
          EndIf
        EndIf
        
      Case #WM_LBUTTONUP
        If GetActiveGadget() = 10
          Select GetGadgetState(10)
            Case 1
              SendMessage_(GadgetID(0),#LVM_SETVIEW,#LV_VIEW_TILE,0)
            Case 2
              SendMessage_(GadgetID(0),#LVM_SETVIEW,#LV_VIEW_LIST,0)
            Case 3
              iSize = 16
              SetGadgetText(15,Str(iSize))
              SendMessage_(GadgetID(0),#LVM_SETVIEW,#LV_VIEW_ICON,0)
            Case 4 To 12          
              iSize = GetGadgetState(10)*16
              SetGadgetText(15,Str(iSize))
              SendMessage_(GadgetID(0),#LVM_SETVIEW,#LV_VIEW_ICON,0)
          EndSelect
          PostMessage_(GadgetID(1), #WM_LBUTTONDOWN, 0, 0)
          PostMessage_(GadgetID(1), #WM_LBUTTONUP, 0, 0)
        EndIf
        
      Case #PB_Event_Gadget
        Select EventGadget()
          Case 0
            Select EventType()
              Case #PB_EventType_Change
                result = SelectElement(File(),ListSize(File()) - GetGadgetState(0) - 1)
                If result > 0
                  SetGadgetText(20 , File()\FileName$)
                EndIf
            EndSelect
            
          Case 1
            Select EventType()
              Case #PB_EventType_LeftClick               
                If ILwnd
                  ImageList_Destroy_(ILwnd)
                EndIf
                ClearList(File())
                ClearGadgetItems(0)
                SetGadgetText(20 , "" )
                SendMessage_(hwndPB, #PBM_SETPOS, 0,0)
                img = CreateImage(#PB_Any,iSize,iSize,32,$FFFFFF)
                ILwnd = ImageList_Create_(iSize,iSize,#ILC_COLOR32|#ILC_MASK, 0, 300)
                SendMessage_(GadgetID(0),#LVM_SETIMAGELIST,#LVSIL_NORMAL,ILwnd) 
                Count = 0
                If GetGadgetText(1) <> "" And ExamineDirectory(0, GetGadgetText(1), "*.*")
                  While NextDirectoryEntry(0)
                    If DirectoryEntryType(0) = #PB_DirectoryEntry_File
                      AddElement(File())
                      File()\FileName$ = GetGadgetText(1) + DirectoryEntryName(0)
                      Count + 1
                    EndIf
                  Wend
                EndIf
                If Count > 0 
                  SendMessage_(hwndPB, #PBM_SETPOS, 100, 0)
                  ;While WindowEvent(): Wend 
                  
                  ForEach File()
                    index = ListIndex(File()) 
                    File()\Image = GetShellThumbnail(File()\FileName$, #PB_Any, iSize, iSize) 
                    If File()\Image = 0
                      File()\Image = CreateImage(#PB_Any, iSize, iSize)
                      If File()\Image And StartDrawing(ImageOutput(File()\Image))
                        Box(0, 0, iSize, iSize, $FFFFFF)
                        If SHGetFileInfo_(@File()\FileName$, 0, @info.SHFILEINFO, SizeOf(SHFILEINFO), #SHGFI_ICON|#SHGFI_LARGEICON)
                          DrawImage(info\hIcon, iSize/2-16, iSize/2-16)
                          DestroyIcon_(info\hIcon)
                        EndIf               
                        StopDrawing()
                      EndIf 
                      If ext$ = "ppm" Or ext$ = "pgm"
                        If LoadPPGM(Image, File()\FileName$)
                          img = ImageID(image)                        
                          GetObject_(img, SizeOf(BITMAP), @bm.BITMAP)
                          _imgscale
                          imgh = CopyImage_(img,#IMAGE_BITMAP,bm\bmWidth*Scale,bm\bmHeight*Scale,#LR_COLOR)
                          File()\Image = CreateImage(#PB_Any, iSize, iSize)
                          If File()\Image And StartDrawing(ImageOutput(File()\Image)) 
                            Box(0,0,iSize,iSize,$FFFFFF)
                            DrawImage(imgh,x,y)                    
                          EndIf                        
                          StopDrawing()
                          FreeImage(Image)
                          DeleteObject_(img)
                        EndIf
                      EndIf
                      If ext$ = "tga"
                        If LoadImage(Image, File()\FileName$)
                          img = ImageID(image)
                          GetObject_(img, SizeOf(BITMAP), @bm.BITMAP)
                          _imgscale
                          imgh = CopyImage_(img,#IMAGE_BITMAP,bm\bmWidth*Scale,bm\bmHeight*Scale,#LR_COLOR)
                          File()\Image = CreateImage(#PB_Any, iSize, iSize)
                          If File()\Image And StartDrawing(ImageOutput(File()\Image)) 
                            Box(0,0,iSize,iSize,$FFFFFF)
                            DrawImage(imgh,x,y)                    
                          EndIf
                          StopDrawing()
                          FreeImage(Image)
                          DeleteObject_(img)
                        EndIf
                      EndIf
                    EndIf           
                    
                    item = ImageList_Add_(ILwnd,ImageID(File()\Image),0)
                    iItem(0, GetFilePart(File()\FileName$) ,item)
                    
                    SendMessage_(hwndPB, #PBM_SETPOS, (index * 100)/Count,0)
                    While WindowEvent(): Wend
                  Next File()
                  SendMessage_(hwndPB, #PBM_SETPOS, 100,0)
                EndIf
            EndSelect            
        EndSelect              
    EndSelect   
  Until Quit = 1
EndIf
End

DataSection
  IID_IShellFolder: ; {000214E6-0000-0000-C000-000000000046}
  Data.l $000214E6
  Data.w $0000, $0000
  Data.b $C0, $00, $00, $00, $00, $00, $00, $46
  
  IID_IExtractImage: ; {BB2E617C-0920-11D1-9A0B-00C04FC2D6C1}
  Data.l $BB2E617C
  Data.w $0920, $11D1
  Data.b $9A, $0B, $00, $C0, $4F, $C2, $D6, $C1
EndDataSection
Edit : XP bug fixed
Last edited by RASHAD on Sat Mar 06, 2021 11:49 am, edited 1 time in total.
Egypt my love
Mesa
Enthusiast
Enthusiast
Posts: 349
Joined: Fri Feb 24, 2012 10:19 am

Re: Tile,List,Small Icon & Thumbnails [Windows]

Post by Mesa »

Very great as usual but i've got a bug, a lot of thumbnails are upside down with a size of 192 but not all of them.
Windows xp 32b, pb 5.73lts x86

If i use this procedure above, i don't have this bug (but i don't know why :) )

Code: Select all

Procedure GetShellThumbnail(FileName$, Image, Width, Height, Depth = 24) 
  Protected Result = 0, ImageResult 
  Protected Desktop.IShellFolder, Folder.IShellFolder 
  Protected Extract.IExtractImage 
  Protected *pidlFolder.ITEMIDLIST, *pidlFile.ITEMIDLIST  
  Protected Priority, Flags, Bitmap = 0, size.SIZE 
  Protected DC, SourceDC, BitmapInfo.BITMAP 
  
  If SHGetDesktopFolder_(@Desktop) >= 0 
    If Desktop\ParseDisplayName(#Null, #Null, GetPathPart(FileName$), #Null, @*pidlFolder, #Null) = #S_OK 
      If Desktop\BindToObject(*pidlFolder, #Null, ?IID_IShellFolder, @Folder) = #S_OK 
        If Folder\ParseDisplayName(#Null, #Null, GetFilePart(FileName$) , #Null, @*pidlFile, #Null) = #S_OK 
          If Folder\GetUIObjectOf(#Null, 1, @*pidlFile, ?IID_IExtractImage, 0, @Extract) = #S_OK 
            
            ImageResult = CreateImage(Image, Width, Height, Depth) 
            If ImageResult 
              If Image = #PB_Any 
                Image = ImageResult 
              EndIf    
              
              size\cx = Width 
              size\cy = Height 
              
              If Extract\GetLocation(Space(#MAX_PATH), #MAX_PATH, @Priority, @size, Depth, @Flags) >= 0                
                If Extract\Extract(@Bitmap) >= 0 And Bitmap 
                  
                  DC = StartDrawing(ImageOutput(Image)) 
                  If DC 
                    SourceDC = CreateCompatibleDC_(DC) 
                    If SourceDC 
                      GetObject_(Bitmap, SizeOf(BITMAP), @BitmapInfo) 
                      SelectObject_(SourceDC, Bitmap) 
                      BitBlt_(DC, 0, 0, BitmapInfo\bmWidth, BitmapInfo\bmHeight, SourceDC, 0, 0, #SRCCOPY) 
                      DeleteDC_(SourceDC) 
                    EndIf
                   DrawingMode(#PB_2DDrawing_Transparent)
;                    DrawText(10,10,GetExtensionPart(File()\FileName$),#Green)
                    StopDrawing()                    
                    Result = ImageResult 
                  EndIf 
                  
                  DeleteObject_(Bitmap) 
                EndIf 
              EndIf                
              Extract\Release() 
            EndIf 
            
            If Result = 0 
              FreeImage(Image) 
            EndIf            
          EndIf 
          
          CoTaskMemFree_(*pidlFile) 
        EndIf                        
        Folder\Release() 
      EndIf      
      CoTaskMemFree_(*pidlFolder)      
    EndIf    
    Desktop\Release() 
  EndIf 
  
  ProcedureReturn Result 
M.
RASHAD
PureBasic Expert
PureBasic Expert
Posts: 4663
Joined: Sun Apr 12, 2009 6:27 am

Re: Tile,List,Small Icon & Thumbnails [Windows]

Post by RASHAD »

Thanks Mesa
Previous post updated to fix XP flipped thumbnails
I did it way to support any other format like TGA,PPM,PGM and many other to come
I noticed that XP don't support Movies thumbnails and display file name in a weird way
Tested with virtual machine
Egypt my love
User avatar
Kwai chang caine
Always Here
Always Here
Posts: 5353
Joined: Sun Nov 05, 2006 11:42 pm
Location: Lyon - France

Re: Tile,List,Small Icon & Thumbnails [Windows]

Post by Kwai chang caine »

Very nice and usefull code RASHAD :D

First.... nothing appear :shock:
Then i have thinking this time to activate XP THEME :wink: :mrgreen:
And that works 8)

I don't know why, a great part of your codes needs this "XP THEME" :shock:
Because never i activate it with all the other code of the forum friends :wink:

A little bit slow with numerous images folder :|
And the resizing does not act on the icons here, just on the images, i don't know if it's normal ?
The square around each icons and title is more big, but the icons not :shock:

Thanks for sharing your nice job 8)
ImageThe happiness is a road...
Not a destination
RASHAD
PureBasic Expert
PureBasic Expert
Posts: 4663
Joined: Sun Apr 12, 2009 6:27 am

Re: Tile,List,Small Icon & Thumbnails [Windows]

Post by RASHAD »

Hi KCC
- Next is a version with and without XP theme
But the flicker not acceptable
- Resizing the icons with more than 64 pix was very bad so I abandoned it
Hope you like it

Code: Select all

UseTGAImageDecoder()

#PBM_SETBARCOLOR = 1033
#PBM_SETBKCOLOR  = 8193

#TBS_TOOLTIPS = $100

Global Image ,ext$ ,hwndPB,oldpos
Global iSize,ILwnd

iSize = 16

Import ""
  PB_Gadget_SendGadgetCommand(hWnd, EventType)
EndImport

Structure PPMColor
  r.a
  g.a
  b.a
EndStructure

Structure PGMColor
  c.a
EndStructure

Structure File
  FileName$
  Image.l
EndStructure

Macro _imgscale
  hScale.f = iSize/bm\bmWidth
  vScale.f = iSize/bm\bmHeight
  If hScale > vScale
    Scale.f = vScale*0.9
  Else
    Scale.f = hScale*0.9
  EndIf
  If Scale > 2
    Scale = 2
  EndIf
  x = (iSize-Scale*bm\bmWidth)/2
  y = (iSize-Scale*bm\bmHeight)/2
EndMacro

Procedure iItem(Gad,itText$,index)
  it.LVITEM
  it\Mask     = #LVIF_TEXT| #LVIF_IMAGE
  it\iItem    = 0
  it\iSubItem = 0
  it\iImage = index
  it\pszText  = @itText$
  SendMessage_(GadgetID(Gad), #LVM_INSERTITEM, 0, @it)
EndProcedure

Procedure LoadPPGM(Image,filename$)
  Result = ReadFile(#PB_Any, filename$,#PB_Ascii)
  If Result
    If CreateImage(Image, 1, 1)
      Format$ = ReadString(Result)
      Dimensions$ = ReadString(Result)
      If Val(StringField(Dimensions$, 1, " ")) = 0
        Dimensions$ = ReadString(Result)
      EndIf
      w  = Val(StringField(Dimensions$, 1, " "))
      h  = Val(StringField(Dimensions$, 2, " "))
      If w = 0 Or h = 0
        ProcedureReturn 1
      EndIf
      ResizeImage(Image, w, h)
      StartDrawing(ImageOutput(Image))
      max = Val(ReadString(Result))
      Select Format$
        Case "P2"
          Stringlen = Lof(Result) - Loc(Result)
          content$  = Space(Stringlen*SizeOf(Character)+1)
          Dim color.s(0)
          ReadData(Result, @content$, Stringlen)
          content$ = PeekS(@content$,-1,#PB_Ascii)
          CreateRegularExpression(1, "\d+")
          ExtractRegularExpression(1, content$, color())
          For y = 0 To h - 1
            For x = 0 To w - 1
              pos = (y*w + x)*1
              Plot(x, y,RGB(Val(color(pos)),Val(color(pos)),Val(color(pos))))
            Next
          Next
          
        Case "P3"
          Stringlen = Lof(Result) - Loc(Result)
          content$  = Space(Stringlen*SizeOf(Character)+1)
          Dim color.s(0)
          ReadData(Result, @content$, Stringlen)
          content$ = PeekS(@content$,-1,#PB_Ascii)
          CreateRegularExpression(1, "\d+")
          ExtractRegularExpression(1, content$, color())
          For y = 0 To h - 1
            For x = 0 To w - 1
              pos = (y*w + x)*3
              r=Val(color(pos))*255 / max
              g=Val(color(pos+1))*255 / max
              b=Val(color(pos+2))*255 / max
              Plot(x, y, RGB(r,g,b))
            Next
          Next
          
        Case "P5"
          Bufferlen = Lof(Result) - Loc(Result)
          *Buffer   = AllocateMemory(Bufferlen)
          ReadData(Result, *Buffer, Bufferlen)
          For y = 0 To h - 1
            For x = 0 To w - 1
              *gray.PGMColor = pos + *Buffer
              Plot(x, y,RGB(*gray\c,*gray\c,*gray\c))
              pos + 1
            Next
          Next
          
        Case "P6"
          Bufferlen = Lof(Result) - Loc(Result)
          *Buffer   = AllocateMemory(Bufferlen)
          ReadData(Result, *Buffer, Bufferlen)
          For y = 0 To h - 1
            For x = 0 To w - 1
              *color.PPMColor = pos + *Buffer
              Plot(x, y, RGB(*color\r*255 / max, *color\g*255 / max, *color\b*255 / max))
              pos + 3
            Next
          Next
      EndSelect
      StopDrawing()     
      CloseFile(Result)
      ProcedureReturn 1
    EndIf
  EndIf
EndProcedure

Procedure GetShellThumbnail(FileName$, Image, Width, Height, Depth = #PB_Image_DisplayFormat)
  Protected Result = 0, ImageResult
  Protected Desktop.IShellFolder, Folder.IShellFolder
  Protected Extract.IExtractImage
  Protected *pidlFolder.ITEMIDLIST, *pidlFile.ITEMIDLIST
  Protected Priority, Flags, Bitmap = 0, size.SIZE
  
  ext$ = GetExtensionPart(FileName$)
  
  If SHGetDesktopFolder_(@Desktop) >= 0
    If Desktop\ParseDisplayName(#Null, #Null, GetPathPart(FileName$), #Null, @*pidlFolder, #Null) = #S_OK
      If Desktop\BindToObject(*pidlFolder, #Null, ?IID_IShellFolder, @Folder) = #S_OK
        If Folder\ParseDisplayName(#Null, #Null, GetFilePart(FileName$) , #Null, @*pidlFile, #Null) = #S_OK
          If Folder\GetUIObjectOf(#Null, 1, @*pidlFile, ?IID_IExtractImage, 0, @Extract) = #S_OK
            
            ImageResult = CreateImage(Image, width,height, Depth)
            If ImageResult
              If Image = #PB_Any
                Image = ImageResult
              EndIf   
              If Depth = #PB_Image_DisplayFormat
                Depth = ImageDepth(Image)
              EndIf
              
              size\cx = Width
              size\cy = Height
              
              If Extract\GetLocation(Space(#MAX_PATH), #MAX_PATH, @Priority, @size, Depth, @Flags) >= 0               
                If Extract\Extract(@Bitmap) >= 0 And Bitmap
                  GetObject_(Bitmap, SizeOf(BITMAP), @bm.BITMAP)                  
                  If ext$ = "3gpp" Or ext$ = "mp4"; Or ext$ = "mkv"
                    If StartDrawing(ImageOutput(Image))
                      Box(0,0,iSize,iSize,$FFFFFF)
                      DrawImage(Bitmap,iSize/2-bm\bmWidth/2,iSize/2-bm\bmHeight/2-2)
                      StopDrawing()                   
                      Result = ImageResult
                    EndIf
                  Else
                    If StartVectorDrawing(ImageVectorOutput(Image))
                      ResetCoordinates()
                      VectorSourceColor($FFEBFEFF)
                      FillVectorOutput()
                      If OSVersion() >= #PB_OS_Windows_Vista
                        MovePathCursor(iSize/2-bm\bmWidth/2,iSize/2+bm\bmHeight/2-2)                         
                        FlipCoordinatesY(180)
                      EndIf
                      DrawVectorImage(Bitmap, 255)
                      StopVectorDrawing()                    
                      Result = ImageResult
                    EndIf
                  EndIf                 
                  DeleteObject_(Bitmap)
                EndIf
              EndIf               
              Extract\Release()
            EndIf
            
            If Result = 0
              FreeImage(Image)
            EndIf           
          EndIf
          
          CoTaskMemFree_(*pidlFile)
        EndIf                       
        Folder\Release()
      EndIf     
      CoTaskMemFree_(*pidlFolder)     
    EndIf   
    Desktop\Release()
  EndIf
  
  ProcedureReturn Result 
  
EndProcedure

Procedure Resizewindow_EX()
  ResizeGadget(5,#PB_Ignore,#PB_Ignore,WindowWidth(0)-40,WindowHeight(0)-55) 
  ResizeGadget(2,#PB_Ignore,#PB_Ignore,WindowWidth(0) - 52,WindowHeight(0)-68)
  MoveWindow_(hwndPB,WindowWidth(0)-24,10,18,WindowHeight(0)-55,1)
  ResizeGadget(10,10,WindowHeight(0) - 34,160,20)
  ResizeGadget(15,180,WindowHeight(0) - 34,40,20)
  ResizeGadget(20,230,WindowHeight(0) - 34,GadgetWidth(5)-220,20)
EndProcedure

NewList File.File()

LoadFont(0,"Consolas",14)

Flag = #PB_Window_SystemMenu|#PB_Window_ScreenCentered| #PB_Window_MinimizeGadget | #PB_Window_MaximizeGadget| #PB_Window_SizeGadget
If OpenWindow(0,0,0,820,600,"Thumbnails Viewer",Flag)
  SmartWindowRefresh(0,1)
  WindowBounds(0,600,400,#PB_Default,#PB_Default)
  
  ContainerGadget(5,10,10,780,545,#PB_Container_Flat)
  SetGadgetColor(5,#PB_Gadget_BackColor,0)
  ListIconGadget(0,0,0,0,0,"",0,#PB_ListIcon_MultiSelect|#LVS_AUTOARRANGE |#WS_DLGFRAME )
  SetWindowLongPtr_(GadgetID(0),#GWL_STYLE,GetWindowLongPtr_(GadgetID(0),#GWL_STYLE)|#WS_CLIPCHILDREN)
  SetGadgetAttribute(0,#PB_ListIcon_DisplayMode,#PB_ListIcon_LargeIcon)
  SendMessage_(GadgetID(0), #LVM_SETEXTENDEDLISTVIEWSTYLE,0,SendMessage_(GadgetID(0),#LVM_GETEXTENDEDLISTVIEWSTYLE,0,0)| #LVS_EX_TRACKSELECT)
  SendMessage_(GadgetID(0), #LVM_SETHOVERTIME, 0, 10)
  ExplorerTreeGadget(1, 0,0,0,0, "*",#PB_Explorer_NoFiles|#PB_Explorer_NoDriveRequester)
  SetGadgetFont(1,FontID(0))
  SetGadgetColor(1,#PB_Gadget_BackColor,$DBDBDB)
  SplitterGadget(2, 5, 5, 768, 532, 1,0, #PB_Splitter_Vertical|#PB_Splitter_Separator)
  SetGadgetState(2,250)
  CloseGadgetList()
  hwndPB = CreateWindowEx_(0, "msctls_progress32", 0, #WS_CHILD|#WS_VISIBLE|#PBS_VERTICAL , WindowWidth(0)-24,10,18,545, WindowID(0), 0, 0, 0)
  SetWindowTheme_(hwndPB, "", "")
  SendMessage_(hwndPB, #PBM_SETBKCOLOR ,0,$DBDBDB)
  SendMessage_(hwndPB, #PBM_SETBARCOLOR ,0,$FDAE4D)
  
  TrackBarGadget(10,10,568,160,24,1,12,#TBS_AUTOTICKS | #TBS_ENABLESELRANGE|#TBS_TOOLTIPS |#TBS_FIXEDLENGTH)
  SendMessage_(GadgetID(10), #TBM_SETTHUMBLENGTH,16,0)
  SendMessage_(GadgetID(10), #TBM_SETPAGESIZE ,0,1)
  
  SetGadgetState(10,3)
  
  TextGadget(15,180,568,40,20,"16",#WS_BORDER | #SS_CENTER|#SS_CENTERIMAGE)
  SetGadgetColor(15,#PB_Gadget_BackColor,$FFFFFF)
  
  TextGadget(20,230,568,582,20,"",#WS_BORDER | #SS_CENTER | #SS_CENTERIMAGE)
  SetGadgetFont(20,FontID(0))
  SetGadgetColor(20,#PB_Gadget_BackColor,$FFFFFF)
  SetGadgetColor(20,#PB_Gadget_FrontColor,$0000FF)
  
  BindEvent(#PB_Event_SizeWindow,@Resizewindow_EX())
  Repeat
    Select WaitWindowEvent()
      Case #PB_Event_CloseWindow
        Quit = 1
        
      Case #WM_MOUSEMOVE
        If GetActiveGadget() = 10
          If GetGadgetState(10) >= 4
            iSize = GetGadgetState(10)*16
            SetGadgetText(15,Str(iSize))
          Else
            SetGadgetText(15,Str(GetGadgetState(10)))
          EndIf
        EndIf
        
      Case #WM_LBUTTONUP
        If GetActiveGadget() = 10
          Select GetGadgetState(10)
            Case 1
              SetGadgetAttribute(0,#PB_ListIcon_DisplayMode,#PB_ListIcon_List)
            Case 2
              SetGadgetAttribute(0,#PB_ListIcon_DisplayMode,#PB_ListIcon_SmallIcon)
            Case 3
              iSize = 16
              SetGadgetText(15,Str(iSize))
              SetGadgetAttribute(0,#PB_ListIcon_DisplayMode,#PB_ListIcon_LargeIcon)
            Case 4 To 12          
              iSize = GetGadgetState(10)*16
              SetGadgetText(15,Str(iSize))
              SetGadgetAttribute(0,#PB_ListIcon_DisplayMode,#PB_ListIcon_LargeIcon)
          EndSelect
          PB_Gadget_SendGadgetCommand(GadgetID(1), #PB_EventType_LeftClick)
        EndIf
        
      Case #PB_Event_Gadget
        Select EventGadget()
          Case 0
            Select EventType()
              Case #PB_EventType_Change
                result = SelectElement(File(),ListSize(File()) - GetGadgetState(0) - 1)
                If result > 0
                  SetGadgetText(20 , File()\FileName$)
                EndIf
            EndSelect
            
          Case 1
            Select EventType()
              Case #PB_EventType_LeftClick               
                If ILwnd
                  ImageList_Destroy_(ILwnd)
                EndIf
                ClearList(File())
                ClearGadgetItems(0)
                SetGadgetText(20 , "" )
                SendMessage_(hwndPB, #PBM_SETPOS, 0,0)
                img = CreateImage(#PB_Any,iSize,iSize,32,$FFFFFF)
                ILwnd = ImageList_Create_(iSize,iSize,#ILC_COLOR32|#ILC_MASK, 0, 300)
                SendMessage_(GadgetID(0),#LVM_SETIMAGELIST,#LVSIL_NORMAL,ILwnd) 
                Count = 0
                If GetGadgetText(1) <> "" And ExamineDirectory(0, GetGadgetText(1), "*.*")
                  While NextDirectoryEntry(0)
                    If DirectoryEntryType(0) = #PB_DirectoryEntry_File
                      AddElement(File())
                      File()\FileName$ = GetGadgetText(1) + DirectoryEntryName(0)
                      Count + 1
                    EndIf
                  Wend
                EndIf
                If Count > 0 
                  SendMessage_(hwndPB, #PBM_SETPOS, 100, 0)
                  While WindowEvent(): Wend 
                  
                  ForEach File()
                    index = ListIndex(File()) 
                    File()\Image = GetShellThumbnail(File()\FileName$, #PB_Any, iSize, iSize) 
                    If File()\Image = 0
                      File()\Image = CreateImage(#PB_Any, iSize, iSize)
                      If File()\Image And StartDrawing(ImageOutput(File()\Image))
                        Box(0, 0, iSize, iSize, $FFFFFF)
                        If SHGetFileInfo_(@File()\FileName$, 0, @info.SHFILEINFO, SizeOf(SHFILEINFO), #SHGFI_ICON|#SHGFI_LARGEICON)
                          DrawImage(info\hIcon, iSize/2-16, iSize/2-16)
                          DestroyIcon_(info\hIcon)
                        EndIf               
                        StopDrawing()
                      EndIf 
                      If ext$ = "ppm" Or ext$ = "pgm"
                        If LoadPPGM(Image, File()\FileName$)
                          img = ImageID(image)                        
                          GetObject_(img, SizeOf(BITMAP), @bm.BITMAP)
                          _imgscale
                          imgh = CopyImage_(img,#IMAGE_BITMAP,bm\bmWidth*Scale,bm\bmHeight*Scale,#LR_COLOR)
                          File()\Image = CreateImage(#PB_Any, iSize, iSize)
                          If File()\Image And StartDrawing(ImageOutput(File()\Image)) 
                            Box(0,0,iSize,iSize,$FFFFFF)
                            DrawImage(imgh,x,y)                    
                          EndIf                        
                          StopDrawing()
                          FreeImage(Image)
                          DeleteObject_(img)
                        EndIf
                      EndIf
                      If ext$ = "tga"
                        If LoadImage(Image, File()\FileName$)
                          img = ImageID(image)
                          GetObject_(img, SizeOf(BITMAP), @bm.BITMAP)
                          _imgscale
                          imgh = CopyImage_(img,#IMAGE_BITMAP,bm\bmWidth*Scale,bm\bmHeight*Scale,#LR_COLOR)
                          File()\Image = CreateImage(#PB_Any, iSize, iSize)
                          If File()\Image And StartDrawing(ImageOutput(File()\Image)) 
                            Box(0,0,iSize,iSize,$FFFFFF)
                            DrawImage(imgh,x,y)                    
                          EndIf
                          StopDrawing()
                          FreeImage(Image)
                          DeleteObject_(img)
                        EndIf
                      EndIf
                    EndIf           
                    
                    item = ImageList_Add_(ILwnd,ImageID(File()\Image),0)
                    iItem(0, GetFilePart(File()\FileName$) ,item)
                    
                    SendMessage_(hwndPB, #PBM_SETPOS, (index * 100)/Count,0)
                    While WindowEvent(): Wend
                  Next File()
                  SendMessage_(hwndPB, #PBM_SETPOS, 100,0)
                EndIf
            EndSelect            
        EndSelect              
    EndSelect   
  Until Quit = 1
EndIf
End

DataSection
  IID_IShellFolder: ; {000214E6-0000-0000-C000-000000000046}
  Data.l $000214E6
  Data.w $0000, $0000
  Data.b $C0, $00, $00, $00, $00, $00, $00, $46
  
  IID_IExtractImage: ; {BB2E617C-0920-11D1-9A0B-00C04FC2D6C1}
  Data.l $BB2E617C
  Data.w $0920, $11D1
  Data.b $9A, $0B, $00, $C0, $4F, $C2, $D6, $C1
EndDataSection
Egypt my love
User avatar
Kwai chang caine
Always Here
Always Here
Posts: 5353
Joined: Sun Nov 05, 2006 11:42 pm
Location: Lyon - France

Re: Tile,List,Small Icon & Thumbnails [Windows]

Post by Kwai chang caine »

Works very well without XP theme now :D
Thanks a lot MASTER 8)
ImageThe happiness is a road...
Not a destination
Mesa
Enthusiast
Enthusiast
Posts: 349
Joined: Fri Feb 24, 2012 10:19 am

Re: Tile,List,Small Icon & Thumbnails [Windows]

Post by Mesa »

For information, only video thumbnails are still upside down on my computer.

M.
RASHAD
PureBasic Expert
PureBasic Expert
Posts: 4663
Joined: Sun Apr 12, 2009 6:27 am

Re: Tile,List,Small Icon & Thumbnails [Windows]

Post by RASHAD »

Hi Mesa
- Modern themes Enabled\Disabled
- Mesa Modification to solve flipped thumbnails
Have fun

Code: Select all

UseTGAImageDecoder()

#PBM_SETBARCOLOR = 1033
#PBM_SETBKCOLOR  = 8193

#TBS_TOOLTIPS = $100

Global Image ,ext$ ,hwndPB,oldpos
Global iSize,ILwnd

iSize = 16

Import ""
  PB_Gadget_SendGadgetCommand(hWnd, EventType)
EndImport

Structure PPMColor
  r.a
  g.a
  b.a
EndStructure

Structure PGMColor
  c.a
EndStructure

Structure File
  FileName$
  Image.l
EndStructure

Macro _imgscale
  hScale.f = iSize/bm\bmWidth
  vScale.f = iSize/bm\bmHeight
  If hScale > vScale
    Scale.f = vScale*0.9
  Else
    Scale.f = hScale*0.9
  EndIf
  If Scale > 2
    Scale = 2
  EndIf
  x = (iSize-Scale*bm\bmWidth)/2
  y = (iSize-Scale*bm\bmHeight)/2
EndMacro

Procedure iItem(Gad,itText$,index)
  it.LVITEM
  it\Mask     = #LVIF_TEXT| #LVIF_IMAGE
  it\iItem    = 0
  it\iSubItem = 0
  it\iImage = index
  it\pszText  = @itText$
  SendMessage_(GadgetID(Gad), #LVM_INSERTITEM, 0, @it)
EndProcedure

Procedure LoadPPGM(Image,filename$)
  Result = ReadFile(#PB_Any, filename$,#PB_Ascii)
  If Result
    If CreateImage(Image, 1, 1)
      Format$ = ReadString(Result)
      Dimensions$ = ReadString(Result)
      If Val(StringField(Dimensions$, 1, " ")) = 0
        Dimensions$ = ReadString(Result)
      EndIf
      w  = Val(StringField(Dimensions$, 1, " "))
      h  = Val(StringField(Dimensions$, 2, " "))
      If w = 0 Or h = 0
        ProcedureReturn 1
      EndIf
      ResizeImage(Image, w, h)
      StartDrawing(ImageOutput(Image))
      max = Val(ReadString(Result))
      Select Format$
        Case "P2"
          Stringlen = Lof(Result) - Loc(Result)
          content$  = Space(Stringlen*SizeOf(Character)+1)
          Dim color.s(0)
          ReadData(Result, @content$, Stringlen)
          content$ = PeekS(@content$,-1,#PB_Ascii)
          CreateRegularExpression(1, "\d+")
          ExtractRegularExpression(1, content$, color())
          For y = 0 To h - 1
            For x = 0 To w - 1
              pos = (y*w + x)*1
              Plot(x, y,RGB(Val(color(pos)),Val(color(pos)),Val(color(pos))))
            Next
          Next
          
        Case "P3"
          Stringlen = Lof(Result) - Loc(Result)
          content$  = Space(Stringlen*SizeOf(Character)+1)
          Dim color.s(0)
          ReadData(Result, @content$, Stringlen)
          content$ = PeekS(@content$,-1,#PB_Ascii)
          CreateRegularExpression(1, "\d+")
          ExtractRegularExpression(1, content$, color())
          For y = 0 To h - 1
            For x = 0 To w - 1
              pos = (y*w + x)*3
              r=Val(color(pos))*255 / max
              g=Val(color(pos+1))*255 / max
              b=Val(color(pos+2))*255 / max
              Plot(x, y, RGB(r,g,b))
            Next
          Next
          
        Case "P5"
          Bufferlen = Lof(Result) - Loc(Result)
          *Buffer   = AllocateMemory(Bufferlen)
          ReadData(Result, *Buffer, Bufferlen)
          For y = 0 To h - 1
            For x = 0 To w - 1
              *gray.PGMColor = pos + *Buffer
              Plot(x, y,RGB(*gray\c,*gray\c,*gray\c))
              pos + 1
            Next
          Next
          
        Case "P6"
          Bufferlen = Lof(Result) - Loc(Result)
          *Buffer   = AllocateMemory(Bufferlen)
          ReadData(Result, *Buffer, Bufferlen)
          For y = 0 To h - 1
            For x = 0 To w - 1
              *color.PPMColor = pos + *Buffer
              Plot(x, y, RGB(*color\r*255 / max, *color\g*255 / max, *color\b*255 / max))
              pos + 3
            Next
          Next
      EndSelect
      StopDrawing()     
      CloseFile(Result)
      ProcedureReturn 1
    EndIf
  EndIf
EndProcedure

Procedure GetShellThumbnail(FileName$, Image, Width, Height, Depth = 24)
  Protected Result = 0, ImageResult
  Protected Desktop.IShellFolder, Folder.IShellFolder
  Protected Extract.IExtractImage
  Protected *pidlFolder.ITEMIDLIST, *pidlFile.ITEMIDLIST 
  Protected Priority, Flags, Bitmap = 0, size.SIZE
  Protected DC, SourceDC, BitmapInfo.BITMAP

  ext$ = GetExtensionPart(FileName$)
  
  If SHGetDesktopFolder_(@Desktop) >= 0
    If Desktop\ParseDisplayName(#Null, #Null, GetPathPart(FileName$), #Null, @*pidlFolder, #Null) = #S_OK
      If Desktop\BindToObject(*pidlFolder, #Null, ?IID_IShellFolder, @Folder) = #S_OK
        If Folder\ParseDisplayName(#Null, #Null, GetFilePart(FileName$) , #Null, @*pidlFile, #Null) = #S_OK
          If Folder\GetUIObjectOf(#Null, 1, @*pidlFile, ?IID_IExtractImage, 0, @Extract) = #S_OK
            
            ImageResult = CreateImage(Image, Width, Height, depth,$FFFFFF)
            If ImageResult
              If Image = #PB_Any
                Image = ImageResult
              EndIf   
              
              size\cx = Width
              size\cy = Height
              
              If Extract\GetLocation(Space(#MAX_PATH), #MAX_PATH, @Priority, @size, Depth, @Flags) >= 0               
                If Extract\Extract(@Bitmap) >= 0 And Bitmap
                  
                  DC = StartDrawing(ImageOutput(Image))
                  If DC
                    SourceDC = CreateCompatibleDC_(DC)
                    If SourceDC
                      GetObject_(Bitmap, SizeOf(BITMAP), @BitmapInfo)
                      SelectObject_(SourceDC, Bitmap)
                      BitBlt_(DC, iSize/2-BitmapInfo\bmWidth/2,iSize/2-BitmapInfo\bmHeight/2, BitmapInfo\bmWidth, BitmapInfo\bmHeight, SourceDC, 0, 0, #SRCCOPY)
                      DeleteDC_(SourceDC)
                    EndIf
                    ;DrawingMode(#PB_2DDrawing_Transparent)
                    ;                    ;DrawText(10,10,GetExtensionPart(File()\FileName$),#Green)
                    StopDrawing()                   
                    Result = ImageResult
                  EndIf
                  
                  DeleteObject_(Bitmap)
                EndIf
              EndIf               
              Extract\Release()
            EndIf
            
            If Result = 0
              FreeImage(Image)
            EndIf           
          EndIf
          
          CoTaskMemFree_(*pidlFile)
        EndIf                       
        Folder\Release()
      EndIf     
      CoTaskMemFree_(*pidlFolder)     
    EndIf   
    Desktop\Release()
  EndIf
  
  ProcedureReturn Result 
EndProcedure

Procedure Resizewindow_EX()
  ResizeGadget(5,#PB_Ignore,#PB_Ignore,WindowWidth(0)-40,WindowHeight(0)-55)
  ResizeGadget(2,#PB_Ignore,#PB_Ignore,WindowWidth(0) - 52,WindowHeight(0)-68)
  MoveWindow_(hwndPB,WindowWidth(0)-24,10,18,WindowHeight(0)-55,1)
  ResizeGadget(10,10,WindowHeight(0) - 34,160,20)
  ResizeGadget(15,180,WindowHeight(0) - 34,40,20)
  ResizeGadget(20,230,WindowHeight(0) - 34,GadgetWidth(5)-220,20)
EndProcedure

NewList File.File()

LoadFont(0,"Consolas",14)

Flag = #PB_Window_SystemMenu|#PB_Window_ScreenCentered| #PB_Window_MinimizeGadget | #PB_Window_MaximizeGadget| #PB_Window_SizeGadget
If OpenWindow(0,0,0,820,600,"Thumbnails Viewer",Flag)
  SmartWindowRefresh(0,1)
  WindowBounds(0,600,400,#PB_Default,#PB_Default)
  
  ContainerGadget(5,10,10,780,545,#PB_Container_Flat)
  SetGadgetColor(5,#PB_Gadget_BackColor,0)
  ListIconGadget(0,0,0,0,0,"",0,#PB_ListIcon_MultiSelect|#LVS_AUTOARRANGE |#WS_DLGFRAME )
  SetWindowLongPtr_(GadgetID(0),#GWL_STYLE,GetWindowLongPtr_(GadgetID(0),#GWL_STYLE)|#WS_CLIPCHILDREN)
  SetGadgetAttribute(0,#PB_ListIcon_DisplayMode,#PB_ListIcon_LargeIcon)
  SendMessage_(GadgetID(0), #LVM_SETEXTENDEDLISTVIEWSTYLE,0,SendMessage_(GadgetID(0),#LVM_GETEXTENDEDLISTVIEWSTYLE,0,0)| #LVS_EX_TRACKSELECT)
  SendMessage_(GadgetID(0), #LVM_SETHOVERTIME, 0, 10)
  ExplorerTreeGadget(1, 0,0,0,0, "*",#PB_Explorer_NoFiles|#PB_Explorer_NoDriveRequester)
  SetGadgetFont(1,FontID(0))
  SetGadgetColor(1,#PB_Gadget_BackColor,$DBDBDB)
  SplitterGadget(2, 5, 5, 768, 532, 1,0, #PB_Splitter_Vertical|#PB_Splitter_Separator)
  SetGadgetState(2,250)
  CloseGadgetList()
  hwndPB = CreateWindowEx_(0, "msctls_progress32", 0, #WS_CHILD|#WS_VISIBLE|#PBS_VERTICAL , WindowWidth(0)-24,10,18,545, WindowID(0), 0, 0, 0)
  SetWindowTheme_(hwndPB, "", "")
  SendMessage_(hwndPB, #PBM_SETBKCOLOR ,0,$DBDBDB)
  SendMessage_(hwndPB, #PBM_SETBARCOLOR ,0,$FDAE4D)
  
  TrackBarGadget(10,10,568,160,24,1,12,#TBS_AUTOTICKS | #TBS_ENABLESELRANGE|#TBS_TOOLTIPS |#TBS_FIXEDLENGTH)
  SendMessage_(GadgetID(10), #TBM_SETTHUMBLENGTH,16,0)
  SendMessage_(GadgetID(10), #TBM_SETPAGESIZE ,0,1)
  
  SetGadgetState(10,3)
  
  TextGadget(15,180,568,40,20,"16",#WS_BORDER | #SS_CENTER|#SS_CENTERIMAGE)
  SetGadgetColor(15,#PB_Gadget_BackColor,$FFFFFF)
  
  TextGadget(20,230,568,582,20,"",#WS_BORDER | #SS_CENTER | #SS_CENTERIMAGE)
  SetGadgetFont(20,FontID(0))
  SetGadgetColor(20,#PB_Gadget_BackColor,$FFFFFF)
  SetGadgetColor(20,#PB_Gadget_FrontColor,$0000FF)
  
  BindEvent(#PB_Event_SizeWindow,@Resizewindow_EX())
  Repeat
    Select WaitWindowEvent()
      Case #PB_Event_CloseWindow
        Quit = 1
        
      Case #WM_MOUSEMOVE
        If GetActiveGadget() = 10
          If GetGadgetState(10) >= 4
            iSize = GetGadgetState(10)*16
            SetGadgetText(15,Str(iSize))
          Else
            SetGadgetText(15,Str(GetGadgetState(10)))
          EndIf
        EndIf
        
      Case #WM_LBUTTONUP
        If GetActiveGadget() = 10
          Select GetGadgetState(10)
            Case 1
              SetGadgetAttribute(0,#PB_ListIcon_DisplayMode,#PB_ListIcon_List)
            Case 2
              SetGadgetAttribute(0,#PB_ListIcon_DisplayMode,#PB_ListIcon_SmallIcon)
            Case 3
              iSize = 16
              SetGadgetText(15,Str(iSize))
              SetGadgetAttribute(0,#PB_ListIcon_DisplayMode,#PB_ListIcon_LargeIcon)
            Case 4 To 12         
              iSize = GetGadgetState(10)*16
              SetGadgetText(15,Str(iSize))
              SetGadgetAttribute(0,#PB_ListIcon_DisplayMode,#PB_ListIcon_LargeIcon)
          EndSelect
          PB_Gadget_SendGadgetCommand(GadgetID(1), #PB_EventType_LeftClick)
        EndIf
        
      Case #PB_Event_Gadget
        Select EventGadget()
          Case 0
            Select EventType()
              Case #PB_EventType_Change
                result = SelectElement(File(),ListSize(File()) - GetGadgetState(0) - 1)
                If result > 0
                  SetGadgetText(20 , File()\FileName$)
                EndIf
            EndSelect
            
          Case 1
            Select EventType()
              Case #PB_EventType_LeftClick               
                If ILwnd
                  ImageList_Destroy_(ILwnd)
                EndIf
                ClearList(File())
                ClearGadgetItems(0)
                SetGadgetText(20 , "" )
                SendMessage_(hwndPB, #PBM_SETPOS, 0,0)
                img = CreateImage(#PB_Any,iSize,iSize,32,$FFFFFF)
                ILwnd = ImageList_Create_(iSize,iSize,#ILC_COLOR32|#ILC_MASK, 0, 300)
                SendMessage_(GadgetID(0),#LVM_SETIMAGELIST,#LVSIL_NORMAL,ILwnd)
                Count = 0
                If GetGadgetText(1) <> "" And ExamineDirectory(0, GetGadgetText(1), "*.*")
                  While NextDirectoryEntry(0)
                    If DirectoryEntryType(0) = #PB_DirectoryEntry_File
                      AddElement(File())
                      File()\FileName$ = GetGadgetText(1) + DirectoryEntryName(0)
                      Count + 1
                    EndIf
                  Wend
                EndIf
                If Count > 0
                  SendMessage_(hwndPB, #PBM_SETPOS, 100, 0)
                  While WindowEvent(): Wend
                  
                  ForEach File()
                    index = ListIndex(File())
                    File()\Image = GetShellThumbnail(File()\FileName$, #PB_Any, iSize, iSize)
                    If File()\Image = 0
                      File()\Image = CreateImage(#PB_Any, iSize, iSize)
                      If File()\Image And StartDrawing(ImageOutput(File()\Image))
                        Box(0, 0, iSize, iSize, $FFFFFF)
                        If SHGetFileInfo_(@File()\FileName$, 0, @info.SHFILEINFO, SizeOf(SHFILEINFO), #SHGFI_ICON|#SHGFI_LARGEICON)
                          DrawImage(info\hIcon, iSize/2-16, iSize/2-16)
                          DestroyIcon_(info\hIcon)
                        EndIf               
                        StopDrawing()
                      EndIf
                      If ext$ = "ppm" Or ext$ = "pgm"
                        If LoadPPGM(Image, File()\FileName$)
                          img = ImageID(image)                       
                          GetObject_(img, SizeOf(BITMAP), @bm.BITMAP)
                          _imgscale
                          imgh = CopyImage_(img,#IMAGE_BITMAP,bm\bmWidth*Scale,bm\bmHeight*Scale,#LR_COLOR)
                          File()\Image = CreateImage(#PB_Any, iSize, iSize)
                          If File()\Image And StartDrawing(ImageOutput(File()\Image))
                            Box(0,0,iSize,iSize,$FFFFFF)
                            DrawImage(imgh,x,y)                   
                          EndIf                       
                          StopDrawing()
                          FreeImage(Image)
                          DeleteObject_(img)
                        EndIf
                      EndIf
                      If ext$ = "tga"
                        If LoadImage(Image, File()\FileName$)
                          img = ImageID(image)
                          GetObject_(img, SizeOf(BITMAP), @bm.BITMAP)
                          _imgscale
                          imgh = CopyImage_(img,#IMAGE_BITMAP,bm\bmWidth*Scale,bm\bmHeight*Scale,#LR_COLOR)
                          File()\Image = CreateImage(#PB_Any, iSize, iSize)
                          If File()\Image And StartDrawing(ImageOutput(File()\Image))
                            Box(0,0,iSize,iSize,$FFFFFF)
                            DrawImage(imgh,x,y)                   
                          EndIf
                          StopDrawing()
                          FreeImage(Image)
                          DeleteObject_(img)
                        EndIf
                      EndIf
                    EndIf           
                    
                    item = ImageList_Add_(ILwnd,ImageID(File()\Image),0)
                    iItem(0, GetFilePart(File()\FileName$) ,item)
                    
                    SendMessage_(hwndPB, #PBM_SETPOS, (index * 100)/Count,0)
                    While WindowEvent(): Wend
                  Next File()
                  SendMessage_(hwndPB, #PBM_SETPOS, 100,0)
                EndIf
            EndSelect           
        EndSelect             
    EndSelect   
  Until Quit = 1
EndIf
End

DataSection
  IID_IShellFolder: ; {000214E6-0000-0000-C000-000000000046}
  Data.l $000214E6
  Data.w $0000, $0000
  Data.b $C0, $00, $00, $00, $00, $00, $00, $46
  
  IID_IExtractImage: ; {BB2E617C-0920-11D1-9A0B-00C04FC2D6C1}
  Data.l $BB2E617C
  Data.w $0920, $11D1
  Data.b $9A, $0B, $00, $C0, $4F, $C2, $D6, $C1
EndDataSection
Egypt my love
Mesa
Enthusiast
Enthusiast
Posts: 349
Joined: Fri Feb 24, 2012 10:19 am

Re: Tile,List,Small Icon & Thumbnails [Windows]

Post by Mesa »

I wanted to use this program to view the images in the firefox cache folder but unfortunately all the cache files are saved without file extension.

Is there a windows function to guess the type of a file without file extension ?

Thanks.

M.
RASHAD
PureBasic Expert
PureBasic Expert
Posts: 4663
Joined: Sun Apr 12, 2009 6:27 am

Re: Tile,List,Small Icon & Thumbnails [Windows]

Post by RASHAD »

Next snippet to recognize the will known image format dimensions
If it does so the Thumbnail Lister will draw it and no need for the extension
In case you need the extension you must read each file header using any hex editor

Code: Select all

Global lvalue

Repeat
  file$ = OpenFileRequester("Please choose file to load", "",  "Image (*.bmp;*.gif;*.jpeg;*.jpg;*.png;*.tif;*.tiff)" , 0)
  If file$
    OpenFile(0,GetHomeDirectory()+"RASHAD.vbs")
      WriteStringN(0,"Dim Img 'As ImageFile")
      WriteStringN(0,"Dim s 'As String")
      WriteStringN(0,"Dim v 'As Vector")
      WriteStringN(0,"Dim File 'As String")
      WriteStringN(0,"File = "+Chr(34)+file$+Chr(34))
      WriteData(0,?RASHAD,?RASHADend-?RASHAD)
    CloseFile(0)
    RunProgram("WScript.exe",GetHomeDirectory()+"RASHAD.vbs","",#PB_Program_Wait)
    DeleteFile(GetHomeDirectory()+"RASHAD.vbs")
   EndIf
   result = MessageRequester("","More Images ?",#PB_MessageRequester_YesNo|#MB_ICONQUESTION)
 Until Result = #PB_MessageRequester_No

           
DataSection
RASHAD:
   Data.q $20676D4920746573,$657461657243203D,$22287463656A624F,$67616D492E414957,$0D2922656C694665
   Data.q $616F4C2E676D490A,$694620656C694664,$20730A0D0A0D656C,$6567616D4922203D,$4620262022203D20
   Data.q $6276202620656C69,$5F202620664C7243,$5722202020200A0D,$22203D2068746469,$572E676D49202620
   Data.q $2220262068746469,$226C657869502020,$4C72436276202620,$200A0D5F20262066,$6769654822202020
   Data.q $262022203D207468,$6965482E676D4920,$2022202620746867,$20226C6578695020,$664C724362762026
   Data.q $20200A0D5F202620,$6874706544222020,$4920262022203D20,$6C657869502E676D,$2026206874706544
   Data.q $2022505042202022,$664C724362762026,$20200A0D5F202620,$7A69726F48222020,$7365526C61746E6F
   Data.q $206E6F6974756C6F,$6F5220262022203D,$2E676D4928646E75,$746E6F7A69726F48,$756C6F7365526C61
   Data.q $202620296E6F6974,$2022495044202022,$664C724362762026,$20200A0D5F202620,$6974726556222020
   Data.q $6C6F7365526C6163,$203D206E6F697475,$6E756F5220262022,$65562E676D492864,$65526C6163697472
   Data.q $6E6F6974756C6F73,$4420202220262029,$6276202620224950,$5F202620664C7243,$4622202020200A0D
   Data.q $6E756F43656D6172,$20262022203D2074,$6D6172462E676D49,$2620746E756F4365,$0D664C7243627620
   Data.q $6D492066490A0D0A,$65646E4973492E67,$6C65786950646578,$742074616D726F46,$2020200A0D6E6568
   Data.q $262073203D207320,$206C657869502220,$6E6F632061746164,$617020736E696174,$6E6920657474656C
   Data.q $2620227365786564,$0D664C7243627620,$0D664920646E450A,$6D492066490A0D0A,$68706C4173492E67
   Data.q $6F466C6578695061,$6568742074616D72,$73202020200A0D6E,$2220262073203D20,$6164206C65786950
   Data.q $6120736168206174,$666E69206168706C,$6E6F6974616D726F,$7243627620262022,$20646E450A0D664C
   Data.q $66490A0D0A0D6649,$4573492E676D4920,$506465646E657478,$6D726F466C657869,$0D6E656874207461
   Data.q $3D2073202020200A,$6950222026207320,$61746164206C6578,$7478652073616820,$6F63206465646E65
   Data.q $6F666E6920726F6C,$206E6F6974616D72,$2F74696220363128,$296C656E6E616863,$7243627620262022
   Data.q $20646E450A0D664C,$66490A0D0A0D6649,$4173492E676D4920,$20646574616D696E,$20200A0D6E656874
   Data.q $2073203D20732020,$6567616D49222026,$6D696E6120736920,$2026202264657461,$0A0D664C72436276
   Data.q $0A0D664920646E45,$676D492066490A0D,$747265706F72502E,$736978452E736569,$3930303422287374
   Data.q $6E65687420292231,$7620746553090A0D,$502E676D49203D20,$6569747265706F72,$3139303034222873
   Data.q $65756C61562E2922,$2073202020200A0D,$542220262073203D,$22203D20656C7469,$7274532E76202620
   Data.q $6276202620676E69,$6E450A0D664C7243,$0A0D0A0D66492064,$502E676D49206649,$6569747265706F72
   Data.q $7374736978452E73,$2232393030342228,$0A0D6E6568742029,$3D20762074655309,$6F72502E676D4920
   Data.q $2873656974726570,$2922323930303422,$0A0D65756C61562E,$203D207320202020,$6D6F432220262073
   Data.q $22203D20746E656D,$7274532E76202620,$6276202620676E69,$6E450A0D664C7243,$0A0D0A0D66492064
   Data.q $502E676D49206649,$6569747265706F72,$7374736978452E73,$2233393030342228,$0A0D6E6568742029
   Data.q $3D20762074655309,$6F72502E676D4920,$2873656974726570,$2922333930303422,$0A0D65756C61562E
   Data.q $203D207320202020,$7475412220262073,$2022203D20726F68,$697274532E762026,$436276202620676E
   Data.q $646E450A0D664C72,$490A0D0A0D664920,$72502E676D492066,$736569747265706F,$287374736978452E
   Data.q $2922343930303422,$090A0D6E65687420,$203D207620746553,$706F72502E676D49,$2228736569747265
   Data.q $2E29223439303034,$200A0D65756C6156,$73203D2073202020,$7779654B22202620,$22203D207364726F
   Data.q $7274532E76202620,$6276202620676E69,$6E450A0D664C7243,$0A0D0A0D66492064,$502E676D49206649
   Data.q $6569747265706F72,$7374736978452E73,$2235393030342228,$0A0D6E6568742029,$3D20762074655309
   Data.q $6F72502E676D4920,$2873656974726570,$2922353930303422,$0A0D65756C61562E,$203D207320202020
   Data.q $6275532220262073,$22203D207463656A,$7274532E76202620,$6276202620676E69,$6E450A0D664C7243
   Data.q $0A0D0A0D66492064,$20262073203D2073,$2A2A2A2A2A2A2A22,$2A2A2A2A2A2A2A2A,$2A2A2A2A2A2A2A2A
   Data.q $2A2A2A2A2A2A2A2A,$2A2A2A2A2A2A2A2A,$2A2A2A2A2A2A2A2A,$2A2A2A2A2A2A2A2A,$0D222A2A2A2A2A2A
   Data.q $6F4267734D0A0D0A,$2020202020732078,$2020202020202020,$20202020
RASHADend:
EndDataSection

Egypt my love
BarryG
Addict
Addict
Posts: 3322
Joined: Thu Apr 18, 2019 8:17 am

Re: Tile,List,Small Icon & Thumbnails [Windows]

Post by BarryG »

RASHAD wrote:Not Fast as Windows file browser
Yeah, I tried it on a folder with 713 assorted files (jpg, txt, pdf, etc) and it takes 85 seconds to show them all. It can't be sped up somehow?
Mesa
Enthusiast
Enthusiast
Posts: 349
Joined: Fri Feb 24, 2012 10:19 am

Re: Tile,List,Small Icon & Thumbnails [Windows]

Post by Mesa »

For information, this soft: https://mark0.net/soft-trid-e.html seems to be verify efficient and usefull to guess a file type (win and linux).

M.
Mesa
Enthusiast
Enthusiast
Posts: 349
Joined: Fri Feb 24, 2012 10:19 am

Re: Tile,List,Small Icon & Thumbnails [Windows]

Post by Mesa »

I have added a guess file type function.


You can download the package here http://frazier.wood.free.fr/pb/Thumbnail.rar
or go to https://mark0.net/soft-trid-e.html
Download https://mark0.net/download/trid_w32.zip and unzip trid.exe in the same folder
Download https://mark0.net/download/triddefs.zip and unzip TrIDDefs.TRD in the same folder

Code: Select all

;https://www.purebasic.fr/english/viewtopic.php?f=12&t=76875

;ATTENTION
;To guess file types, you will need:
;Go to  here https://mark0.net/soft-trid-e.html
;Download https://mark0.net/download/trid_w32.zip and unzip trid.exe in the same folder
;Download https://mark0.net/download/triddefs.zip and unzip TrIDDefs.TRD in the same folder

;Make a right clic on a folder and guess type...
;Make a left clic on a thumnail and see what happen
;Make a right clic on a thumbnail and see what happen

UseTGAImageDecoder() 

#PBM_SETBARCOLOR = 1033 
#PBM_SETBKCOLOR  = 8193 

#TBS_TOOLTIPS = $100 

Global Image ,ext$ ,hwndPB,oldpos 
Global iSize,ILwnd 



iSize = 192 

Import "" 
	PB_Gadget_SendGadgetCommand(hWnd, EventType) 
EndImport 

Structure PPMColor 
	r.a 
	g.a 
	b.a 
EndStructure 

Structure PGMColor 
	c.a 
EndStructure 

Structure File 
	FileName$ 
	Image.l 
EndStructure 
Global NewList File.File() 

Macro _imgscale 
	hScale.f = iSize/bm\bmWidth 
	vScale.f = iSize/bm\bmHeight 
	If hScale > vScale 
		Scale.f = vScale*0.9 
	Else 
		Scale.f = hScale*0.9 
	EndIf 
	If Scale > 2 
		Scale = 2 
	EndIf 
	x = (iSize-Scale*bm\bmWidth)/2 
	y = (iSize-Scale*bm\bmHeight)/2 
EndMacro 

Procedure iItem(Gad,itText$,index) 
	it.LVITEM 
	it\Mask     = #LVIF_TEXT| #LVIF_IMAGE 
	it\iItem    = 0 
	it\iSubItem = 0 
	it\iImage = index 
	it\pszText  = @itText$ 
	SendMessage_(GadgetID(Gad), #LVM_INSERTITEM, 0, @it) 
EndProcedure 

Procedure LoadPPGM(Image,filename$) 
	Result = ReadFile(#PB_Any, filename$,#PB_Ascii) 
	If Result 
		If CreateImage(Image, 1, 1) 
			Format$ = ReadString(Result) 
			Dimensions$ = ReadString(Result) 
			If Val(StringField(Dimensions$, 1, " ")) = 0 
				Dimensions$ = ReadString(Result) 
			EndIf 
			w  = Val(StringField(Dimensions$, 1, " ")) 
			h  = Val(StringField(Dimensions$, 2, " ")) 
			If w = 0 Or h = 0 
				ProcedureReturn 1 
			EndIf 
			ResizeImage(Image, w, h) 
			StartDrawing(ImageOutput(Image)) 
			max = Val(ReadString(Result)) 
			Select Format$ 
				Case "P2" 
					Stringlen = Lof(Result) - Loc(Result) 
					content$  = Space(Stringlen*SizeOf(Character)+1) 
					Dim color.s(0) 
					ReadData(Result, @content$, Stringlen) 
					content$ = PeekS(@content$,-1,#PB_Ascii) 
					CreateRegularExpression(1, "\d+") 
					ExtractRegularExpression(1, content$, color()) 
					For y = 0 To h - 1 
						For x = 0 To w - 1 
							pos = (y*w + x)*1 
							Plot(x, y,RGB(Val(color(pos)),Val(color(pos)),Val(color(pos)))) 
						Next 
					Next 
					
				Case "P3" 
					Stringlen = Lof(Result) - Loc(Result) 
					content$  = Space(Stringlen*SizeOf(Character)+1) 
					Dim color.s(0) 
					ReadData(Result, @content$, Stringlen) 
					content$ = PeekS(@content$,-1,#PB_Ascii) 
					CreateRegularExpression(1, "\d+") 
					ExtractRegularExpression(1, content$, color()) 
					For y = 0 To h - 1 
						For x = 0 To w - 1 
							pos = (y*w + x)*3 
							r=Val(color(pos))*255 / max 
							g=Val(color(pos+1))*255 / max 
							b=Val(color(pos+2))*255 / max 
							Plot(x, y, RGB(r,g,b)) 
						Next 
					Next 
					
				Case "P5" 
					Bufferlen = Lof(Result) - Loc(Result) 
					*Buffer   = AllocateMemory(Bufferlen) 
					ReadData(Result, *Buffer, Bufferlen) 
					For y = 0 To h - 1 
						For x = 0 To w - 1 
							*gray.PGMColor = pos + *Buffer 
							Plot(x, y,RGB(*gray\c,*gray\c,*gray\c)) 
							pos + 1 
						Next 
					Next 
					
				Case "P6" 
					Bufferlen = Lof(Result) - Loc(Result) 
					*Buffer   = AllocateMemory(Bufferlen) 
					ReadData(Result, *Buffer, Bufferlen) 
					For y = 0 To h - 1 
						For x = 0 To w - 1 
							*color.PPMColor = pos + *Buffer 
							Plot(x, y, RGB(*color\r*255 / max, *color\g*255 / max, *color\b*255 / max)) 
							pos + 3 
						Next 
					Next 
			EndSelect 
			StopDrawing()      
			CloseFile(Result) 
			ProcedureReturn 1 
		EndIf 
	EndIf 
EndProcedure 

Procedure GetShellThumbnail(FileName$, Image, Width, Height, Depth = 24) 
	Protected Result = 0, ImageResult 
	Protected Desktop.IShellFolder, Folder.IShellFolder 
	Protected Extract.IExtractImage 
	Protected *pidlFolder.ITEMIDLIST, *pidlFile.ITEMIDLIST  
	Protected Priority, Flags, Bitmap = 0, size.SIZE 
	Protected DC, SourceDC, BitmapInfo.BITMAP 
	
	ext$ = GetExtensionPart(FileName$) 
	
	If SHGetDesktopFolder_(@Desktop) >= 0 
		If Desktop\ParseDisplayName(#Null, #Null, GetPathPart(FileName$), #Null, @*pidlFolder, #Null) = #S_OK 
			If Desktop\BindToObject(*pidlFolder, #Null, ?IID_IShellFolder, @Folder) = #S_OK 
				If Folder\ParseDisplayName(#Null, #Null, GetFilePart(FileName$) , #Null, @*pidlFile, #Null) = #S_OK 
					If Folder\GetUIObjectOf(#Null, 1, @*pidlFile, ?IID_IExtractImage, 0, @Extract) = #S_OK 
						
						ImageResult = CreateImage(Image, Width, Height, depth,$FFFFFF) 
						If ImageResult 
							If Image = #PB_Any 
								Image = ImageResult 
							EndIf    
							
							size\cx = Width 
							size\cy = Height 
							
							If Extract\GetLocation(Space(#MAX_PATH), #MAX_PATH, @Priority, @size, Depth, @Flags) >= 0                
								If Extract\Extract(@Bitmap) >= 0 And Bitmap 
									
									DC = StartDrawing(ImageOutput(Image)) 
									If DC 
										SourceDC = CreateCompatibleDC_(DC) 
										If SourceDC 
											GetObject_(Bitmap, SizeOf(BITMAP), @BitmapInfo) 
											SelectObject_(SourceDC, Bitmap) 
											BitBlt_(DC, iSize/2-BitmapInfo\bmWidth/2,iSize/2-BitmapInfo\bmHeight/2, BitmapInfo\bmWidth, BitmapInfo\bmHeight, SourceDC, 0, 0, #SRCCOPY) 
											DeleteDC_(SourceDC) 
										EndIf 
										;DrawingMode(#PB_2DDrawing_Transparent) 
										; ;DrawText(10,10,GetExtensionPart(File()\FileName$),#Green) 
										StopDrawing()                    
										Result = ImageResult 
									EndIf 
									
									DeleteObject_(Bitmap) 
								EndIf 
							EndIf                
							Extract\Release() 
						EndIf 
						
						If Result = 0 
							FreeImage(Image) 
						EndIf            
					EndIf 
					
					CoTaskMemFree_(*pidlFile) 
				EndIf                        
				Folder\Release() 
			EndIf      
			CoTaskMemFree_(*pidlFolder)      
		EndIf    
		Desktop\Release() 
	EndIf 
	
	ProcedureReturn Result  
EndProcedure 



Procedure ProcessMe()
	If ILwnd 
		ImageList_Destroy_(ILwnd) 
	EndIf 
	ClearList(File()) 
	ClearGadgetItems(0) 
	SetGadgetText(20 , "" ) 
	SendMessage_(hwndPB, #PBM_SETPOS, 0,0) 
	img = CreateImage(#PB_Any,iSize,iSize,32,$FFFFFF) 
	ILwnd = ImageList_Create_(iSize,iSize,#ILC_COLOR32|#ILC_MASK, 0, 300) 
	SendMessage_(GadgetID(0),#LVM_SETIMAGELIST,#LVSIL_NORMAL,ILwnd) 
	Count = 0 
	If GetGadgetText(1) <> "" And ExamineDirectory(0, GetGadgetText(1), "*.*") 
		
		While NextDirectoryEntry(0) 
			
			If DirectoryEntryType(0) = #PB_DirectoryEntry_File 
				;Check month and year
				DateFc = DirectoryEntryDate(0, #PB_Date_Created) 
				DateFa = DirectoryEntryDate(0, #PB_Date_Accessed)
				DateFm = DirectoryEntryDate(0, #PB_Date_Modified)
				
				year=Val(GetGadgetText(50))
				yearok=0
				If year
					If year=Year(DateFc)
						yearok=1
					Else
						yearok=-1
					EndIf
				EndIf
				go=0
				If Bool(yearok=1 Or yearok=0)
					month=Month(Year(DateFc))
					If GetGadgetState(month+30)=#PB_Checkbox_Checked  
						go=1
					EndIf
					month=Month(Year(DateFa))
					If GetGadgetState(month+30)=#PB_Checkbox_Checked  
						go=1
					EndIf
					month=Month(Year(DateFm))
					If GetGadgetState(month+30)=#PB_Checkbox_Checked  
						go=1
					EndIf
				EndIf
				
				If go=1
					AddElement(File()) 	
					File()\FileName$ = GetGadgetText(1) + DirectoryEntryName(0)
					Count + 1 
				EndIf		
			EndIf 
		Wend 
	EndIf 
	If Count > 0 
		SendMessage_(hwndPB, #PBM_SETPOS, 100, 0) 
		While WindowEvent(): Wend 
		
		ForEach File() 
			index = ListIndex(File()) 
			File()\Image = GetShellThumbnail(File()\FileName$, #PB_Any, iSize, iSize) 
			If File()\Image = 0 
				File()\Image = CreateImage(#PB_Any, iSize, iSize) 
				If File()\Image And StartDrawing(ImageOutput(File()\Image)) 
					Box(0, 0, iSize, iSize, $FFFFFF) 
					If SHGetFileInfo_(@File()\FileName$, 0, @info.SHFILEINFO, SizeOf(SHFILEINFO), #SHGFI_ICON|#SHGFI_LARGEICON) 
						DrawImage(info\hIcon, iSize/2-16, iSize/2-16) 
						DestroyIcon_(info\hIcon) 
					EndIf                
					StopDrawing() 
				EndIf 
				If ext$ = "ppm" Or ext$ = "pgm" 
					If LoadPPGM(Image, File()\FileName$) 
						img = ImageID(image)                        
						GetObject_(img, SizeOf(BITMAP), @bm.BITMAP) 
						_imgscale 
						imgh = CopyImage_(img,#IMAGE_BITMAP,bm\bmWidth*Scale,bm\bmHeight*Scale,#LR_COLOR) 
						File()\Image = CreateImage(#PB_Any, iSize, iSize) 
						If File()\Image And StartDrawing(ImageOutput(File()\Image)) 
							Box(0,0,iSize,iSize,$FFFFFF) 
							DrawImage(imgh,x,y)                    
						EndIf                        
						StopDrawing() 
						FreeImage(Image) 
						DeleteObject_(img) 
					EndIf 
				EndIf 
				If ext$ = "tga" 
					If LoadImage(Image, File()\FileName$) 
						img = ImageID(image) 
						GetObject_(img, SizeOf(BITMAP), @bm.BITMAP) 
						_imgscale 
						imgh = CopyImage_(img,#IMAGE_BITMAP,bm\bmWidth*Scale,bm\bmHeight*Scale,#LR_COLOR) 
						File()\Image = CreateImage(#PB_Any, iSize, iSize) 
						If File()\Image And StartDrawing(ImageOutput(File()\Image)) 
							Box(0,0,iSize,iSize,$FFFFFF) 
							DrawImage(imgh,x,y)                    
						EndIf 
						StopDrawing() 
						FreeImage(Image) 
						DeleteObject_(img) 
					EndIf 
				EndIf 
			EndIf            
			
			item = ImageList_Add_(ILwnd,ImageID(File()\Image),0) 
			iItem(0, GetFilePart(File()\FileName$) ,item) 
			
			SendMessage_(hwndPB, #PBM_SETPOS, (index * 100)/Count,0) 
			While WindowEvent(): Wend 
		Next File() 
		SendMessage_(hwndPB, #PBM_SETPOS, 100,0) 
	EndIf 	
EndProcedure
Procedure GuessFileType(path$)
	
	; yes=MessageRequester("Attention", "Do you want to guess file type",#PB_MessageRequester_YesNo)
	;	If yes=#PB_MessageRequester_Yes
	
	If FileSize(GetCurrentDirectory()+"trid.exe")>0
		If FileSize(GetCurrentDirectory()+"triddefs.trd")>0
			path$=Chr(34)+path$+"*.*"+Chr(34)+" -ae" 
			running=RunProgram(GetCurrentDirectory()+"trid.exe",path$,GetCurrentDirectory(),#PB_Program_Hide|#PB_Program_Open)
			
			If running 
				While ProgramRunning(running)
					If GetGadgetText(20)="Running"
					Else
						SetGadgetText(20,"Running")
					EndIf
				Wend
				CloseProgram(running) 
				SetGadgetText(20,"Done")
			EndIf
			;PB_Gadget_SendGadgetCommand(GadgetID(1), #PB_EventType_LeftClick); doesn't work !
			;PostEvent(#PB_Event_Gadget,0, 1, #PB_EventType_LeftClick); doesn't work !
			ProcessMe()
		EndIf
	EndIf
	; EndIf
	
EndProcedure
Procedure Resizewindow_EX() 
	dy=50
	ResizeGadget(5,#PB_Ignore,#PB_Ignore,WindowWidth(0)-40,WindowHeight(0)-55-dy);ContainerGadget 
	ResizeGadget(2,#PB_Ignore,#PB_Ignore,WindowWidth(0) - 52,WindowHeight(0)-68-dy);SplitterGadget 
	MoveWindow_(hwndPB,WindowWidth(0)-24,10,18,WindowHeight(0)-55,1)							 ;progress 
	ResizeGadget(10,10,WindowHeight(0) - 34-dy,160,20)														 ;TrackBarGadget 
	ResizeGadget(15,180,WindowHeight(0) - 34-dy,40,20)														 ;TextGadget 
	ResizeGadget(20,230,WindowHeight(0) - 34-dy,GadgetWidth(5)-220,20)						 ;TextGadget 
	ResizeGadget(30,10,WindowHeight(0) - 04-dy,WindowWidth(0)-40,#PB_Ignore)			 ;FrameGadget
	For i=0 To 11
		ResizeGadget(i+31,#PB_Ignore,WindowHeight(0) + 14-dy,#PB_Ignore,#PB_Ignore);CheckBoxGadget
	Next i
	ResizeGadget(50,#PB_Ignore,WindowHeight(0) + 14-dy,#PB_Ignore,#PB_Ignore);StringGadget
EndProcedure 

; 	Global NewList File.File() 

LoadFont(0,"Consolas",14) 

Flag = #PB_Window_SystemMenu|#PB_Window_ScreenCentered| #PB_Window_MinimizeGadget | #PB_Window_MaximizeGadget| #PB_Window_SizeGadget 
If OpenWindow(0,0,0,820,700,"Thumbnails Viewer",Flag) 
	SmartWindowRefresh(0,1) 
	WindowBounds(0,600,400,#PB_Default,#PB_Default) 
	
	ContainerGadget(5,10,10,780,545,#PB_Container_Flat) 
	SetGadgetColor(5,#PB_Gadget_BackColor,0) 
	ListIconGadget(0,0,0,0,0,"",0,#PB_ListIcon_MultiSelect|#LVS_AUTOARRANGE |#WS_DLGFRAME ) 
	SetWindowLongPtr_(GadgetID(0),#GWL_STYLE,GetWindowLongPtr_(GadgetID(0),#GWL_STYLE)|#WS_CLIPCHILDREN) 
	SetGadgetAttribute(0,#PB_ListIcon_DisplayMode,#PB_ListIcon_LargeIcon) 
	SendMessage_(GadgetID(0), #LVM_SETEXTENDEDLISTVIEWSTYLE,0,SendMessage_(GadgetID(0),#LVM_GETEXTENDEDLISTVIEWSTYLE,0,0)| #LVS_EX_TRACKSELECT) 
	SendMessage_(GadgetID(0), #LVM_SETHOVERTIME, 0, 10) 
	ExplorerTreeGadget(1, 0,0,0,0, "*",#PB_Explorer_NoFiles|#PB_Explorer_NoDriveRequester) 
	SetGadgetFont(1,FontID(0)) 
	SetGadgetColor(1,#PB_Gadget_BackColor,$DBDBDB) 
	SplitterGadget(2, 5, 5, 768, 532, 1,0, #PB_Splitter_Vertical|#PB_Splitter_Separator) 
	SetGadgetState(2,250) 
	CloseGadgetList() 
	hwndPB = CreateWindowEx_(0, "msctls_progress32", 0, #WS_CHILD|#WS_VISIBLE|#PBS_VERTICAL , WindowWidth(0)-24,10,18,545, WindowID(0), 0, 0, 0) 
	SetWindowTheme_(hwndPB, "", "") 
	SendMessage_(hwndPB, #PBM_SETBKCOLOR ,0,$DBDBDB) 
	SendMessage_(hwndPB, #PBM_SETBARCOLOR ,0,$FDAE4D) 
	
	TrackBarGadget(10,10,568,160,24,1,12,#TBS_AUTOTICKS | #TBS_ENABLESELRANGE|#TBS_TOOLTIPS |#TBS_FIXEDLENGTH) 
	SendMessage_(GadgetID(10), #TBM_SETTHUMBLENGTH,16,0) 
	SendMessage_(GadgetID(10), #TBM_SETPAGESIZE ,0,1) 
	
	SetGadgetState(10,12) 
	
	TextGadget(15,180,568,40,20,"192",#WS_BORDER | #SS_CENTER|#SS_CENTERIMAGE) 
	SetGadgetColor(15,#PB_Gadget_BackColor,$FFFFFF) 
	
	TextGadget(20,230,568,582,20,"",#WS_BORDER | #SS_CENTER | #SS_CENTERIMAGE) 
	SetGadgetFont(20,FontID(0)) 
	SetGadgetColor(20,#PB_Gadget_BackColor,$FFFFFF) 
	SetGadgetColor(20,#PB_Gadget_FrontColor,$0000FF) 
	
	;SetGadgetText(1, "C:\")
	
	
	FrameGadget(30,10,598,802,50,"Select Month")
	For i=0 To 11
		x=20+i*35
		CheckBoxGadget(i+31,x,610,30,25,Str(i+1))
		SetGadgetState(i+31,#True)
		
	Next i
	x=20+i*35
	StringGadget(50,x,610,60,25,"",#PB_String_Numeric)
	SendMessage_(GadgetID(50),#EM_SETCUEBANNER,#True,@" All years")
	GadgetToolTip(50, "Select year")
	
	
	If CreatePopupMenu(0)       
		MenuTitle("Guess File Types")        
		MenuItem(1, "Process all files")    
		MenuBar()	
	EndIf
	
	If CreatePopupMenu(1)       
		
		MenuItem(2, "Delete")    
		MenuBar()	
	EndIf
	BindEvent(#PB_Event_SizeWindow,@Resizewindow_EX()) 
	
	
	;- loop
	Repeat 
		Select WaitWindowEvent() 
			Case #PB_Event_CloseWindow 
				Quit = 1 
				
			Case #WM_MOUSEMOVE 
				If GetActiveGadget() = 10 
					If GetGadgetState(10) >= 4 
						iSize = GetGadgetState(10)*16 
						SetGadgetText(15,Str(iSize)) 
					Else 
						SetGadgetText(15,Str(GetGadgetState(10))) 
					EndIf 
				EndIf 
				
			Case #WM_LBUTTONUP 
				If GetActiveGadget() = 10 
					Select GetGadgetState(10) 
						Case 1 
							SetGadgetAttribute(0,#PB_ListIcon_DisplayMode,#PB_ListIcon_List) 
						Case 2 
							SetGadgetAttribute(0,#PB_ListIcon_DisplayMode,#PB_ListIcon_SmallIcon) 
						Case 3 
							iSize = 16 
							SetGadgetText(15,Str(iSize)) 
							SetGadgetAttribute(0,#PB_ListIcon_DisplayMode,#PB_ListIcon_LargeIcon) 
						Case 4 To 12          
							iSize = GetGadgetState(10)*16 
							SetGadgetText(15,Str(iSize)) 
							SetGadgetAttribute(0,#PB_ListIcon_DisplayMode,#PB_ListIcon_LargeIcon) 
					EndSelect 
					PB_Gadget_SendGadgetCommand(GadgetID(1), #PB_EventType_LeftClick) 
				EndIf 
				
			Case #PB_Event_Gadget 
				Select EventGadget() 
					Case 0 ;listicon
						Select EventType() 
							Case #PB_EventType_Change 
								result = SelectElement(File(),ListSize(File()) - GetGadgetState(0) - 1) 
								If result > 0 
									SetGadgetText(20 , File()\FileName$) 
								EndIf
							Case #PB_EventType_LeftClick
								; 								  Debug "ok";File()\FileName$
								RunProgram(File()\FileName$)
							Case #PB_EventType_RightClick
								result = SelectElement(File(),ListSize(File()) - GetGadgetState(0) - 1) 
								If result > 0 
									SetGadgetText(20 , File()\FileName$) 
								EndIf
								DisplayPopupMenu(1,WindowID(0))
								; 								
						EndSelect 
						
						
					Case 1 ;explorertree
						Select EventType() 
							Case	#PB_EventType_RightClick 
								DisplayPopupMenu(0,WindowID(0)) 
								
							Case #PB_EventType_LeftClick 
								ProcessMe()
								;{
								; 							 If ILwnd 
								; 								ImageList_Destroy_(ILwnd) 
								; 								EndIf 
								; 								ClearList(File()) 
								; 								ClearGadgetItems(0) 
								; 								SetGadgetText(20 , "" ) 
								; 								SendMessage_(hwndPB, #PBM_SETPOS, 0,0) 
								; 								img = CreateImage(#PB_Any,iSize,iSize,32,$FFFFFF) 
								; 								ILwnd = ImageList_Create_(iSize,iSize,#ILC_COLOR32|#ILC_MASK, 0, 300) 
								; 								SendMessage_(GadgetID(0),#LVM_SETIMAGELIST,#LVSIL_NORMAL,ILwnd) 
								; 								Count = 0 
								; 								If GetGadgetText(1) <> "" And ExamineDirectory(0, GetGadgetText(1), "*.*") 
								; 									
								; 									
								; 									While NextDirectoryEntry(0) 
								; 										If DirectoryEntryType(0) = #PB_DirectoryEntry_File 
								; 											AddElement(File()) 
								; 											File()\FileName$ = GetGadgetText(1) + DirectoryEntryName(0)
								; 											Count + 1 
								; 										EndIf 
								; 									Wend 
								; 								EndIf 
								; 								If Count > 0 
								; 									SendMessage_(hwndPB, #PBM_SETPOS, 100, 0) 
								; 									While WindowEvent(): Wend 
								; 									
								; 									ForEach File() 
								; 										index = ListIndex(File()) 
								; 										File()\Image = GetShellThumbnail(File()\FileName$, #PB_Any, iSize, iSize) 
								; 										If File()\Image = 0 
								; 											File()\Image = CreateImage(#PB_Any, iSize, iSize) 
								; 											If File()\Image And StartDrawing(ImageOutput(File()\Image)) 
								; 												Box(0, 0, iSize, iSize, $FFFFFF) 
								; 												If SHGetFileInfo_(@File()\FileName$, 0, @info.SHFILEINFO, SizeOf(SHFILEINFO), #SHGFI_ICON|#SHGFI_LARGEICON) 
								; 													DrawImage(info\hIcon, iSize/2-16, iSize/2-16) 
								; 													DestroyIcon_(info\hIcon) 
								; 												EndIf                
								; 												StopDrawing() 
								; 											EndIf 
								; 											If ext$ = "ppm" Or ext$ = "pgm" 
								; 												If LoadPPGM(Image, File()\FileName$) 
								; 													img = ImageID(image)                        
								; 													GetObject_(img, SizeOf(BITMAP), @bm.BITMAP) 
								; 													_imgscale 
								; 													imgh = CopyImage_(img,#IMAGE_BITMAP,bm\bmWidth*Scale,bm\bmHeight*Scale,#LR_COLOR) 
								; 													File()\Image = CreateImage(#PB_Any, iSize, iSize) 
								; 													If File()\Image And StartDrawing(ImageOutput(File()\Image)) 
								; 														Box(0,0,iSize,iSize,$FFFFFF) 
								; 														DrawImage(imgh,x,y)                    
								; 													EndIf                        
								; 													StopDrawing() 
								; 													FreeImage(Image) 
								; 													DeleteObject_(img) 
								; 												EndIf 
								; 											EndIf 
								; 											If ext$ = "tga" 
								; 												If LoadImage(Image, File()\FileName$) 
								; 													img = ImageID(image) 
								; 													GetObject_(img, SizeOf(BITMAP), @bm.BITMAP) 
								; 													_imgscale 
								; 													imgh = CopyImage_(img,#IMAGE_BITMAP,bm\bmWidth*Scale,bm\bmHeight*Scale,#LR_COLOR) 
								; 													File()\Image = CreateImage(#PB_Any, iSize, iSize) 
								; 													If File()\Image And StartDrawing(ImageOutput(File()\Image)) 
								; 														Box(0,0,iSize,iSize,$FFFFFF) 
								; 														DrawImage(imgh,x,y)                    
								; 													EndIf 
								; 													StopDrawing() 
								; 													FreeImage(Image) 
								; 													DeleteObject_(img) 
								; 												EndIf 
								; 											EndIf 
								; 										EndIf            
								; 										
								; 										item = ImageList_Add_(ILwnd,ImageID(File()\Image),0) 
								; 										iItem(0, GetFilePart(File()\FileName$) ,item) 
								; 										
								; 										SendMessage_(hwndPB, #PBM_SETPOS, (index * 100)/Count,0) 
								; 										While WindowEvent(): Wend 
								; 									Next File() 
								; 									SendMessage_(hwndPB, #PBM_SETPOS, 100,0) 
								; 								EndIf 
								;}
						EndSelect            
				EndSelect 
				
			Case #PB_Event_Menu      
				Select EventMenu()     
					Case 1 
						GuessFileType(GetGadgetText(1))
						
					Case 2 ;delete
						re=MessageRequester("Attention","Delete "+GetGadgetText(20)+" ?",#PB_MessageRequester_YesNo|#PB_MessageRequester_Warning)
						If re=#PB_MessageRequester_Yes    
							DeleteFile(GetGadgetText(20),#PB_FileSystem_Force)
							
						EndIf
						
				EndSelect
				
				
		EndSelect    
	Until Quit = 1 
EndIf 
End 

DataSection 
	IID_IShellFolder: ; {000214E6-0000-0000-C000-000000000046} 
	Data.l $000214E6 
	Data.w $0000, $0000 
	Data.b $C0, $00, $00, $00, $00, $00, $00, $46 
	
	IID_IExtractImage: ; {BB2E617C-0920-11D1-9A0B-00C04FC2D6C1} 
	Data.l $BB2E617C 
	Data.w $0920, $11D1 
	Data.b $9A, $0B, $00, $C0, $4F, $C2, $D6, $C1 
EndDataSection 
Make a right clic on a folder and guess type...
Make a left clic on a thumnail and see what happen
Make a right clic on a thumbnail and see what happen

M.
RASHAD
PureBasic Expert
PureBasic Expert
Posts: 4663
Joined: Sun Apr 12, 2009 6:27 am

Re: Tile,List,Small Icon & Thumbnails [Windows]

Post by RASHAD »

Nice work Mesa
I did not test it thoroughly yet but apparently it is very nice
Egypt my love
Post Reply