何となく Blog by Jitta
Microsoft .NET 考

目次

Blog 利用状況
  • 投稿数 - 761
  • 記事 - 18
  • コメント - 36192
  • トラックバック - 222
ニュース
  • IE7以前では、表示がおかしい。div の解釈に問題があるようだ。
    IE8の場合は、「互換」表示を OFF にしてください。
  • 検索エンジンで来られた方へ:
    お望みの情報は見つかりましたか? よろしければ、コメント欄にどのような情報を探していたのか、ご記入ください。
It's ME!
  • はなおか じった
  • 世界遺産の近くに住んでます。
  • Microsoft MVP for Visual Developer ASP/ASP.NET 10, 2004 - 9, 2011
広告

記事カテゴリ

書庫

日記カテゴリ

ギャラリ

その他

わんくま同盟

同郷

 

次のアプリケーションの提案がてら、興味本位で。

ドラッグ アンド ドロップについては、Control.DoDragDrop の説明などに書いてあるので、詳しいことは割愛。私が参考にしたのは、Drag&Drop(ドラッグ&ドロップ)を行う(DOBON.NET)

MSDN ライブラリや DOBON.NET には、ListBox に表示された項目の D&D について、書かれています。ここで行いたいのは、コントロールです。なので、違いを中心に書き留めます。


まず、準備。


// マウス ボタンを押し込んだところ。
private Point mouseDownPoint = Point.Empty;
// ドラッグするコントロールの親コントロール座標での、コントロールの配置座標とマウス座標の差。
private Point pickedPoint = Point.Empty;
// ドラッグするコントロールの大きさ。
private Size controlSize = Size.Empty;

pickedPoint と、controlSize が、追加したもの。コントロールは、描画されるオブジェクトの左上の座標を指定します。しかし、押し込むことができるのは、コントロール全体です。この、左上座標とマウス ボタンが押し込まれた座標との差を、保存します。controlSize は後述。

次、MouseDown イベント ハンドラ。


private void button1_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e) {
    Button btn = sender as Button;
    if (btn == null) { return; }
    if (e.Button == MouseButtons.Left) {
        mouseDownPoint = new Point(e.X, e.Y);
        Point sp = this.PointToClient(btn.PointToScreen(new Point(e.X, e.Y)));
        pickedPoint = new Point(btn.Left - sp.X, btn.Top - sp.Y);
    } else {
        mouseDownPoint = Point.Empty;
        pickedPoint = Point.Empty;
        controlSize = Size.Empty;
    }
}

とりあえず、ここでは Button を動かすことにしています。まぁ、この辺は適宜アレンジが必要ということで。

mouseDownPoint に、マウス ボタンを押し込んだ座標を格納するところは同じですが、その後に、ボタン上のクライアント座標からスクリーン座標に、そこからフォーム(ドラッグしたいコントロールの親コントロール)のクライアント座標に変換したものを、sp に入れています。その座標を、コントロールの左上座標からの差を計算し、pickedPoint に格納しています。あれ?e.X, e.Y をそのまま持っていて、後で補正すればいいのでは?

次、MouseMove イベント ハンドラ。


private void button1_MouseMove(object sender, System.Windows.Forms.MouseEventArgs e) {
    if (mouseDownPoint == Point.Empty) { return; }
    Rectangle moveRect = new Rectangle(
        mouseDownPoint.X - SystemInformation.DragSize.Width / 2,
        mouseDownPoint.Y - SystemInformation.DragSize.Height / 2,
        SystemInformation.DragSize.Width,
        SystemInformation.DragSize.Height);
    if (moveRect.Contains(e.X, e.Y)) { return; }
    mouseDownPoint = Point.Empty;
    Button btn = sender as Button;
    controlSize = new Size(btn.Width, btn.Height);
    DragDropEffects dde = btn.DoDragDrop(btn, DragDropEffects.Move);
    if (beforeRect != Rectangle.Empty) {
        // フォームの外にドロップされたときも、トラッカーを消す
        ControlPaint.DrawReversibleFrame(beforeRect, Color.Black, FrameStyle.Dashed);
        beforeRect = Rectangle.Empty;
    }
    btn.Refresh();
}

同じく前半の説明は省略。btn 以降。

controlSize に、ドラッグ対象のコントロールの Width, Height を保存します。これを、トラッカーに使います。「トラッカー」は、ここでは「ドラッグ中に移動対象の大きさを示す枠」の意味で使います。「トラックレクト」とか、「ラバーバンド」とか、そんな言葉が使われるかもしれません。

ここで、DoDragDrop メソッドにはまりました。これ、ドラッグ開始をマークして、すぐに抜けてくるんだと思っていました。しかし、ドラッグ中はこの中から他のイベント ハンドラが呼び出されています。そのため、このメソッドから抜けてきた後に、トラッカーを消す処理を入れています。また、対象のコントロール上にトラッカーの跡が残るので、リフレッシュさせています。

次、あまり説明するもののない、MouseUp, DragOver, DragEnter イベント ハンドラ。


private void button1_MouseUp(object sender, System.Windows.Forms.MouseEventArgs e) {
    mouseDownPoint = Point.Empty;
    pickedPoint = Point.Empty;
    controlSize = Size.Empty;
}

private void Form1_DragEnter(object sender, System.Windows.Forms.DragEventArgs e) {
    if (e.Data.GetDataPresent(typeof(Button))) {
        e.Effect = DragDropEffects.Move;
    } else {
        e.Effect = DragDropEffects.None;
    }
}

private void Form1_DragOver(object sender, System.Windows.Forms.DragEventArgs e) {
    if (e.Data.GetDataPresent(typeof(Button))) {
        e.Effect = DragDropEffects.Move;
    } else {
        e.Effect = DragDropEffects.None;
    }
}

見てわかるように、MouseUp はドラッグ対象コントロールのイベントですが、DragOver, DragEnter は、ドロップが実行されるコントロールのイベントです。

次、ドロップが実行される、DragDrop イベント ハンドラ。


private void Form1_DragDrop(object sender, System.Windows.Forms.DragEventArgs e) {
    if (e.Data.GetDataPresent(typeof(Button))) {
        Button target = e.Data.GetData(typeof(Button)) as Button;
        if (target == null) {
            e.Effect = DragDropEffects.None;
            return;
        }
        ControlPaint.DrawReversibleFrame(beforeRect, Color.White, FrameStyle.Dashed);
        beforeRect = Rectangle.Empty;
        Point newPoint = this.PointToClient(new Point(e.X, e.Y));
        target.Left = newPoint.X + pickedPoint.X;
        target.Top = newPoint.Y + pickedPoint.Y;
        mouseDownPoint = Point.Empty;
        pickedPoint = Point.Empty;
        controlSize = Size.Empty;
    } else {
        e.Effect = DragDropEffects.None;
    }
}

ListBox の項目を移動させる時、項目を引いて足したのと同じようなことを行います。ここでは、コントロールの新しい場所を指定しています。また、トラッカーの表示などに使った変数をクリアします。

最後、トラッカーを表示するための QueryContinueDrag イベント ハンドラ。


private Rectangle beforeRect = Rectangle.Empty;
private void button1_QueryContinueDrag(object sender, System.Windows.Forms.QueryContinueDragEventArgs e) {
    if ((e.KeyState & 2) == 2) {
        e.Action = DragAction.Cancel;
    }
    // トラッカー表示
    if (controlSize == Size.Empty) { return; }
    Point loc = new Point(Control.MousePosition.X + pickedPoint.X, Control.MousePosition.Y + pickedPoint.Y);
    Rectangle rect = new Rectangle(loc, controlSize);
    if (beforeRect.Equals(rect)) { return; }
    if (beforeRect != Rectangle.Empty) {
        ControlPaint.DrawReversibleFrame(beforeRect, Color.Black, FrameStyle.Dashed);
    }
    ControlPaint.DrawReversibleFrame(rect, Color.White, FrameStyle.Dashed);
    beforeRect = new Rectangle(rect.Location, rect.Size);
}

rect には、今ドラッグを終了するとどこにコントロールが置かれるかを示す範囲情報が入ります。beforeRect は、描いたトラッカーを消すために、前回表示した範囲情報を入れます。loc は、マウス カーソルの現在位置から pickedPoint の補正を加えた、コントロールの Left, Top 情報が入ります。Rectangle は値型なので、メンバを変更することができません。そのため、毎回 new します。


わかっている問題

  • 時々、トラッカーが残る。

  • ドラッグ中、コントロール上にトラッカーが残る。

投稿日時 : 2008年2月20日 22:26
コメント
  • # ルイヴィトン時計
    rectehv@softbank.ne.jp
    Posted @ 2017/07/16 6:33
    いつか、私はデウィット・ジェローム氏についてのより多くを書いて、すべてのものは機械の両方の祖とおそらくナポレオン・ボナパルトのと同様に、デウィット種類さんの才能は誰の礼儀正しくて内気な恋人、ニューヨークスタイルを意図した動作で話すようなエンジンと旧世界の貴族社会の期待に着陸した。
  • # ブルガリ コピー 時計
    cxyiiowxi@docomo.ne.jp
    Posted @ 2017/10/29 9:04
    シャネル 財布 激安
    2017年人気最新品、新素材入荷
    2017年最新の更新のルイヴィトンの包み、財布。
    2017年に最新の更新のグッチ(GUCCI)は包みで、財布。
    2017年に最新の更新のプラダ(PRADA)は包んで、。
    2017年最新の更新のローレックスの腕時計
    2017年最新の更新のエルメスの包み、財布。
    2017年最新の更新シャネル (chanel) 包み、財布。
    新旧(の程度)の取引先を歓迎して予約して買いにきます
    今回の私の店は特大な大安売りさを提供します
    歓迎に分からないで、問合わせの事.
    当店の信用の第1、商品の品質第一.
    各位の顧客に安心して送料無料!!
    ブルガリ コピー 時計 http://www.bagssjp.com
  • # ブランド コピー
    zkctvcvdvvf@ezwen.ne.jp
    Posted @ 2017/11/02 17:28
    此のヴィトンは美品で価格も安価で落札出来ました。やはり
    ヴィトンはいいですね。幾つ有っても飽きがきませんから
    不思議です。ブランドという魅力は怖いです!!
    ブランド コピー http://www.yamamo78.com/web/watch-ro001.htm
  • # We are a group of volunteers and opening a new scheme in our community. Your website offered us with valuable information to work on. You've done a formidable job and our whole community will be grateful to you.
    We are a group of volunteers and opening a new sch
    Posted @ 2021/08/28 16:29
    We are a group of volunteers and opening a new scheme in our community.

    Your website offered us with valuable information to
    work on. You've done a formidable job and our whole community will be grateful to you.
  • # Hi there colleagues, how is all, and what you wish for to say concerning this post, in my view its genuinely awesome for me.
    Hi there colleagues, how is all, and what you wish
    Posted @ 2021/09/02 9:58
    Hi there colleagues, how is all, and what you wish for
    to say concerning this post, in my view its genuinely awesome for me.
  • # Hi there colleagues, how is all, and what you wish for to say concerning this post, in my view its genuinely awesome for me.
    Hi there colleagues, how is all, and what you wish
    Posted @ 2021/09/02 9:59
    Hi there colleagues, how is all, and what you wish for
    to say concerning this post, in my view its genuinely awesome for me.
  • # Hi there colleagues, how is all, and what you wish for to say concerning this post, in my view its genuinely awesome for me.
    Hi there colleagues, how is all, and what you wish
    Posted @ 2021/09/02 10:00
    Hi there colleagues, how is all, and what you wish for
    to say concerning this post, in my view its genuinely awesome for me.
  • # Hi there colleagues, how is all, and what you wish for to say concerning this post, in my view its genuinely awesome for me.
    Hi there colleagues, how is all, and what you wish
    Posted @ 2021/09/02 10:01
    Hi there colleagues, how is all, and what you wish for
    to say concerning this post, in my view its genuinely awesome for me.
  • # Hello! Do you know if they make any plugins to assist with SEO? I'm trying to get my blog to rank for some targeted keywords but I'm not seeing very good success. If you know of any please share. Many thanks!
    Hello! Do you know if they make any plugins to ass
    Posted @ 2021/09/03 11:42
    Hello! Do you know if they make any plugins to assist
    with SEO? I'm trying to get my blog to rank for some targeted keywords but I'm not seeing very good success.
    If you know of any please share. Many thanks!
  • # I constantly spent my half an hour to read this weblog's articles daily along with a mug of coffee.
    I constantly spent my half an hour to read this we
    Posted @ 2021/09/04 18:09
    I constantly spent my half an hour to read this weblog's articles daily along with a mug of coffee.
  • # I constantly spent my half an hour to read this weblog's articles daily along with a mug of coffee.
    I constantly spent my half an hour to read this we
    Posted @ 2021/09/04 18:10
    I constantly spent my half an hour to read this weblog's articles daily along with a mug of coffee.
  • # I constantly spent my half an hour to read this weblog's articles daily along with a mug of coffee.
    I constantly spent my half an hour to read this we
    Posted @ 2021/09/04 18:11
    I constantly spent my half an hour to read this weblog's articles daily along with a mug of coffee.
  • # I constantly spent my half an hour to read this weblog's articles daily along with a mug of coffee.
    I constantly spent my half an hour to read this we
    Posted @ 2021/09/04 18:12
    I constantly spent my half an hour to read this weblog's articles daily along with a mug of coffee.
  • # of course like your website however you have to check the spelling on quite a few of your posts. Many of them are rife with spelling issues and I in finding it very bothersome to tell the truth nevertheless I will certainly come back again.
    of course like your website however you have to ch
    Posted @ 2021/09/06 7:49
    of course like your website however you have
    to check the spelling on quite a few of your posts. Many
    of them are rife with spelling issues and I in finding
    it very bothersome to tell the truth nevertheless I will certainly come back again.
  • # of course like your website however you have to check the spelling on quite a few of your posts. Many of them are rife with spelling issues and I in finding it very bothersome to tell the truth nevertheless I will certainly come back again.
    of course like your website however you have to ch
    Posted @ 2021/09/06 7:50
    of course like your website however you have
    to check the spelling on quite a few of your posts. Many
    of them are rife with spelling issues and I in finding
    it very bothersome to tell the truth nevertheless I will certainly come back again.
  • # of course like your website however you have to check the spelling on quite a few of your posts. Many of them are rife with spelling issues and I in finding it very bothersome to tell the truth nevertheless I will certainly come back again.
    of course like your website however you have to ch
    Posted @ 2021/09/06 7:51
    of course like your website however you have
    to check the spelling on quite a few of your posts. Many
    of them are rife with spelling issues and I in finding
    it very bothersome to tell the truth nevertheless I will certainly come back again.
  • # of course like your website however you have to check the spelling on quite a few of your posts. Many of them are rife with spelling issues and I in finding it very bothersome to tell the truth nevertheless I will certainly come back again.
    of course like your website however you have to ch
    Posted @ 2021/09/06 7:52
    of course like your website however you have
    to check the spelling on quite a few of your posts. Many
    of them are rife with spelling issues and I in finding
    it very bothersome to tell the truth nevertheless I will certainly come back again.
  • # Wonderful beat ! I would like to apprentice at the same time as you amend your web site, how could i subscribe for a weblog website? The account aided me a acceptable deal. I have been tiny bit acquainted of this your broadcast offered vivid transparent
    Wonderful beat ! I would like to apprentice at the
    Posted @ 2021/09/11 14:57
    Wonderful beat ! I would like to apprentice at the
    same time as you amend your web site, how could i subscribe for a weblog website?
    The account aided me a acceptable deal. I have been tiny bit acquainted
    of this your broadcast offered vivid transparent idea
    quest bars http://bitly.com/3jZgEA2 quest bars
  • # Wonderful beat ! I would like to apprentice at the same time as you amend your web site, how could i subscribe for a weblog website? The account aided me a acceptable deal. I have been tiny bit acquainted of this your broadcast offered vivid transparent
    Wonderful beat ! I would like to apprentice at the
    Posted @ 2021/09/11 14:58
    Wonderful beat ! I would like to apprentice at the
    same time as you amend your web site, how could i subscribe for a weblog website?
    The account aided me a acceptable deal. I have been tiny bit acquainted
    of this your broadcast offered vivid transparent idea
    quest bars http://bitly.com/3jZgEA2 quest bars
  • # Wonderful beat ! I would like to apprentice at the same time as you amend your web site, how could i subscribe for a weblog website? The account aided me a acceptable deal. I have been tiny bit acquainted of this your broadcast offered vivid transparent
    Wonderful beat ! I would like to apprentice at the
    Posted @ 2021/09/11 14:59
    Wonderful beat ! I would like to apprentice at the
    same time as you amend your web site, how could i subscribe for a weblog website?
    The account aided me a acceptable deal. I have been tiny bit acquainted
    of this your broadcast offered vivid transparent idea
    quest bars http://bitly.com/3jZgEA2 quest bars
  • # Wonderful beat ! I would like to apprentice at the same time as you amend your web site, how could i subscribe for a weblog website? The account aided me a acceptable deal. I have been tiny bit acquainted of this your broadcast offered vivid transparent
    Wonderful beat ! I would like to apprentice at the
    Posted @ 2021/09/11 15:00
    Wonderful beat ! I would like to apprentice at the
    same time as you amend your web site, how could i subscribe for a weblog website?
    The account aided me a acceptable deal. I have been tiny bit acquainted
    of this your broadcast offered vivid transparent idea
    quest bars http://bitly.com/3jZgEA2 quest bars
  • # I've learn several excellent stuff here. Certainly value bookmarking for revisiting. I surprise how so much attempt you put to create one of these magnificent informative web site. quest bars https://www.iherb.com/search?kw=quest%20bars quest bars
    I've learn several excellent stuff here. Certainly
    Posted @ 2021/09/12 19:51
    I've learn several excellent stuff here. Certainly value bookmarking for
    revisiting. I surprise how so much attempt you put
    to create one of these magnificent informative web site.
    quest bars https://www.iherb.com/search?kw=quest%20bars quest bars
  • # I've learn several excellent stuff here. Certainly value bookmarking for revisiting. I surprise how so much attempt you put to create one of these magnificent informative web site. quest bars https://www.iherb.com/search?kw=quest%20bars quest bars
    I've learn several excellent stuff here. Certainly
    Posted @ 2021/09/12 19:52
    I've learn several excellent stuff here. Certainly value bookmarking for
    revisiting. I surprise how so much attempt you put
    to create one of these magnificent informative web site.
    quest bars https://www.iherb.com/search?kw=quest%20bars quest bars
  • # I've learn several excellent stuff here. Certainly value bookmarking for revisiting. I surprise how so much attempt you put to create one of these magnificent informative web site. quest bars https://www.iherb.com/search?kw=quest%20bars quest bars
    I've learn several excellent stuff here. Certainly
    Posted @ 2021/09/12 19:53
    I've learn several excellent stuff here. Certainly value bookmarking for
    revisiting. I surprise how so much attempt you put
    to create one of these magnificent informative web site.
    quest bars https://www.iherb.com/search?kw=quest%20bars quest bars
  • # I've learn several excellent stuff here. Certainly value bookmarking for revisiting. I surprise how so much attempt you put to create one of these magnificent informative web site. quest bars https://www.iherb.com/search?kw=quest%20bars quest bars
    I've learn several excellent stuff here. Certainly
    Posted @ 2021/09/12 19:54
    I've learn several excellent stuff here. Certainly value bookmarking for
    revisiting. I surprise how so much attempt you put
    to create one of these magnificent informative web site.
    quest bars https://www.iherb.com/search?kw=quest%20bars quest bars
  • # I visited various web pages but the audio quality for audio songs present at this site is truly marvelous.
    I visited various web pages but the audio quality
    Posted @ 2021/10/25 21:19
    I visited various web pages but the audio quality for audio songs
    present at this site is truly marvelous.
  • # 誕生日プレゼント
    ifpmeuvzsj@live.jp
    Posted @ 2021/11/16 19:24
    ブランドスマホケース/カバー激安通販ショップ
    ご来店いただき誠にありがとうございます。
    当店では「信頼第一、サービス第一」をモットーに、お客様第一主義で営業しております。取扱商品としては、iPhoneスマホケース、iPadケース、SAMSUNG GALAXY スマホケース、バッテリー&充電器や、関係する物などです。皆様のニーズにお応えすべく各種製品を取り揃えております。
    ごゆっくりお買い物をお楽しみください。皆様のお求めになりたい商品がきっと見つかります。
    シャネルiphone6 plusケース積み木iphone6 iphone6 plusケース MCM iphone6カバー 手帳型シャネル革iphone6 保護ケース 4.7インチiPhone 6 Plus カバー5.5 インチ ブランド SAMSUNG GALAXY NOTE4ケースCHANEL SAMSUNG NOTE4カバーブランド iphone6ケース ルイヴィトンエルメス Hermes iphone6ケースGUCCI iphone6ケース
    休業日: 365天受付年中無休
  • # It's enormous that you are getting thoughts from this piece of writing as well as from our discussion made at this time.
    It's enormous that you are getting thoughts from
    Posted @ 2021/12/24 23:40
    It's enormous that you are getting thoughts from this piece
    of writing as well as from our discussion made at this time.
  • # It's enormous that you are getting thoughts from this piece of writing as well as from our discussion made at this time.
    It's enormous that you are getting thoughts from
    Posted @ 2021/12/24 23:41
    It's enormous that you are getting thoughts from this piece
    of writing as well as from our discussion made at this time.
  • # It's enormous that you are getting thoughts from this piece of writing as well as from our discussion made at this time.
    It's enormous that you are getting thoughts from
    Posted @ 2021/12/24 23:42
    It's enormous that you are getting thoughts from this piece
    of writing as well as from our discussion made at this time.
  • # It's enormous that you are getting thoughts from this piece of writing as well as from our discussion made at this time.
    It's enormous that you are getting thoughts from
    Posted @ 2021/12/24 23:43
    It's enormous that you are getting thoughts from this piece
    of writing as well as from our discussion made at this time.
  • # ブランド偽物
    cltbodskny@ocn.ne.jp
    Posted @ 2023/03/22 3:34
    革新的で優雅な超歓迎店舗超特価!
    『激安価格』本物保証!
    【正規品】激安専門店.
    エルメス新作超特価SALE!
    市場!
    【最安値開催中】全品無料!
    セット組み合わせ!
    100%新品!
    割引【超特価】信用第一!
    【大特価!】100%本物保証!
    2023【品質保証書】全国送料無料!
    限定SALE手ごろなお値段!
    【限定特価】史上最も激安い!
    専門店【新入荷】
    激安通販,配送のアイテムは返品送料無料
    ブランド偽物 https://www.gmt78.com/product/detail/4120.htm
タイトル
名前
Url
コメント