???? ID: 320584 - ????? ???????: 04 ?????? 2010 - ??????: 2.0

???????? ??? ??????????? ?? ????? C# ?? ????? ???? trap ???? ????

?????? ??????This article applies to a different operating system than the one you are using. Article content that may not be relevant to you is disabled.

?? ????? ??

??? ?? ??????? ???? | ??? ?? ??????? ????

??????

?? ??? ?? ??? ???? ????????? ???? ?? ?? Windows ??????? ???????? ??? ??????????? trap ???? ????? ?? ??? ??? ????? ??? ?? ????? ??, ?? ?? ???? ???-??? ????????? intercept ?? ???? ???? ?? ????? ??????, CTRL ?? ALT intercept ?? ?? ???? ???? ?? ????? ?????? ??????? ????? ?????? ???? ??? ??? ??, ??? ??????????? ?? ???????? ????????, ???? ???? ??? ??????? ?? CD-ROM ??? ?????? ????????? ???????? ?? ??? ???????? ?? ?? ???? ?? ? ???????

??????? ????????, ???? ?? ???KeyUp,KeyDown, ??KeyPress????? ?????? ?? ??????????? ?? ????? ???? ?? ??? ???????? ???? ???????, ??? ???????? ??? ?????? ?? ??????? ??? ??????????? ?? ??? ?? ??????? ????

?????? ?? ???, ?? ????? ????DataGrid????????: ??? ??? ???? ????? ?? ??? ????? ???? ???, ??? ???????? (LEFT ARROW, RIGHT ARROW, UP ???, ?? ???? ???) ??????? ????KeyUp?????? ???? ????????, ???? ??? ?? 4, ??? ??? ????? ???? ???DataGrid????, ??? ???? ???????? ???????? ???????? ?? ??? ????? ????? ?? ??? ??? ??????? ??? ????????? ??? ?? ???? ???????? ?? 4 ??? ????? ???? ???, ???? ???KeyUp, ?? ??? ??? ?????, ?? ???? ?? ???? ???????? ??? ????? ?? ??? ??? ?? ???????????? ??? ?? ???????? ?? ?????? ?? ???? ??????????? ?? ?????? ???? ?? ??? ?? ???? ??? ??? ????? ?? ???? ?? ???? ????

?? ???? ??? ??? ????? ?? ??? ????? ???? ?? ??? ???? ?? ???DataGrid, ???????DataGrid???????? ?? ??? ?? ?? ?????? ?? ????? ???????? ??? ?? ???? .NET ???????? ?? ??? ???? approach ?? ????? ?? ???? ???

????? Trap ??? ????

Trap ??????????? ?? ???? Windows ??????? ???????? ??? ??, ?? ??? ???? ???????? ?? ?? ?? ????? ???, ?? ?? ??????? ?? ???? ?? ?????? ?? ??? ???? derive ???? ?????ProcessCmdKey?????? ??? ?? overridden ?????? ??? trap ???? ????? ??? ?? ??????????? ?? ??????? ???? ?? ??? ??? ???? ????? ????? ????? ??? ?? ?? ??? ???? ?? ??? ??????? ?????? ?? ?? ?????? ??:
class MyDataGrid : System.Windows.Forms.DataGrid
{
   protected override bool ProcessCmdKey(ref Message msg, Keys keyData)	
   {
   }
}	
				

Implement the Overridden Method

The system passes two parameters to theProcessCmdKey????:msg, ??keyData. Themsgparameter contains the Windows Message, such as WM_KEYDOWN. ThekeyDataparameter contains the key code of the key that was pressed. If CTRL or ALT was also pressed, thekeyDataparameter contains the ModifierKey information.

?? ????? ????msgparameter is not mandatory; you can ignore it. It is good practice, however, to test the message. In this example, you test WM_KEYDOWN to verify that this is a keystroke event. ?? ?? ??????? WM_SYSKEYDOWN, ????? ???????? ???????? (??????? ALT ?? CTRL) ????? ????????? ???????? ??? ???? ???? ???

??????? ???????? trap, ?? ??? ?? keyCode ???? ?? ??? ?? ????? ?? ??? ????????? ?? ???????????????? ????? ??? ????? ????????? ???? ?? ?? ???? UP ???, ???? ???, TAB, CTRL + M, ?? ALT + Z ??????????? ??? ???? ?? ???:
const int WM_KEYDOWN = 0x100;
const int WM_SYSKEYDOWN = 0x104;
   
if ((msg.Msg == WM_KEYDOWN) || (msg.Msg == WM_SYSKEYDOWN))
{
   switch(keyData)
      {
         case Keys.Down:
            Console.WriteLine("Down Arrow Captured");
            break;
      
         case Keys.Up:
            Console.WriteLine("Up Arrow Captured");
            break;
 
         case Keys.Tab:
            Console.WriteLine("Tab Key Captured");
            break;
 
         case Keys.Control | Keys.M:
            Console.WriteLine("<CTRL> + m Captured");
            break;
 
         case Keys.Alt | Keys.Z:
            Console.WriteLine("<ALT> + z Captured");
            break;
      }
}
				

?? ?????? ?????

????? ?????? ?? ??? ??????????? trap ???? ?????? ??DataGrid???????? ???
  1. ??? ?? Windows ???????? ????????? ????????? ??? ??? ????? C#.
  2. ???? ?? ??? ??? ?????UserControl1, ?? ???? ??? ?? ??? ?? ??? ?????MyDataGrid.
  3. ???????? ????????? ?? ??? ??? ?? ?????, ?? ??? ??? ?? ????? ?????? ?? ?????
    public class MyDataGrid : System.Windows.Forms.UserControl
    					
    ?????:
    public class MyDataGrid : System.Windows.Forms.DataGrid
    					Note In Visual Studio 2005 or Visual Studio 2008, change the following line of code: 
    public partial class MyDataGrid : System.Windows.Forms.DataGrid 
  4. ???? ?? ??? ????? ???? ??????MyDataGrid????:
    protected override bool ProcessCmdKey(ref Message msg, Keys keyData)	
    {
       const int WM_KEYDOWN = 0x100;
       const int WM_SYSKEYDOWN = 0x104;
    
       if ((msg.Msg == WM_KEYDOWN) || (msg.Msg == WM_SYSKEYDOWN))
       {
          switch(keyData)
          {
             case Keys.Down:
                this.Parent.Text="Down Arrow Captured";
                break;
          
             case Keys.Up:
                this.Parent.Text="Up Arrow Captured";
                break;
     
             case Keys.Tab:
                this.Parent.Text="Tab Key Captured";
                break;
     
             case Keys.Control | Keys.M:
                this.Parent.Text="<CTRL> + M Captured";
                break;
     
             case Keys.Alt | Keys.Z:
                this.Parent.Text="<ALT> + Z Captured";
                break;
          }				
       }
    
       return base.ProcessCmdKey(ref msg,keyData);
    }
    					
  5. ????????? ??????
  6. ??? ?? Windows ????????? ????????? ??? ??? ????? C#. ???????? ??? ??, Form1 ??? ???? ??????? ?? ???? ???
  7. ????? ????????????? ??,????? ????? ???????? ????

    ???:Visual Studio 2005 ?? Visual Studio 2008, ??? ????? ????????? ????? ???? ?????.
  8. ????? ????.NET ????? ?????? ?? ????? ????..
  9. ????? ????,???????, ????????/DLL ?? ??? ????? ??? ??, ?? ???? ???OK.
  10. ????????MyDataGrid?? ????? ????? ??? ????? ???? ??? Form1 ?? ?? ????????:: ?? ????? ?? ???? ??? ??? ??? ????? ??? ????????? ???? ?? ??? ????? ?? ??? ????? ???? ?? ????? ?? ????

  11. ??????? ?? ??? ????? ??? ????? ??? ??????? ?? ??? ?? ???? ?? ???? ?? ??????? ???? ?? ???????? ???? ?? ??? ?? ???? ????
    // This structure is only used in providing sample data for the grid.
    public struct gridData
    {
       private string make;
       private int year;
    
       public gridData(string n,int y)
       {
          make=n;
          year=y;
       }
    
       public string Make
       {
          get{return make;}
          set{make = value;}
       }
    
       public int Year
       {
          get{return year;}
          set{year=value;}
       }
    }
    					
  12. ??????? ???? ?? ???, "Windows ??????? ??????? ??? ????? ???? ???" ??? ?? ????? ????? ????? ??? ??????:
    protected gridData[] dataArray=new gridData[5];
    					
  13. ????? ??? ????????? ????Form1 ?????:
    // Create some sample data.
    dataArray[0]=new gridData("ford",1999);
    dataArray[1]=new gridData("chevrolet",1999);
    dataArray[2]=new gridData("plymouth",1988);
    dataArray[3]=new gridData("honda",1999);
    dataArray[4]=new gridData("fiat",1987);
    
    // Assign the data to the grid.
    myDataGrid1.DataSource=dataArray;
    					
  14. ????? ?????, ?? ??????? ??????????? trapped (UP ???, ???? ???, TAB, CTRL + M ?? ALT + Z) ?? ??? ??? ?? ???? ?? ?????? ????? ?????? ??????? ?? ???-?? ????????? ????? ?????? ?? ??? ?????? ?? ???

???? ???? ???? ??:
  • Microsoft Visual C# .NET 2002 Standard Edition
  • Microsoft Visual C# 2005 Express Edition
  • Microsoft Visual C# 2008 Express Edition
??????: 
kbhowtomaster kbmt KB320584 KbMthi
???? ?????? ???????????? ?????? ????????
??????????: ?? ???? ?? ???? ??????? ?? ????? ?? Microsoft ????-?????? ?????????? ?????? ?????? ???? ??? ??. Microsoft ???? ??? ????-???????? ?? ????-???????? ????? ?????? ?? ???? ???????? ???? ?? ???? ????? ????? ??? ?? ??? ?????? ?? ???? ???? ???? ??? ????? ??. ???????, ????-???????? ???? ????? ???? ???? ???? ???. ?????, ????????, ?????-???? ?? ??????? ?? ???????? ?? ???? ???, ???? ?? ??? ?????? ???? ???? ??? ????? ??? ?? ???? ??. Microsoft ??????? ??? ???? ?? ?????? ?? ??????????, ????????? ?? ??? ?????? ?? ???? ????? ?? ???? ???????? ?? ??? ???? ????? ?? ??? ????????? ???? ??. Microsoft ????-?????? ?????????? ?? ????? ?????? ?? ?? ??? ??.
?????????? ?? ??????? ????????? ??????? ??:320584  (http://support.microsoft.com/kb/320584/en-us/ )