Results 1 to 5 of 5

Thread: [RESOLVED] Opening font file in Windows Font Viewer using ShellExecute?

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 2011
    Location
    Palm Coast, FL
    Posts
    435

    Resolved [RESOLVED] Opening font file in Windows Font Viewer using ShellExecute?

    My app contains standard ShellExecute code to open a file using the Windows app associated with the file's extension. It works for all file types but it doesn't work for font files however (.tff for example).

    When I double click a font in File Explorer, Font Viewer shows the font. When I try it from code, it doesn't? Should Windows Font Viewer open and show the font with this code or are fonts different than other file types?

    Code snippet of the routine that opens files:
    Code:
        strFile = "filename.tff"
        strTopic = "Open"
        strParams = vbNullString
        strDirectory = vbNullString
        
        lngRetVal = ShellExecuteW(0&, StrPtr(strTopic), StrPtr(strFile), StrPtr(strParams), StrPtr(strDirectory), 1)
    Last edited by AAraya; May 21st, 2022 at 06:39 PM.

  2. #2
    PowerPoster
    Join Date
    Jul 2010
    Location
    NYC
    Posts
    5,711

    Re: Opening font file in Windows Font Viewer using ShellExecute?

    Fonts don't appear to have an 'Open' verb registered by default.

    When you right click them in Explorer, note that the bolded item is "Preview", not open, so you're invoking the 'preview' verb on double click.

    Per MSDN, if you use null instead of open, that's the equivalent of double clicking the file--- leaving it as null invokes the default action. When you specify "open", you're asking for that specific action, which an application will have needed to register to handle-- and it's not just font files that have a different one, like "preview", as their default, and don't have "open" registered at all. So if you want to simulate double clicking,

    lngRetVal = ShellExecuteW(0&, 0&, StrPtr(strFile), StrPtr(strParams), StrPtr(strDirectory), 1)

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 2011
    Location
    Palm Coast, FL
    Posts
    435

    Re: Opening font file in Windows Font Viewer using ShellExecute?

    Brilliant! Once again to my rescue @fafalone! I've lost count of the number of times you've helped me.

    Here's a follow up question. Is there a way for me to know if the "Open" verb is available for a given file - other than calling ShellExecute and checking the return code for a failure? Ideally, I'd like to continue to use "Open" if available and fall back on Null if it's not.

  4. #4

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 2011
    Location
    Palm Coast, FL
    Posts
    435

    Re: Opening font file in Windows Font Viewer using ShellExecute?

    Nevermind, I found the answer myself in this MSDN article

    In general, trying to determine the list of available verbs for a particular file is somewhat complicated. In many cases, you can simply set the lpVerb parameter to NULL, which invokes the default command for the file type. This procedure is usually equivalent to setting lpVerb to "open", but some file types may have a different default command.

  5. #5
    PowerPoster
    Join Date
    Jul 2010
    Location
    NYC
    Posts
    5,711

    Re: [RESOLVED] Opening font file in Windows Font Viewer using ShellExecute?

    It's indeed a large pain to list verbs. There's numerous places they can be found in the registry that varies by Windows version, and for whatever reason, even when you programmatically load the right-click context menu from Explorer... there will be many menu items that are there, can be clicked on and successfully completed, but return no verb, or sometimes, a help description but not a verb, when you use the documented method for listing them:

    Code:
    Private Sub dbg_EnumVerbs(pCtxMenu As oleexp.IContextMenu)
    Dim hMenu As Long
    Dim tCmdInfo As oleexp.CMINVOKECOMMANDINFO
    Dim tCmdInfoEx As oleexp.CMINVOKECOMMANDINFOEX
    hMenu = CreatePopupMenu()
    Dim sVerb As String
    Dim pCtxMenu2 As oleexp.IContextMenu2
    'sVerb = pszVerb
    Dim lpCtxMenud As Long
    Dim PT As oleexp.Point
    Dim nItems As Long
    Dim sDesc As String
    Dim i As Long
    Dim bGo As Boolean
    lpCtxMenud = -1
    If hMenu Then
    '    Set pCtxMenu2 = pCtxMenu
        If (pCtxMenu Is Nothing) = False Then
            DebugAppend "Got pCtxMenu", 11
            pCtxMenu.QueryContextMenu hMenu, 0&, 1&, &H7FFF, CMF_NORMAL
            nItems = GetMenuItemCount(hMenu)
            DebugAppend "InvokeVerb nItems=" & nItems, 11
            For i = 0 To nItems - 1
                Err.Clear
                Err.Number = 0
                lpCtxMenud = GetMenuItemID(hMenu, i)
                DebugAppend "i=" & i & ", lpCtxMenud=" & lpCtxMenud, 11
                If lpCtxMenud <= 0 Then GoTo nxt
                On Error Resume Next
                sVerb = String$(MAX_PATH, 0&)
                pCtxMenu.GetCommandString lpCtxMenud - 1, GCS_VERBW, 0&, StrPtr(sVerb), Len(sVerb)
                On Error GoTo InvokeVerb_Err
                If (Err.Number = 0&) Then
                    sVerb = TrimNullW(sVerb)
                    DebugAppend "CmdStr=" & sVerb, 11
                    DebugAppend "InvokeVerb hMenu=" & hMenu, 11
                    On Error Resume Next
                    sDesc = String$(MAX_PATH, 0&)
                    pCtxMenu.GetCommandString lpCtxMenud - 1, GCS_HELPTEXTW, 0&, StrPtr(sDesc), Len(sDesc)
                    sDesc = TrimNullW(sDesc)
                    On Error GoTo InvokeVerb_Err
                    DebugAppend "HelpStr=" & sDesc, 11
    '                If LCase$(sVerb) = LCase$(pszVerb) Then
    '                    DebugAppend "InvokeVerb go on " & sVerb
    '                    bGo = True
    '                    Exit For
    '                End If
                Else
                    DebugAppend "InvokeVerb.GetCommandString Error::" & Err.Description, 11
                    Err.Clear
                End If
    nxt:
            Next i
        End If
    End If
    Exit Sub
    InvokeVerb_Err:
    DebugAppend "dbg_EnumVerbs.Error->" & Err.Description, 11
    End Sub

Tags for this Thread

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