我的需求是在点击按钮时将进度条的颜色更改为红色。我不想注释掉Application.EnableVisualStyles()。
因此,我尝试使用SendMessage。我的代码如下:
[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;
});
}
它不起作用。我不知道为什么。你能帮忙吗?
PBST_NORMAL = 0x0001; /* 绿色 */
;但是,如果值超出了[1..3]
范围,将使用绿色。我已经编辑了答案。 - Dmitry Bychenko[1..3]
范围是最好的策略。 - Dmitry Bychenko