ElrondCT
asked on
PathCompactPath parameters
I want to use the PathCompactPath API to make a long file name fit into a fixed label width. The API guide I have lists three parameters: the string with the path name, the maximum width in pixels, and "hDC: Handle to the device context used for font metrics." The first two are obvious; the third one has me baffled, and checking a number of online references to the API hasn't helped. Some of them have VB code that uses "Me.hDC" as this parameter passed, but it I try that I get an error of "hdc is not a member of frmMain." And really I wouldn't expect to refer to something at the form level; I would think what I really want to pass is a Font. But the parameter is supposed to be a Long, so a Font won't work. I see a GetHDC method for Graphics objects, which sounds like it might be usable as part of this, but I don't see how to associate a font with that without actually drawing something on the form, which I'm not interested in doing.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
You could also draw the string yourself using Graphics.DrawString() with the StringFormat.StringTrimmin g.Ellipsis Path flag as in this example:
https://www.experts-exchange.com/questions/21407534/Shorten-filepath-with-ellipsis.html
https://www.experts-exchange.com/questions/21407534/Shorten-filepath-with-ellipsis.html
ASKER
Drawing the string directly doesn't work, because this is actually going into a panel of a status bar, not a label. Thanks anyway.
Hi ElrondCT;
To your question, "what is the purpose of using the stringbuilder, rather than a simple string of the path?". Because the String Class is a immutable object and the non .Net API will change its value I feel that it is safer to use a class that would not have to be recreated when the value is changed.
Fernando
To your question, "what is the purpose of using the stringbuilder, rather than a simple string of the path?". Because the String Class is a immutable object and the non .Net API will change its value I feel that it is safer to use a class that would not have to be recreated when the value is changed.
Fernando
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Idle_Mind, unfortunately the exception problem became worse when I put the Dispose method into my code. My actual code, in both the Form Resize sub and my File Open sub, is:
Dim strCompactFile As String = strFullFile
Dim g As Graphics = StatusBar1.CreateGraphics
Dim hDC As IntPtr = g.GetHdc()
PathCompactPath(hDC, strCompactFile, ToInt64(sbpFile.Width * 1.2))
sbpFile.Text = "File: " & strCompactFile
strCompactFile = Nothing
'g.Dispose()
When I uncomment the g.Dispose(), I get the InvalidOperationException: "The object is currently in use elsewhere" as soon as the form loads. When I have it commented out, the exception pops up only after doing a substantial amount of resizing. And I don't really expect my users to be doing any resizing at all, because there's no benefit to them of doing it, though at this point I'm not preventing it.
Dim strCompactFile As String = strFullFile
Dim g As Graphics = StatusBar1.CreateGraphics
Dim hDC As IntPtr = g.GetHdc()
PathCompactPath(hDC, strCompactFile, ToInt64(sbpFile.Width * 1.2))
sbpFile.Text = "File: " & strCompactFile
strCompactFile = Nothing
'g.Dispose()
When I uncomment the g.Dispose(), I get the InvalidOperationException:
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Thanx Bob...was just about to post that as well... =)
It seems like a tag team thing sometimes, huh? ;) =:)
Bob
Bob
ASKER
That makes sense; I should have thought about that. I discovered that "Hide advanced members" was turned on for statement completion so Intellisense wasn't displaying methods like ReleaseHdc, which didn't make it any easier... I now seem to be in good shape on resizing as far as not crashing, though I don't understand why the measurement I'm giving PathCompactPath isn't working quite right. But again, I don't expect much resizing, so that's not important.
I appreciate everyone's help. I'm very much still in learning mode on VB, and since I'm a one-man computer department, your assistance is invaluable. There's so much I don't know that it seems rather scary that I'm more than halfway to EE's "master" level with the questions I've answered here, but I guess that means I'm actually learning something!
I appreciate everyone's help. I'm very much still in learning mode on VB, and since I'm a one-man computer department, your assistance is invaluable. There's so much I don't know that it seems rather scary that I'm more than halfway to EE's "master" level with the questions I've answered here, but I guess that means I'm actually learning something!
Yeah, with the amount of questions asked, you can just keep answering the same questions over and over again, and pile up the points and not learn many new things.
Bob
Bob
ASKER
Well, I often will intentionally look at questions that I don't know the answer to, if they seem relevant to what I am or might soon be doing, and I'm gleaning a lot of ideas from what the rest of you post. (Whether I'll remember them when I need them is another question.) I've been programming for a long time (longer than some of the people here have been alive), but had only dabbled in object-oriented programming before last year, so there's a lot of new stuff to learn. Nonetheless, I'm finding that a lot of the "tricks of the trade" do carry over from one language to another, which is part of what enables me to answer questions here.
ASKER
FernandoSoto, what is the purpose of using the stringbuilder, rather than a simple string of the path?