ProcessCmdKey

http://msdn.microsoft.com/en-us/library/7tas5c80.aspx

当你在使用此自定义的DataGridView日历列时,可能会想要在编辑日历单元格的时候点击Tab键不结束编辑,这时你就可以重写一下CalendarEditingControl.ProcessCmdKey方法来截获按键。

如果你只是想不做任何处理,那么你大可以直接返回true在你判断当前按键是Tab。

        protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
        {
            if (Keys.Tab == keyData)
            {
                return true;
            }
            return base.ProcessCmdKey(ref msg, keyData);
        }

而如果你还有附加要求,你要求Tab键在按下的时候,此时要像按下方向键一样切换输入区域,那你可以用SendMessage函数在ProcessCmdKey返回之前发送一条有方向键按下的Message。

        [DllImport("user32.dll", CharSet = CharSet.Auto)]
        static extern IntPtr SendMessage(IntPtr hWnd, UInt32 Msg, IntPtr wParam, IntPtr lParam);

        protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
        {
            if (Keys.Tab == keyData)
            {
                // VK_RIGHT 0x27
                // msg	{msg=0x100 (WM_KEYDOWN) hwnd=0x12f0564 wparam=0x27 lparam=0x14d0001 result=0x0}	System.Windows.Forms.Message
                SendMessage(msg.HWnd, 0x100, new IntPtr(0x27), new IntPtr(0x14d0001));
                return true;
            }
            return base.ProcessCmdKey(ref msg, keyData);
        }

关于按键消息,大家可以直接阅读MSDN文档:http://msdn.microsoft.com/en-us/library/ms646280(VS.85).aspx

 

其实你还可以直接重写DataGridView.ProcessDialogKey,来截获发送到此控件窗体的按键消息,如:

    public class DataGridViewEx : DataGridView
    {
        protected override bool ProcessDialogKey(Keys keyData)
        {
            if(Keys.Tab == keyData)
            {
                return true;
            }
            return base.ProcessDialogKey(keyData);
        }
    }
但我不建议重写DataGridView.ProcessDialogKey,这样的话你就需要再自定义一个控件了(如果你之前没有自定义DataGridView的需求),还有就是DataGridView是宿主,人家还有很多事情要做呢,所以自己的事情自己办比较好,还是CalendarEditingControl咋自己来处理自己的事情。 

ref: http://social.msdn.microsoft.com/Forums/en-US/winformsdatacontrols/thread/eb73180f-8f32-489f-9d09-30cd838d7966

-------------------------------------------------------------------------------

This method is called during message preprocessing to handle command keys. Command keys are keys that always take precedence over regular input keys. Examples of command keys include accelerators and menu shortcuts. The method must return true to indicate that it has processed the command key, or false to indicate that the key is not a command key. This method is only called when the control is hosted in a Windows Forms application or as an ActiveX control.

The ProcessCmdKey method first determines whether the control has a ContextMenu, and if so, enables the ContextMenu to process the command key. If the command key is not a menu shortcut and the control has a parent, the key is passed to the parent's ProcessCmdKey method. The net effect is that command keys are "bubbled" up the control hierarchy. In addition to the key the user pressed, the key data also indicates which, if any, modifier keys were pressed at the same time as the key. Modifier keys include the SHIFT, CTRL, and ALT keys.

Notes to Inheritors

When overriding the ProcessCmdKey method in a derived class, a control should return true to indicate that it has processed the key. For keys that are not processed by the control, the result of calling the base class's ProcessCmdKey method should be returned. Controls will seldom, if ever, need to override this method.

 
 

posted on 2012-05-15 11:20  刺客mrchenzh  阅读(498)  评论(0编辑  收藏  举报

导航