Build-in support for more images formats

Got an idea for enhancing PureBasic? New command(s) you'd like to see?
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by David.

Would be nice if we could load PNG images (a build-in stuff, with no addicional DLLs, etc), wich i think is better then JPG and smaller then BMP files.

I used to use them a lot in Blitz Basic.

Cya,
David.
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by TheBeck.

PNG would be a great format to support, however, PNG is a very complex file format and it would take considerable resources to implement in a PureBasic library. I bet it will done by someone eventually, but I don't see it getting added anytime soon. Maybe you or someone else would like to write a library for PNG and submit it to Fred?
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by David.

" Maybe you or someone else would like to write a library for PNG and submit it to Fred?"
I can't even use GadGets yet.. hehehe Maybe in a far away future :wink:

Cya.
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by dmoc.

Got a reference to the PNG file format?
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by El_Choni.

You can use PNGlib: [url]http://www.MadWizard.org" target="_blank[/url]

Or wait until I finish a little PB project that uses gdiplus.dll (available in XP and downloadable for Windows 9X/Me/2k), which should allow to view/convert between PNG, TIFF, JPEG, BMP and GIF, among others. But documentation about Gdiplus is awful, it's being a bit hard to get it working.
Got a reference to the PNG file format?
http://www.wotsit.org

Bye,

El_Choni
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by Danilo.

But GDI+ isnt for Win95 if i remember correctly.

cya,
...Danilo
(registered PureBasic user)
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by El_Choni.

No, Win98 and up, AFAIK. But it's better than nothing, isn't it? :wink:

El_Choni
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by fred.

Yep :).

Fred - AlphaSND
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by El_Choni.

Here's some work in progress. If you don't have Windows XP, you'll need to copy gdiplus.dll into your system directory (it doesn't work in Windows 95). Download it from here (I hope the link is still valid, anyway, it's not difficult to find in http://www.microsoft.com):

http://download.microsoft.com/download/ ... s_dnld.exe

Currently converts to/from TIFF, PNG, BMP, GIF, JPG and some others but it's not finished yet. I'd like to read your comments, since I'll be away for two weeks and won't be able to work further in this or other projects until then:

Code: Select all

Procedure Error(message.s, fatal.b)
  ErrorBuffer$ = Space(256)
  FormatMessage_(#FORMAT_MESSAGE_FROM_SYSTEM, 0, GetLastError_(), 0, ErrorBuffer$, Len(ErrorBuffer$), 0)
  MessageRequester("Error", message+Chr(10)+Chr(10)+ErrorBuffer$, 0)
  If fatal
    End
  EndIf
EndProcedure

Global MDIFrame, MDIClient, hChild, hStatus, hToolBar, hMainMenu, hChildMenu, ActiveMDIChild
Global xPos, yPos, WindowWidth, WindowHeight, ChildNumber, ExtraWidth, ExtraHeight
Global hInstance
Global wc.WNDCLASSEX
Global ClassName$

#Open = 0
#SaveAs = #Open+1
#Close = #SaveAs+1
#CloseAll = #Close+1
#Quit = #CloseAll+1
#TileHorizontal = #Quit+1
#TileVertical = #TileHorizontal+1
#Cascade = #TileVertical+1
#Arrange = #Cascade+1
#Next = #Arrange+1
#About = #Next+1

Dim MenuCommands$(#About+1)

tab.s = Chr(9)

MenuCommands$(#Open) = "&Open"+tab+"Ctl+O"
MenuCommands$(#SaveAs) = "Save as..."+tab+"Ctl+S"
MenuCommands$(#Close) = "Close"+tab+"Ctl+W"
MenuCommands$(#CloseAll) = "Close all"+tab+"Ctl+Shift+W"
MenuCommands$(#Quit) = "&Quit"+tab+"Ctl+Q"
MenuCommands$(#TileHorizontal) = "Tile horizontal"
MenuCommands$(#TileVertical) = "Tile vertical"
MenuCommands$(#Cascade) = "Cascade"
MenuCommands$(#Arrange) = "Arrange"
MenuCommands$(#Next) = "Next window"
MenuCommands$(#About) = "About"+tab+"F1"

Dim Tooltips$(#About+1)
For i=0 To #About
  Tooltips$(i) = ReplaceString(MenuCommands$(i), "&", "")
  ReplaceString(Tooltips$(i), Chr(9), " ", 2)
Next i

#IDM_BASE = 10000
#IDM_FILE_OPEN = #IDM_BASE+#Open
#IDM_FILE_SAVEAS = #IDM_BASE+#SaveAs
#IDM_FILE_CLOSE = #IDM_BASE+#Close
#IDM_FILE_CLOSEALL = #IDM_BASE+#CloseAll
#IDM_FILE_QUIT = #IDM_BASE+#Quit
#IDM_WINDOW_TILEHORIZONTAL = #IDM_BASE+#TileHorizontal
#IDM_WINDOW_TILEVERTICAL = #IDM_BASE+#TileVertical
#IDM_WINDOW_CASCADE = #IDM_BASE+#Cascade
#IDM_WINDOW_ARRANGE = #IDM_BASE+#Arrange
#IDM_WINDOW_NEXT = #IDM_BASE+#Next
#IDM_HELP_ABOUT = #IDM_BASE+#About

#IDM_FIRSTCHILD = 20000

Structure GdiplusStartupInput
  GdiplusVersion.l
  DebugEventCallback.l
  SuppressBackgroundThread.l
  SuppressExternalCodecs.l
EndStructure

Structure CLSID
  Data1.l
  Data2.w
  Data3.w
  Data4.b:)
EndStructure

Structure GUID
  Data1.l
  Data2.w
  Data3.w
  Data4.b:)
EndStructure

Structure ImageItemInfo
  Clsid.CLSID         ; Codec identifier. 
  FormatID.GUID       ; File format identifier. GUIDs that identify various file formats (ImageFormatBMP, ImageFormatEMF, And the like) are defined in Gdiplusimaging.h. 
  CodecName.s         ; WCHAR * Pointer To a null-terminated string that contains the codec name. 
  DllName.s           ; WCHAR * Pointer To a null-terminated string that contains the path name of the DLL in which the codec resides. If the codec is not in a DLL, this pointer is NULL. 
  FormatDescription.s ; WCHAR * Pointer To a null-terminated string that contains the name of the file format used by the codec. 
  FilenameExtension.s ; WCHAR * Pointer To a null-terminated string that contains all file-name extensions associated with the codec. The extensions are separated by semicolons. 
  MimeType.s          ; WCHAR * Pointer To a null-terminated string that contains the mime type of the codec. 
  Flags.l             ; DWORD  Combination of flags from the ImageCodecFlags enumeration. 
  Version.l           ; DWORD  Integer that indicates the version of the codec. 
  SigCount.l          ; DWORD  Integer that indicates the number of signatures used by the file format associated with the codec. 
  SigSize.l           ; DWORD  Integer that indicates the number of bytes in each signature. 
  SigPattern.l        ; BYTE * Pointer To an array of bytes that contains the pattern For each signature. 
  SigMask.l           ; BYTE * Pointer To an array of bytes that contains the mask For each signature. 
EndStructure

Global Gdiplus, gdiplusToken, bitmap, hbmReturn, encoderClsid
Global *GdiplusStartup, *GdipCreateBitmapFromFile, *GdipCreateHBITMAPFromBitmap, *GdipGetImageHeight, *GdipGetImageWidth, *GdiplusShutdown, *GdipSaveImageToFile, *GdipGetImageEncodersSize, *GdipGetImageEncoders, *GdipCreateBitmapFromHBITMAP

Procedure GetEncoderClsid(Format$, *Clsid)
  num = 0
  size = 0
  FormatWSize = (Len(Format$)*2)+2
  *FormatW = CoTaskMemAlloc_(FileWSize)
  If *FormatW
    If MultiByteToWideChar_(#CP_ACP, 0, Format$, -1, *FormatW, FormatWSize)
      FormatW$ = PeekS(*FormatW)
      CallFunctionFast(*GdipGetImageEncodersSize, @num, @size)
      If size=0
        result = -1
      Else
        *ImageCodecInfoArray = CoTaskMemAlloc_(size)
        If *ImageCodecInfoArray=#NULL
          result = -1
        Else
          CallFunctionFast(*GdipGetImageEncoders, num, size, *ImageCodecInfoArray)
          For j=0 To num
            *pImageCodecInfo.ImageItemInfo = *ImageCodecInfoArray+(SizeOf(ImageItemInfo)*j)
            If PeekS(*pImageCodecInfo\MimeType)=FormatW$
              PokeL(*Clsid, *pImageCodecInfo\Clsid)
              result = j
            EndIf
          Next j
        EndIf
        CoTaskMemFree_(*pImageCodecInfo)
      EndIf
    EndIf
  EndIf
  CoTaskMemFree_(*FormatW)
  ProcedureReturn result
EndProcedure

Procedure CloseEnumProc(hWnd, lParam)
  If GetWindow_(hWnd, #GW_OWNER)
    ProcedureReturn #TRUE
  EndIf
  GetParent_(hWnd)
  SendMessage_(GetParent_(hWnd), #WM_MDIRESTORE, hWnd, 0)
  If SendMessage_(hWnd, #WM_QUERYENDSESSION, 0, 0)=0
    ProcedureReturn #TRUE
  EndIf
  SendMessage_(GetParent_(hWnd), #WM_MDIDESTROY, hWnd, 0)
  ProcedureReturn #TRUE
  SubMenu = GetSubMenu_(hMainMenu, 0)
  If SubMenu
    SendMessage_(MDIClient, #WM_MDISETMENU, hMainMenu, SubMenu)
  EndIf
  DrawMenuBar_(MDIFrame)
EndProcedure

Procedure MDIChildProc(hWnd, uMsg, wParam, lParam)
  result = 0
  Select uMsg
    Case #WM_CREATE
      If ChildNumber=0
        FreeToolBar(0)
        hToolBar = CreateToolBar(1, MDIFrame)
        If hToolBar
          ToolBarStandardButton(#IDM_FILE_OPEN, #PB_ToolBarIcon_Open)
          ToolBarToolTip(#IDM_FILE_OPEN, Tooltips$(#Open)) 
          ToolBarStandardButton(#IDM_FILE_SAVEAS, #PB_ToolBarIcon_Save)
          ToolBarToolTip(#IDM_FILE_SAVEAS, Tooltips$(#SaveAs)) 
          ToolBarStandardButton(#IDM_FILE_CLOSE, #PB_ToolBarIcon_Delete)
          ToolBarToolTip(#IDM_FILE_CLOSE, Tooltips$(#Close)) 
          ToolBarStandardButton(#IDM_HELP_ABOUT, #PB_ToolBarIcon_Help)
          ToolBarToolTip(#IDM_HELP_ABOUT, Tooltips$(#About))
        Else
          Error("Can't create child toolbar.", 1)
        EndIf
        hChildMenu = CreateMenu(1, MDIFrame)
        If hChildMenu
          MenuTitle("&File")
            MenuItem(#IDM_FILE_OPEN, MenuCommands$(#Open))
            MenuItem(#IDM_FILE_SAVEAS, MenuCommands$(#SaveAs))
            MenuItem(#IDM_FILE_CLOSE, MenuCommands$(#Close))
            MenuItem(#IDM_FILE_CLOSEALL, MenuCommands$(#CloseAll))
            MenuBar()
            MenuItem(#IDM_FILE_QUIT, MenuCommands$(#Quit))
          MenuTitle("&Windows")
            MenuItem(#IDM_WINDOW_TILEHORIZONTAL, MenuCommands$(#TileHorizontal))
            MenuItem(#IDM_WINDOW_TILEVERTICAL, MenuCommands$(#TileVertical))
            MenuItem(#IDM_WINDOW_CASCADE, MenuCommands$(#Cascade))
            MenuItem(#IDM_WINDOW_ARRANGE, MenuCommands$(#Arrange))
            MenuItem(#IDM_WINDOW_NEXT, MenuCommands$(#Next))
          MenuTitle("&Help")
            MenuItem(#IDM_HELP_ABOUT, MenuCommands$(#About))
        Else
          Error("Can't create child menu.", 1)
        EndIf
      EndIf
      If CreateGadgetList(hWnd)
        width.l
        height.l
        CallFunctionFast(*GdipGetImageWidth, bitmap, @width)
        CallFunctionFast(*GdipGetImageHeight, bitmap, @height)
        MoveWindow_(hWnd, #CW_USEDEFAULT, #CW_USEDEFAULT, width+ExtraWidth, height+ExtraHeight, 1)
        GetWindowRect_(hWnd, rc.RECT)
        ImageGadget(ChildNumber, ((rc\right-rc\left)-width)/2, 0, width, height, hbmReturn)
        SetWindowLong_(hWnd, #GWL_USERDATA, ChildNumber)
      Else
        Error("GadgetList() failed in MDI child window number "+Str(ChildNumber), 1)
      EndIf
      result = DefMDIChildProc_(hWnd, uMsg, wParam, lParam)
    Case #WM_GETMINMAXINFO
      If GetWindow_(hWnd, #GW_CHILD)
        ChildIndex = GetWindowLong_(hWnd, #GWL_USERDATA)
        *mmi.MINMAXINFO = lParam
        *mmi\ptMinTrackSize\x = GadgetWidth(ChildIndex)+ExtraWidth
        *mmi\ptMinTrackSize\y = GadgetHeight(ChildIndex)+ExtraHeight
      EndIf
      result = DefMDIChildProc_(hWnd, uMsg, wParam, lParam)
    Case #WM_SIZE
      If GetWindow_(hWnd, #GW_CHILD)
        ChildWidth = lParam&$ffff
        ChildHeight = lParam>>16
        ChildIndex = GetWindowLong_(hWnd, #GWL_USERDATA)
        ResizeGadget(ChildIndex, (ChildWidth-GadgetWidth(ChildIndex))/2, (ChildHeight-GadgetHeight(ChildIndex))/2, -1, -1)
      EndIf
      result = DefMDIChildProc_(hWnd, uMsg, wParam, lParam)
    Case #WM_KEYDOWN
      Select wParam
        Case #VK_F6
          SendMessage_(MDIClient, #WM_MDINEXT, #NULL, 0)
      EndSelect
      result = DefMDIChildProc_(hWnd, uMsg, wParam, lParam)
    Case #WM_MDIACTIVATE
      If lParam=hWnd
        ActiveMDIChild = hWnd
        If ChildNumber=0
          SubMenu = GetSubMenu_(hChildMenu, 1)
          If SubMenu
            SendMessage_(MDIClient, #WM_MDISETMENU, hChildMenu, SubMenu)
          EndIf
        EndIf
        DrawMenuBar_(MDIFrame)
      Else
        result = DefMDIChildProc_(hWnd, uMsg, wParam, lParam)
      EndIf
    Case #WM_QUERYENDSESSION
      SendMessage_(hWnd, #WM_CLOSE, 0, 0)
    Case #WM_CLOSE
      SendMessage_(MDIClient, #WM_MDIDESTROY, hWnd, 0)
      ChildNumber-1
      If GetWindow_(MDIClient, #GW_CHILD)=0
        ChildNumber = 0
        SubMenu = GetSubMenu_(hMainMenu, 0)
        If SubMenu
          SendMessage_(MDIClient, #WM_MDISETMENU, hMainMenu, SubMenu)
        EndIf
        FreeToolBar(1)
        hToolBar = CreateToolBar(0, MDIFrame)
        If hToolBar
          ToolBarStandardButton(#IDM_FILE_OPEN, #PB_ToolBarIcon_Open)
          ToolBarToolTip(#IDM_FILE_OPEN, Tooltips$(#Open)) 
          ToolBarStandardButton(#IDM_HELP_ABOUT, #PB_ToolBarIcon_Help)
          ToolBarToolTip(#IDM_HELP_ABOUT, Tooltips$(#About))
        Else
          Error("Can't create main toolbar.", 1)
        EndIf
        DrawMenuBar_(MDIFrame)
      EndIf
    Default
      result = DefMDIChildProc_(hWnd, uMsg, wParam, lParam)
  EndSelect
  ProcedureReturn result
EndProcedure

Procedure OpenImageFile(File$)
  result = 0
  If Gdiplus
    FileWSize = (Len(File$)*2)+2
    *FileW = CoTaskMemAlloc_(FileWSize)
    If *FileW
      If MultiByteToWideChar_(#CP_ACP, 0, File$, -1, *FileW, FileWSize)
        bitmap = 0
        CallFunctionFast(*GdipCreateBitmapFromFile, *FileW, @bitmap)
        If bitmap
          hbmReturn = 0
          CallFunctionFast(*GdipCreateHBITMAPFromBitmap, bitmap, @hbmReturn, 0)
          If hbmReturn
            hChild = CreateWindowEx_(#WS_EX_MDICHILD, ClassName$, "", #WS_CHILDWINDOW|#WS_TILEDWINDOW|#WS_CLIPSIBLINGS|#WS_CLIPCHILDREN|#WS_VISIBLE|$0000C000|#MDIS_ALLCHILDSTYLES, #CW_USEDEFAULT, #CW_USEDEFAULT, #CW_USEDEFAULT, #CW_USEDEFAULT, MDIClient, #NULL, hInstance, #NULL)
            If hChild
              SetWindowText_(hChild, File$)
              ShowWindow_(hChild, #SW_SHOW)
              ChildNumber+1
              result = 1
            EndIf
          EndIf
        Else
          Error("GdipCreateBitmapFromFile() failed."+Chr(10)+File$+" is not a valid image file.", 0)
        EndIf
      EndIf
    Else
      SetLastError_(#ERROR_NOT_ENOUGH_MEMORY)
    EndIf
    CoTaskMemFree_(*FileW)
  EndIf
  ProcedureReturn result
EndProcedure

Procedure SaveImageFile(File$)
  result = 0
  If GetEncoderClsid("image/"+mimetype$, @encoderClsid)
    FileWSize = (Len(File$)*2)+2
    *FileW = CoTaskMemAlloc_(FileWSize)
    If *FileW
      If MultiByteToWideChar_(#CP_ACP, 0, File$, -1, *FileW, FileWSize)
        hbm = SendMessage_(GetWindow_(ActiveMDIChild, #GW_CHILD), #STM_GETIMAGE, #IMAGE_BITMAP, 0)
        If hbm
          bitmap = 0
          CallFunctionFast(*GdipCreateBitmapFromHBITMAP, hbm, 0, @bitmap)
          If bitmap
            result = CallFunctionFast(*GdipSaveImageToFile, bitmap, *FileW, encoderClsid, 0) ; encoderParams)
          EndIf
        EndIf
      EndIf
    EndIf
    CoTaskMemFree_(*FileW)
  EndIf
  ProcedureReturn result
EndProcedure

Procedure WndProc(hWnd, uMsg, wParam, lParam)
  result = 0
  Select uMsg
    Case #WM_COMMAND
      If lParam=0 Or lParam=hToolBar
        Select wParam&$ffff
          Case #IDM_FILE_OPEN
            File$ = OpenFileRequester("Open image file:", "", "BMP image (*.bmp)|*.bmp|JPEG image (*.jpg)|*.jpg|PNG image (*.png)|*.png|TIFF image (*.tif)|*.tif|GIF image (*.gif)|*.gif|All files (*.*)|*.*", #PB_Requester_MultiSelection)
            While File$
              OpenImageFile(File$)
              File$ = NextSelectedFileName()
            Wend
          Case #IDM_FILE_SAVEAS
            Buffer$ = Space(512)
            GetWindowText_(ActiveMDIChild, @Buffer$, 512)
            File$ = SaveFileRequester("Save image file:", Buffer$, "BMP image (*.bmp)|*.bmp|JPEG image (*.jpg)|*.jpg|PNG image (*.png)|*.png|TIFF image (*.tif)|*.tif|GIF image (*.gif)|*.gif|All files (*.*)|*.*", 0)
            If File$
              If SaveImageFile(File$)=0
                SetWindowText_(ActiveMDIChild, File$)
              Else
                Error("Unable to save "+File$, 0)
              EndIf
            EndIf
          Case #IDM_FILE_CLOSE
            SendMessage_(SendMessage_(MDIClient, #WM_MDIGETACTIVE, 0, 0), #WM_CLOSE, 0, 0)
          Case #IDM_FILE_CLOSEALL
            EnumChildWindows_(MDIClient, @CloseEnumProc(), 0)
          Case #IDM_FILE_QUIT
            PostQuitMessage_(#NULL)
          Case #IDM_HELP_ABOUT
            ShellAbout_(MDIFrame, "PictureViewer v 0.5", "El_Choni 05/02/2003", LoadIcon_(#NULL, #IDI_APPLICATION))
          Case #IDM_WINDOW_TILEHORIZONTAL
            SendMessage_(MDIClient, #WM_MDITILE, #MDITILE_HORIZONTAL, 0)
          Case #IDM_WINDOW_TILEVERTICAL
            SendMessage_(MDIClient, #WM_MDITILE, #MDITILE_VERTICAL, 0)
          Case #IDM_WINDOW_CASCADE
            SendMessage_(MDIClient, #WM_MDICASCADE, #MDITILE_SKIPDISABLED, 0)
          Case #IDM_WINDOW_ARRANGE
            SendMessage_(MDIClient, #WM_MDIICONARRANGE, 0, 0)
          Case #IDM_WINDOW_NEXT
            SendMessage_(MDIClient, #WM_MDINEXT, #NULL, 0)
          Default
            result = DefFrameProc_(hWnd, MDIClient, uMsg, wParam, lParam)
        EndSelect
      EndIf
    Case #WM_CREATE
      hToolBar = CreateToolBar(0, hWnd)
      If hToolBar
        ToolBarStandardButton(#IDM_FILE_OPEN, #PB_ToolBarIcon_Open)
        ToolBarToolTip(#IDM_FILE_OPEN, Tooltips$(#Open)) 
        ToolBarStandardButton(#IDM_HELP_ABOUT, #PB_ToolBarIcon_Help)
        ToolBarToolTip(#IDM_HELP_ABOUT, Tooltips$(#About))
      Else
        Error("Can't create main toolbar.", 1)
      EndIf
      hMainMenu = CreateMenu(0, hWnd)
      If hMainMenu
        MenuTitle("&File")
          MenuItem(#IDM_FILE_OPEN, MenuCommands$(#Open))
          MenuBar()
          MenuItem(#IDM_FILE_QUIT, MenuCommands$(#Quit))
        MenuTitle("&Help")
          MenuItem(#IDM_HELP_ABOUT, MenuCommands$(#About))
      Else
        Error("Can't create main menu.", 1)
      EndIf
      GetClientRect_(hWnd, rc.RECT)
      WindowWidth = rc\right-rc\left
      hStatus = CreateStatusBar(0, hWnd)
      If hStatus
        AddStatusBarField(WindowWidth/3)
        AddStatusBarField(WindowWidth/3)
        AddStatusBarField(WindowWidth/3)
        StatusBarText(0, 0, "")
        StatusBarText(0, 1, "")
        StatusBarText(0, 2, "")
      Else
        Error("Can't create status bar.", 1)
      EndIf
      ClassName$ = "mdi_child"
      wc\cbSize = SizeOf(WNDCLASSEX)
      wc\style = #CS_HREDRAW|#CS_VREDRAW|#CS_BYTEALIGNWINDOW
      wc\lpfnWndProc = @MDIChildProc()
      wc\cbClsExtra = 0
      wc\cbWndExtra = 0
      wc\hInstance = hInstance
      wc\hbrBackground = #COLOR_BTNFACE+1
      wc\lpszMenuName = #NULL
      wc\lpszClassName = @ClassName$
      wc\hIcon = LoadIcon_(#NULL, #IDI_APPLICATION)
      wc\hCursor = LoadCursor_(#NULL, #IDC_ARROW)
      wc\hIconSm = 0
      If RegisterClassEx_(wc)
        ExtraWidth = GetSystemMetrics_(#SM_CXFRAME)*2
        ExtraHeight = GetSystemMetrics_(#SM_CYCAPTION)+(GetSystemMetrics_(#SM_CYFRAME)*2)
        cs.CLIENTCREATESTRUCT
        cs\hWindowMenu = GetSubMenu_(hMainMenu, 0)
        cs\idFirstChild = #IDM_FIRSTCHILD
        ChildNumber = 0
        MDIClient = CreateWindowEx_(#WS_EX_CLIENTEDGE, @"MDICLIENT", #NULL, #MDIS_ALLCHILDSTYLES|#WS_CHILD|#WS_CLIPCHILDREN|#WS_VISIBLE|#WS_VSCROLL|#WS_HSCROLL|#WS_CLIPSIBLINGS|#WS_CHILDWINDOW, 0, 0, 0, 0, hWnd, 0, hInstance, cs)
        If MDIClient=0
          Error("Failed to create the MDI Client window.", 1)
        EndIf
        DragAcceptFiles_(hWnd, #TRUE)
        CoInitialize_(#NULL)
        Gdiplus = OpenLibrary(0, "GDIPLUS.DLL")
        If Gdiplus
          *GdiplusStartup = IsFunction(0, "GdiplusStartup")
          If *GdiplusStartup
            gdpsi.GdiplusStartupInput
            gdpsi\GdiplusVersion = 1
            gdpsi\DebugEventCallback = 0
            gdpsi\SuppressBackgroundThread = 0
            gdpsi\SuppressExternalCodecs = 0
            CallFunctionFast(*GdiplusStartup, @gdiplusToken, gdpsi, #NULL)
            If gdiplusToken
              *GdipCreateBitmapFromFile = IsFunction(0, "GdipCreateBitmapFromFile")
              *GdipCreateHBITMAPFromBitmap = IsFunction(0, "GdipCreateHBITMAPFromBitmap")
              *GdipGetImageWidth = IsFunction(0, "GdipGetImageWidth")
              *GdipGetImageHeight = IsFunction(0, "GdipGetImageHeight")
              *GdipGetImageEncodersSize = IsFunction(0, "GdipGetImageEncodersSize")
              *GdipGetImageEncoders = IsFunction(0, "GdipGetImageEncoders")
              *GdipCreateBitmapFromHBITMAP = IsFunction(0, "GdipCreateBitmapFromHBITMAP")
              *GdipSaveImageToFile = IsFunction(0, "GdipSaveImageToFile")
              *GdiplusShutdown = IsFunction(0, "GdiplusShutdown")
            Else
              Gdiplus = 0
            EndIf
          Else
            Gdiplus = 0
          EndIf
        Else
          Gdiplus = 0
        EndIf
      Else
        Error("Failed to register the MDI Child window class.", 1)
      EndIf
    Case #WM_MOVE
      If IsIconic_(MDIFrame)=0 And IsZoomed_(hWnd)=0
        xWpos = lParam&$ffff
        yWpos = lParam>>16
      EndIf
    Case #WM_SIZE
      If hWnd=MDIFrame
        If IsZoomed_(hWnd)=0
          Maximized = 0
        Else
          Maximized = #WS_MAXIMIZE
        EndIf
        If IsIconic_(hWnd)=0
          WindowWidth = lParam&$ffff
          WindowHeight = lParam>>16
          SendMessage_(hToolBar, #TB_AUTOSIZE, 0, 0)
          MoveWindow_(hStatus, 0, 0, 0, 0, #TRUE)
          GetWindowRect_(hToolBar, rc.RECT)
          ToolBarHeight = rc\bottom-rc\top
          GetWindowRect_(hStatus, rc)
          StatusHeight = rc\bottom-rc\top
          MoveWindow_(MDIClient, 0, ToolBarHeight, WindowWidth, WindowHeight-ToolBarHeight-StatusHeight, #TRUE)
        EndIf
      EndIf
    Case #WM_DROPFILES
      DroppedFiles = AllocateMemory(0, 255, 0)
      hDrop = wParam
      nFiles = DragQueryFile_(hDrop, -1, DroppedFiles, 255)
      For i=1 To nFiles
        DragQueryFile_(hDrop, i, DroppedFiles, 255)
        File$ = PeekS(MemoryID())
        If OpenImageFile(File$)=0
          Error(File$+" is not a valid image file", 0)
        EndIf
      Next i
      FreeMemory(0)
      DragFinish_(hDrop)
      result = 0
    Case #WM_QUERYENDSESSION
      SendMessage_(hWnd, #WM_CLOSE, 0, 0)
    Case #WM_CLOSE
      SendMessage_(hWnd, #WM_COMMAND, #IDM_FILE_CLOSEALL, 0)
      If GetWindow_(MDIClient, #GW_CHILD)=0
        result = DefFrameProc_(hWnd, MDIClient, uMsg, wParam, lParam)
      EndIf
    Case #WM_DESTROY
      If hWnd=MDIFrame
        CallFunctionFast(*GdiplusShutdown, gdiplusToken)
        CoUninitialize_()
        PostQuitMessage_(#NULL)
      EndIf
    Default
      result = DefFrameProc_(hWnd, MDIClient, uMsg, wParam, lParam)
  EndSelect
  ProcedureReturn result
EndProcedure

hInstance = GetModuleHandle_(0)
ClassName$ = "MDIFrame"
wc\cbSize = SizeOf(WNDCLASSEX)
wc\style = #CS_HREDRAW|#CS_VREDRAW|#CS_BYTEALIGNWINDOW
wc\lpfnWndProc = @WndProc()
wc\cbClsExtra = 0
wc\cbWndExtra = 0
wc\hInstance = hInstance
wc\hbrBackground = #NULL
wc\lpszMenuName = #NULL
wc\lpszClassName = @ClassName$
wc\hIcon = LoadIcon_(#NULL, #IDI_APPLICATION)
wc\hCursor = LoadCursor_(#NULL, #IDC_ARROW)
wc\hIconSm = 0
If RegisterClassEx_(@wc)
  xPos = 320
  yPos = 200
  WindowWidth = 640
  WindowHeight = 480
  hAccel = CreateAcceleratorTable_(?AcceleratorTable, 6)
  MDIFrame = CreateWindowEx_(#NULL, ClassName$, @"MDI Template", #WS_OVERLAPPEDWINDOW, xPos, yPos, WindowWidth, WindowHeight, #NULL, 0, hInstance, #NULL)
  If MDIFrame
    ShowWindow_(MDIFrame, #SW_SHOWNORMAL)
    UpdateWindow_(MDIFrame)
    File$ = ProgramParameter()
    While File$
      OpenImageFile(File$)
      File$ = ProgramParameter()
    Wend
    msg.MSG
    While GetMessage_(@msg, 0, 0, 0)
      If TranslateAccelerator_(MDIFrame, hAccel, @msg)=0 And TranslateMDISysAccel_(MDIClient, @msg)=0
        TranslateMessage_(@msg)
        DispatchMessage_(@msg)
      EndIf
    Wend
    DestroyMenu_(hChildMenu)
    DestroyAcceleratorTable_(hAccel)
  Else
    Error("Failed to open the MDI Frame window", 1)
  EndIf
Else
  Error("Failed to register the MDI Frame window class", 1)
EndIf
End

DataSection
AcceleratorTable:
Data.w #FCONTROL|#FVIRTKEY|#FNOINVERT
Data.w #VK_O
Data.w #IDM_FILE_OPEN
Data.w #FCONTROL|#FVIRTKEY|#FNOINVERT
Data.w #VK_S
Data.w #IDM_FILE_SAVEAS
Data.w #FCONTROL|#FVIRTKEY|#FNOINVERT
Data.w #VK_W
Data.w #IDM_FILE_CLOSE
Data.w #FCONTROL|#FVIRTKEY|#FNOINVERT|#FSHIFT
Data.w #VK_W
Data.w #IDM_FILE_CLOSE
Data.w #FCONTROL|#FVIRTKEY|#FNOINVERT
Data.w #VK_Q
Data.w #IDM_FILE_QUIT
Data.w #FVIRTKEY|#FNOINVERT
Data.w #VK_F1
Data.w #IDM_HELP_ABOUT
EndDataSection
I hope you find it useful. Bye,

El_Choni
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by plouf.

Looks promising :wink:
btw it load fine here (even on the other end of the
parent window) but it crashes whenever i try to save
XP Home here

Christos
Post Reply