Add Ellipsis to a Path in a WinForms Program without Win32 API call (revisited)

后端 未结 2 609
离开以前
离开以前 2021-01-01 02:50

I was searching for a way to insert an ellipsis in a C# path, and found an answer here on stackoverflow: C# Path Ellipsis without Win32 API call

Using the RTM versio

2条回答
  •  情话喂你
    2021-01-01 03:01

    My original string is changed to be identical to the output string.

    You've asked for this to happen by specifying TextFormatFlags.ModifyString, which the docs say

    Modifies the specified string to match the displayed text. This value has no effect unless EndEllipsis or PathEllipsis is also specified.

    This is (to my mind) an unusual way for a .NET Framework call to operate, but it does clearly say it will do this. Both the 'original' string and the 'output' string end up being modified, because string is a reference type (though usually with immutable value semantics) - when you say

    string ellipsisedPath = OriginalPath;
    

    you are actually just making ellipsisedPath refer to the same string instance as OriginalPath does. When this instance gets modified by the API call, both the references to it will see the modification.

    As for

    the path is truncated as expected, but is followed by what looks like a C-style terminating null and a chunk of the original path

    my guess would be that the abstraction this managed wrapper provides around the Win32 API call is being somewhat leaky, as abstractions are prone to being - it's not shielding you from the fact that the underlying call works with C-style strings. It might be that you'll have to deal with yourself.

提交回复
热议问题