【问题标题】:Right to Left Layout Property in Below Code下面代码中从右到左的布局属性
【发布时间】:2017-01-04 07:15:35
【问题描述】:

我正在为 Windows CE .Net 2.0 C# 应用程序创建自己的自定义进度条控件。下面是运行良好的代码,唯一缺少的是我想从右到左显示进度任何人都可以帮助我修改此代码以获取 RTL 属性

Code Refering

using System;
using System.Drawing;
using System.Windows.Forms;
class CustomProgressBar:UserControl
{
    int min = 0;    // Minimum value for progress range
    int max = 100;  // Maximum value for progress range
    int val = 0;        // Current progress
    Color BarColor = Color.Blue;        // Color of progress meter
    protected override void OnResize(EventArgs e)
    {
        // Invalidate the control to get a repaint.
        this.Invalidate();
    }
    protected override void OnPaint(PaintEventArgs e)
    {
        Graphics g = e.Graphics;
        SolidBrush brush = new SolidBrush(BarColor);
        float percent = (float)(val - min) / (float)(max - min);
        Rectangle rect = this.ClientRectangle;

        // Calculate area for drawing the progress.
        rect.Width = (int)((float)rect.Width * percent);

        // Draw the progress meter.
        g.FillRectangle(brush, rect);

        // Draw a three-dimensional border around the control.
        Draw3DBorder(g);

        // Clean up.
        brush.Dispose();
        g.Dispose();
    }

    public int Minimum
    {
        get
        {
            return min;
        }

        set
        {
            // Prevent a negative value.
            if (value < 0)
            {
                min = 0;
            }

            // Make sure that the minimum value is never set higher than the maximum value.
            if (value > max)
            {
                min = value;
                min = value;
            }

            // Ensure value is still in range
            if (val < min)
            {
                val = min;
            }

            // Invalidate the control to get a repaint.
            this.Invalidate();
        }
    }

    public int Maximum
    {
        get
        {
            return max;
        }

        set
        {
            // Make sure that the maximum value is never set lower than the minimum value.
            if (value < min)
            {
                min = value;
            }

            max = value;

            // Make sure that value is still in range.
            if (val > max)
            {
                val = max;
            }

            // Invalidate the control to get a repaint.
            this.Invalidate();
        }
    }

    public int Value
    {
        get
        {
            return val;
        }

        set
        {
            int oldValue = val;

            // Make sure that the value does not stray outside the valid range.
            if (value < min)
            {
                val = min;
            }
            else if (value > max)
            {
                val = max;
            }
            else
            {
                val = value;
            }

            // Invalidate only the changed area.
            float percent;

            Rectangle newValueRect = this.ClientRectangle;
            Rectangle oldValueRect = this.ClientRectangle;

            // Use a new value to calculate the rectangle for progress.
            percent = (float)(val - min) / (float)(max - min);
            newValueRect.Width = (int)((float)newValueRect.Width * percent);

            // Use an old value to calculate the rectangle for progress.
            percent = (float)(oldValue - min) / (float)(max - min);
            oldValueRect.Width = (int)((float)oldValueRect.Width * percent);

            Rectangle updateRect = new Rectangle();

            // Find only the part of the screen that must be updated.
            if (newValueRect.Width > oldValueRect.Width)
            {
                updateRect.X = oldValueRect.Size.Width;
                updateRect.Width = newValueRect.Width - oldValueRect.Width;
            }
            else
            {
                updateRect.X = newValueRect.Size.Width;
                updateRect.Width = oldValueRect.Width - newValueRect.Width;
            }

            updateRect.Height = this.Height;

            // Invalidate the intersection region only.
            this.Invalidate(updateRect);
        }
    }

    public Color ProgressBarColor
    {
        get
        {
            return BarColor;
        }

        set
        {
            BarColor = value;

            // Invalidate the control to get a repaint.
            this.Invalidate();
        }
    }

    private void Draw3DBorder(Graphics g)
    {
        int PenWidth = (int)Pens.White.Width;

        g.DrawLine(Pens.DarkGray,
            new Point(this.ClientRectangle.Left, this.ClientRectangle.Top),
            new Point(this.ClientRectangle.Width - PenWidth, this.ClientRectangle.Top));
        g.DrawLine(Pens.DarkGray,
            new Point(this.ClientRectangle.Left, this.ClientRectangle.Top),
            new Point(this.ClientRectangle.Left, this.ClientRectangle.Height - PenWidth));
        g.DrawLine(Pens.White,
            new Point(this.ClientRectangle.Left, this.ClientRectangle.Height - PenWidth),
            new Point(this.ClientRectangle.Width - PenWidth, this.ClientRectangle.Height - PenWidth));
        g.DrawLine(Pens.White,
            new Point(this.ClientRectangle.Width - PenWidth, this.ClientRectangle.Top),
            new Point(this.ClientRectangle.Width - PenWidth, this.ClientRectangle.Height - PenWidth));
    }
}

【问题讨论】:

  • 从 MAX 开始,一直到 MIN。
  • 我的代码在哪里修改
  • 我不知道在哪里修改“你的”代码,因为我没有看到你的代码。我的意思是,不是从左(MIN)前进到右(MAX),而是从右(MAX)前进到左(MIN)。假设您的代码控制进度何时更新,然后只需调整代码以减少当前值。您发布的代码只是指向 MS 支持站点的链接。你需要展示你已经尝试过的东西,然后人们才能提供帮助。您应该查看以下内容...How to create a Minimal, Complete, and Verifiable example

标签: c# winforms compact-framework windows-ce


【解决方案1】:

在上面的代码中,进度矩形是这样创建的:

float percent = (float)(val - min) / (float)(max - min);
Rectangle rect = this.ClientRectangle;
rect.Width = (int)((float)rect.Width * percent);

您可以简单地将代码更改为以下代码,以从控件右侧填充进度矩形:

float percent = (float)(val - min) / (float)(max - min);
Rectangle rect = this.ClientRectangle;
var w = (int)((float)rect.Width * percent);
rect.X = rect.Width - w;
rect.Width = w;

【讨论】:

  • 注1:作为将LTR坐标转换为RTL的通用方案,可以使用GetRTLCoordinates from this post
  • 注2:作为镜像绘制的另一种通用解决方案,您可以使用e.Graphics.Transform = new Matrix(-1, 0, 0, 1, Width, 0);这种方式使用变换矩阵,然后执行绘制,然后使用e.Graphics.ResetTransform();重置变换
  • 受保护的覆盖无效 OnPaint(PaintEventArgs e) { 图形 g = e.Graphics; SolidBrush 笔刷 = 新的 SolidBrush(BarColor);浮动百分比 = (float)(val - min) / (float)(max - min);矩形 rect = this.ClientRectangle; var w = (int)((float)rect.Width * percent); rect.X = rect.Width - w; rect.Width = w; g.FillRectangle(brush,rect); Draw3DBorder(g);刷子.Dispose(); g.处置(); } 什么都没发生还有什么办法?
  • 这是因为Value 属性,代码使刚刚更新的部分无效。您可以以相同的方式更正坐标,或者作为一种简单的替代方法,您可以通过删除 Value` 属性中 // Invalidate only the changed area. 之后的所有代码并将其替换为 this.Invalidate(); 来简单地修复它。
  • 一个小查询,您可以看到限制在 0 到 100 之间,我可以将最小值和最大值设为负值吗? -100 到 50 , -50 到 0
猜你喜欢
  • 1970-01-01
  • 2013-10-02
  • 1970-01-01
  • 1970-01-01
  • 2021-01-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-09-12
相关资源
最近更新 更多