控制 LTR 和 RTL 语言之间的对齐切换

Control alignment switching between LTR and RTL languages(控制 LTR 和 RTL 语言之间的对齐切换)

本文介绍了控制 LTR 和 RTL 语言之间的对齐切换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

虽然这个问题很笼统,可以应用于网络,但我对 WinForms 尤其感兴趣.

Although this question is general enough to apply to the web, I'm interested in WinForms in particular.

应用程序 UI 在 LTR 和 RTL 语言之间切换而不会发生意外.唯一的障碍是放置与文本框等输入控件相关的标签.

The application UI switches between LTR and RTL languages without incident. The only obstacle is placement of labels that are associated with input controls such as text boxes.

从左到右:

从右到左:

RTL 图像上的标签位置也应相应更改.

The label placement on the RTL image should also change accordingly.

是否有一种通用的、程序化的方式来实现这一点?

Is there a generalized, programmatic way to achieve this?

推荐答案

选项 1 - 镜像表单(也可以镜像标题栏)​​

如果 RightToLeftLayoutRightToLeft 属性都为 true,则会为表单打开镜像,并且控件放置和文本流将是从右到左的.因此,将 RightToLeftLayout 设置为 true 并将 RightToLeft 设置为 yes 以获得完整的从右到左布局.

If both the RightToLeftLayout and RightToLeft properties are true, mirroring will be turned on for the form, and control placement and text flow will be right-to-left. So set RightToLeftLayout to true and set RightToLeft to yes to have a complete right to left layout.

这样,表单标题栏也将被镜像,控制框将显示在左侧.

This way also the form title bar will be mirrored and control box will be shown at left.

选项 2 - 镜像面板(不镜像标题栏)​​

如果您不喜欢从右到左的标题栏和左侧的控件框,您应该自己创建从右到左的容器并将控件放入其中,然后设置 RightToLeftLayoutcontainer 为 true 并将容器的 RightToLeft 设置为 yes 以在容器中具有完整的从右到左的布局,而不改变标题栏和控制框的布局:

If you don't like to have right to left title bar and the control box at left, you should create your right to left container yourself and put controls in it and then set RightToLeftLayout of the container to true and set RightToLeft of the container to yes to have a complete right to left layout in the container without changing the layout of title bar and control box:

using System;
using System.ComponentModel;
using System.Windows.Forms;
public class ExPanel : Panel
{
    const int WS_EX_LAYOUTRTL = 0x400000;
    const int WS_EX_NOINHERITLAYOUT = 0x100000;
    private bool rightToLeftLayout = false;

    [Localizable(true)]
    public bool RightToLeftLayout
    {
        get { return rightToLeftLayout; }
        set
        {
            if (rightToLeftLayout != value)
            {
                rightToLeftLayout = value;
                this.RecreateHandle();
            }
        }
    }
    protected override CreateParams CreateParams
    {
        get
        {
            CreateParams CP;
            CP = base.CreateParams;
            if (this.RightToLeftLayout &&
                this.RightToLeft == System.Windows.Forms.RightToLeft.Yes)
                CP.ExStyle = CP.ExStyle | WS_EX_LAYOUTRTL | WS_EX_NOINHERITLAYOUT;
            return CP;
        }
    }
}

截图

这是选项1的屏幕截图.查看标题栏左侧的关闭按钮:

Here is a screenshot of Option 1. Look at Close button at left side of title bar:

这是选项2的截图.查看标题栏右侧的关闭按钮:

Here is a screenshot of Option 2. Look at Close button at right side of title bar:

这篇关于控制 LTR 和 RTL 语言之间的对齐切换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:控制 LTR 和 RTL 语言之间的对齐切换

基础教程推荐