zoukankan      html  css  js  c++  java
  • WPF与Win32的交互(1)

    1、把Win32的按钮添加到WPF窗口中

    1)添加如下的using指令

    using System.Windows.Interop;
    using System.Runtime.InteropServices;

    2)创建Win32ButtonHost类

      public class Win32ButtonHost : HwndHost
        {               
            IntPtr hwndHost = IntPtr.Zero;
            IntPtr hwndButton = IntPtr.Zero;
    
            public int ButtonWidth = 0;
            public int ButtonHeight = 0;
    
            private const int WS_CHILD = 0X40000000;
            private const int WS_VISIBLE = 0X10000000;
            private const int WS_BORDER = 0X00800000;
    
            public Win32ButtonHost(int width, int height)
            {
                ButtonWidth = width;
                ButtonHeight = height;
            }
    
            //CreateWindowEx函数
            [DllImport("user32.dll", EntryPoint = "CreateWindowEx", CharSet = CharSet.Auto)]
            internal static extern IntPtr CreateWindowEx(
                int dwExStyle,
                string lpszClassName,
                string lpszWindowName,
                int style,
                int x, int y,
                int width, int height,
                IntPtr hwndParent,
                IntPtr hMenu,
                IntPtr hInst,
                [MarshalAs(UnmanagedType.AsAny)] object pvParam
                );
    
            protected override HandleRef BuildWindowCore(HandleRef hwndParent)
            {
                hwndHost = CreateWindowEx(0, "static", "", WS_CHILD | WS_VISIBLE, 0, 0, ButtonWidth, ButtonHeight, hwndParent.Handle, IntPtr.Zero, IntPtr.Zero, 0);
                hwndButton = CreateWindowEx(0, "button", "Win32 Button", WS_CHILD | WS_VISIBLE | WS_BORDER, 0, 0, ButtonWidth, ButtonHeight, hwndHost, IntPtr.Zero, IntPtr.Zero, 0);
                return new HandleRef(this, hwndHost);
            }
    
            //DestoryWindow函数
            [DllImport("user32.dll", EntryPoint = "BuildWindowCore", CharSet = CharSet.Auto)]
            internal static extern bool DestoryWindow(IntPtr hwnd);
    
        
            protected override void DestroyWindowCore(HandleRef hwnd)
            {
                DestoryWindow(hwnd.Handle);
            }
        }
    
        
     
    3)WPF页面中使用Win32ButtonHost类
     
     <Grid>
            <Border x:Name="myBorder" Margin="20" BorderBrush="LightBlue"  BorderThickness="2"/>
        </Grid>
    
      Win32ButtonHost  win32ButtonHost= new Win32ButtonHost(100, 50);
       myBorder.Child = win32ButtonHost;
    

     
     
     
     
  • 相关阅读:
    沈阳集训day2
    ac自动机
    2018沈阳集训day1
    洛谷P1875 佳佳的魔法药水
    洛谷P1941 飞扬的小鸟
    Noip2016day2
    1123: [POI2008]BLO
    1718: [Usaco2006 Jan] Redundant Paths 分离的路径
    P3119 [USACO15JAN]草鉴定Grass Cownoisseur
    [LeetCode] Clone Graph
  • 原文地址:https://www.cnblogs.com/linlf03/p/2171860.html
Copyright © 2011-2022 走看看