This article supporting source code
Although WPF is very powerful, some things Win32 have done well, we can totally copycat.
One. How to create a Win32 control
1. First define a Wndclassex class, refer to the Http://baike.baidu.com/view/1750396.html?tp=0_11
WNDCLASSEX wndClsEx = new WNDCLASSEX();
wndClsEx.Init();//(uint)Marshal.SizeOf(this);得到类的大小
wndClsEx.style = WndClassType.CS_VREDRAW | WndClassType.CS_HREDRAW;//窗口的风格
wndClsEx.lpfnWndProc = new WndProcDelegate(User32Dll.DefWindowProc);//处理类的消息,这 里用的是默认处理
wndClsEx.cbClsExtra = 0;//指定紧跟在窗口类结构后的附加字节数
wndClsEx.cbWndExtra = 0;//如果一个应用程序在资源中用CLASS伪指令注册一个对话框类时,则必 须把这个成员设成DLGWINDOWEXTRA
wndClsEx.hInstance = Kernal32Dll.GetModuleHandle (null);//模块的句柄
wndClsEx.hIcon = IntPtr.Zero;//图标句柄
wndClsEx.hIconSm = IntPtr.Zero;//和窗口类关联的小图标。如果该值为NULL。则把hCursor中的图标转换成大小合适的小 图标。
wndClsEx.hCursor = IntPtr.Zero;//光标句柄
wndClsEx.hbrBackground = IntPtr.Zero;//背景画刷句柄
wndClsEx.lpszClassName = m_WndClsName;//定义自己的类名, 比如curry,或XXX
wndClsEx.lpszMenuName = null;//菜单名称
2. Registration class, return value not 0 for success
bool success = User32Dll.RegisterClassEx(ref wndClsEx) != 0;
Debug.Assert(success, "RegisterWndClass failed.");
3. Create a window, refer to Http://baike.baidu.com/view/1080304.htm
IntPtr windowHandle = User32Dll.CreateWindowEx(ExtendedWndStyle.WS_EX_LAYOUTRTL//扩展样式
, m_WndClsName //刚才注册完的名称
, null //窗体名称
, WndStyle.WS_VISIBLE | WndStyle.WS_CHILD //子窗体
, this.Left //X坐标
, this.Top //Y 坐标
, this.Width //宽度
, this.Height //高度
, this.Parent.Handle //父对象句柄
, IntPtr.Zero //上下文菜单句柄
, Kernal32Dll.GetModuleHandle(null)//实例句柄
, IntPtr.Zero//指向一个值的指针,该值传递给窗口 WM_CREATE消息
);
Debug.Assert(User32Dll.IsWindow(windowHandle), "CreateWindowEx failed.");
If you want to refer to the other window's style information, you can use the Spy + + tool to see
4. Display window
User32Dll.ShowWindow(windowHandle, (int) (this.Visible ? WindowShowStyle.Show : WindowShowStyle.Hide));
5. Destroy the window and unregister the class
User32Dll.DestroyWindow(windowHandle);
windowHandle = IntPtr.Zero;
User32Dll.UnregisterClass(m_WndClsName, Kernal32Dll.GetModuleHandle(null));