[C#]Set Windows 7 Progress Bar's State
#region DllImport
[DllImport("user32.dll", CharSet = CharSet.Auto)]
internal static extern int SendMessage(IntPtr hWnd, int wMsg, int wParam, int lParam);
#endregion
#region Private Method
void SetPaused(ProgressBar progressBar)
{
SendMessage(progressBar.Handle, PBM_SETSTATE, PBST_PAUSE, 0);
}
void SetError(ProgressBar progressBar)
{
SendMessage(progressBar.Handle, PBM_SETSTATE, PBST_ERROR, 0);
}
void SetNormal(ProgressBar progressBar)
{
SendMessage(progressBar.Handle, PBM_SETSTATE, PBST_NORMAL, 0);
}
#endregion</pre></div>
namespace WindowsFormsApplication3 { public partial class Form1 : Form { #region Const const int PBM_SETPOS = 0x402; const int PBM_GETPOS = 0x0408; const int PBM_SETSTATE = 0x410; const int PBST_PAUSE = 0x0003; const int PBST_ERROR = 0x0002; const int PBST_NORMAL = 0x0001; #endregion
#region DllImport
[DllImport("user32.dll", CharSet = CharSet.Auto)]
internal static extern int SendMessage(IntPtr hWnd, int wMsg, int wParam, int lParam);
#endregion
#region Private Method
void SetPaused(ProgressBar progressBar)
{
SendMessage(progressBar.Handle, PBM_SETSTATE, PBST_PAUSE, 0);
}
void SetError(ProgressBar progressBar)
{
SendMessage(progressBar.Handle, PBM_SETSTATE, PBST_ERROR, 0);
}
void SetNormal(ProgressBar progressBar)
{
SendMessage(progressBar.Handle, PBM_SETSTATE, PBST_NORMAL, 0);
}
#endregion
public Form1()
{
InitializeComponent();
}
private void trackBar1_Scroll(object sender, EventArgs e)
{
SendMessage(progressBar1.Handle, PBM_SETPOS, trackBar1.Value, 0);
}
private void button1_Click(object sender, EventArgs e)
{
SetNormal(progressBar1);
}
private void button2_Click(object sender, EventArgs e)
{
SetPaused(progressBar1);
}
private void button3_Click(object sender, EventArgs e)
{
SetError(progressBar1);
}
}
}
#region DllImport
[DllImport("user32.dll", CharSet = CharSet.Auto)]
internal static extern int SendMessage(IntPtr hWnd, int wMsg, int wParam, int lParam);
#endregion
#region Public Method
public static void SetNormalState(this ProgressBar progressBar)
{
SendMessage(progressBar.Handle, PBM_SETSTATE, PBST_NORMAL, 0);
}
public static void SetPauseState(this ProgressBar progressBar)
{
SendMessage(progressBar.Handle, PBM_SETSTATE, PBST_PAUSE, 0);
}
public static void SetErrorState(this ProgressBar progressBar)
{
SendMessage(progressBar.Handle, PBM_SETSTATE, PBST_ERROR, 0);
}
#endregion
}</pre></div>
private void trackBar1_Scroll(object sender, EventArgs e)
{
progressBar1.Value = trackBar1.Value;
}
private void button1_Click(object sender, EventArgs e)
{
progressBar1.SetNormalState();
}
private void button2_Click(object sender, EventArgs e)
{
progressBar1.SetPauseState();
}
private void button3_Click(object sender, EventArgs e)
{
progressBar1.SetErrorState();
}
}</pre></div>