CGdiplus AfxNova test

Started by Frank Bruebach, July 26, 2025, 12:27:39 PM

Previous topic - Next topic

Frank Bruebach

Hello after having also some Problems with Tiko and AfxNova I managed to make my running Setup..
I Made a Test with Cgdiplus and AfxNova and all is Working fine now.

Before my AfxNova example were running I included a simple Print Message for AfxNova/cWindow.inc File and AfxNova/AfxWin.inc File  that These Files are loading without Problems

Thanks Jose for your great Job and Paul for this excellent Tiko Editor too.
Yesterday I still thought I would never use Tiko and afxNova anymore together lol because of some Problems AS mentioned before.. today all its OK


' ########################################################################################
' Microsoft Windows
' File: ImageGetThumbnailImage.bas
' Contents: GDI+ - ImageGetThumbnailImage example
' Compiler: FreeBasic 32 & 64 bit
' Copyright (c) 2025 José Roca. Freeware. Use at your own risk.
' THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER
' EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES OF
' MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE.
' ########################################################################################

#define UNICODE
#INCLUDE ONCE "AfxNova/CGdiPlus.inc"
#INCLUDE ONCE "AfxNova/CGraphCtx.inc"
USING AfxNova

CONST IDC_GRCTX = 1001

DECLARE FUNCTION wWinMain (BYVAL hInstance AS HINSTANCE, _
                          BYVAL hPrevInstance AS HINSTANCE, _
                          BYVAL pwszCmdLine AS WSTRING PTR, _
                          BYVAL nCmdShow AS LONG) AS LONG

  END wWinMain(GetModuleHandleW(NULL), NULL, wCOMMAND(), SW_NORMAL)

' // Forward declaration
DECLARE FUNCTION WndProc (BYVAL hwnd AS HWND, BYVAL uMsg AS UINT, BYVAL wParam AS WPARAM, BYVAL lParam AS LPARAM) AS LRESULT

' ========================================================================================
' The following example creates an Image object based on a metafile and then draws the image.
' Next, the code calls the Image.GetBounds method to get the bounding rectangle for the image.
' The code makes two attempts to display 75 percent of the image. The first attempt fails
' because it specifies (0, 0) for the upper-left corner of the source rectangle. The second
' attempt succeeds because it uses the X and Y data members returned by Image.GetBounds to
' specify the upper-left corner of the source rectangle.
' ========================================================================================
SUB Example_GetThumbnailImage (BYVAL hdc AS HDC)

  ' // Create a graphics object from the window device context
  DIM graphics AS CGpGraphics = hdc
  ' // Get the DPI scaling ratios
  DIM rxRatio AS SINGLE = graphics.GetDpiX / 96
  DIM ryRatio AS SINGLE = graphics.GetDpiY / 96
  ' // Set the scale transform
  graphics.ScaleTransform(rxRatio, ryRatio)

  DIM pImage AS CGpImage = "laub1.jpg" '' "climber.jpg"
  DIM pThumbnail AS CGpImage
  pImage.GetThumbnailImage(160, 80, @pThumbnail)

  graphics.DrawImage(@pImage, 10, 10, pImage.GetWidth, pImage.GetHeight)
  'graphics.DrawImage(@pThumbnail, 220, 10, pThumbnail.GetWidth, pThumbnail.GetHeight)
  graphics.DrawImage(@pThumbnail, 220, 330, pThumbnail.GetWidth, pThumbnail.GetHeight)

END SUB
' ========================================================================================

' ========================================================================================
' Main
' ========================================================================================
FUNCTION wWinMain (BYVAL hInstance AS HINSTANCE, _
                  BYVAL hPrevInstance AS HINSTANCE, _
                  BYVAL pwszCmdLine AS WSTRING PTR, _
                  BYVAL nCmdShow AS LONG) AS LONG

  ' // Set process DPI aware
  SetProcessDpiAwareness(PROCESS_SYSTEM_DPI_AWARE)
  ' // Enable visual styles without including a manifest file
  AfxEnableVisualStyles

  ' // Create the main window
  DIM pWindow AS CWindow = "MyClassName"
  pWindow.CreateOverlapped(NULL, "GDI+ ImageGetThumbnailImage", @WndProc)
  ' // Size it by setting the wanted width and height of its client area
  pWindow.SetClientSize(630, 450)
  ' // Center the window
  pWindow.Center

  ' // Add a graphic control
  DIM pGraphCtx AS CGraphCtx = CGraphCtx(@pWindow, IDC_GRCTX, "", 0, 0, pWindow.ClientWidth, pWindow.ClientHeight)
  pGraphCtx.Clear RGB_WHITE
  ' // Get the memory device context of the graphic control
  DIM hdc AS HDC = pGraphCtx.GetMemDc
  ' // Draw the graphics
  Example_GetThumbnailImage(hdc)

  ' // Displays the window and dispatches the Windows messages
  FUNCTION = pWindow.DoEvents(nCmdShow)

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

' ========================================================================================
' Main window procedure
' ========================================================================================
FUNCTION WndProc (BYVAL hwnd AS HWND, BYVAL uMsg AS UINT, BYVAL wParam AS WPARAM, BYVAL lParam AS LPARAM) AS LRESULT

  SELECT CASE uMsg

      ' // If an application processes this message, it should return zero to continue
      ' // creation of the window. If the application returns –1, the window is destroyed
      ' // and the CreateWindowExW function returns a NULL handle.
      CASE WM_CREATE
        AfxEnableDarkModeForWindow(hwnd)
        RETURN 0

      ' // Theme has changed
      CASE WM_THEMECHANGED
        AfxEnableDarkModeForWindow(hwnd)
        RETURN 0

      CASE WM_COMMAND
        SELECT CASE CBCTL(wParam, lParam)
            CASE IDCANCEL
              ' // If ESC key pressed, close the application by sending an WM_CLOSE message
              IF CBCTLMSG(wParam, lParam) = BN_CLICKED THEN
                  SendMessageW hwnd, WM_CLOSE, 0, 0
                  RETURN 0
              END IF
        END SELECT

        CASE WM_DESTROY
        ' // Ends the application by sending a WM_QUIT message
        PostQuitMessage(0)
        RETURN 0

  END SELECT

  ' // Default processing of Windows messages
  FUNCTION = DefWindowProcW(hwnd, uMsg, wParam, lParam)

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