.net compact c# 支持 图片的 按钮控件

在wince 下,button不支持带图片,在别人控件的基础上,封装了一个按钮类,可以在设计期添加图片,设置居左,居右,填充属性

效果如下:

using System;

using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using System.Drawing.Imaging;

namespace FrontLinkControls
{
    public partial class FrontLinkButtonPicture : Control, ISupportInitialize
    {
        bool bPressed;

        public FrontLinkButtonPicture()
        {
            InitializeComponent();
        }

        #region 设计组件 ISupportInitialize Members

        void ISupportInitialize.BeginInit()
        {
        }

        void ISupportInitialize.EndInit()
        {

        }

        #endregion

        #region Protected/Private Services

        private Bitmap DoubleBufferImage = null;

        protected override void OnResize(EventArgs e)
        {
            DoubleBufferImage = new Bitmap(this.ClientSize.Width, this.ClientSize.Height);

            base.OnResize(e);
            Invalidate();
        }

        protected override void OnLostFocus(EventArgs e)
        {
            base.OnLostFocus(e);
            Invalidate();
        }

        protected override void OnGotFocus(EventArgs e)
        {
            base.OnGotFocus(e);
            this.Invalidate();
        }

        protected override void OnMouseDown(System.Windows.Forms.MouseEventArgs e)
        {
            this.bPressed = true;
            // button receives input focus
            Focus();
            base.OnMouseDown(e);
            Invalidate();
        }

        public new bool Enabled
        {
            get
            {
                return base.Enabled;
            }
            set
            {
                base.Enabled = value;
                Invalidate();
            }
        }

        protected override void OnMouseUp(System.Windows.Forms.MouseEventArgs e)
        {
            this.bPressed = false;
            base.OnMouseUp(e);
            Invalidate();
        }


        protected override void OnPaint(PaintEventArgs e)
        {
            // 双缓冲技术,先将所有要画的画在该图片中,再调用GS画出该图片.
            Graphics g = Graphics.FromImage((System.Drawing.Image)DoubleBufferImage);

            OnDrawLine(ref g);

            e.Graphics.DrawImage((System.Drawing.Image)DoubleBufferImage, 0, 0);
            g.Dispose();

            base.OnPaint(e);
        }



        private void OnDrawLine(ref Graphics g)
        {
            #region 画边框

            SolidBrush brushBack;
            SolidBrush brushText;

            //Pen penFrame, penFrameUp, penFrameDown;
           
            //if ( bPressed )
            //{
            //    penFrame= new Pen(Color.Black);
            //    penFrameUp = new Pen(SystemColors.ControlLight);
            //    penFrameDown = new Pen(SystemColors.ControlDark);

            //brushBack = new SolidBrush(this.BackColor);
            //brushText = new SolidBrush(this.ForeColor);
            //}
            //else
            //{
            //    if( this.Enabled )
            //    {
            //        penFrame = new Pen(Color.FromArgb(112, 112, 112));
            //        penFrameUp = new Pen(SystemColors.ControlLight);
            //        penFrameDown = new Pen(Color.FromArgb(160, 160, 160));

            //        brushBack = new SolidBrush(this.BackColor);
            //        brushText = new SolidBrush(this.ForeColor);
            //    }
            //    else
            //    {
            //        penFrame = new Pen(Color.Gray);
            //        penFrameUp = new Pen(Color.Gray);
            //        penFrameDown = new Pen(Color.Gray);

            //        brushBack = new SolidBrush(this.BackColor);
            //        brushText = new SolidBrush(Color.Gray);
            //    }
            //}

            //g.FillRectangle(brushBack, 0, 0, Width, Height);
            //g.DrawRectangle(penFrame, 0, 0, Width - 1, Height - 1);

            //if ( this.Enabled )
            //{
            //    // 上边框亮线
            //    g.DrawLine(penFrameUp, 0, 1, Width - 2, 1);
            //    g.DrawLine(penFrameUp, 1, 1, 1, this.Height - 2);

            //    // 下边框阴影
            //    g.DrawLine(penFrameDown, 0, this.Height - 2, this.Width - 1, this.Height - 2);
            //    g.DrawLine(penFrameDown, this.Width - 2, 1, this.Width - 2, this.Height - 2);
            //}

            // 画刷
            brushBack = new SolidBrush(this.BackColor);
            if (base.Enabled)
            {
                brushText = new SolidBrush(this.ForeColor);
            }
            else
            {
                brushText = new SolidBrush(Color.Gray);
            }
            g.FillRectangle(brushBack, 0, 0, Width, Height);

            // 填充背景
            IntPtr hDC = g.GetHdc();

            if (!base.Enabled)
            {
                IntPtr inActBorderPen = Win32.CreatePen(Win32.PS_SOLID, 1, Win32.RGB(160, 160, 160));

                IntPtr hOldPen = Win32.SelectObject(hDC, inActBorderPen);
                IntPtr hOldBrush = Win32.SelectObject(hDC, Win32.GetStockObject(Win32.WHITE_BRUSH));
                
                Win32.RoundRect(hDC, 0, 0, this.Width, this.Height, 6, 6);

                Win32.SelectObject(hDC, hOldPen);
                Win32.SelectObject(hDC, hOldBrush);

                Win32.DeleteObject(inActBorderPen);


                IntPtr inActBgBrush = Win32.CreateSolidBrush(Win32.RGB(236, 236, 236));
                Win32.RECT rc = new Win32.RECT(2, 2, this.Width - 2, this.Height - 2);

                Win32.FillRect(hDC, rc, inActBgBrush);

                Win32.DeleteObject(inActBgBrush);
            }
            else
            {

                IntPtr borderPen = Win32.CreatePen(Win32.PS_SOLID, 1, Win32.RGB(112, 112, 112));

                IntPtr hOldPen = Win32.SelectObject(hDC, borderPen);
                IntPtr hOldBrush = Win32.SelectObject(hDC, Win32.GetStockObject(Win32.WHITE_BRUSH));

                Win32.RoundRect(hDC, 0, 0, this.Width, this.Height, 6, 6);

                Win32.SelectObject(hDC, hOldPen);
                Win32.SelectObject(hDC, hOldBrush);

                Win32.DeleteObject(borderPen);


                Win32.GRADIENT_RECT[] gra = new Win32.GRADIENT_RECT[2] { new Win32.GRADIENT_RECT(0, 1), new Win32.GRADIENT_RECT(2, 3) };

                Win32.TRIVERTEX[] tva = new Win32.TRIVERTEX[4];
                tva[0] = new Win32.TRIVERTEX(2, 2, 0xf2, 0xf2, 0xf2, 0x00);
                tva[1] = new Win32.TRIVERTEX(Width - 2, Height / 2, 0xEB, 0xEB, 0xEB, 0x00);
                tva[2] = new Win32.TRIVERTEX(2, Height / 2, 0xDD, 0xDD, 0xDD, 0x00);
                tva[3] = new Win32.TRIVERTEX(Width - 2, Height - 2, 0xCF, 0xCF, 0xCF, 0x00);

                Win32.GradientFill(hDC, tva, 4, gra, 2, Win32.GRADIENT_FILL_RECT_V);

            }

            g.ReleaseHdc(hDC);

            #endregion

            #region 画图片

            int xPic = 8, yPic = 2;

            if (Image != null)
            {
                int picWidth = Image.Width, picHeight = Image.Height;
                if (Image.Height < this.Height)
                {
                    yPic = (this.Height - Image.Height) / 2;
                }

                if (ImageAlign == ImageAlignType.Left)
                {
                    ;
                }
                else if (ImageAlign == ImageAlignType.Center)
                {
                    xPic = (this.Width - Image.Width) / 2;
                }
                else if (ImageAlign == ImageAlignType.Right)
                {
                    xPic = this.Width - Image.Width - xPic;
                }
                else
                {
                    // 全部添充
                    g.FillRectangle(brushBack, 0, 0, Width, Height);

                    xPic = 0;
                    yPic = 0;
                    picWidth = this.Width;
                    picHeight = this.Height;
                }

                if (bPressed)
                {
                    xPic++;
                    yPic++;
                }

                g.DrawImage(Image, new Rectangle(xPic, yPic, picWidth, picHeight), 0, 0, Image.Width, Image.Height, GraphicsUnit.Pixel, _ImageAttri);
            }

            #endregion
            
            #region region 画字体

            if ( this.Text.Length>0 )
            {
                Font font = new Font(this.Font.Name, this.Font.Size, this.Font.Style);

                SizeF textSize = new SizeF();
                textSize = g.MeasureString(Text, font);

                float x = 8, y = 2;
                // 垂直居中显示
                if (textSize.Height < Height)
                {
                    y = (Height - textSize.Height) / 2;
                }

                if (TextAlign == TextAlignType.Center)
                {
                    if (textSize.Width < Width)
                    {
                        x = (Width - textSize.Width) / 2;
                    }
                }
                else if (TextAlign == TextAlignType.Right)
                {
                    x = this.Width - textSize.Width - x;
                }

                if (bPressed)
                {
                    x++;
                    y++;
                }
                g.DrawString(Text, font, brushText, x, y);
            }

            #endregion

            // 画焦点线
            if ( this.Focused )
            {
                Pen pen = new Pen(Color.FromArgb(112, 112, 112));
                pen.DashStyle = System.Drawing.Drawing2D.DashStyle.Dash;

                Rectangle rc = new Rectangle(3, 3, Width - 7, Height - 7);
                g.DrawRectangle(pen, rc);
            }
        }

        #region 实现tab键切换控件
       


        #endregion

        /// <summary>
        /// 回车键事件
        /// </summary>
        /// <param name="e"></param>
        protected override void OnKeyPress(KeyPressEventArgs e)
        {
            base.OnKeyPress(e);

            if (e.KeyChar == '\r')
            {
                this.OnClick(e);
                Invalidate();
            }
        }



        /// <summary>
        /// 获取焦点的时候重绘
        /// </summary>
        /// <returns></returns>
        public new bool Focus()
        {
            bool result = base.Focus();
            Invalidate();
            return result;
        }

        #endregion

        public new string Text
        {
            get{
                return base.Text;
            }
            set{
                base.Text = value;
                Invalidate();
            }
        }


        //private static void DrawImageTransparent(Graphics gx, Image image, Rectangle destRect)
        //{
        //    //ImageAttributes imageAttr = new ImageAttributes();
        //    //Color transpColor = GetTransparentColor(image);
        //    //imageAttr.SetColorKey(transpColor, transpColor);
        //    //gx.DrawImage(image, destRect, 0, 0, image.Width, image.Height,
        //    //    GraphicsUnit.Pixel, imageAttr);
        //}
        //private Color GetTransparentColor(Image image)
        //{
        //    return ((Bitmap)image).GetPixel(0, 0);
        //}


        #region 图片与文本属性

        private Bitmap _Image = null;
        private ImageAttributes _ImageAttri = null;
        public Bitmap Image
        {
            get
            {
                return _Image;
            }
            set
            {
                _Image = value;
                if (value != null)
                {
                    _ImageAttri = new ImageAttributes();
                    Color transpColor = value.GetPixel(0, 0);
                    _ImageAttri.SetColorKey(transpColor, transpColor);
                }

                Invalidate();
            }
        }

        public enum ImageAlignType
        {
            Left,
            Center,
            Right,
            Fill
        }

        ImageAlignType _ImageAlign = ImageAlignType.Left;
        public ImageAlignType ImageAlign
        {
            get
            {
                return _ImageAlign;
            }
            set
            {
                _ImageAlign = value;
                Invalidate();
            }
        }

        public enum TextAlignType
        {
            Left,
            Center,
            Right
        }

        TextAlignType _TextAlign = TextAlignType.Center;
        public TextAlignType TextAlign
        {
            get
            {
                return _TextAlign;
            }
            set
            {
                _TextAlign = value;
                Invalidate();
            }
        }

        #endregion

    }
}


 


using System;
using System.Runtime.InteropServices;
using System.Drawing;

namespace FrontLinkControls
{
    public class Win32
    {

        public static bool m_PlatformCE = Environment.OSVersion.Platform == PlatformID.WinCE;

        // defines
        public const int IDC_WAIT = 32514;

        // functions
        [DllImport("coredll.dll")]
        public static extern IntPtr LoadCursor(IntPtr hInstance, int lpCursorName);

        [DllImport("coredll.dll")]
        public static extern IntPtr SetCursor(IntPtr hCursor);




        [DllImport("coredll.dll")]
        public static extern IntPtr FindWindow([MarshalAs(UnmanagedType.LPWStr)]string lpClassName, [MarshalAs(UnmanagedType.LPWStr)]string lpWindowName);

        [DllImport("coredll.dll")]
        [return: MarshalAs(UnmanagedType.Bool)]
        public static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);


        public const uint WM_KEYFIRST = 0x0100;
        public const uint WM_KEYDOWN = 0x0100;
        public const uint WM_KEYUP = 0x0101;
        public const uint WM_KEYLAST = 0x0108;

        public const int WM_HOTKEY = 0x0312;

        public const uint MOD_ALT = 1;
        public const uint MOD_CONTROL = 2;
        public const uint MOD_KEYUP = 0x1000;
        public const uint MOD_SHIFT = 4;
        public const uint MOD_WIN = 8;

        public const uint VK_NUMPAD0 = 0x60;
        public const uint VK_NUMPAD1 = 0x61;
        public const uint VK_NUMPAD2 = 0x62;
        public const uint VK_NUMPAD3 = 0x63;
        public const uint VK_NUMPAD4 = 0x64;
        public const uint VK_NUMPAD5 = 0x65;
        public const uint VK_NUMPAD6 = 0x66;
        public const uint VK_NUMPAD7 = 0x67;
        public const uint VK_NUMPAD8 = 0x68;
        public const uint VK_NUMPAD9 = 0x69;

        public const uint VK_F1 = 0x70;
        public const uint VK_F2 = 0x71;
        public const uint VK_F3 = 0x72;
        public const uint VK_F4 = 0x73;
        public const uint VK_F5 = 0x74;
        public const uint VK_F6 = 0x75;
        public const uint VK_F7 = 0x76;
        public const uint VK_F8 = 0x77;
        public const uint VK_F9 = 0x78;

        public const uint VK_F23 = 0x86;
        public const uint VK_F24 = 0x87;

        public const uint VK_ESCAPE = 0x1B;
        public const uint VK_RETURN = 0x0D;
        public const uint VK_CAPITAL = 0x14;
        public const uint VK_NUMBER = 0x0B;

        [DllImport("coredll.dll")]
        [return: MarshalAs(UnmanagedType.Bool)]
        public static extern bool RegisterHotKey(IntPtr hWnd, int id, uint fsModifiers, uint vk);

        [DllImport("coredll.dll")]
        [return: MarshalAs(UnmanagedType.Bool)]
        public static extern bool UnregisterHotKey(IntPtr hWnd, int id);

        [DllImport("coredll.dll")]
        public static extern short GetKeyState(int nVirtKey);


        public const uint WAIT_TIMEOUT = 0x102;
        public const uint WAIT_FAILED = 0xffffffffu;
        public const uint INFINITE = 0xffffffffu;

        [DllImport("coredll.dll")]
        public static extern uint WaitForSingleObject(IntPtr hHandle, uint dwMilliseconds);

        [DllImport("coredll.dll")]
        public static extern uint WaitForMultipleObjects(uint nCount, IntPtr[] lpHandles, [MarshalAs(UnmanagedType.Bool)]bool fWaitAll, uint dwMilliseconds);

        [DllImport("coredll.dll")]
        public static extern IntPtr CreateEvent(IntPtr lpEventAttributes, [MarshalAs(UnmanagedType.Bool)]bool bManualReset, [MarshalAs(UnmanagedType.Bool)]bool bInitialState, [MarshalAs(UnmanagedType.LPWStr)]string lpName);


        public const uint EVENT_PULSE = 1;
        public const uint EVENT_RESET = 2;
        public const uint EVENT_SET = 3;

        [DllImport("coredll.dll")]
        [return: MarshalAs(UnmanagedType.Bool)]
        public static extern bool EventModify(IntPtr hEvent, uint func);

        [DllImport("coredll.dll")]
        [return: MarshalAs(UnmanagedType.Bool)]
        public static extern bool CloseHandle(IntPtr hObject);


        [StructLayout(LayoutKind.Sequential)]
        public class MSGQUEUEOPTIONS
        {
            public uint dwSize;
            public uint dwFlags;
            public uint dwMaxMessages;
            public uint cbMaxMessage;
            public bool bReadAccess;
        }

        [DllImport("coredll.dll")]
        public static extern IntPtr CreateMsgQueue([MarshalAs(UnmanagedType.LPWStr)]string lpszName, MSGQUEUEOPTIONS lpOptions);

        [DllImport("coredll.dll")]
        [return: MarshalAs(UnmanagedType.Bool)]
        public static extern bool CloseMsgQueue(IntPtr hMsgQ);


        [DllImport("coredll.dll")]
        [return: MarshalAs(UnmanagedType.Bool)]
        public static extern bool ReadMsgQueue(IntPtr hMsgQ, [MarshalAs(UnmanagedType.AsAny)]out object lpBuffer, uint cbBufferSize, out uint lpNumberOfBytesRead, uint dwTimeout, out uint pdwFlags);

        [StructLayout(LayoutKind.Sequential)]
        public struct POWER_BROADCAST_POWER_INFO
        {
            public uint dwNumLevels;
            public uint dwBatteryLifeTime;
            public uint dwBatteryFullLifeTime;
            public uint dwBackupBatteryLifeTime;
            public uint dwBackupBatteryFullLifeTime;
            public byte bACLineStatus;
            public byte bBatteryFlag;
            public byte bBatteryLifePercent;
            public byte bBackupBatteryFlag;
            public byte bBackupBatteryLifePercent;
        }

        [StructLayout(LayoutKind.Sequential)]
        public struct POWER_BROADCAST
        {
            public uint Message;    // one of PBT_Xxx
            public uint Flags;      // one of POWER_STATE_Xxx
            public uint Length;     // byte count of data starting at SystemPowerStateName
            public POWER_BROADCAST_POWER_INFO PI;
        }

        [DllImport("coredll.dll")]
        [return: MarshalAs(UnmanagedType.Bool)]
        public static extern bool ReadMsgQueue(IntPtr hMsgQ, out POWER_BROADCAST BroadCast, uint cbBufferSize, out uint lpNumberOfBytesRead, uint dwTimeout, out uint pdwFlags);


        public const uint PBT_TRANSITION = 0x00000001;  // broadcast specifying system power state transition
        public const uint PBT_RESUME = 0x00000002;  // broadcast notifying a resume, specifies previous state
        public const uint PBT_POWERSTATUSCHANGE = 0x00000004;  // power supply switched to/from AC/DC
        public const uint PBT_POWERINFOCHANGE = 0x00000008;  // some system power status field has changed


        [DllImport("coredll.dll")]
        public static extern IntPtr RequestPowerNotifications(IntPtr hMsgQ, uint Flags);

        [DllImport("coredll.dll")]
        [return: MarshalAs(UnmanagedType.Bool)]
        public static extern bool StopPowerNotifications(IntPtr hMsgQ);

        [StructLayout(LayoutKind.Sequential)]
        public class SYSTEM_POWER_STATUS_EX2
        {
            public byte ACLineStatus;
            public byte BatteryFlag;
            public byte BatteryLifePercent;
            public byte Reserved1;
            public uint BatteryLifeTime;
            public uint BatteryFullLifeTime;
            public byte Reserved2;
            public byte BackupBatteryFlag;
            public byte BackupBatteryLifePercent;
            public byte Reserved3;
            public uint BackupBatteryLifeTime;
            public uint BackupBatteryFullLifeTime;
            public uint BatteryVoltage;
            public uint BatteryCurrent;
            public uint BatteryAverageCurrent;
            public uint BatteryAverageInterval;
            public uint BatterymAHourConsumed;
            public uint BatteryTemperature;
            public uint BackupBatteryVoltage;
            public byte BatteryChemistry;
        }

        [DllImport("coredll.dll")]
        public static extern uint GetSystemPowerStatusEx2(SYSTEM_POWER_STATUS_EX2 pSystemPowerStatusEx2, uint dwLen, [MarshalAs(UnmanagedType.Bool)]bool fUpdate);

        public const uint POWER_STATE_ON = 0x00010000u;        // on state
        public const uint POWER_STATE_OFF = 0x00020000u;        // no power, full off
        public const uint POWER_STATE_CRITICAL = 0x00040000u;        // critical off
        public const uint POWER_STATE_BOOT = 0x00080000u;        // boot state
        public const uint POWER_STATE_IDLE = 0x00100000u;        // idle state
        public const uint POWER_STATE_SUSPEND = 0x00200000u;        // suspend state
        public const uint POWER_STATE_RESET = 0x00800000u;        // reset state

        public const uint POWER_FORCE = 0x00001000u;


        [DllImport("coredll.dll")]
        public static extern uint SetSystemPowerState([MarshalAs(UnmanagedType.LPWStr)]string psState, uint StateFlags, uint Options);


        [DllImport("coredll.dll")]
        public static extern IntPtr GetDC(IntPtr hWnd);

        [DllImport("coredll.dll")]
        public static extern int ReleaseDC(IntPtr hWnd, IntPtr hDC);

        [DllImport("coredll.dll")]
        public static extern IntPtr CreateCompatibleDC(IntPtr hDC);

        [DllImport("coredll.dll")]
        [return: MarshalAs(UnmanagedType.Bool)]
        public static extern bool DeleteDC(IntPtr hDC);

        [DllImport("coredll.dll")]
        public static extern IntPtr CreateCompatibleBitmap(IntPtr hDC, int nWidth, int nHeight);

        [DllImport("gdi32.dll", EntryPoint = "SelectObject")]
        internal static extern IntPtr SelectObjectWIN(IntPtr hDC, IntPtr hgdiobj);
        [DllImport("coredll.dll", EntryPoint = "SelectObject")]
        internal static extern IntPtr SelectObjectCE(IntPtr hDC, IntPtr hgdiobj);
        public static IntPtr SelectObject(IntPtr hDC, IntPtr hgdiobj)
        {
            if ( m_PlatformCE )
            {
                return SelectObjectCE(hDC, hgdiobj);
            }
            else
            {
                return SelectObjectWIN(hDC, hgdiobj);
            }
        }

        [DllImport("gdi32.dll", EntryPoint = "DeleteObject")]
        [return: MarshalAs(UnmanagedType.Bool)]
        internal static extern bool DeleteObjectWIN(IntPtr hgdiobj);
        [DllImport("coredll.dll", EntryPoint = "DeleteObject")]
        [return: MarshalAs(UnmanagedType.Bool)]
        internal static extern bool DeleteObjectCE(IntPtr hgdiobj);
        public static bool DeleteObject(IntPtr hgdiobj)
        {
            if (m_PlatformCE)
            {
                return DeleteObjectCE(hgdiobj);
            }
            else
            {
                return DeleteObjectWIN(hgdiobj);
            }
        }

        public const uint SRCCOPY = 0x00CC0020u; /* dest = source                   */
        public const uint SRCPAINT = 0x00EE0086u; /* dest = source OR dest           */
        public const uint SRCAND = 0x008800C6u; /* dest = source AND dest          */
        public const uint SRCINVERT = 0x00660046u; /* dest = source XOR dest          */
        public const uint SRCERASE = 0x00440328u; /* dest = source AND (NOT dest )   */
        public const uint NOTSRCCOPY = 0x00330008u; /* dest = (NOT source)             */
        public const uint NOTSRCERASE = 0x001100A6u; /* dest = (NOT src) AND (NOT dest) */
        public const uint MERGECOPY = 0x00C000CAu; /* dest = (source AND pattern)     */
        public const uint MERGEPAINT = 0x00BB0226u; /* dest = (NOT source) OR dest     */
        public const uint PATCOPY = 0x00F00021u; /* dest = pattern                  */
        public const uint PATPAINT = 0x00FB0A09u; /* dest = DPSnoo                   */
        public const uint PATINVERT = 0x005A0049u; /* dest = pattern XOR dest         */
        public const uint DSTINVERT = 0x00550009u; /* dest = (NOT dest)               */
        public const uint BLACKNESS = 0x00000042u; /* dest = BLACK                    */
        public const uint WHITENESS = 0x00FF0062u; /* dest = WHITE                    */

        [DllImport("coredll.dll")]
        [return: MarshalAs(UnmanagedType.Bool)]
        public static extern bool BitBlt(IntPtr hdcDest, int nXDest, int nYDest, int nWidth, int nHeight, IntPtr hdcSrc, int nXSrc, int nYSrc, uint dwRop);


        public struct TRIVERTEX
        {
            public int x;
            public int y;
            public ushort Red;
            public ushort Green;
            public ushort Blue;
            public ushort Alpha;

            public TRIVERTEX(int x, int y, ushort red, ushort green, ushort blue, ushort alpha)
            {
                this.x = x;
                this.y = y;
                this.Red = (ushort)(red << 8);
                this.Green = (ushort)(green << 8);
                this.Blue = (ushort)(blue << 8);
                this.Alpha = (ushort)(alpha << 8);
            }
        }

        public struct GRADIENT_RECT
        {
            public uint UpperLeft;
            public uint LowerRight;

            public GRADIENT_RECT(uint ul, uint lr)
            {
                this.UpperLeft = ul;
                this.LowerRight = lr;
            }
        }

        public const int GRADIENT_FILL_RECT_H = 0x00000000;
        public const int GRADIENT_FILL_RECT_V = 0x00000001;

        [DllImport("msimg32.dll", EntryPoint = "GradientFill")]
        [return: MarshalAs(UnmanagedType.Bool)]
        internal extern static bool GradientFillWIN(IntPtr hdc, TRIVERTEX[] pVertex, uint dwNumVertex, GRADIENT_RECT[] pMesh, uint dwNumMesh, uint dwMode);
        [DllImport("coredll.dll", EntryPoint = "GradientFill")]
        [return: MarshalAs(UnmanagedType.Bool)]
        internal extern static bool GradientFillCE(IntPtr hdc, TRIVERTEX[] pVertex, uint dwNumVertex, GRADIENT_RECT[] pMesh, uint dwNumMesh, uint dwMode);
        public static bool GradientFill(IntPtr hdc, TRIVERTEX[] pVertex, uint dwNumVertex, GRADIENT_RECT[] pMesh, uint dwNumMesh, uint dwMode)
        {
            if (m_PlatformCE)
            {
                return GradientFillCE(hdc, pVertex, dwNumVertex, pMesh, dwNumMesh, dwMode);
            }
            else
            {
                return GradientFillWIN(hdc, pVertex, dwNumVertex, pMesh, dwNumMesh, dwMode);
            }
        }

        [DllImport("gdi32.dll", EntryPoint = "RoundRect")]
        [return: MarshalAs(UnmanagedType.Bool)]
        internal static extern bool RoundRectWIN(IntPtr hdc, int nLeftRect, int nTopRect, int nRightRect, int nBottomRect, int nWidth, int nHeight);
        [DllImport("coredll.dll", EntryPoint = "RoundRect")]
        [return: MarshalAs(UnmanagedType.Bool)]
        internal static extern bool RoundRectCE(IntPtr hdc, int nLeftRect, int nTopRect, int nRightRect, int nBottomRect, int nWidth, int nHeight);
        public static bool RoundRect(IntPtr hdc, int nLeftRect, int nTopRect, int nRightRect, int nBottomRect, int nWidth, int nHeight)
        {
            if (m_PlatformCE)
            {
                return RoundRectCE(hdc, nLeftRect, nTopRect, nRightRect, nBottomRect, nWidth, nHeight);
            }
            else
            {
                return RoundRectWIN(hdc, nLeftRect, nTopRect, nRightRect, nBottomRect, nWidth, nHeight);
            }
        }

        public const int WHITE_BRUSH = 0;
        public const int LTGRAY_BRUSH = 1;
        public const int GRAY_BRUSH = 2;
        public const int DKGRAY_BRUSH = 3;
        public const int BLACK_BRUSH = 4;
        public const int NULL_BRUSH = 5;
        public const int HOLLOW_BRUSH = 5;
        public const int WHITE_PEN = 6;
        public const int BLACK_PEN = 7;
        public const int NULL_PEN = 8;
        public const int SYSTEM_FONT = 13;
        public const int DEFAULT_PALETTE = 15;
        public const int BORDERX_PEN = 32;
        public const int BORDERY_PEN = 33;


        [DllImport("gdi32.dll", EntryPoint = "GetStockObject")]
        internal static extern IntPtr GetStockObjectWIN(int fnObject);
        [DllImport("coredll.dll", EntryPoint = "GetStockObject")]
        internal static extern IntPtr GetStockObjectCE(int fnObject);
        public static IntPtr GetStockObject(int fnObject)
        {
            if (m_PlatformCE)
            {
                return GetStockObjectCE(fnObject);
            }
            else
            {
                return GetStockObjectWIN(fnObject);
            }
        }

        public const int PS_SOLID = 0;
        public const int PS_DASH = 1;
        public const int PS_NULL = 5;

        public static uint RGB(byte r, byte g, byte b)
        {
            return (uint)r | (uint)((uint)g << 8) | (uint)((uint)b << 16);
        }

        [DllImport("gdi32.dll", EntryPoint = "CreatePen")]
        internal static extern IntPtr CreatePenWIN(int fnPenStyle, int nWidth, uint crColor);
        [DllImport("coredll.dll", EntryPoint = "CreatePen")]
        internal static extern IntPtr CreatePenCE(int fnPenStyle, int nWidth, uint crColor);
        public static IntPtr CreatePen(int fnPenStyle, int nWidth, uint crColor)
        {
            if ( m_PlatformCE )
            {
                return CreatePenCE(fnPenStyle, nWidth, crColor);
            }
            else
            {
                return CreatePenWIN(fnPenStyle, nWidth, crColor);
            }
        }

        [DllImport("gdi32.dll", EntryPoint = "CreateSolidBrush")]
        internal static extern IntPtr CreateSolidBrushWIN(uint crColor);
        [DllImport("coredll.dll", EntryPoint = "CreateSolidBrush")]
        internal static extern IntPtr CreateSolidBrushCE(uint crColor);
        public static IntPtr CreateSolidBrush(uint crColor)
        {
            if (m_PlatformCE)
            {
                return CreateSolidBrushCE(crColor);
            }
            else
            {
                return CreateSolidBrushWIN(crColor);
            }
        }


        public class RECT
        {
            int left;
            int top;
            int right;
            int bottom;

            public RECT(int left, int top, int right, int bottom)
            {
                this.left = left;
                this.top = top;
                this.right = right;
                this.bottom = bottom;
            }
        }


        [DllImport("User32.dll", EntryPoint = "FillRect")]
        internal static extern int FillRectWIN(IntPtr hDC, RECT lprc, IntPtr hbr);
        [DllImport("coredll.dll", EntryPoint = "FillRect")]
        internal static extern int FillRectCE(IntPtr hDC, RECT lprc, IntPtr hbr);
        public static int FillRect(IntPtr hDC, RECT lprc, IntPtr hbr)
        {
            if (m_PlatformCE)
            {
                return FillRectCE(hDC, lprc, hbr);
            }
            else
            {
                return FillRectWIN(hDC, lprc, hbr);
            }
        }

        [StructLayout(LayoutKind.Sequential)]
        public class KBDLLHOOKSTRUCT
        {
            public uint vkCode;  // virtual key code 
            public uint scanCode;  // scan code 
            public uint flags;  // flags 
            public uint time;   // time stamp for this message 
            public uint dwExtraInfo; // extra info from the driver or keybd_event 

        }

        public delegate int HOOKPROC(int code, uint wParam, KBDLLHOOKSTRUCT lParam);


        public const int WH_JOURNALRECORD = 0;
        public const int WH_JOURNALPLAYBACK = 1;
        public const int WH_KEYBOARD_LL = 20;

        public const int HC_ACTION = 0;

        [DllImport("coredll.dll", EntryPoint = "SetWindowsHookExW")]
        public static extern IntPtr SetWindowsHookEx(int idHook, [MarshalAs(UnmanagedType.FunctionPtr)]HOOKPROC lpfn, IntPtr hmod, uint dwThreadId);

        [DllImport("coredll.dll")]
        [return: MarshalAs(UnmanagedType.Bool)]
        public static extern bool UnhookWindowsHookEx(IntPtr hhk);

        [DllImport("coredll.dll")]
        public static extern int CallNextHookEx(IntPtr hhk, int nCode, uint wParam, KBDLLHOOKSTRUCT lParam);

        public const uint SND_ALIAS = 0x00010000;   // name is a WIN.INI [sounds] entry
        public const uint SND_FILENAME = 0x00020000;   // name is a file name

        public const uint SND_SYNC = 0x00000000;   // play synchronously (default)
        public const uint SND_ASYNC = 0x00000001;   // play asynchronously
        public const uint SND_NODEFAULT = 0x00000002;   // silence not default, if sound not found
        public const uint SND_MEMORY = 0x00000004;   // lpszSoundName points to a memory file
        public const uint SND_LOOP = 0x00000008;   // loop the sound until next sndPlaySound
        public const uint SND_NOSTOP = 0x00000010;   // don't stop any currently playing sound


        [DllImport("coredll.dll")]
        [return: MarshalAs(UnmanagedType.Bool)]
        public static extern bool sndPlaySound(byte[] lpszSoundName, uint fuSound);


        [StructLayout(LayoutKind.Sequential)]
        public class SYSTEMTIME
        {
            public ushort wYear;
            public ushort wMonth;
            public ushort wDayOfWeek;
            public ushort wDay;
            public ushort wHour;
            public ushort wMinute;
            public ushort wSecond;
            public ushort wMilliseconds;

            public SYSTEMTIME(DateTime datetime)
            {
                this.wYear = (ushort)datetime.Year;
                this.wMonth = (ushort)datetime.Month;
                this.wDay = (ushort)datetime.Day;
                this.wHour = (ushort)datetime.Hour;
                this.wMinute = (ushort)datetime.Minute;
                this.wSecond = (ushort)datetime.Second;
                this.wMilliseconds = (ushort)datetime.Millisecond;
            }
        }

        [DllImport("coredll.dll")]
        [return: MarshalAs(UnmanagedType.Bool)]
        public static extern bool SetLocalTime(SYSTEMTIME lpSystemTime);

        [DllImport("user32.dll", EntryPoint = "keybd_event", SetLastError = true)]
        internal static extern void keybd_eventWIN(byte bVk, byte bScan, int dwFlags, int dwExtraInfo);
        [DllImport("coredll.dll", EntryPoint = "keybd_event", SetLastError = true)]
        internal static extern void keybd_eventCE(byte bVk, byte bScan, int dwFlags, int dwExtraInfo);
        public static void keybd_event(byte bVk, byte bScan, int dwFlags, int dwExtraInfo)
         {
            if (m_PlatformCE)
            {
                keybd_eventCE(bVk, bScan, dwFlags, dwExtraInfo);
            }
            else
            {
                keybd_eventWIN(bVk, bScan, dwFlags, dwExtraInfo);
            }
        }

        #region 文本框单击事件使用

        /// <summary>
        /// A callback to a Win32 window procedure (wndproc)
        /// </summary>
        /// <param name="hwnd">The handle of the window receiving a message</param>
        /// <param name="msg">The message</param>
        /// <param name="wParam">The message's parameters (part 1)</param>
        /// <param name="lParam">The message's parameters (part 2)</param>
        /// <returns>A integer as described for the given message in MSDN</returns>
        public delegate int WndProc(IntPtr hwnd, uint msg, uint wParam, int lParam);

        public const uint WM_NOTIFY = 0x4E;
        public const uint WM_LBUTTONDOWN = 0x0201;
        public const uint WM_LBUTTONUP = 0x0202;

        //public enum WM
        //{
        //    LBUTTONDOWN = 0x0201,
        //    LBUTTONUP = 0x0202
        //}

        [System.Runtime.InteropServices.StructLayout(LayoutKind.Sequential)]
        public class NMHDR
        {
            public IntPtr hwndFrom;
            public uint idFrom;
            public uint code;
        }



        /// <summary>
        /// Helper function to convert a Windows lParam into a Point
        /// </summary>
        /// <param name="lParam">The parameter to convert</param>
        /// <returns>A Point where X is the low 16 bits and Y is the
        /// high 16 bits of the value passed in</returns>
        public static Point LParamToPoint(int lParam)
        {
            uint ulParam = (uint)lParam;
            return new Point(
                (int)(ulParam & 0x0000ffff),
                (int)((ulParam & 0xffff0000) >> 16));
        }


#if DESKTOP
        [DllImport("user32.dll")]
#else
        [DllImport("coredll.dll")]
#endif
        public extern static int DefWindowProc(IntPtr hwnd, uint msg, uint wParam, int lParam);


#if DESKTOP
        [DllImport("user32.dll")]
#else
        [DllImport("coredll.dll")]
#endif
        public extern static IntPtr SetWindowLong(IntPtr hwnd, int nIndex, IntPtr dwNewLong);

        public const int GWL_WNDPROC = -4;

#if DESKTOP
        [DllImport("user32.dll")]
#else
        [DllImport("coredll.dll")]
#endif
        public extern static int CallWindowProc(IntPtr lpPrevWndFunc, IntPtr hwnd, uint msg, uint wParam, int lParam);


#if DESKTOP
        [DllImport("user32.dll")]
#else
        [DllImport("coredll.dll")]
#endif
        public extern static uint GetMessagePos();

#if DESKTOP
        [DllImport("user32.dll")]
#else
        [DllImport("coredll.dll")]
#endif
        public extern static int SendMessage(IntPtr hwnd, uint msg, uint wParam);

        #endregion
    }
}


 

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值