PlanetSquires Forums

Please login or register.

Login with username, password and session length
Advanced search  

Author Topic: dpi aware without CWindow  (Read 1044 times)

James Fuller

  • FireFly3 Registered User
  • Senior FireFly Member
  • *
  • Posts: 276
  • FF3 User
dpi aware without CWindow
« on: November 15, 2016, 12:58:44 PM »

Jose,
  For my "Is Ease of use worth the increase in exe size" article  I was looking for a way to do dpi without the use of CWindow. This was under my nose all along but somehow I missed it:
http://www.jose.it-berater.org/smfforum/index.php?topic=5018.msg21094#msg21094

Is this acceptable?
Here is  FreeBasic dpi aware source of the HelloDDT example.

Code: [Select]

#INCLUDE ONCE "windows.bi"
#INCLUDE ONCE "afx/afxwin.inc"
CONST IDC_OK = 1001
CONST IDC_CANCEL = 1002
CONST IDC_TEXT = 1003
DIM SHARED UserName As STRING * 128
DECLARE FUNCTION WinMain (BYVAL hInstance AS HINSTANCE, _
                          BYVAL hPrevInstance AS HINSTANCE, _
                          BYVAL szCmdLine AS ZSTRING PTR, _
                          BYVAL nCmdShow AS LONG) AS LONG

   END WinMain(GetModuleHandleW(NULL), NULL, COMMAND(), SW_NORMAL)

DECLARE FUNCTION WndProc (BYVAL hwnd AS HWND, BYVAL uMsg AS UINT, BYVAL wParam AS WPARAM, BYVAL lParam AS LPARAM) AS LRESULT
'==============================================================================
Function dpi(BYVAL nPix AS LONG) AS LONG
    STATIC AS float ratio
    If ratio = 0 Then
        Dim As HDC hDc = GetDC(0)
        ratio = (GetDeviceCaps(hDc, LOGPIXELSX) / 96.0f)
        ReleaseDC(0,hDc)
    EndIf
    Function = CAST(LONG,nPix * ratio)
End Function
'==============================================================================
FUNCTION WndProc(Byval hWnd As HWND,Byval wMsg As UINT,Byval wParam As WPARAM,Byval lParam as LPARAM) As LRESULT
    Static As HWND hText,hOk,hCancel
    Static As Long RetVal
    Static As HFONT hFont
   
    SELECT CASE wMsg
        CASE WM_CREATE
            DIM AS HDC hDc
            DIM As CREATESTRUCT Ptr cstruct
            hDc = GetDC(0)
            cstruct = CAST(CREATESTRUCT Ptr,lParam)
            hFont = CreateFont(-MulDiv(9.0,GetDeviceCaps(hDc, LOGPIXELSY),72),0,0,0,FW_NORMAL,0,0,0,DEFAULT_CHARSET,0,0,0,0,"Segoe UI")
            SendMessage(hWnd,WM_SETFONT,CAST(WPARAM,hFont),CAST(LPARAM,1))
            hText = CreateWindowEx(&H00000200,"Edit","",&H50010000,dpi(25),dpi(23),dpi(235),dpi(23),hWnd,CAST(HMENU,IDC_TEXT),cstruct->hInstance,NULL)
            SendMessage(hText,WM_SETFONT,CAST(WPARAM,hFont),CAST(LPARAM,1))
            hOk = CreateWindowEx(0,"Button","OK",&H50010000,dpi(60),dpi(60),dpi(70),dpi(26),hWnd,CAST(HMENU,IDC_OK),cstruct->hInstance,NULL)
            SendMessage(hOk,WM_SETFONT,CAST(WPARAM,hFont),CAST(LPARAM,1))
            hCancel = CreateWindowEx(0,"Button","Cancel",&H50010000,dpi(152),dpi(60),dpi(70),dpi(26),hWnd,CAST(HMENU,IDC_CANCEL),cstruct->hInstance,NULL)
            SendMessage(hCancel,WM_SETFONT,CAST(WPARAM,hFont),CAST(LPARAM,1))
            ReleaseDC(hWnd,hDc)
            SetFocus(hText)
            Exit Function
        CASE WM_COMMAND
            Select Case CAST(HWND,lParam)
                Case hCancel
                    RetVal = 0
                    SendMessage(hWnd,WM_CLOSE,0,0)
                Case hOk   
                    RetVal = 1
                    GetWindowText(hText,UserName,128)
                    SendMessage(hWnd,WM_CLOSE,0,0)
            End Select   
        CASE WM_DESTROY
            DeleteObject(hFont)
            PostQuitMessage(RetVal)
            Exit Function
    End Select   
    Function = DefWindowProc( hWnd, wMsg, wParam, lParam )   
End Function
'==============================================================================
FUNCTION WinMain (BYVAL hInstance AS HINSTANCE, _
                  BYVAL hPrevInstance AS HINSTANCE, _
                  BYVAL szCmdLine AS ZSTRING PTR, _
                  BYVAL nCmdShow AS LONG) AS LONG
    DIM AS MSG wMsg
    DIM AS WNDCLASS wcls
    DIM AS HWND hWnd
    AfxSetProcessDPIAware
        with wcls
            .style         = CS_HREDRAW or CS_VREDRAW
            .lpfnWndProc   = @WndProc
            .cbClsExtra    = 0
            .cbWndExtra    = 0
            .hInstance     = hInstance
            .hIcon         = LoadIcon( NULL, IDI_APPLICATION )
            .hCursor       = LoadCursor( NULL, IDC_ARROW )
            .hbrBackground = CAST(HBRUSH,COLOR_BTNFACE+1)
            .lpszMenuName  = NULL
            .lpszClassName = @"hellodpi"
        end with
             
        if( RegisterClass( @wcls ) = FALSE ) then
           MessageBox( null, "Failed to register wcls!", "Error", MB_ICONERROR )
           exit function
        end if
       
        hWnd = CreateWindowEx( 0, _
                                "hellodpi", _
                               "Dpi Test", _
                               WS_OVERLAPPEDWINDOW, _
                               0, _
                               0, _
                               dpi(296), _
                               dpi(133), _
                               NULL, _
                               NULL, _
                               hInstance, _
                               NULL )
        AfxCenterWindow(hWnd)                     
        ShowWindow( hWnd, nCmdShow )
        UpdateWindow( hWnd )
         
        While( GetMessage( @wMsg, NULL, 0, 0 ) <> FALSE )   
            TranslateMessage( @wMsg )
            DispatchMessage( @wMsg )
        Wend
       

        If wMsg.wParam = 1 Then
            MessageBox(0,"Hello " & UserName,"What's your Name",MB_OK)
        EndIf
        function = wMsg.wParam
   
END FUNCTION                 


James
Logged

Josť Roca

  • Moderator
  • Master FireFly Member
  • *****
  • Posts: 2862
    • Jos
Re: dpi aware without CWindow
« Reply #1 on: November 15, 2016, 09:56:12 PM »

I don't understand why you give so many turns to the matter. It is fairly simple: you have to scale all the UI elements. How you do it is up to you. CWindow gives much more than scaling; otherwise, I would have not bothered to write it.