Proposed new wrapper functions

Started by José Roca, February 28, 2012, 10:34:07 PM

Previous topic - Next topic

José Roca

Thanks for the link. However, I only need the headers and the M$ SDK installers force you to install the latest version of .NET, install a lot of crap and many times spoil your current SDK installation.

Anyway, PBer's aren't yet using the new technologies of Windows 7...

Richard Kelly

Quote from: TechSupport on March 01, 2012, 11:01:13 AM
Quote from: Richard Kelly on March 01, 2012, 12:24:48 AM
Is there any merit is FF having a standard procedure call either just before a window create or immediately after so whatever cleanup adjustments can be made?
The goal with the redesign of FF's DPI awareness is to ensure that you don't have to think about these types of issues. FF will automatically scale Forms/Controls/Fonts for you during code generation. Everything should be proportional. The only problem occurs if you go outside the norm and manually create controls via CreateControlEx or Fonts via CreateFont(Indirect). In those case you will need to do the scaling yourself. FF's built in FF_MakeFontEx function has already been modified to create and return scaled fonts.

You da man! I just want to focus on my application presentations and process flow and have the smart guys like you keep me from hurting myself:-)


José Roca

The following functions load and convert icons in .png, .jpg, .gif., .tiff format to Windows icons or bitmaps suitable to be used in controls, toolbars, etc. The ones embedded in a resource file must be done as raw data, e.g.

UpFolder RCDATA "up_folder_24_h.png"



' ========================================================================================
' Loads an image from a file using GDI+, converts it to an icon and returns the icon handle.
' Parameter:
' - bstrFileName = [in] Path of the image to load and convert.
' Return Value:
'   If the function succeeds, the return value is the handle of the created icon.
'   If the function fails, the return value is NULL.
' ========================================================================================
FUNCTION GdiPlusCreateHICONFromFile (BYVAL bstrFileName AS WSTRING) AS DWORD

   LOCAL hStatus AS LONG                       ' // Status
   LOCAL token AS DWORD                        ' // Token to shutdown GDI+
   LOCAL StartupInput AS GdiplusStartupInput   ' // Structure to initialize GDI+
   LOCAL pImage AS DWORD                       ' // Image handle
   LOCAL hIcon AS DWORD                        ' // Icon handle

   StartupInput.GdiplusVersion = 1
   hStatus = GdiplusStartup(token, StartupInput, BYVAL %NULL)
   IF hStatus <> %StatusOk THEN EXIT FUNCTION

   hStatus = GdipLoadImageFromFile(BYCOPY bstrFileName, pImage)
   IF hStatus = %StatusOk THEN
      hStatus = GdipCreateHICONFromBitmap(pImage, hIcon)
      GdipDisposeImage pImage
   END IF
   GdiplusShutdown token

   FUNCTION = hIcon
   
END FUNCTION
' ========================================================================================

' ========================================================================================
' Loads an image from a resource using GDI+, converts it to an icon and returns the icon handle.
' Parameter:
' - hInstance  = [in] Handle to the instance that contains the resource.
' - bstrImage  = [in] Name of the image in the resource file (.RES). If the image resource uses
'                an integral identifier, bstrImage should begin with a number symbol (#)
'                followed by the identifier in an ASCII format, e.g., "#998". Otherwise,
'                use the text identifier name for the image. Only images embedded as raw data
'                (type RCDATA) are valid. These must be icons in format .png, .jpg, .gif, .tiff.
' Return Value:
'   If the function succeeds, the return value is the handle of the created icon.
'   If the function fails, the return value is NULL.
'   Call GetLasrError to retrieve the error code.
' ========================================================================================
FUNCTION GdiPlusCreateHICONFromResource (BYVAL hInstance AS DWORD, BYVAL bstrImage AS WSTRING) AS DWORD

   LOCAL hStatus AS LONG                        ' // Status
   LOCAL token AS DWORD                         ' // Token to shutdown GDI+
   LOCAL StartupInput AS GdiplusStartupInput    ' // Structure to initialize GDI+
   LOCAL pImage AS DWORD                        ' // Image handle
   LOCAL hIcon AS DWORD                         ' // Icon handle
   LOCAL hResource     AS DWORD                 ' // Resource handle
   LOCAL pResourceData AS DWORD                 ' // Pointer to the resoruce data
   LOCAL hGlobal       AS DWORD                 ' // Global memory handle
   LOCAL pGlobalBuffer AS DWORD                 ' // Pointer to global memory buffer
   LOCAL pImageStream  AS IStream               ' // IStream interface pointer
   LOCAL imageSize     AS DWORD                 ' // Image size
   LOCAL wID AS WORD
   LOCAL dwID AS DWORD

   IF hInstance = 0 THEN EXIT FUNCTION

   StartupInput.GdiplusVersion = 1
   hStatus = GdiplusStartup(token, StartupInput, BYVAL %NULL)
   IF hStatus <> %S_OK THEN EXIT FUNCTION

   ' // Find the resource and lock it
   IF LEFT$(bstrImage, 1) = "#" THEN
      wID = VAL(MID$(bstrImage, 2))
      dwID = MAK(DWORD, wID, 0)
      hResource = FindResourceW(hInstance, BYVAL dwID, BYVAL %RT_RCDATA)
   ELSE
      hResource = FindResourceW(hInstance, BYCOPY bstrImage, BYVAL %RT_RCDATA)
   END IF
   IF hResource = %NULL THEN SetLastError(%E_INVALIDARG) : GOTO LExit
   imageSize = SizeofResource(hInstance, hResource)
   IF imageSize = 0 THEN SetLastError(%E_INVALIDARG) : GOTO LExit
   pResourceData = LockResource(LoadResource(hInstance, hResource))
   IF pResourceData = %NULL THEN SetLastError(%E_INVALIDARG) : GOTO LExit
   ' // Allocate memory to hold the image
   hGlobal = GlobalAlloc(%GMEM_MOVEABLE, imageSize)
   IF hGlobal THEN
      ' // Lock the memory
      pGlobalBuffer = GlobalLock(hGlobal)
      IF pGlobalBuffer THEN
         ' // Copy the image from the resource file to global memory
         CopyMemory pGlobalBuffer, pResourceData, imageSize
         ' // Create an stream in global memory
         IF CreateStreamOnHGlobal(hGlobal, %FALSE, pImageStream) = %S_OK THEN
            ' // Create a bitmap from the data contained in the stream
            hStatus = GdipCreateBitmapFromStream(pImageStream, pImage)
            IF hStatus = %StatusOk THEN
               IF pImage THEN
                  hStatus = GdipCreateHICONFromBitmap(pImage, hIcon)
                  GdipDisposeImage pImage
               END IF
            END IF
         END IF
         ' // Unlock the memory
         GlobalUnlock pGlobalBuffer
      END IF
      ' // Free the memory
      GlobalFree hGlobal
   END IF

LExit:

   GdiplusShutdown token

   FUNCTION = hIcon
   
END FUNCTION
' ========================================================================================

' ========================================================================================
' Loads an image from a file using GDI+, converts it to an icon and returns the icon handle.
' Parameters:
' - bstrFileName  = [in] Path of the image to load and convert.
' - clrBackground = [in] The background color. This parameter is ignored if the bitmap is totally opaque.
' Return Value:
'   If the function succeeds, the return value is the handle of the created icon.
'   If the function fails, the return value is NULL.
' ========================================================================================
FUNCTION GdiPlusCreateHBITMAPFromFile (BYVAL bstrFileName AS WSTRING, BYVAL clrBackground AS DWORD) AS DWORD

   LOCAL hStatus AS LONG                       ' // Status
   LOCAL token AS DWORD                        ' // Token to shutdown GDI+
   LOCAL StartupInput AS GdiplusStartupInput   ' // Structure to initialize GDI+
   LOCAL pImage AS DWORD                       ' // Image handle
   LOCAL hBitmap AS DWORD                      ' // Bitmap handle

   StartupInput.GdiplusVersion = 1
   hStatus = GdiplusStartup(token, StartupInput, BYVAL %NULL)
   IF hStatus <> %StatusOk THEN EXIT FUNCTION

   hStatus = GdipLoadImageFromFile(BYCOPY bstrFileName, pImage)
   IF hStatus = %StatusOk THEN
      hStatus = GdipCreateHBITMAPFromBitmap(pImage, hBitmap, clrBackground)
      GdipDisposeImage pImage
   END IF
   GdiplusShutdown token

   FUNCTION = hBitmap
   
END FUNCTION
' ========================================================================================

' ========================================================================================
' Loads an image from a resource using GDI+, converts it to an icon and returns the icon handle.
' Parameters:
' - hInstance  = [in] Handle to the instance that contains the resource.
' - bstrImage  = [in] Name of the image in the resource file (.RES). If the image resource uses
'                an integral identifier, bstrImage should begin with a number symbol (#)
'                followed by the identifier in an ASCII format, e.g., "#998". Otherwise,
'                use the text identifier name for the image. Only images embedded as raw data
'                (type RCDATA) are valid. These must be icons in format .png, .jpg, .gif, .tiff.
' - clrBackground = [in] The background color. This parameter is ignored if the bitmap is totally opaque.
' Return Value:
'   If the function succeeds, the return value is the handle of the created icon.
'   If the function fails, the return value is NULL.
'   Call GetLasrError to retrieve the error code.
' ========================================================================================
FUNCTION GdiPlusCreateHBITMAPFromResource (BYVAL hInstance AS DWORD, BYVAL bstrImage AS WSTRING, BYVAL clrBackground AS DWORD) AS DWORD

   LOCAL hStatus AS LONG                        ' // Status
   LOCAL token AS DWORD                         ' // Token to shutdown GDI+
   LOCAL StartupInput AS GdiplusStartupInput    ' // Structure to initialize GDI+
   LOCAL pImage AS DWORD                        ' // Image handle
   LOCAL hBitmap AS DWORD                       ' // Bitmap handle
   LOCAL hResource     AS DWORD                 ' // Resource handle
   LOCAL pResourceData AS DWORD                 ' // Pointer to the resoruce data
   LOCAL hGlobal       AS DWORD                 ' // Global memory handle
   LOCAL pGlobalBuffer AS DWORD                 ' // Pointer to global memory buffer
   LOCAL pImageStream  AS IStream               ' // IStream interface pointer
   LOCAL imageSize     AS DWORD                 ' // Image size
   LOCAL wID AS WORD
   LOCAL dwID AS DWORD

   IF hInstance = 0 THEN EXIT FUNCTION

   StartupInput.GdiplusVersion = 1
   hStatus = GdiplusStartup(token, StartupInput, BYVAL %NULL)
   IF hStatus <> %S_OK THEN EXIT FUNCTION

   ' // Find the resource and lock it
   IF LEFT$(bstrImage, 1) = "#" THEN
      wID = VAL(MID$(bstrImage, 2))
      dwID = MAK(DWORD, wID, 0)
      hResource = FindResourceW(hInstance, BYVAL dwID, BYVAL %RT_RCDATA)
   ELSE
      hResource = FindResourceW(hInstance, BYCOPY bstrImage, BYVAL %RT_RCDATA)
   END IF
   IF hResource = %NULL THEN SetLastError(%E_INVALIDARG) : GOTO LExit
   imageSize = SizeofResource(hInstance, hResource)
   IF imageSize = 0 THEN SetLastError(%E_INVALIDARG) : GOTO LExit
   pResourceData = LockResource(LoadResource(hInstance, hResource))
   IF pResourceData = %NULL THEN SetLastError(%E_INVALIDARG) : GOTO LExit
   ' // Allocate memory to hold the image
   hGlobal = GlobalAlloc(%GMEM_MOVEABLE, imageSize)
   IF hGlobal THEN
      ' // Lock the memory
      pGlobalBuffer = GlobalLock(hGlobal)
      IF pGlobalBuffer THEN
         ' // Copy the image from the resource file to global memory
         CopyMemory pGlobalBuffer, pResourceData, imageSize
         ' // Create an stream in global memory
         IF CreateStreamOnHGlobal(hGlobal, %FALSE, pImageStream) = %S_OK THEN
            ' // Create a bitmap from the data contained in the stream
            hStatus = GdipCreateBitmapFromStream(pImageStream, pImage)
            IF hStatus = %StatusOk THEN
               IF pImage THEN
                  hStatus = GdipCreateHBITMAPFromBitmap(pImage, hBitmap, clrBackground)
                  GdipDisposeImage pImage
               END IF
            END IF
         END IF
         ' // Unlock the memory
         GlobalUnlock pGlobalBuffer
      END IF
      ' // Free the memory
      GlobalFree hGlobal
   END IF

LExit:

   GdiplusShutdown token

   FUNCTION = hBitmap
   
END FUNCTION
' ========================================================================================