文本框不可点击,但编辑 [英] Textbox not clickable but editable

查看:185
本文介绍了文本框不可点击,但编辑的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个小表格10文本框,我让他们在正确的选项卡顺序设置当前我希望他们能够标签的方式。我不知道是否有一种方法来设置文本框起来,使他们不能选择编辑,除非它们被分页成。 IE浏览器...我不希望最终用户能够点击文本框编辑它们,我只希望他们通过编辑Tab键。

I have a small form with 10 textboxes, I have them set in the right Tab Order currently the way I want them to Tab To. I was wondering if there is a way to set the textboxes up so they cannot be selected for editing unless they are Tabbed into. ie... I don't want the end user to be able to click on the textbox to edit them, I only want them editable through Tabbing.

推荐答案

这应该做的伎俩

public partial class PoorTextBox : TextBox
{
    protected override void WndProc(ref Message m)
    {
        if (m.Msg == (int) WM.LBUTTONDOWN)
        {
            return;//Eat mouse down events 
        }
        base.WndProc(ref m);
    }
}



窗口消息枚举可以发现的此处



如何做没有继承文本框

class EatMouseDown : NativeWindow
{
    protected override void WndProc(ref Message m)
    {
        if (m.Msg == (int)WM.LBUTTONDOWN)
        {
            return;
        }
        base.WndProc(ref m);
    }
}

protected override void OnLoad(EventArgs e)
{
    base.OnLoad(e);

    new EatMouseDown().AssignHandle(textBox1.Handle);//Subclass a Handle
}



如何做到这一点没有任何继承:


How to do it without any inheritance:

清理忽略的一部分,这也很重要。这可能是越野车,但工程。推荐方法是使用继承。所需的方法从.NET FW SRC拉。

Clean up part omitted, which is also important. This may be buggy but that works. Recommended way is to use inheritance. Required methods pulled from .net fw src.

class EatMouseDown
{
    public delegate IntPtr WndProc(IntPtr hWnd, int msg, IntPtr wParam, IntPtr lParam);

    #region External methods...

    [DllImport("user32.dll", CharSet = CharSet.Auto)]
    public static extern IntPtr SetWindowLong(HandleRef hWnd, int nIndex, WndProc wndproc);

    [DllImport("user32.dll", CharSet = CharSet.Auto)]
    public static extern IntPtr SetWindowLongPtr(HandleRef hWnd, int nIndex, WndProc wndproc);

    [DllImport("user32.dll", CharSet = CharSet.Auto)]
    public static extern IntPtr GetWindowLong(HandleRef hWnd, int nIndex);

    [DllImport("user32.dll", CharSet = CharSet.Auto)]
    public static extern IntPtr GetWindowLongPtr(HandleRef hWnd, int nIndex);

    [DllImport("user32.dll", CharSet = CharSet.Auto)]
    public static extern IntPtr DefWindowProc(IntPtr hWnd, int msg, IntPtr wParam, IntPtr lParam);

    [DllImport("user32.dll", CharSet = CharSet.Auto)]
    public static extern IntPtr CallWindowProc(IntPtr wndProc, IntPtr hWnd, int msg, IntPtr wParam, IntPtr lParam);

    #endregion

    private const int GWLP_WNDPROC = -4;
    private IntPtr handle;
    private IntPtr originalWndProc;
    private IntPtr currentWndProc;

    public static IntPtr SetWindowLongHelper(HandleRef hWnd, int nIndex, WndProc wndProc)
    {
        return IntPtr.Size == 4
            ? SetWindowLong(hWnd, nIndex, wndProc)
            : SetWindowLongPtr(hWnd, nIndex, wndProc);
    }

    public static IntPtr GetWindowLongHelper(HandleRef hWnd, int nIndex)
    {
        return IntPtr.Size == 4
            ? GetWindowLong(hWnd, nIndex)
            : GetWindowLongPtr(hWnd, nIndex);
    }

    internal void SubclassHandle(IntPtr handle)
    {
        this.handle = handle;
        this.originalWndProc = GetWindowLongHelper(new HandleRef(this, handle), GWLP_WNDPROC);
        SetWindowLongHelper(new HandleRef(this, handle), GWLP_WNDPROC, new WndProc(this.Callback));
        this.currentWndProc = GetWindowLongHelper(new HandleRef(this, handle), GWLP_WNDPROC);
    }

    private IntPtr Callback(IntPtr hwnd, int msg, IntPtr wparam, IntPtr lparam)
    {
        var m = Message.Create(hwnd, msg, wparam, lparam);
        if (m.Msg == (int)WM.LBUTTONDOWN)
        {
            return IntPtr.Zero;
        }
        return CallWindowProc(originalWndProc, hwnd, msg, wparam, lparam);
    }
}

protected override void OnLoad(EventArgs e)
{
    base.OnLoad(e);

    new EatMouseDown().SubclassHandle(textBox1.Handle);//Subclass a Handle
}

这篇关于文本框不可点击,但编辑的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆