Converting Dialog Units into Pixels
I've been trying to convert the measurements for Wizards from the Wizard97 specification into pixels for god knows how long now and I can't ever seem to get it to look right at all.
If you see this page on Welcome and Completion Page Layouts, all the measurements are in Dialog Units for some special reason I can't recall at this point in time.
Anyway, after spending a while looking for the stuff todo it, I found the GetDialogBaseUnits() function, made a little wrapper for it and when I try to align the controls on my form to the measurements provided by the specification, they are nowhere near them.
Here's my attempt at wrapping the API up:
VB Code:
Public Class DialogUnits
Private Declare Function GetDialogBaseUnits Lib "user32" () As Integer
Private _Width As Integer
Private _Height As Integer
Public Sub New()
Call CalculateDialogUnits()
End Sub
Public ReadOnly Property Width() As Integer
Get
Return _Width
End Get
End Property
Public ReadOnly Property Height() As Integer
Get
Return _Height
End Get
End Property
Public Function GetHeightInPixels(ByVal dialogUnits As Integer) As Integer
Return (_Height * dialogUnits) / 8
End Function
Public Function GetWidthInPixels(ByVal dialogUnits As Integer) As Integer
Return (_Width * dialogUnits) / 4
End Function
Public ReadOnly Property Size() As SizeF
Get
Return New SizeF(_Width, _Height)
End Get
End Property
Private Sub CalculateDialogUnits()
Dim DialogUnits As Long = GetDialogBaseUnits
_Width = LongManipulation.GetLoWord(DialogUnits)
_Height = LongManipulation.GetHiWord(DialogUnits)
End Sub
End Class
And the LongManipulation class:
VB Code:
Public Class LongManipulation
Public Shared Function GetLoWord(ByVal sourceLong As Long) As Integer
Return (sourceLong / &H10000) And &HFFFF&
End Function
Public Shared Function GetHiWord(ByVal sourceLong As Long) As Integer
Return sourceLong And &HFFFF&
End Function
Public Shared Function IntegersToLong(ByVal loWord As Integer, ByVal hiWord As Integer) As Long
Return (hiWord * &H10000) + (loWord And &HFFFF&)
End Function
End Class
Can someone please show me where I'm going wrong because I got no idea what I'm doing wrong. It seems to be logically correct, but something's a miss.
Re: Converting Dialog Units into Pixels
Re: Converting Dialog Units into Pixels
VB Code:
Public Class LongManipulation
Public Shared Function GetLoWord(ByVal sourceLong As Long) As Integer
Return CType(sourceLong And &HFFFF&, Integer)
End Function
Public Shared Function GetHiWord(ByVal sourceLong As Long) As Integer
Return CType((sourceLong >> 16) And &HFFFF&, Integer)
End Function
Public Shared Function IntegersToLong(ByVal loWord As Integer, ByVal hiWord As Integer) As Long
Return (hiWord << 16) Or loWord
End Function
End Class
Re: Converting Dialog Units into Pixels
... that doesn't exactly help me :confused:. I'm not worried about the IntegersToLong function, that was just something that was tagged on when I found that code, and the KB article didn't help all too much sorry.
Re: Converting Dialog Units into Pixels
I revised my code again, try it now.
Re: Converting Dialog Units into Pixels
Quote:
Originally Posted by Ideas Man
I'm not worried about the IntegersToLong function
Perhaps you should be.
You just don't do binary operations with division and multiplication operators. Especially when extracting words.
Use bitshift operators (<<, >>) instead.
If you use divide instead of >> then you will get incorrect results because the low byte is included in the operation, >> 16 ignores the low byte.
That whole LongManipulation class looked a bit suspect if you ask me.
Re: Converting Dialog Units into Pixels
No, doesn't seem to.
Have a go, put a label on the form and use my functions to try and place it as they have it on the form.
Re: Converting Dialog Units into Pixels
Quote:
Originally Posted by wossname
Perhaps you should be.
You just don't do binary operations with division and multiplication operators. Especially when extracting words.
Use bitshift operators (<<, >>) instead.
If you use divide instead of >> then you will get incorrect results because the low byte is included in the operation, >> 16 ignores the low byte.
That whole LongManipulation class looked a bit suspect if you ask me.
Well, I didn't supply the code for it, so I don't know how it works, it just "seemed" to work.
1 Attachment(s)
Re: Converting Dialog Units into Pixels
Quote:
Originally Posted by Ideas Man
No, doesn't seem to.
Have a go, put a label on the form and use my functions to try and place it as they have it on the form.
Looks OK to me.
(I didn't know what the font for that label was supposed to be so i took a very rough guess)...
VB Code:
Public Class DialogUnits
Private Declare Function GetDialogBaseUnits Lib "user32" () As Integer
Private _Width As Integer
Private _Height As Integer
Public Sub New()
Call CalculateDialogUnits()
End Sub
Public ReadOnly Property Width() As Integer
Get
Return _Width
End Get
End Property
Public ReadOnly Property Height() As Integer
Get
Return _Height
End Get
End Property
Public Function GetHeightInPixels(ByVal dialogUnits As Integer) As Integer
Return (_Height * dialogUnits) \ 8
End Function
Public Function GetWidthInPixels(ByVal dialogUnits As Integer) As Integer
Return (_Width * dialogUnits) \ 4
End Function
Public ReadOnly Property Size() As SizeF
Get
Return New SizeF(_Width, _Height)
End Get
End Property
Private Sub CalculateDialogUnits()
Dim DialogUnits As Long = GetDialogBaseUnits
_Width = LongManipulation.GetLoWord(DialogUnits)
_Height = LongManipulation.GetHiWord(DialogUnits)
End Sub
End Class
Public Class LongManipulation
Public Shared Function GetLoWord(ByVal sourceLong As Long) As Integer
Return CType(sourceLong And &HFFFF&, Integer)
End Function
Public Shared Function GetHiWord(ByVal sourceLong As Long) As Integer
Return CType((sourceLong >> 16) And &HFFFF&, Integer)
End Function
Public Shared Function IntegersToLong(ByVal hiWord As Integer, ByVal loWord As Integer) As Long
Return (hiWord << 16) Or loWord
End Function
End Class
Re: Converting Dialog Units into Pixels
Here's the code for the button...
VB Code:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim du As DialogUnits = New DialogUnits
With Label1
.Location = New Point(du.GetWidthInPixels(115), du.GetHeightInPixels(8))
.Size = New Size(du.GetWidthInPixels(195), du.GetHeightInPixels(24))
End With
Me.Size = New Size(du.GetWidthInPixels(317), du.GetHeightInPixels(193))
End Sub
Re: Converting Dialog Units into Pixels
Nah, see that's too big. Compare the form when you click the button to the picture or any wizard in Windows such as the New Connection Wizard in Windows XP. The inner area is bigger than the entire form for the wizard.
Which makes me think that the numbers are supposed to be some decimal number, not whole integers because when you divide by 4 and 8 for width and height respectivly, you get like 1 & 2 (or 2 & 1, one or the other) which is too big, and when you set it to the size of the dialog units, it's too small.
BTW, the title font is Verdana size 12, Bold.