ProgressBar 的颜色没有改变 c# [英] Color of ProgressBar is not changing c#

查看:41
本文介绍了ProgressBar 的颜色没有改变 c#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的要求是每当我单击按钮时将进度条的颜色更改为红色.我不想注释掉 Application.EnableVisualStyles().

My requirement is to change the color of progress bar to red whenever I click a button. I don't want to comment out Application.EnableVisualStyles().

所以我尝试使用 SendMessage.我的代码:

So I tried using SendMessage. My code:

    [DllImport("user32.dll")]
    private static extern bool SendMessage(IntPtr hWnd, Int32 msg, Int32 wParam, Int32 lParam);

    private const Int32 WM_USER = 0x0400;
    private const Int32 CCM_FIRST = 0x2000;
    private const Int32 PBM_SETBARCOLOR = WM_USER + 9;
    private const Int32 PBM_SETBKCOLOR = CCM_FIRST + 1;

    public Form1()
    {
        InitializeComponent();

    }

    private void button1_Click(object sender, EventArgs e)
    {
        this.Invoke((MethodInvoker)delegate
        {
            SendMessage(this.progressBar1.Handle, PBM_SETBARCOLOR, 0, ColorTranslator.ToWin32(Color.Red));
            SendMessage(this.progressBar1.Handle, PBM_SETBKCOLOR, 0, ColorTranslator.ToWin32(Color.Red));
            progressBar1.Style = ProgressBarStyle.Continuous;
            progressBar1.Value = progressBar1.Maximum;
        });
    }

它不起作用.我不知道为什么.你能帮忙吗

It is not working. I don't know why. Can u please help

推荐答案

如果您想将 ProgressBar 颜色从初始绿色更改为 红色(这是一种标准状态)你可以简单地做

If you want to change ProgressBar color from initial Green to Red (which is a standard state) you can do simply

https://msdn.microsoft.com/ru-ru/library/windows/desktop/bb760850(v=vs.85).aspx

  // 1040 - PBM_SETSTATE
  // 2 - red (error), 3 - yellow (paused), 1 - green (in progress) 
  SendMessage(progressBar1.Handle, 1040, 2, 0);

实现:

  this.Invoke(() => {
    progressBar1.Value = progressBar1.Maximum;
    progressBar1.Style = ProgressBarStyle.Continuous;

    SendMessage(progressBar1.Handle, 1040, 2, 0); 
  });

这篇关于ProgressBar 的颜色没有改变 c#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆