www.pudn.com > sudoku.rar > MarqueeProgressBar.cs


//-------------------------------------------------------------------------- 
//  
//  Copyright (c) Microsoft Corporation.  All rights reserved.  
//  
//  File: MarqueeProgressBar.cs 
// 
//  Description: A progress bar that supports the WinXP marquee style. 
//  
//-------------------------------------------------------------------------- 
 
using System; 
using System.Drawing; 
using System.Drawing.Drawing2D; 
using System.Collections; 
using System.Windows.Forms; 
using System.ComponentModel; 
using System.Runtime.InteropServices; 
using Microsoft.Sudoku.Utilities; 
 
namespace Microsoft.Sudoku.Controls 
{ 
	/// A marquee progress bar control. 
	[ToolboxBitmap(typeof(ProgressBar))] 
	internal sealed class MarqueeProgressBar : Control 
	{ 
		/// Initializes the control. 
		public MarqueeProgressBar() 
		{ 
			SetStyle(ControlStyles.SupportsTransparentBackColor, true); 
			SetStyle(ControlStyles.Selectable | ControlStyles.UserPaint, false); 
			BackColor = Color.Transparent; 
			ForeColor = SystemColors.Highlight; 
		} 
 
		/// Gets the required creation parameters when the control handle is created. 
		protected override CreateParams CreateParams 
		{ 
			get 
			{ 
				CreateParams cp = base.CreateParams; 
				cp.ClassName = "msctls_progress32"; 
				if (!DesignMode) cp.Style |= PBS_MARQUEE; 
				if (RightToLeft == RightToLeft.Yes) 
				{ 
					cp.ExStyle |= WS_EX_LAYOUTRTL; 
					cp.ExStyle &= ~(WS_EX_RIGHT | WS_EX_RTLREADING | WS_EX_LEFTSCROLLBAR); 
				} 
				return cp; 
			} 
		} 
 
		/// Raises the ForeColorChanged event. 
		/// An EventArgs that contains the event data. 
		protected override void OnForeColorChanged(EventArgs e) 
		{ 
			base.OnForeColorChanged(e); 
			if (base.IsHandleCreated) 
			{ 
				SendMessage(PBM_SETBARCOLOR, 0, ColorTranslator.ToWin32(this.ForeColor)); 
			} 
		} 
  
		/// Raises the BackColorChanged event. 
		/// An EventArgs that contains the event data. 
		protected override void OnBackColorChanged(EventArgs e) 
		{ 
			base.OnBackColorChanged(e); 
			if (base.IsHandleCreated) 
			{ 
				SendMessage(PBM_SETBKCOLOR, 0, ColorTranslator.ToWin32(this.BackColor)); 
			} 
		} 
 
		/// Gets the default Input Method Editor (IME) mode supported by the control. 
		protected override ImeMode DefaultImeMode { get { return ImeMode.Disable; } } 
 
		/// Gets the default size of the control. 
		protected override Size DefaultSize { get { return new Size(100, 23); } } 
 
		/// Creates a handle for the control. 
		protected override void CreateHandle() 
		{ 
			if (!RecreatingHandle) 
			{ 
				NativeMethods.INITCOMMONCONTROLSEX icc = new NativeMethods.INITCOMMONCONTROLSEX(); 
				icc.dwSize = 0; 
				icc.dwICC = ICC_PROGRESS_CLASS; 
				NativeMethods.InitCommonControlsEx(icc); 
			} 
			base.CreateHandle(); 
		} 
 
		/// Raises the HandleCreated event and starts the progress bar's marquee. 
		/// An EventArgs that contains the event data. 
		protected override void OnHandleCreated(EventArgs e) 
		{ 
			base.OnHandleCreated(e); 
			SendMessage(PBM_SETRANGE, MINIMUM, MAXIMUM); 
			SendMessage(PBM_SETSTEP, STEP, 0); 
			SendMessage(PBM_SETPOS, VALUE, 0); 
			SendMessage(PBM_SETBKCOLOR, 0, ColorTranslator.ToWin32(BackColor)); 
			SendMessage(PBM_SETBARCOLOR, 0, ColorTranslator.ToWin32(ForeColor)); 
			Start(DEFAULTSPEED); 
		} 
 
		/// Starts the marquee moving. 
		/// The speed at which to move the marquee. 
		private void Start(int speed) 
		{ 
			if (!DesignMode && IsHandleCreated) SendMessage(PBM_SETMARQUEE, speed == 0 ? 0 : 1, speed); 
		} 
 
		/// Sends a windows message. 
		/// The message to send. 
		/// WPARAM 
		/// LPARAM 
		/// Result. 
		private IntPtr SendMessage(int msg, int wparam, int lparam) 
		{ 
			return NativeMethods.SendMessage(new HandleRef(this, this.Handle), msg, new IntPtr(wparam), new IntPtr(lparam)); 
		} 
		 
		const int PBS_MARQUEE = 0x8, WS_EX_LAYOUTRTL = 0x00400000; 
		const int WS_EX_RIGHT = 0x1000, WS_EX_RTLREADING = 0x2000, WS_EX_LEFTSCROLLBAR = 0x4000; 
		const int PBM_SETRANGE = 0x406, PBM_SETSTEP = 0x404, PBM_SETPOS=0x402; 
		const int PBM_SETBKCOLOR = 0x2001, PBM_SETBARCOLOR = 0x409; 
		const int ICC_PROGRESS_CLASS = 0x20; 
		const int PBM_SETMARQUEE = 0x40a; 
		const int MINIMUM=0, MAXIMUM=100, STEP=1, VALUE=1, DEFAULTSPEED=50; 
	} 
}