CreateParams in the Compact Framework

 ̄綄美尐妖づ 提交于 2019-12-12 02:58:40

问题


I'm using the .NET Compact Framework and would like to subclass a PictureBox control (in this case to remove the CS_DBLCLKS style from specific instances of the PictureBox control). The code below works on .NET standard, but not the Compact Framework:

using System;
using System.Windows.Forms;

namespace NoDblClick
{
    public partial class NoDblClickPicControl : PictureBox
    {
        private const int CS_DBLCLKS = 0x008;
        public NoDblClickPicControl()
        {
        }

        protected override CreateParams CreateParams
        {
            get
            {
                // No compile, missing directive or assembly directive
                CreateParams cp = base.CreateParams;
                cp.ClassStyle &= ~CS_DBLCLKS;
                return cp;
            }
        }
    }
}

How do I get this to work on the Compact Framework? Perhaps I can PInvoke the functionality (from coredll.dll say)?


回答1:


These styles are applied when the window class is created, and to my knowledge cannot be changed on the compact framework. Besides CreateParams, the full framework allows a window handle to be recreated, which also is not possible on the compact framework.

You could manually filter the messages sent to the control, and convert the double click message back to a mouse down message:

public partial class NoDblClickPicControl : PictureBox
{
    private const int WM_LBUTTONDBLCLK = 0x0203;
    private const int WM_LBUTTONDOWN = 0x0201;
    private const int GWL_WNDPROC = -4;

    [DllImport("coredll.dll", SetLastError = true)]
    private static extern IntPtr SetWindowLong(IntPtr hWnd, int nIndex, IntPtr newWndProc);
    [DllImport("coredll.dll", SetLastError = true)]
    private static extern IntPtr CallWindowProc(IntPtr lpPrevWndFunc, IntPtr hWnd, uint msg, IntPtr wParam, IntPtr lParam);

    private delegate IntPtr WndProcDelegate(IntPtr hWnd, uint msg, IntPtr wParam, IntPtr lParam);

    private IntPtr prevWndProc;
    private WndProcDelegate @delegate;

    protected override void OnHandleCreated(EventArgs e)
    {
        base.OnHandleCreated(e);
        @delegate = new WndProcDelegate(MyWndProc);
        prevWndProc = SetWindowLong(Handle, GWL_WNDPROC, Marshal.GetFunctionPointerForDelegate(@delegate));
    }

    protected override void OnHandleDestroyed(EventArgs e)
    {
        base.OnHandleDestroyed(e);
        SetWindowLong(Handle, GWL_WNDPROC, prevWndProc);
    }

    private IntPtr MyWndProc(IntPtr hWnd, uint msg, IntPtr wParam, IntPtr lParam)
    {
        if (msg == WM_LBUTTONDBLCLK)
        {
            msg = WM_LBUTTONDOWN;
        }

        return CallWindowProc(prevWndProc, hWnd, msg, wParam, lParam);
    }
}


来源:https://stackoverflow.com/questions/30998731/createparams-in-the-compact-framework

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!