WPF custom Controls--using the Win32 control

Source: Internet
Author: User
Tags assert win32

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));

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.