当前位置:编程学习 > C#/ASP.NET >>

C#调API创建窗口

目的:C#调API创建窗口

注册窗口类:
ATOM RegisterClassEx(CONST WNDCLASSEX *Ipwcx)(这里参数怎么写?)
创建窗口:
HWND CreateWindowEx(
        DWORD dwExStyle,
        LPCTSTR IpClassName,
        LPCTSTR lpWindowName,
        DWORD dwStyle,
        int x,
        int y,
        int nWidth,
        int nHeight,
        HWND hWndParent,
        HMENUhMenu,
        HANDLE hlnstance,
        LPVOID lpParam)

--------------------编程问答-------------------- 你可以翻看.net 2.0 framework的源代码,看看form.show是如何调用api的。 --------------------编程问答--------------------

        [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
        public struct WNDCLASSEX
        {
            [MarshalAs(UnmanagedType.U4)]
            public int cbSize;
            [MarshalAs(UnmanagedType.U4)]
            public int style;
            public IntPtr lpfnWndProc; // not WndProc
            public int cbClsExtra;
            public int cbWndExtra;
            public IntPtr hInstance;
            public IntPtr hIcon;
            public IntPtr hCursor;
            public IntPtr hbrBackground;
            public string lpszMenuName;
            public string lpszClassName;
            public IntPtr hIconSm;
        }

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


这是定义的代码,相关说明可以参见MSDN:
RegisterClassEx function
WNDCLASSEX structure --------------------编程问答--------------------
引用 1 楼  的回复:
你可以翻看.net 2.0 framework的源代码,看看form.show是如何调用api的。

怎么看源码? --------------------编程问答--------------------
引用 2 楼  的回复:
C# code

        [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
        public struct WNDCLASSEX
        {
            [MarshalAs(UnmanagedType.U4)]
            public int cbSize;……


问下 
RegisterClassEx注册窗口类
CreateWindowEx创建窗体
那 RegisterClassEx 怎么为将要创建的窗体注册,我实在有点晕
能不能帮忙写个例子啊,就是创建个窗体出来就行 --------------------编程问答-------------------- 贴下我现在的代码,求解答

[DllImport("User32.dll", EntryPoint = "CreateWindowEx")]
        public static extern IntPtr CreateWindowEx(
            ExWindowStyle dwExStyle,
            string lpClassName,
            string lpWindowName,
            WindowStyle dwStyle,
            int x,
            int y,
            uint nWidth,
            uint nherght,
            IntPtr hWndParent,
            IntPtr hMenu,
            IntPtr hlnstance,
            int lpParam);
[DllImport("user32.dll", EntryPoint = "RegisterClassEx")]
        public static extern int RegisterClassEx(
                  [In] ref WNDCLASSEX Ipwcx
                 );

WNDCLASSEX aWNDCLASSEX = new WNDCLASSEX();
            aWNDCLASSEX.cbSize = (int)Marshal.SizeOf(aWNDCLASSEX);
            aWNDCLASSEX.style = 0;
            aWNDCLASSEX.lpfnWndProc = new IntPtr();
            aWNDCLASSEX.cbClsExtra = 0;
            aWNDCLASSEX.cbWndExtra = 0;
            aWNDCLASSEX.hIcon = new IntPtr();
            aWNDCLASSEX.hCursor = new IntPtr();
            aWNDCLASSEX.hbrBackground = new IntPtr();
            aWNDCLASSEX.lpszMenuName = null;
            aWNDCLASSEX.lpszClassName = "WinClass";
            aWNDCLASSEX.hIconSm = new IntPtr();
            RegisterClassEx(ref aWNDCLASSEX);

            int lpParam = 0;
            IntPtr hWndParent = new IntPtr();
            IntPtr hMenu = new IntPtr();
            IntPtr i = CreateWindowEx (ExWindowStyle.WS_EX_CLIENTEDGE, "lpClassName", "lpWindowName", WindowStyle.WS_BORDER, 100, 100, 100, 100, hWndParent, hMenu, aWNDCLASSEX.hInstance, lpParam);
            ShowWindow(i, 1);
--------------------编程问答-------------------- 累不累哦

--------------------编程问答--------------------
引用 6 楼  的回复:
累不累哦

新手嘛,走得累点应该的
补充:.NET技术 ,  C#
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,