2013-09-27 4 views
0

C#의 Form (System.Windows.Forms)에 toolStrip1을 배치하고 다섯 개의 ToolStrip 단추를 추가했습니다. 이제 사용자가이 버튼을 toolStrip1 내의 다른 위치로 드래그하여 재주문하게하는 방법에 대해 궁금합니다. toolStrip1.AllowItemReorder를 true로 설정합니다.AllowDrop을 false로 설정합니다.은 Microsoft에서 제공하는 문서에서 확인할 수 있습니다.C# VS 2008에서 Alt 키를 누르지 않고 동일한 도구 막대 내에서 도구 순서 바꾸기

이제 toolStrip1에서 항목 재정렬을 자동으로 처리 할 수 ​​있어야합니다. 그러나 그것은 작동하지 않습니다. - ALT-Key를 누른 상태에서만 toolStrip1이 사용자의 재정렬 시도에 반응합니다. 항목을 재정렬하는 동안 Alt 키를 누르지 않도록하려면 DragEvent, DragEnter, DragLeave을 직접 처리해야합니까?

그렇다면 Internet Explorer 즐겨 찾기와 같이 아무 키도 누르지 않고 toolStrip1 내의 다른 위치로 한 항목을 드래그하려면이 이벤트가 toolStripButtons가있는 toolStrip에서 어떻게 보이는지 예를 들어주세요. 않습니다). 나는이 문제에 경험이 없다.

답변

2

글쎄, 당신은 조금 해킹이 솔루션을 사용해야 할 수도 있습니다. 전체 아이디어는 Alt 키를 키로 길게 누릅니다. 나는 MouseDown 이벤트 (심지어 PreFilterMessage handler에 있음)로 시도했지만 실패했습니다. 유일한 이벤트는 Alt 키를 발사 할 때 MouseEnter 키를 길게 누르면 적합합니다. ToolStripItems에 대해 MouseEnter 이벤트 핸들러를 등록해야합니다. 마우스가 이러한 항목 중 하나를 벗어나면 MouseLeave 이벤트 핸들러에서 Alt 키를 해제해야합니다. Alt 키를 놓은 후에 양식을 활성화하려면 ESC 키를 보내야합니다 (그렇지 않으면 제어 버튼 (Minimize, Maximize, Close 포함)에서도 모든 호버 (hover) 효과가 무시되는 것처럼 보임). 다음은 작동하는 코드입니다.

public partial class Form1 : Form { 
    [DllImport("user32.dll", SetLastError = true)] 
    static extern void keybd_event(byte bVk, byte bScan, int dwFlags, int dwExtraInfo); 
    public Form1(){ 
    InitializeComponent(); 
    //Register event handlers for all the toolstripitems initially 
    foreach (ToolStripItem item in toolStrip1.Items){ 
     item.MouseEnter += itemsMouseEnter; 
     item.MouseLeave += itemsMouseLeave; 
    } 
    //We have to do this if we add/remove some toolstripitem at runtime 
    //Otherwise we don't need the following code 
    toolStrip1.ItemAdded += (s,e) => { 
     item.MouseEnter += itemsMouseEnter; 
     item.MouseLeave += itemsMouseLeave; 
    }; 
    toolStrip1.ItemRemoved += (s,e) => { 
     item.MouseEnter -= itemsMouseEnter; 
     item.MouseLeave -= itemsMouseLeave; 
    }; 
    } 
    bool pressedAlt; 
    private void itemsMouseEnter(object sender, EventArgs e){ 
     if (!pressedAlt) { 
      //Hold the Alt key 
      keybd_event(0x12, 0, 0, 0);//VK_ALT = 0x12 
      pressedAlt = true; 
     } 
    } 
    private void itemsMouseLeave(object sender, EventArgs e){ 
     if (pressedAlt){ 
      //Release the Alt key 
      keybd_event(0x12, 0, 2, 0);//flags = 2 -> Release the key 
      pressedAlt = false; 
      SendKeys.Send("ESC");//Do this to make the GUI active again 
     }    
    } 
}