人生のプチ美学を教えてください!!

C#でのドラッグ&ドロップの質問です
ピクチャーボックスがあります。
そのピクチャーボックスをD&Dで動かしたいです。

例をあげると、
ペイントソフトの範囲選択した後にD&Dすると移動できるような感じです。

掴んで動かせる感じですね。


そんなことが書いてあるサイトやソースを教えてくだされば有り難いです。

A 回答 (4件)

こんにちは。



ヒントだけ乗せておきます。
MouseDown/MouseMove/MouseUp
これを使えば実現できます。
この単語から調べると色々参考サイトが出ると思います。

この回答への補足

Mouseイベントにはたどり着いたのですが
MouseDownのときにクリックしたカーソルの位置の座標は
どうすれば、取得できるのでしょうか…。

Cursorを使えばいいということもわかりました
しかし、これだけでは不十分な気がします…。

補足日時:2010/04/28 18:48
    • good
    • 0

不十分?どこがですか?



もうすでに回答があるようなので大丈夫だと思いますが…
もう少しプロパティとかを見た方がいいです。
この手のプログラムのサンプルソースなんていくらでもあります。
最近は丸投げの人が多いですがそういう人は成長しません。
探す事も一つの力です。
頑張りましょう。
    • good
    • 0

Point DragStart;



private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
{
 if (e.Button == MouseButtons.Left)
  DragStart = e.Location;
}

private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
{
 if (e.Button == MouseButtons.Left)
 {
  Point mouse_pos = pictureBox1.PointToScreen(e.Location);
  mouse_pos = this.PointToClient(mouse_pos);

  pictureBox1.Left = mouse_pos.X - DragStart.X;
  pictureBox1.Top = mouse_pos.Y - DragStart.Y;
 }
}

とりあえずこんな感じで出来るみたいです。
    • good
    • 0

参考URLは画面自体がドラッグできますが、同様な手続きをピクチャボックスを継承したクラスを作成し、張り替えると楽チンです。



大きく手順は三つです。
フォームから飛び出させる方法も載せておりますが、不要であれば消してください。
(チラツキは無視w)

----------------
[mypic.cs]の新規作成
----------------
//http://www.ipentec.com/document/document.aspx?pa …
using System;
using System.Drawing;
using System.ComponentModel;
using System.Windows.Forms;
using System.Runtime.InteropServices;

public class mypic : PictureBox
{
private const int WM_SYSCOMMAND = 0x112;
private const int SC_MOVE = 0xF010;
[DllImport("User32.dll")]
private static extern bool SetCapture(IntPtr hWnd);
[DllImport("User32.dll")]
private static extern bool ReleaseCapture();
[DllImport("User32.dll")]
private static extern int SendMessage(IntPtr hWnd, int Msg, int wParam, int lParam);
[DllImport("User32.dll")]
private static extern int SetParent(IntPtr hWndChild, IntPtr hWndNewParent);
[DllImport("User32.dll")]
private static extern IntPtr GetParent(IntPtr hWnd);
[DllImport("User32.dll")]
private static extern IntPtr GetDesktopWindow();

[DllImport("User32.dll")]
private static extern int ClientToScreen(IntPtr hWmd, ref Point lpPoint);

protected override void OnMouseDown(MouseEventArgs e)
{
base.OnMouseDown(e);

#region フォームを飛び出させたいとき
{
IntPtr hDeskTop = GetDesktopWindow();
IntPtr hParent = GetParent(this.Handle);

if (hDeskTop != hParent)
{
Point pnt = this.Location;
int hh = Marshal.SizeOf(pnt);

hh = Marshal.SizeOf(this.Location.GetType());

ClientToScreen(hParent, ref pnt);

SetParent(this.Handle, GetDesktopWindow());
this.Location = pnt;
}
}
#endregion

SetCapture(this.Handle);
ReleaseCapture();
SendMessage(this.Handle, WM_SYSCOMMAND, SC_MOVE | 2, 0);
}
}


----------------
フォーム1に「pictureBox1」を作成
----------------

----------------
[Form1.Designer.cs]の一部を修正
----------------
this.pictureBox1 = new System.Windows.Forms.PictureBox();
を以下に↓
this.pictureBox1 = new mypic();

private System.Windows.Forms.PictureBox pictureBox1;
を以下に↓
private mypic pictureBox1;

参考URL:http://www.ipentec.com/document/document.aspx?pa …
    • good
    • 0

お探しのQ&Aが見つからない時は、教えて!gooで質問しましょう!