Click here to Skip to main content
15,888,579 members
Articles / Desktop Programming / Windows Forms

Cool, Semi-transparent and Shaped Dialogs with Standard Controls for Windows 2000 and Above

Rate me:
Please Sign up or sign in to vote.
4.85/5 (95 votes)
28 Sep 2012CPOL3 min read 2.9M   32.8K   350  
This article tries to find a way to show Windows standard controls on layered windows. Provides both Native MFC and WinForms source code.
using System;
using System.Runtime.InteropServices;

namespace ImgDlgSample
{
    /// <summary>
    /// Define the window messages
    /// </summary>
    public static class WindowsMessage
    {
        public const Int32 WM_LBUTTONDOWN = 0x0201;
        public const Int32 WM_NCLBUTTONDOWN = 0x00A1;
        public const Int32 WM_SYSCOMMAND = 0x112;
        public const Int32 WM_PAINT = 0x000F;
        public const Int32 WM_MOVE = 0x0003;
        public const Int32 WM_CTLCOLORMSGBOX = 0x0132;
        public const Int32 WM_CTLCOLOREDIT = 0x0133;
        public const Int32 WM_CTLCOLORLISTBOX = 0x0134;
        public const Int32 WM_CTLCOLORBTN = 0x0135;
        public const Int32 WM_CTLCOLORDLG = 0x0136;
        public const Int32 WM_CTLCOLORSCROLLBAR = 0x0137;
        public const Int32 WM_CTLCOLORSTATIC = 0x0138;
        public const Int32 WM_CAPTURECHANGED = 0x0215;
    }

    /// <summary>
    /// Define the window class types
    /// </summary>
    public static class WndClassType
    {
        public const Int32 CS_VREDRAW          = 0x0001;
        public const Int32 CS_HREDRAW          = 0x0002;
        public const Int32 CS_DBLCLKS          = 0x0008;
        public const Int32 CS_OWNDC            = 0x0020;
        public const Int32 CS_CLASSDC          = 0x0040;
        public const Int32 CS_PARENTDC         = 0x0080;
        public const Int32 CS_NOCLOSE          = 0x0200;
        public const Int32 CS_SAVEBITS         = 0x0800;
        public const Int32 CS_BYTEALIGNCLIENT  = 0x1000;
        public const Int32 CS_BYTEALIGNWINDOW  = 0x2000;
        public const Int32 CS_GLOBALCLASS      = 0x4000;
        public const Int32 CS_IME              = 0x00010000;
        public const Int32 CS_DROPSHADOW       = 0x00020000;
    }
    
    /// <summary>
    /// Window style
    /// </summary>
    public static class WndStyle
    {
        public const UInt32 WS_OVERLAPPED = 0x00000000;
        public const UInt32 WS_POPUP = 0x80000000;
        public const UInt32 WS_CHILD = 0x40000000;
        public const UInt32 WS_MINIMIZE = 0x20000000;
        public const UInt32 WS_VISIBLE = 0x10000000;
        public const UInt32 WS_DISABLED = 0x08000000;
        public const UInt32 WS_CLIPSIBLINGS = 0x04000000;
        public const UInt32 WS_CLIPCHILDREN = 0x02000000;
        public const UInt32 WS_MAXIMIZE = 0x01000000;
        public const UInt32 WS_CAPTION = 0x00C00000;
        public const UInt32 WS_BORDER = 0x00800000;
        public const UInt32 WS_DLGFRAME = 0x00400000;
        public const UInt32 WS_VSCROLL = 0x00200000;
        public const UInt32 WS_HSCROLL = 0x00100000;
        public const UInt32 WS_SYSMENU = 0x00080000;
        public const UInt32 WS_THICKFRAME = 0x00040000;
        public const UInt32 WS_GROUP = 0x00020000;
        public const UInt32 WS_TABSTOP = 0x00010000;
        public const UInt32 WS_MINIMIZEBOX = 0x00020000;
        public const UInt32 WS_MAXIMIZEBOX = 0x00010000;
        public const UInt32 WS_TILED = WS_OVERLAPPED;
        public const UInt32 WS_ICONIC = WS_MINIMIZE;
        public const UInt32 WS_SIZEBOX = WS_THICKFRAME;
        public const UInt32 WS_TILEDWINDOW = WS_OVERLAPPEDWINDOW;
        public const UInt32 WS_OVERLAPPEDWINDOW = (WS_OVERLAPPED | WS_CAPTION | WS_SYSMENU | WS_THICKFRAME | WS_MINIMIZEBOX | WS_MAXIMIZEBOX);
        public const UInt32 WS_POPUPWINDOW = (WS_POPUP | WS_BORDER | WS_SYSMENU);
        public const UInt32 WS_CHILDWINDOW = (WS_CHILD);

    }

    /// <summary>
    /// Extended Window Styles
    /// </summary>
    public static class ExtendedWndStyle
    {
        public const UInt32 WS_EX_DLGMODALFRAME = 0x00000001;
        public const UInt32 WS_EX_NOPARENTNOTIFY = 0x00000004;
        public const UInt32 WS_EX_TOPMOST = 0x00000008;
        public const UInt32 WS_EX_ACCEPTFILES = 0x00000010;
        public const UInt32 WS_EX_TRANSPARENT = 0x00000020;
        public const UInt32 WS_EX_MDICHILD = 0x00000040;
        public const UInt32 WS_EX_TOOLWINDOW = 0x00000080;
        public const UInt32 WS_EX_WINDOWEDGE = 0x00000100;
        public const UInt32 WS_EX_CLIENTEDGE = 0x00000200;
        public const UInt32 WS_EX_CONTEXTHELP = 0x00000400;
        public const UInt32 WS_EX_RIGHT = 0x00001000;
        public const UInt32 WS_EX_LEFT = 0x00000000;
        public const UInt32 WS_EX_RTLREADING = 0x00002000;
        public const UInt32 WS_EX_LTRREADING = 0x00000000;
        public const UInt32 WS_EX_LEFTSCROLLBAR = 0x00004000;
        public const UInt32 WS_EX_RIGHTSCROLLBAR = 0x00000000;
        public const UInt32 WS_EX_CONTROLPARENT = 0x00010000;
        public const UInt32 WS_EX_STATICEDGE = 0x00020000;
        public const UInt32 WS_EX_APPWINDOW = 0x00040000;
        public const UInt32 WS_EX_OVERLAPPEDWINDOW = (WS_EX_WINDOWEDGE | WS_EX_CLIENTEDGE);
        public const UInt32 WS_EX_PALETTEWINDOW = (WS_EX_WINDOWEDGE | WS_EX_TOOLWINDOW | WS_EX_TOPMOST);
        public const UInt32 WS_EX_LAYERED = 0x00080000;
        public const UInt32 WS_EX_NOINHERITLAYOUT = 0x00100000;
        public const UInt32 WS_EX_LAYOUTRTL = 0x00400000;
        public const UInt32 WS_EX_COMPOSITED = 0x02000000;
        public const UInt32 WS_EX_NOACTIVATE = 0x08000000;
    }

    /// <summary>
    /// WNDPROC
    /// </summary>
    /// <param name="hWnd"></param>
    /// <param name="msg"></param>
    /// <param name="wParam"></param>
    /// <param name="lParam"></param>
    /// <returns></returns>
    public delegate IntPtr WndProcDelegate(IntPtr hWnd, uint msg, IntPtr wParam, IntPtr lParam);

    /// <summary>
    /// WNDCLASSEX
    /// </summary>
    [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
    public struct WNDCLASSEX
    {
        [MarshalAs(UnmanagedType.U4)]
        public uint cbSize;
        [MarshalAs(UnmanagedType.U4)]
        public uint style;
        public WndProcDelegate lpfnWndProc;
        public int cbClsExtra;
        public int cbWndExtra;
        public IntPtr hInstance;
        public IntPtr hIcon;
        public IntPtr hCursor;
        public IntPtr hbrBackground;
        [MarshalAs(UnmanagedType.LPWStr)]
        public string lpszMenuName;
        [MarshalAs(UnmanagedType.LPWStr)]
        public string lpszClassName;
        public IntPtr hIconSm;

        public void Init()
        {
            cbSize = (uint)Marshal.SizeOf(this);
        }
    }

    /// <summary>
    /// The wrapper class for user32.dll
    /// </summary>
    public static class User32Dll
    {
        [DllImport("user32.dll", CharSet = CharSet.Unicode, SetLastError = false)]
        public static extern IntPtr SendMessage(IntPtr hWnd, Int32 nMsg, IntPtr wParam, IntPtr lParam);

        [DllImport("user32.dll", CharSet = CharSet.Unicode)]
        public static extern IntPtr CreateWindowEx( uint dwExStyle
            , string lpClassName
            , string lpWindowName
            , uint dwStyle
            , int x
            , int y
            , int nWidth
            , int nHeight
            , IntPtr hWndParent
            , IntPtr hMenu
            , IntPtr hInstance
            , IntPtr lpParam
            );
        

        [DllImport("user32.dll", CharSet = CharSet.Unicode)]
        [return: MarshalAs(UnmanagedType.U2)]
        public static extern short RegisterClassEx([In] ref WNDCLASSEX lpwcx);

        [DllImport("user32.dll")]
        public static extern IntPtr DefWindowProc(IntPtr hWnd, uint uMsg, IntPtr wParam, IntPtr lParam);

        [DllImport("user32.dll")]
        public static extern bool UnregisterClass(string lpClassName, IntPtr hInstance);

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

        [DllImport("user32.dll")]
        public static extern bool DestroyWindow(IntPtr hWnd);

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

        

    
        [DllImport("user32.dll", ExactSpelling = true, SetLastError = true)]
        public static extern bool UpdateLayeredWindow(IntPtr hwnd
            , IntPtr hdcDst
            , ref POINT pptDst
            , ref SIZE psize
            , IntPtr hdcSrc
            , ref POINT pptSrc
            , uint crKey
            , [In] ref BLENDFUNCTION pblend
            , uint dwFlags
            );

        [DllImport("user32.dll")]
        [return: MarshalAs(UnmanagedType.Bool)]
        public static extern bool IsWindow(IntPtr hWnd);

        [DllImport("user32.dll")]
        public static extern bool MoveWindow(IntPtr hWnd, int X, int Y, int nWidth, int nHeight, bool bRepaint);


        public static IntPtr SetWindowLongPtr(IntPtr hWnd, int nIndex, IntPtr dwNewLong)
        {
            if (IntPtr.Size == 8)
                return SetWindowLongPtr64(hWnd, nIndex, dwNewLong);
            else
                return new IntPtr(SetWindowLong32(hWnd, nIndex, dwNewLong.ToInt32()));
        }

        [DllImport("user32.dll", EntryPoint = "SetWindowLongW")]
        private static extern int SetWindowLong32(IntPtr hWnd, int nIndex, int dwNewLong);

        [DllImport("user32.dll", EntryPoint = "SetWindowLongPtrW")]
        private static extern IntPtr SetWindowLongPtr64(IntPtr hWnd, int nIndex, IntPtr dwNewLong);

        [DllImport("user32.dll", EntryPoint = "GetWindowLongW")]
        private static extern int GetWindowLongPtr32(IntPtr hWnd, int nIndex);

        [DllImport("user32.dll", EntryPoint = "GetWindowLongPtrW")]
        private static extern IntPtr GetWindowLongPtr64(IntPtr hWnd, int nIndex);

        public static IntPtr GetWindowLongPtr(IntPtr hWnd, int nIndex)
        {
            if (IntPtr.Size == 8)
                return GetWindowLongPtr64(hWnd, nIndex);
            else
                return new IntPtr(GetWindowLongPtr32(hWnd, nIndex));
        }

        [DllImport("user32.dll")]
        public static extern IntPtr CallWindowProc(IntPtr lpPrevWndFunc, IntPtr hWnd, uint Msg, IntPtr wParam, IntPtr lParam);

        [DllImport("user32.dll")]
        public static extern int GetCaretPos(out POINT lpPoint);
    }
}

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
Team Leader
China China
Jerry is from China. He was captivated by computer programming since 13 years old when first time played with Q-Basic.



  • Windows / Linux & C++
  • iOS & Obj-C
  • .Net & C#
  • Flex/Flash & ActionScript
  • HTML / CSS / Javascript
  • Gaming Server programming / video, audio processing / image & graphics


Contact: vcer(at)qq.com
Chinese Blog: http://blog.csdn.net/wangjia184

Comments and Discussions