方法 : 使用のプラットフォーム用のヘルパー クラスを呼び出す

[このドキュメントはプレビュー版であり、後のリリースで変更されることがあります。 空白のトピックは、プレースホルダーとして挿入されています。]

このサンプル プログラムは、構造体をカプセル化、プラットフォーム呼び出し宣言、およびデリゲートをネイティブ ウィンドウ プロシージャを呼び出します。 このクラスを次のトピックにしてください。

方法 : サブクラスをネイティブのコールバック関数を使用して、ツリー ビュー

方法 : サブクラスをネイティブのコールバック関数を使用したボタン

このサンプル プログラムについては 管理ウィンドウ プロシージャでコントロールをサブクラス化 で詳しく説明します。

使用例

                        using System;
using System.Drawing;
using System.Runtime.InteropServices;

// Contains managed wrappers and implementations of Win32// structures, delegates, constants and platform invokes// used by the GradientFill and Subclassing samples.publicsealedclass Win32
    {
        publicstruct TRIVERTEX
        {
            publicint x;
            publicint y;
            publicushort Red;
            publicushort Green;
            publicushort Blue;
            publicushort Alpha;
            public TRIVERTEX(int x, int y, Color color)
                : this(x, y, color.R, color.G, color.B, color.A)
            {
            }
            public TRIVERTEX(
                int x, int y,
                ushort red, ushort green, ushort blue,
                ushort alpha)
            {
                this.x = x;
                this.y = y;
                this.Red = (ushort)(red << 8);
                this.Green = (ushort)(green << 8);
                this.Blue = (ushort)(blue << 8);
                this.Alpha = (ushort)(alpha << 8);
            }
        }
        publicstruct GRADIENT_RECT
        {
            publicuint UpperLeft;
            publicuint LowerRight;
            public GRADIENT_RECT(uint ul, uint lr)
            {
                this.UpperLeft = ul;
                this.LowerRight = lr;
            }
        }
        publicstruct GRADIENT_TRIANGLE
        {
            publicuint Vertex1;
            publicuint Vertex2;
            publicuint Vertex3;
            public GRADIENT_TRIANGLE(uint v1, uint v2, uint v3)
            {
                this.Vertex1 = v1;
                this.Vertex2 = v2;
                this.Vertex3 = v3;
            }
        }

        // WM_NOTIFY notification message header.
        [System.Runtime.InteropServices.StructLayout(LayoutKind.Sequential)]
        publicclass NMHDR
        {
            private IntPtr hwndFrom;
            publicuint idFrom;
            publicuint code;
        }

        //[System.Runtime.InteropServices.StructLayout(LayoutKind.Sequential)]publicstruct TVITEM
        {
            publicint mask;
            private IntPtr hItem;
            publicint state;
            publicint stateMask;
            private IntPtr pszText;
            publicint cchTextMax;
            publicint iImage;
            publicint iSelectedImage;
            publicint cChildren;
            private IntPtr lParam;
        }

        // Native representation of a point.publicstruct POINT
        {
            publicint X;
            publicint Y;
        }

        publicstruct TVHITTESTINFO
        {
            public POINT pt;
            publicuint flags;
            private IntPtr hItem;
        }

        // A callback to a Win32 window procedure (wndproc):// Parameters://   hwnd - The handle of the window receiving a message.//   msg - The message//   wParam - The message's parameters (part 1).//   lParam - The message's parameters (part 2).//  Returns an integer as described for the given message in MSDN.publicdelegateint WndProc(IntPtr hwnd, uint msg, uint wParam, int lParam);

        [DllImport("coredll.dll", SetLastError = true, EntryPoint = "GradientFill")]
        publicexternstaticbool GradientFill(
            IntPtr hdc,
            TRIVERTEX[] pVertex,
            uint dwNumVertex,
            GRADIENT_RECT[] pMesh,
            uint dwNumMesh,
            uint dwMode);

        publicconstint GRADIENT_FILL_RECT_H = 0x00000000;
        publicconstint GRADIENT_FILL_RECT_V = 0x00000001;

        // Not supported on Windows CE:publicconstint GRADIENT_FILL_TRIANGLE = 0x00000002;


        [DllImport("coredll.dll")]
        publicexternstatic IntPtr SetWindowLong(
            IntPtr hwnd, int nIndex, IntPtr dwNewLong);
        publicconstint GWL_WNDPROC = -4;

        [DllImport("coredll.dll")]
        publicexternstaticint CallWindowProc(
            IntPtr lpPrevWndFunc, IntPtr hwnd, uint msg, uint wParam, int lParam);

        [DllImport("coredll.dll")]
        publicexternstaticint DefWindowProc(
            IntPtr hwnd, uint msg, uint wParam, int lParam);

        [DllImport("coredll.dll")]
        publicexternstaticint SendMessage(
            IntPtr hwnd, uint msg, uint wParam, ref TVHITTESTINFO lParam);

        [DllImport("coredll.dll", SetLastError = true)]
        publicexternstaticint SendMessage(
            IntPtr hwnd, uint msg, uint wParam, ref TVITEM lParam);

        [DllImport("coredll.dll")]
        publicexternstaticuint GetMessagePos();

        [DllImport("coredll.dll")]
        publicexternstatic IntPtr BeginPaint(IntPtr hwnd, ref PAINTSTRUCT ps);

        [DllImport("coredll.dll")]
        publicexternstaticbool EndPaint(IntPtr hwnd, ref PAINTSTRUCT ps);

        publicstruct PAINTSTRUCT
        {
            private IntPtr hdc;
            publicbool fErase;
            public Rectangle rcPaint;
            publicbool fRestore;
            publicbool fIncUpdate;
            [MarshalAs(UnmanagedType.ByValArray, SizeConst = 32)]
            publicbyte[] rgbReserved;
        }

        [DllImport("coredll.dll")]
        publicexternstatic IntPtr GetDC(IntPtr hwnd);

        [DllImport("coredll.dll")]
        publicexternstaticbool ReleaseDC(IntPtr hwnd, IntPtr hdc);

        // Helper function to convert a Windows lParam into a Point.//   lParam - The parameter to convert.// Returns a Point where X is the low 16 bits and Y is the// high 16 bits of the value passed in.publicstatic Point LParamToPoint(int lParam)
        {
            uint ulParam = (uint)lParam;
            returnnew Point(
                (int)(ulParam & 0x0000ffff),
                (int)((ulParam & 0xffff0000) >> 16));
        }

        // Windows messagespublicconstuint WM_PAINT = 0x000F;
        publicconstuint WM_ERASEBKGND = 0x0014;
        publicconstuint WM_KEYDOWN = 0x0100;
        publicconstuint WM_KEYUP = 0x0101;
        publicconstuint WM_MOUSEMOVE = 0x0200;
        publicconstuint WM_LBUTTONDOWN = 0x0201;
        publicconstuint WM_LBUTTONUP = 0x0202;
        publicconstuint WM_NOTIFY = 0x4E;

        // Notificationspublicconstuint NM_CLICK = 0xFFFFFFFE;
        publicconstuint NM_DBLCLK = 0xFFFFFFFD;
        publicconstuint NM_RCLICK = 0xFFFFFFFB;
        publicconstuint NM_RDBLCLK = 0xFFFFFFFA;

        // Keypublicconstuint VK_SPACE = 0x20;
        publicconstuint VK_RETURN = 0x0D;

        // Treeviewpublicconstuint TV_FIRST = 0x1100;
        publicconstuint TVM_HITTEST = TV_FIRST + 17;

        publicconstuint TVHT_NOWHERE = 0x0001;
        publicconstuint TVHT_ONITEMICON = 0x0002;
        publicconstuint TVHT_ONITEMLABEL = 0x0004;
        publicconstuint TVHT_ONITEM = (TVHT_ONITEMICON | TVHT_ONITEMLABEL | TVHT_ONITEMSTATEICON);
        publicconstuint TVHT_ONITEMINDENT = 0x0008;
        publicconstuint TVHT_ONITEMBUTTON = 0x0010;
        publicconstuint TVHT_ONITEMRIGHT = 0x0020;
        publicconstuint TVHT_ONITEMSTATEICON = 0x0040;
        publicconstuint TVHT_ABOVE = 0x0100;
        publicconstuint TVHT_BELOW = 0x0200;
        publicconstuint TVHT_TORIGHT = 0x0400;
        publicconstuint TVHT_TOLEFT = 0x0800;

        publicconstuint TVM_GETITEM = TV_FIRST + 62;  //TVM_GETITEMWpublicconstuint TVIF_TEXT = 0x0001;
        publicconstuint TVIF_IMAGE = 0x0002;
        publicconstuint TVIF_PARAM = 0x0004;
        publicconstuint TVIF_STATE = 0x0008;
        publicconstuint TVIF_HANDLE = 0x0010;
        publicconstuint TVIF_SELECTEDIMAGE = 0x0020;
        publicconstuint TVIF_CHILDREN = 0x0040;
        publicconstuint TVIF_DI_SETITEM = 0x1000;

}

参照

処理手順

方法 : Windows のプロシージャをフックのクラスを使用します。

方法 : サブクラスをネイティブのコールバック関数を使用して、ツリー ビュー

方法 : サブクラスをネイティブのコールバック関数を使用したボタン

概念

管理ウィンドウ プロシージャでコントロールをサブクラス化

.NET コンパクトなフレームワーク方法を説明したトピックの検索

その他の技術情報

.NET Compact Framework の相互運用性