Click here to Skip to main content
15,885,278 members
Articles / Programming Languages / C#

VS Style Tooltips and A Whole Lot More...

Rate me:
Please Sign up or sign in to vote.
4.99/5 (82 votes)
6 Dec 2008CPOL10 min read 99K   2.3K   189  
A tooltip replacement class
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.Diagnostics;

namespace ApiTest
{
    public partial class frmMain : Form
    {

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

        [DllImport("user32.dll")]
        private static extern IntPtr GetDC(IntPtr handle);

        [DllImport("user32.dll")]
        private static extern int ReleaseDC(IntPtr handle, IntPtr hdc);

        [DllImport("gdi32.dll", ExactSpelling = true, PreserveSig = true)]
        private static extern IntPtr SelectObject(IntPtr hdc, IntPtr hgdiobj);

        [DllImport("gdi32.dll")]
        [return: MarshalAs(UnmanagedType.Bool)]
        private static extern bool DeleteObject(IntPtr hObject);

        private cTiming _cTimer = new cTiming();
        private cStoreDc _cDc = new cStoreDc();

        public frmMain()
        {
            InitializeComponent();
            
        }
        
        private void btnStart_Click(object sender, EventArgs e)
        {
            ssResults.Items.Clear();
            ssResults.Items.Add("");
            int i = 0;
            int iW = ApiTest.Properties.Resources.menuback.Width;
            int iH = ApiTest.Properties.Resources.menuback.Height;
            double mT = 0;
            IntPtr hdc = GetDC(pbDraw.Handle);
            _cDc.Width = iW;
            _cDc.Height = iH;
            IntPtr hOld = SelectObject(_cDc.Hdc, ApiTest.Properties.Resources.menuback.GetHbitmap());

            // api start timer
            _cTimer.Start();
            do
            {
                BitBlt(hdc, 0, 0, iW, iH, _cDc.Hdc, 0, 0, 0xCC0020);
                i++;
            } while (i < 1000);

            //results
            mT = _cTimer.Elapsed() / 1000;
            ssResults.Items[0].Text = " API: " + mT.ToString("#.######");
            SelectObject(_cDc.Hdc, hOld);
            ReleaseDC(pbDraw.Handle, hdc);

            i = 0;
            hdc = GetDC(pbDraw.Handle);
            Graphics g = Graphics.FromHdc(hdc);

            // graphics start timer
            _cTimer.Start();
            do
            {
                g.DrawImage(ApiTest.Properties.Resources.menuback, 0, 0, iW, iH);
                i++;
            } while (i < 1000);

            //results
            mT = _cTimer.Elapsed() / 1000;
            ssResults.Items[0].Text += " - Graphics: " + mT.ToString("#.######");
            ReleaseDC(pbDraw.Handle, hdc);
            g.Dispose();
        }
    }
    #region StoreDc
    [System.Security.Permissions.PermissionSet(System.Security.Permissions.SecurityAction.Demand, Name = "FullTrust")]
    public class cStoreDc
    {
        [DllImport("gdi32.dll")]
        private static extern IntPtr CreateDCA([MarshalAs(UnmanagedType.LPStr)]string lpszDriver, [MarshalAs(UnmanagedType.LPStr)]string lpszDevice, [MarshalAs(UnmanagedType.LPStr)]string lpszOutput, int lpInitData);

        [DllImport("gdi32.dll")]
        private static extern IntPtr CreateDCW([MarshalAs(UnmanagedType.LPWStr)]string lpszDriver, [MarshalAs(UnmanagedType.LPWStr)]string lpszDevice, [MarshalAs(UnmanagedType.LPWStr)]string lpszOutput, int lpInitData);

        [DllImport("gdi32.dll")]
        private static extern IntPtr CreateCompatibleDC(IntPtr hdc);

        [DllImport("gdi32.dll")]
        private static extern IntPtr CreateCompatibleBitmap(IntPtr hdc, int nWidth, int nHeight);

        [DllImport("gdi32.dll")]
        [return: MarshalAs(UnmanagedType.Bool)]
        private static extern bool DeleteDC(IntPtr hdc);

        [DllImport("gdi32.dll", ExactSpelling = true, PreserveSig = true)]
        private static extern IntPtr SelectObject(IntPtr hdc, IntPtr hgdiobj);

        [DllImport("gdi32.dll")]
        [return: MarshalAs(UnmanagedType.Bool)]
        private static extern bool DeleteObject(IntPtr hObject);

        private int _Height = 0;
        private int _Width = 0;
        private IntPtr _Hdc = IntPtr.Zero;
        private IntPtr _Bmp = IntPtr.Zero;
        private IntPtr _BmpOld = IntPtr.Zero;

        public IntPtr Hdc
        {
            get { return _Hdc; }
        }

        public IntPtr HBmp
        {
            get { return _Bmp; }
        }

        public int Height
        {
            get { return _Height; }
            set
            {
                _Height = value;
                ImageCreate(_Width, _Height);
            }
        }

        public int Width
        {
            get { return _Width; }
            set
            {
                _Width = value;
                ImageCreate(_Width, _Height);
            }
        }

        private void ImageCreate(int Width, int Height)
        {
            IntPtr pHdc = IntPtr.Zero;

            ImageDestroy();
            pHdc = CreateDCW("DISPLAY", "", "", 0);
            _Hdc = CreateCompatibleDC(pHdc);
            _Bmp = CreateCompatibleBitmap(pHdc, _Width, _Height);
            _BmpOld = SelectObject(_Hdc, _Bmp);
            if (_BmpOld == IntPtr.Zero)
            {
                ImageDestroy();
            }
            else
            {
                _Width = Width;
                _Height = Height;
            }
            DeleteDC(pHdc);
        }

        private void ImageDestroy()
        {
            if (_BmpOld != IntPtr.Zero)
            {
                SelectObject(_Hdc, _BmpOld);
                _BmpOld = IntPtr.Zero;
            }
            if (_Bmp != IntPtr.Zero)
            {
                DeleteObject(_Bmp);
                _Bmp = IntPtr.Zero;
            }
            if (_Hdc != IntPtr.Zero)
            {
                DeleteDC(_Hdc);
                _Hdc = IntPtr.Zero;
            }
        }

        public void Dispose()
        {
            ImageDestroy();
        }
    }
    #endregion
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Network Administrator vtdev.com
Canada Canada
Network and programming specialist. Started in C, and have learned about 14 languages since then. Cisco programmer, and lately writing a lot of C# and WPF code, (learning Java too). If I can dream it up, I can probably put it to code. My software company, (VTDev), is on the verge of releasing a couple of very cool things.. keep you posted.

Comments and Discussions