- PR -

ドラッグ中に半透明のイメージを表示

1
投稿者投稿内容
Sak-Tak
会議室デビュー日: 2007/06/07
投稿数: 13
投稿日時: 2007-06-07 14:19
はじめまして。C#初心者の小林といいます。
pictureBox1からpictureBox2へイメージをドラッグドロップするプログラムを作成しておりますが、ドラッグ中にエクスプローラのように半透明のイメージを表示させたいと考えております。ドラッグドロップはできるようになったのですが、それから先が全く分からない状態です。よろしくご教授ください。
[C#2005、WinXp]

private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
{
PictureBox pbx = (PictureBox)sender;
Bitmap bitmap = (Bitmap)pictureBox1.Image;
DataObject dataObj =
new DataObject(DataFormats.Bitmap, bitmap);

DragDropEffects dde =
pbx.DoDragDrop(dataObj, DragDropEffects.All);

}
private void pictureBox2_DragDrop(object sender, DragEventArgs e)
{
pictureBox2.Image = (Bitmap)e.Data.GetData(DataFormats.Bitmap, false);
}

private void pictureBox2_DragEnter(object sender, DragEventArgs e)
{
if (e.Data.GetDataPresent(DataFormats.Bitmap))
e.Effect = DragDropEffects.Copy;
else e.Effect = DragDropEffects.None;
}

private void Form1_Load(object sender, EventArgs e)
{
pictureBox2.AllowDrop = true;
}
ガルマ・ザビ
ベテラン
会議室デビュー日: 2007/06/07
投稿数: 55
お住まい・勤務地: ジオン公国
投稿日時: 2007-06-07 14:28
先ほど別の質問をしておいて、ここで返信させていただくのもあれですが、
私もD&Dで悩んでいたので、ちょうど目に飛び込んできました。

以下のサイトが参考になると思います。
http://ant0x.udap.jp/tips/tips_DragGhostImage.htm

APIを利用する必要があるので、少し面倒くさいかもしれません。
私が作成しているプログラムでも実装できたので、いけると思います。
Sak-Tak
会議室デビュー日: 2007/06/07
投稿数: 13
投稿日時: 2007-06-07 16:18
ガルマさん。ありがとうございます。
早速チャレンジしてみたのですが、コンパイル時にDragImageでエラーになってしまいます。DragImageというクラスは単純にWin32 APIを宣言したクラスなので・・・と書かれているのでWin32 APIの呼び出しがうまくいってないのでしょうか。
うーん、難しい。でもがんばります。

すいません。なんとかコンパイル通りました。
しかし・・・



[ メッセージ編集済み 編集者: Sak-Tak 編集日時 2007-06-07 17:08 ]
ガルマ・ザビ
ベテラン
会議室デビュー日: 2007/06/07
投稿数: 55
お住まい・勤務地: ジオン公国
投稿日時: 2007-06-07 16:59
>コンパイル時にDragImageでエラーになってしまいます。
>DragImageというクラスは単純>にWin32 APIを宣言したクラスなので・・・
>と書かれているのでWin32 APIの呼び出しが>うまくいってないのでしょうか。

「DragImage」は、先に紹介したサイトの方が勝手につけたクラス名です。
つまり、DragImageクラスの中にAPIを呼び出せるような記述がされていると言うわけです。

サイト内にもリンクがありますが、以下はサンプルソースです。
こちらをご覧になれば理解できると思います。
(VS2003で作成されているものなので、起動時に変換が必要です。)

http://members.at.infoseek.co.jp/ant_009/CompressedFiles/Tips/DragGhostImageSample.zip
Sak-Tak
会議室デビュー日: 2007/06/07
投稿数: 13
投稿日時: 2007-06-07 17:25
コンパイルは何とか通って、実行すると予想通り思った動きにはなりません。
当たり前ですよね。そんなに甘くない。
とりあえずがんばってるのですが、ここから先が・・・
どちらにせよWIN32APIは必需品ってことですよね。

引き続きがんばります。


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace ドラッグドロップ
{
public partial class Form1 : System.Windows.Forms.Form
{
public Form1()
{
InitializeComponent();
}

// MouseDown時の座標保存変数
private Point mouseDownPoint = Point.Empty;

private void Form1_Load(object sender, EventArgs e)
{
pictureBox2.AllowDrop = true;
}

#region ドラッグ元の処理
private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
PictureBox pbx = (PictureBox)sender;
Bitmap bitmap = (Bitmap)pictureBox1.Image;
DataObject dataObj =
new DataObject(DataFormats.Bitmap, bitmap);

DragDropEffects dde =
pbx.DoDragDrop(dataObj, DragDropEffects.All);

mouseDownPoint = new Point(e.X, e.Y);
}
else
{
mouseDownPoint = Point.Empty;
}
}

private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
{
if (mouseDownPoint != Point.Empty)
{
Rectangle dragRegion = new Rectangle(
mouseDownPoint.X - SystemInformation.DragSize.Width / 2,
mouseDownPoint.Y - SystemInformation.DragSize.Height / 2,
SystemInformation.DragSize.Width,
SystemInformation.DragSize.Height);
if (!dragRegion.Contains(e.X, e.Y))
{
// Imageの初期化
imageList1.Images.Clear();
Rectangle itemRect = pictureBox1.ClientRectangle;
imageList1.ImageSize = new Size(itemRect.Width, itemRect.Height);

// 半透明イメージの元画像を作成、ImageListに追加
Bitmap bmp = new Bitmap(itemRect.Width, itemRect.Height);
Graphics g = Graphics.FromImage(bmp);
//g.DrawString(itemText, lst.Font, new SolidBrush(lst.ForeColor), 0, 0);
imageList1.Images.Add(bmp);

// ImageList_BeginDragにはドラッグする
// イメージの中における相対座標を指定する
if (DragImage.ImageList_BeginDrag(imageList1.Handle, 0,e.X - itemRect.Left,e.Y - itemRect.Top))
{
pictureBox1.DoDragDrop(bmp, DragDropEffects.Copy);
DragImage.ImageList_EndDrag();
}
mouseDownPoint = Point.Empty;
}
}
}

private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
{
mouseDownPoint = Point.Empty;
}

private void pictureBox1_DragEnter(object sender, DragEventArgs e)
{
// ImageList_DragEnterにはクライアント領域における相対座標ではなく
// タイトルバーなどの非クライアント領域を含むWindowにおける相対座標を指定する
Point p = this.PointToClient(Cursor.Position);
int x = Cursor.Position.X - this.Left;
int y = Cursor.Position.Y - this.Top;
// ドラッグ中は半透明イメージを表示し続けたいのでImageList_DragEnterには
// ListBoxのHandleを渡すのでなく、FormのHandleを渡す
DragImage.ImageList_DragEnter(this.Handle, x, y);
}
#endregion
#region ドラッグ操作中の処理
private void pictureBox1_GiveFeedback(object sender, GiveFeedbackEventArgs e)
{
e.UseDefaultCursors = false;
}

private void pictureBox1_QueryContinueDrag(object sender, QueryContinueDragEventArgs e)
{
// ImageList_DragEnter同様Windowにおける相対座標を指定する
int x = Cursor.Position.X - this.Left;
int y = Cursor.Position.Y - this.Top;
DragImage.ImageList_DragMove(x, y);
}
#endregion

#region ドラッグ先の処理
private void pictureBox2_DragDrop(object sender, DragEventArgs e)
{
DragImage.ImageList_DragLeave(this.Handle);
pictureBox2.Image = (Bitmap)e.Data.GetData(DataFormats.Bitmap, false);
}

private void pictureBox2_DragEnter(object sender, DragEventArgs e)
{
if (e.Data.GetDataPresent(DataFormats.Bitmap))
e.Effect = DragDropEffects.Copy;
else e.Effect = DragDropEffects.None;
}
#endregion

}
}
1

スキルアップ/キャリアアップ(JOB@IT)