Results 1 to 5 of 5
  1. #1
    3 Star Lounger
    Join Date
    Aug 2001
    Location
    Jeddah, Saudi Arabia
    Posts
    243
    Thanks
    0
    Thanked 1 Time in 1 Post

    Device Context (VB .NET 2003)

    I want to call the DrawText API function to create ellipsed paths in VB .NET and I need to pass it the Device Context handle of the form without having access to PaintEventArgs from the Paint event.

    I can use this code to get the Device Context

    Dim hWnd as IntPtr
    Dim hDC as IntPtr

    hWnd = Me.Handle
    hDC = Graphics.FromHwnd(hWnd).GetHdc

    This seems to work OK BUT the documentation says that all calls to GetDc should be followed by calls to ReleaseDC and I can't figure out how to release the Device Context.

    Has anyone tried anything like this?

    Better still, does anybody know how to create ellipsed paths using pure .NET instead of using the API functions?

    Regards,
    Kevin Bell

  2. #2
    Silver Lounger
    Join Date
    Jan 2001
    Location
    Indianapolis, Indiana, USA
    Posts
    1,862
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Device Context (VB .NET 2003)

    Hi Kevin,

    I'm certainly no expert on the Windows side of .NET, but it seems that one would benefit most by using the Framework's native methods rather than API calls (where possible).

    The methods you're looking for can be found in the System.Drawing namespace under the Graphics class (Draw Bezier, Draw Curve, Draw Ellipse, etc.)

    Here's a site with an example of the Ellipse: http://www.vb-helper.com/howto_net_hatched_ellipse.html

    Hope this helps!

  3. #3
    3 Star Lounger
    Join Date
    Aug 2001
    Location
    Jeddah, Saudi Arabia
    Posts
    243
    Thanks
    0
    Thanked 1 Time in 1 Post

    Re: Device Context (VB .NET 2003)

    Thanks Mark, I'm sorry but I didn't describe my problem correctly. It's not a ellipse that I'm trying to draw.

    I want to make sure that a string containing a fully qualified path, for example, crogram filesvisual basicsome other folderand yet another foldersome file name.xxx, will fit into a control's Text property by replacing part of the path with an ellipsis character (...), for example crogram files...and yet another foldersome file name.xxx

    The DrawText API call performs this function but it needs a device context for the font information amongst other things.

    There is the StringFormat object that has a Trimming property that let's you specify how the string is to be trimmed but for the life of me I can't figure out how to use it to convert a string like the one above into an ellipsed path.

    Regards,
    Kevin Bell

  4. #4
    Silver Lounger
    Join Date
    Jan 2001
    Location
    Indianapolis, Indiana, USA
    Posts
    1,862
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Device Context (VB .NET 2003)

    Ahh - <img src=/S/blush.gif border=0 alt=blush width=15 height=15> I should know better than trying to answer code-related questions so early on a Saturday morning <img src=/S/laugh.gif border=0 alt=laugh width=15 height=15>

    In that instance, I would probably use functionality of the String class - Substring, in particular.

    This function will allow you to set the number of characters to show at the beginning and end, then add ellipsis in the middle: <pre>Private Function GetPartialPath(ByVal fullPath As String) As String
    Dim startingLengthToShow As Integer = 20 'length to show at beginning
    Dim endingLengthToShow As Integer = 10 'length to show at end
    Dim partialPath As String = "" 'partial path to return

    'If full path is longer than startingLengthToShow and endingLengthToShow
    If fullPath.Length > startingLengthToShow + endingLengthToShow Then
    'read first characters, add ellipsis, read last characters
    partialPath = fullPath.Substring(0, startingLengthToShow) _
    & " ... " & fullPath.Substring(fullPath.Length - endingLengthToShow)
    Else
    'return fullPath
    partialPath = fullPath
    End If

    Return partialPath
    End Function</pre>


    You can use the following function to return the first foldername, followed by an ellipsis, follwed by the last filename:<pre>Private Function GetPartialPath2(ByVal fullPath As String) As String
    Dim partialPath As String = "" 'partial path to return
    Dim startingLengthToShow As Integer 'length to show at beginning
    Dim endingLengthToShow As Integer 'length to show at end

    'Find second backslash (leaving first folder visible)
    startingLengthToShow = fullPath.IndexOf("", fullPath.IndexOf("") + 1) + 1

    'Find last backslash (leaving filename visible)
    endingLengthToShow = fullPath.Length - fullPath.LastIndexOf("")

    'If full path is longer than startingLengthToShow and endingLengthToShow
    If fullPath.Length > startingLengthToShow + endingLengthToShow Then
    'read first characters, add ellipsis, read last characters
    partialPath = fullPath.Substring(0, startingLengthToShow) _
    & " ... " & fullPath.Substring(fullPath.Length - endingLengthToShow)
    Else
    'return fullPath
    partialPath = fullPath
    End If

    Return partialPath

    End Function</pre>

    I realize it's somewhat redundant to use the fullPath.Length twice, but I did that to keep the If logic consistent with the first function.

    Please let me know if you have any questions - or if this didn't quite get what you needed. <img src=/S/bow.gif border=0 alt=bow width=15 height=15>

  5. #5
    3 Star Lounger
    Join Date
    Aug 2001
    Location
    Jeddah, Saudi Arabia
    Posts
    243
    Thanks
    0
    Thanked 1 Time in 1 Post

    Re: Device Context (VB .NET 2003)

    Thanks for the code Mark. There are actually 3 API calls that will make an ellipsed path. Two of them, DrawText and PathCompactPath require a handle to the device context. The third, PathCompactPathEx does not so I'm going to use that one.

    The code module I have created is:

    Module MakeEllipsedPath

    Private Const MAX_PATH As Integer = 260

    Private Declare Function PathCompactPathEx Lib "shlwapi.dll" Alias "PathCompactPathExA" _
    (ByVal pszOut As String, _
    ByVal pszSrc As String, _
    ByVal cchMax As Integer, _
    ByVal dwFlags As Integer) As Integer

    Public Function EllipsedPath(ByVal sPath As String, ByVal cchMax As Integer) As String
    Dim ret As Integer, buff As String

    '
    ' sPath is the string containing the path to be ellipsed
    ' cchMax is the maximum number of characters allowed in the ellipsed path

    cchMax += 1
    buff = Space(MAX_PATH)

    ret = PathCompactPathEx(buff, sPath, cchMax, 0)

    Return TrimNull(buff)

    End Function
    Public Function TrimNull(ByVal startstr As String) As String

    If startstr.LastIndexOf(ControlChars.NullChar) <= 0 Then
    TrimNull = startstr
    Else
    TrimNull = startstr.Substring(0, startstr.LastIndexOf(ControlChars.NullChar))
    End If

    End Function

    End Module

    This works a treat but I am still convinced that there is a way to do this using the .NET framework without having to call the API. I will investigate, it will not defeat me, I will rule the world ..... err, sorry, got a bit carried away there.

    Regards,

    Kevin Bell

Posting Permissions

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