How to make RightToLeftLayout work for controls inside GroupBoxes and Panels?

前端 未结 4 1947
执念已碎
执念已碎 2020-12-21 00:22

According to MSDN

form.RightToLeftLayout = True;
form.RightToLeft = ifWeWantRTL() ? RightToLeft.True : RightToLeft.False;

is enough to mir

相关标签:
4条回答
  • 2020-12-21 01:04

    If you have a class derived from Control that contains child controls (like a ContainerControl), you can add the following code to force all child controls to mirror when the parent form's RightToLeftLayout is set to true and when your control's RightToLeft is set to RightToLeft.Yes.

    protected override CreateParams CreateParams
    {
        get
        {
            CreateParams createParams = base.CreateParams;
            Form parent = this.FindForm();
            bool parentRightToLeftLayout = parent != null ? parent.RightToLeftLayout : false;
            if ((this.RightToLeft == RightToLeft.Yes) && parentRightToLeftLayout)
            {
                createParams.ExStyle |= 0x500000; // WS_EX_LAYOUTRTL | WS_EX_NOINHERITLAYOUT
                createParams.ExStyle &= ~0x7000; // WS_EX_RIGHT | WS_EX_RTLREADING | WS_EX_LEFTSCROLLBAR
            }
            return createParams;
        }
    }
    
    protected override void OnRightToLeftChanged(EventArgs e)
    {
        base.OnRightToLeftChanged(e);
        RecreateHandle();
    }
    
    0 讨论(0)
  • 2020-12-21 01:08

    According to the article Visual Studio 2005: Developing Arabic Windows Forms applications I am left with just two alternatives

    • continue adding TableLayoutPanels here and there
    • reposition child controls on RTL change myself

    It is a real pity that it has to be that way.

    0 讨论(0)
  • 2020-12-21 01:19

    It does seen that you have quite a nasty problem on your hands. Have played with it for a while and come up with the following:

    Making use of a little recursion you can run though all the controls and do the manaul RTL conversion for those controls trapped in Pannels and GroupBoxes.

    This is a quick little mock of code that I slapped together. I would suggest you put this in your BaseForm (heres hoping you have one of these) and call on base form load.

    private void SetRTL (bool setRTL)
    {
        ApplyRTL(setRTL, this);
    }
    
    private void ApplyRTL(bool yes, Control startControl)
    {
        if ((startControl is Panel ) || (startControl is GroupBox))
        {
            foreach (Control control in startControl.Controls)
            {
                control.Location = CalculateRTL(control.Location, startControl.Size, control.Size);
            }
        }
    
        foreach (Control control in startControl.Controls)
            ApplyRTL(yes, control);
    }
    
    private Point CalculateRTL (Point currentPoint, Size parentSize, Size currentSize)
    {
        return new Point(parentSize.Width - currentSize.Width - currentPoint.X, currentPoint.Y);
    }
    
    0 讨论(0)
  • 2020-12-21 01:28

    i dont remember where i first saw this tip on overriding CreateParams, but here you are ;) fastest, and easiest way is to Inherit from the Panel, GroupBox or Usercontrol and override the CreateParams Property

        protected override CreateParams CreateParams
        {
            get
            {
                return Control_RTF(base.CreateParams, base.RightToLeft);
            }
        }
    
        private CreateParams Control_RTF(CreateParams CP, RightToLeft rightToLeft)
        {
            if (rightToLeft == System.Windows.Forms.RightToLeft.Yes)
                CP.ExStyle = ((CP.ExStyle | 0x400000) | 0x100000);
            return CP;
        }
    
    0 讨论(0)
提交回复
热议问题