starwing network

C#:Spin Control

こんな感じ。

using System;
using System.Windows.Forms;
using System.Runtime.InteropServices;

namespace Std.Controls
{
    /// <summary>
    /// スピンコントロール
    /// </summary>
    public class SpinControl : UserControl
    {
        private System.ComponentModel.Container components = null;
        public SpinControl()
        {
            components = new System.ComponentModel.Container();
            SetStyle(ControlStyles.UserPaint, false);
        }

        /// <summary>
        /// 使用されているリソースに後処理を実行します。
        /// </summary>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
                components.Dispose();
            base.Dispose(disposing);
        }

        protected override CreateParams CreateParams
        {
            get
            {
                CreateParams cp = base.CreateParams;
                cp.ClassName = "msctls_updown32";
                return cp;
            }
        }

        /// <summary>
        /// 現在の位置を取得します。
        /// </summary>
        public int Position
        {
            get
            {
                return GetLoWord((IntPtr)SendMessage(this.Handle, UDM_GETPOS, IntPtr.Zero, IntPtr.Zero));
            }

            set
            {
                SendMessage(this.Handle, UDM_SETPOS, IntPtr.Zero, (IntPtr)value);
            }
        }

        /// <summary>
        /// 最大値を指定します。
        /// </summary>
        public int Range
        {
            get
            {
                return GetLoWord((IntPtr)
                SendMessage(
                this.Handle, UDM_GETRANGE,
                IntPtr.Zero, IntPtr.Zero));
            }
            set
            {
                SendMessage(
                    this.Handle, UDM_SETRANGE,
                    IntPtr.Zero, MakeLong(0, value));
            }
        }

        public const int UDM_SETRANGE = 0x0465;
        public const int UDM_GETRANGE = 0x0466;
        public const int UDM_SETPOS = 0x0467;
        public const int UDM_GETPOS = 0x0468;

        public static int GetLoWord(IntPtr word)
        {
            return (int)((ushort)((uint)(word) & 0xffff));
        }

        [DllImport("user32.dll", CharSet = CharSet.Unicode)]
        extern public static IntPtr SendMessage(IntPtr hWnd, uint msg, IntPtr wp, IntPtr lp);

        public static IntPtr MakeLong(int a, int b)
        {
            return (IntPtr)((long)(((ushort)(a)) | ((uint)((ushort)(b))) << 16));
        }
    }
}

注意:かろうじて表示できる程度です。いろいろ不備です。