...ing logging 4.0

はてなブログに移行しました。D言語の話とかいろいろ。

DFL: ProgressBar のサンプルコード

DFL の ProgressBar クラスを更新しました。 表示形式を動的に設定できるようにしたり、マーキー形式の描画間隔を設定できるようにしたり。

さて、Windows の仕様で、ブロック形式(BLOCKS)の表示方法は、クラシックスタイルでしか使えません。こちらがクラシックスタイル。

下がビジュアルスタイルです。ビジュアルスタイルでは、スムーズ形式(CONTINUOUS)と区別が付かないようになってしまいます。

右下図のマーキー形式(MARQUEE)は、段階が数えられないような場合に使う表示形式で、ずっとバーが動き続けます。 こちらはビジュアルスタイルでしか使えません。

import dfl;

version(Have_dfl) // For DUB.
{
}
else
{
    pragma(lib, "dfl.lib");
}

class MainForm : Form
{
    private ProgressBar _progress;
    private Button _incre;
    private Button _reset;
    private ComboBox _mode;

    public this()
    {
        this.text = "ProgressBar example";
        this.size = Size(300, 300);

        _progress = new ProgressBar();
        _progress.parent = this;
        _progress.location = Point(20, 100);
        _progress.size = Size(200, 30);
        _progress.minimum = 0;
        _progress.maximum = 100;
        _progress.step = 5;
        _progress.value = 50;
        _progress.marqueeAnimationSpeed = 0; // Default; 30 ms.

        _mode = new ComboBox();
        _mode.parent =  this;
        _mode.location = Point(20, 150);
        _mode.dropDownStyle = ComboBoxStyle.DROP_DOWN_LIST;
        _mode.items.add("BLOCKS");
        _mode.items.add("CONTINUOUS");
        _mode.items.add("MARQUEE");
        _mode.selectedIndex = 0;
        _mode.selectedValueChanged ~= (Control c, EventArgs e)
        {
            if (_mode.selectedItem.toString() == "BLOCKS")
            {
                _progress.style = ProgressBarStyle.BLOCKS; // ビジュアルスタイルではCONTINUOUSと同じ
            }
            else if (_mode.selectedItem.toString() == "CONTINUOUS")
            {
                _progress.style = ProgressBarStyle.CONTINUOUS;
            }
            else if (_mode.selectedItem.toString() == "MARQUEE")
            {
                _progress.style = ProgressBarStyle.MARQUEE; // ビジュアルスタイルのみ
            }
        };

        _incre = new Button();
        _incre.parent = this;
        _incre.location = Point(20, 20);
        _incre.text = "Increment";
        _incre.click ~= (Control c, EventArgs e)
        {
            if (_progress.style == ProgressBarStyle.MARQUEE)
                msgBox("Can't increment.");
            else
            {
                // 事前に設定した固定値分だけ進める
                _progress.performStep();
                // 任意の値分だけ進める
                _progress.increment(5);
            }
        };

        _reset = new Button();
        _reset.parent = this;
        _reset.location = Point(120, 20);
        _reset.text = "Reset";
        _reset.click ~= (Control c, EventArgs e)
        {
            if (_progress.style == ProgressBarStyle.MARQUEE)
                msgBox("Can't reset.");
            else
                _progress.value = 50;
        };
    }
}

static this()
{
    Application.enableVisualStyles(); // ビジュアルスタイルにする(コメントアウトして実験)
}

void main()
{
    Application.run(new MainForm());
}

DFL のダウンロード

github.com

参考文献