开发者

Wrapping msctls_hotkey32 in .NET Windows Forms

I'm trying to get a hotkey control working in C# based on both the Control class and the msctls_hotkey32 win32 class. What I have works, though it's not perfect:

    class Hotkey : TextBox
    {
        protected override CreateParams CreateParams
        {
        开发者_高级运维    get
            {
                CreateParams parms = base.CreateParams;
                parms.ClassName = "msctls_hotkey32";
                return parms;
            }
        }
        // (some other p/invoke stuff for HKM_SETHOTKEY, etc.)
    }

The advantages are that it's simple and it works. The disadvantage is that it inherits a bunch of functionality from TextBox that doesn't apply to this control. So my question is - is there a better way to do this without reinventing the wheel?

Thanks.


You could always inherit directly from System.Windows.Forms.Control. And then of course add the specific methods and properties that you want to support. You will need to set the control styles appropriately for this to work, here is a quick example.

using System;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
  class HotKey : Control  
  {
    public HotKey()
    {
      SetStyle(ControlStyles.UserPaint, false);
    }

    protected override CreateParams CreateParams
    {
      get
      {
        CreateParams cp = base.CreateParams;
        cp.ClassName = "msctls_hotkey32";

        return cp;
      }
    }
  }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜