开发者

Control Scaling Problem in Windows 7

开发者 https://www.devze.com 2023-04-08 13:13 出处:网络
I have some custom user controls in my .net winforms program that do not display correctly when the user has selected larger text size. This setting:

I have some custom user controls in my .net winforms program that do not display correctly when the user has selected larger text size. This setting:

Control Scaling Problem in Windows 7

My cont开发者_C百科rols look like this,

Control Scaling Problem in Windows 7

Instead of like this,

Control Scaling Problem in Windows 7

The bill to area and ship to area are both custom controls. I don't know if this is contributing to the problem but I do have code in each to help scale the phone/fax areas to stretch nicely, like this code from the bill to control,

    Private Sub panFaxPhone_Resize(sender As Object, e As System.EventArgs) Handles panFaxPhone.Resize
            panFax.Width = (panFaxPhone.Width / 2) - 1
            panPhone.Width = (panFaxPhone.Width / 2) - 1
            panFax.Left = panFaxPhone.Width - panFax.Width
    End Sub

How can I get my controls to size correctly while still respecting the users choice for larger text (I don't want to just set the AutoScaleMode to None) ?

Update: After playing with this for a long time it seems to be a problem with anchors in the child controls. See this below image, the inner black box is the control with its border turned on, the text boxes (like name) are anchored left and right and should stretch to fill the control, but don't.

Control Scaling Problem in Windows 7


It just plain seems like the default control scaling just isn't working with the anchors. I don't know why, and can't explain it. But I did find a work around. See below code I added to the control. If you can provide an explanation, I would appreciate it.

    Private ChildControlScale As Double = 0
    Public Sub New()

        ' This call is required by the designer.
        InitializeComponent()

        ' Add any initialization after the InitializeComponent() call.
        ChildControlScale = txtAddress.Width / Me.Width

    End Sub

    Protected Overrides Sub ScaleControl(factor As System.Drawing.SizeF, specified As System.Windows.Forms.BoundsSpecified)
        MyBase.ScaleControl(factor, specified)
        If ChildControlScale <> 0 Then
            For Each ctrl As Control In Me.Controls
                ctrl.Width = Me.Width * ChildControlScale
            Next
        End If
    End Sub
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号