2011-12-06 3 views
3

フォームに2つのラベルがあります。私は1つのラベルを他のラベルにドラッグできるようにしたいと思うし、マウスの左ボタンがまだダウンしている間にスペースキーを押して "foo"と "bar"の間でターゲットラベルのテキストを切り替えることができるようにしたい。ドラッグ中のキーボードイベント

マウスの左ボタンが放されていない間、すべての入力イベントのように見えます。

何か不足していますか?どんなサンプル?

答えて

2

GiveFeedbackイベントを確認してください。キーが押されているかどうかチェックすることができます。

EDIT:

void panel1_QueryContinueDrag(object sender, QueryContinueDragEventArgs e) 
{ 
    if (Keyboard.IsKeyDown(Key.Space)) 
    { 
     if (label1.Text == "foo") label1.Text = "bar"; else label1.Text = "foo"; 
    } 
} 

とPresentaionCoreとへの参照を追加します。WindowBaseは(あなたが見つけることで:C:\プログラムファイル(x86の)\ ReferenceAssembliesマイクロソフト\ Frameworkの\ v3.0の\ \。 )

これで少し遊ばなければなりません。

+0

これは近いです。 QueryContinueDragと連動してトリックを行うことはできますが、スペースキーの状態はまだ得られません。 – Ramunas

+0

@Ramunas多分(!)これらが役立つかもしれません:http://stackoverflow.com/questions/1100285/how-to-detect-the-currently -pressed-keyと:http://stackoverflow.com/questions/1752494/detect-if-any-key-is-pressed-in-c-sharp-not-ab-but-any – ispiro

+0

@Ramunas私の編集を参照してください。 – ispiro

-1

//はこのような何かを試してみて、ドラッグされた要素は、元のフォームではなく、D & D・メカニズムを使用してのマウスイベントの解釈を検討残したことがない場合は、あなたの作業例

public partial class Form1 : Form 
    { 
     public Form1() 
     { 
     InitializeComponent(); 
     label1.MouseDown += new MouseEventHandler(label1_MouseDown);   
     textBox1.AllowDrop = true; 
     textBox1.DragEnter += new DragEventHandler(textBox1_DragEnter); 
     textBox1.DragDrop += new DragEventHandler(textBox1_DragDrop); 
     } 

     void label1_MouseDown(object sender, MouseEventArgs e) 
     { 
     DoDragDrop(label1.Text, DragDropEffects.Copy); 
     } 

     void textBox1_DragEnter(object sender, DragEventArgs e) 
     { 
     if (e.Data.GetDataPresent(DataFormats.Text)) 
     e.Effect = DragDropEffects.Copy; 
     } 

     void textBox1_DragDrop(object sender, DragEventArgs e) 
     { 
     textBox1.Text = (string)e.Data.GetData(DataFormats.Text); 
     } 
    } 
+4

これはどのようにして質問に答えますか? –

+1

彼はラベルのドラッグアンドドロップの例を望んでいました – MethodMan

+0

私はOPがキーを押す部分に固執していると思います。 – LarsTech

0

に合わせて必要な場所、物事を変更します。それほど良いものではありませんが、ドラッグ中に他のメッセージを解釈できるようになります。

public class MyForm : Form 
{ 
    private Label label; 

    public MyForm() 
    { 
     KeyPress += new KeyPressEventHandler(Form_KeyPress); 
     label = new Label(); 
     label.Text = "foo"; 
     label.MouseMove += new MouseEventHandler(label_MouseMove); 
     Controls.Add(label); 
    } 

    private void label_MouseMove(object sender, MouseEventArgs e) 
    { 
     if (MouseButtons == MouseButtons.Left) 
     { 
      Point loc = label.Location; 
      loc.Offset(e.X, e.Y); 
      label.Location = loc; 
     } 
    } 

    private void Form_KeyPress(object sender, KeyPressEventArgs e) 
    { 
     if (e.KeyChar == ' ') 
     { 
      if (label.Text == "foo") 
       label.Text = "bar"; 
      else 
       label.Text = "foo"; 
     } 
    } 
} 
関連する問題