CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Apr 2001
    Posts
    25

    Problem with API GetDC

    Hi All,
    Actually i am trying to send the form name to this procedure, but i am geting error saying Type mismatch.

    Public Function GetFontDialogUnits() As Long

    Dim hFont As Long
    Dim hFontOld As Long
    Dim r As Long
    Dim avgWidth As Long
    Dim hDc As Long
    Dim tmp As String
    Dim sz As SIZE

    'get the hdc to the main window
    hDc = GetDC(Form1.hwnd)

    'with the current font attributes, select the font
    hFont = GetStockObject(ANSI_VAR_FONT)
    hFontOld = SelectObject(hDc, hFont&)

    'get its length, then calculate the average character width
    tmp = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
    GetTextExtentPoint32(hDc, tmp, 52, sz)
    avgWidth = (sz.cx \ 52)

    're-select the previous font & delete the hDc
    SelectObject(hDc, hFontOld)
    DeleteObject(hFont)
    ReleaseDC(Form1.hwnd, hDc)

    'return the average character width
    GetFontDialogUnits = avgWidth

    End Function


    Please help is needed.

    Mahesh


  2. #2
    Join Date
    Dec 1999
    Location
    Dublin, Ireland
    Posts
    1,173

    Re: Problem with API GetDC


    public Function GetFontDialogUnits(byval frmIn as Form) as Long

    Dim hFont as Long
    Dim hFontOld as Long
    Dim r as Long
    Dim avgWidth as Long
    Dim hDc as Long
    Dim tmp as string
    Dim sz as SIZE

    'get the hdc to the main window
    hDc = GetDC(frmIn.hwnd)

    'with the current font attributes, select the font
    hFont = GetStockObject(ANSI_VAR_FONT)
    hFontOld = SelectObject(hDc, hFont&)

    'get its length, then calculate the average character width
    tmp = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
    GetTextExtentPoint32(hDc, tmp, 52, sz)
    avgWidth = (sz.cx \ 52)

    're-select the previous font & delete the hDc
    SelectObject(hDc, hFontOld)
    DeleteObject(hFont)
    ReleaseDC(frmIn.hwnd, hDc)

    'return the average character width
    GetFontDialogUnits = avgWidth

    End Function




    To use:

    Debug.print GetFontDialogUnits(Form1)




    HTH,
    Duncan

    -------------------------------------------------
    Ex. Datis: Duncan Jones
    Merrion Computing Ltd
    http://www.merrioncomputing.com
    '--8<-----------------------------------------
    NEW -The printer usage monitoring application
    '--8<------------------------------------------

  3. #3
    Join Date
    Apr 2001
    Posts
    25

    Re: Problem with API GetDC

    Hi Duncan,
    Any thanx since my code is working fine, in don't know what was the problem with that.

    Regards,
    Mahesh


  4. #4
    Join Date
    Apr 2001
    Posts
    25

    Re: Problem with API GetDC

    Hi Duncan,
    Anyway thanx since my code is working fine, I don't know what was the problem with that.

    Regards,
    Mahesh


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured