Problem with GetTextExtentPoint32
-
Greetings all. I'm trying to meassure the contents of a string in a webapplication, and have problem with the GetTextExtentPoint32 function and how it works. (I'm using VB.NET cause that is what we have to program in at work, so eventhough this is more for my own benefit and interest, I'll still use VB) I've never used this API before, so please have some compasion for my newbie questions :) Most of this information has been put together using msdn and a few webpages (I lack some decent code exampels in VB which I can read from) So far what I've done is declaring a number of functions, amongst others: Declare Function GetDesktopWindow Lib "user32" () As Long Declare Function GetWindowDC Lib "user32" (ByVal hwnd As Long) As Long Private Declare Function GetTextExtentPoint32 Lib "gdi32" Alias "GetTextExtentPoint32A" _ (ByVal hDC As Long, ByVal lpsz As String, ByVal cbString As Long, ByRef lpSize As SIZE) As Long I've created a stucture: Private Structure SIZE Dim cx As Long Dim cy As Long End Structure Then in my function I do this: Dim sz As Size Dim strTestString As String = ""ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"" Dim hDc as long = GetWindowDC(GetDesktopWindow) Dim hFont = GetStockObject(ANSI_VAR_FONT) dim hFontOld = SelectObject(hDc, hFont&) As far as I understand hDc is the handler to the "screen-area" used for drawing. I'm by far not sure what hFont of hFontOld does (ANSI_VAR_FONT = 12), but font-buisness is a good guess I hope :D (Taken from websites, not my own) I then call: GetTextExtentPoint32(hDc, strTestString , 52, sz) 52 I guess is the number of characters in the string. (String and 52 is taken from websites, and not my own) And then I get a NullReference exception when running the code (and here I'm stuck)..... Does anybody have any information which can guide me further, or have some good code exampels I can use/read? I'd really like to this work, seeing as if it does I could perhaps implement it at work, but also because it is starting to nag at me, and I hate having to leave it unfinished. Please help if possible :) With regards and hopefullness "Alsvha" --------------------------- 127.0.0.1 - Sweet 127.0.0.1
-
Greetings all. I'm trying to meassure the contents of a string in a webapplication, and have problem with the GetTextExtentPoint32 function and how it works. (I'm using VB.NET cause that is what we have to program in at work, so eventhough this is more for my own benefit and interest, I'll still use VB) I've never used this API before, so please have some compasion for my newbie questions :) Most of this information has been put together using msdn and a few webpages (I lack some decent code exampels in VB which I can read from) So far what I've done is declaring a number of functions, amongst others: Declare Function GetDesktopWindow Lib "user32" () As Long Declare Function GetWindowDC Lib "user32" (ByVal hwnd As Long) As Long Private Declare Function GetTextExtentPoint32 Lib "gdi32" Alias "GetTextExtentPoint32A" _ (ByVal hDC As Long, ByVal lpsz As String, ByVal cbString As Long, ByRef lpSize As SIZE) As Long I've created a stucture: Private Structure SIZE Dim cx As Long Dim cy As Long End Structure Then in my function I do this: Dim sz As Size Dim strTestString As String = ""ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"" Dim hDc as long = GetWindowDC(GetDesktopWindow) Dim hFont = GetStockObject(ANSI_VAR_FONT) dim hFontOld = SelectObject(hDc, hFont&) As far as I understand hDc is the handler to the "screen-area" used for drawing. I'm by far not sure what hFont of hFontOld does (ANSI_VAR_FONT = 12), but font-buisness is a good guess I hope :D (Taken from websites, not my own) I then call: GetTextExtentPoint32(hDc, strTestString , 52, sz) 52 I guess is the number of characters in the string. (String and 52 is taken from websites, and not my own) And then I get a NullReference exception when running the code (and here I'm stuck)..... Does anybody have any information which can guide me further, or have some good code exampels I can use/read? I'd really like to this work, seeing as if it does I could perhaps implement it at work, but also because it is starting to nag at me, and I hate having to leave it unfinished. Please help if possible :) With regards and hopefullness "Alsvha" --------------------------- 127.0.0.1 - Sweet 127.0.0.1
GetTextExtents tells you the physical dimensions a string will require when graphically rendered on the specified DC using the specified font. .NET offers a similar method in Graphics.MeasureString(). The most obvious question is "What are you trying to do? Under what environments?" I'm not sure what value this API offers in a web application.
-
GetTextExtents tells you the physical dimensions a string will require when graphically rendered on the specified DC using the specified font. .NET offers a similar method in Graphics.MeasureString(). The most obvious question is "What are you trying to do? Under what environments?" I'm not sure what value this API offers in a web application.
What I'm trying to do is find information as to how "wide" a string would be if I were to write it to the screen. This would allow me to size menus, checkbox and all sorts of other onscreen containers et al, before actually having to write the string(s) to the screen and then meassure them up by hand. So what GetTextExtents tells me - is excatly the information I'd like to get ..... but I can't just get it to work, because of the NullReference (maybe because of the wrong DC) I tried dabbeling with Graphics.MeasureString, but fell short when I appaently needed to get the PaintEventArgs to run it from. Graphics.MeasureString wouldn't compile, but PaintEventArgs.Graphics.MeasureString would - so I discontinued this path, because I'm not sure I could get such an event for what I want to do. --------------------------- 127.0.0.1 - Sweet 127.0.0.1
-
What I'm trying to do is find information as to how "wide" a string would be if I were to write it to the screen. This would allow me to size menus, checkbox and all sorts of other onscreen containers et al, before actually having to write the string(s) to the screen and then meassure them up by hand. So what GetTextExtents tells me - is excatly the information I'd like to get ..... but I can't just get it to work, because of the NullReference (maybe because of the wrong DC) I tried dabbeling with Graphics.MeasureString, but fell short when I appaently needed to get the PaintEventArgs to run it from. Graphics.MeasureString wouldn't compile, but PaintEventArgs.Graphics.MeasureString would - so I discontinued this path, because I'm not sure I could get such an event for what I want to do. --------------------------- 127.0.0.1 - Sweet 127.0.0.1
You'll never get it to work in a web application. Since the browser can display data in any text size, independent of what you tell it, you can't get the length of the string as displayed in the browser. On top of that, that function will only work in a Windows Forms application. No Window means no window handle, and no hDC. The only way your going to accomplish this is with JavaScript on the client side built into the web page. No, I don't know how to do it. I just know how it's not going to work. RageInTheMachine9532 "...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome