// 拖动图片到窗体中显示using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Windows.Forms;class ImageDrop: Form
{
     bool bIsTarget;
     protected string strProgName;
     protected string strFileName;
     protected Image  image;     public static void Main()
     {
Application.Run(new ImageDrop());
     }
     public ImageDrop()
     {
          Text = strProgName = "Image Drop";          ResizeRedraw = true;
          AllowDrop = true;
     }
     protected override void OnDragOver(DragEventArgs dea)
     {
          if (dea.Data.GetDataPresent(DataFormats.FileDrop) ||
              dea.Data.GetDataPresent(typeof(Metafile))     ||
              dea.Data.GetDataPresent(typeof(Bitmap)))
          {
               if ((dea.AllowedEffect & DragDropEffects.Move) != 0)
                    dea.Effect = DragDropEffects.Move;               if (((dea.AllowedEffect & DragDropEffects.Copy) != 0) &&
                   ((dea.KeyState & 0x08) != 0))    // Ctrl key
                    dea.Effect = DragDropEffects.Copy;
          }
     }
     protected override void OnDragDrop(DragEventArgs dea)
     {
          if (dea.Data.GetDataPresent(DataFormats.FileDrop))
          {
               string[] astr = (string[]) 
                                   dea.Data.GetData(DataFormats.FileDrop);
               try
               {
                    image = Image.FromFile(astr[0]);
               }
               catch (Exception exc)
               {
                    MessageBox.Show(exc.Message, Text);
                    return;
               }
               strFileName = astr[0];
               Text = strProgName + " - " + Path.GetFileName(strFileName);
               Invalidate();
          }
          else 
          {
               if (dea.Data.GetDataPresent(typeof(Metafile)))
                    image = (Image) dea.Data.GetData(typeof(Metafile));               else if (dea.Data.GetDataPresent(typeof(Bitmap)))
                    image = (Image) dea.Data.GetData(typeof(Bitmap));               bIsTarget = true;
               strFileName = "DragAndDrop";
               Text = strProgName + " - " + strFileName;
               Invalidate();
          }
     }
     protected override void OnMouseDown(MouseEventArgs mea)
     {
          if (image != null)
          {
               bIsTarget = false;               DragDropEffects dde = DoDragDrop(image, 
                              DragDropEffects.Copy | DragDropEffects.Move);               if (dde == DragDropEffects.Move && !bIsTarget)
                    image = null;
          }
     }
 protected override void OnPaint(PaintEventArgs pea)
     {
          Graphics grfx = pea.Graphics;          if (image != null)
               grfx.DrawImage(image, 0, 0);
     }
}

解决方案 »

  1.   

    [C#]
    临时挂起控件的布局逻辑。
    public void SuspendLayout();恢复正常的布局逻辑。
    public void ResumeLayout();当调整控件的多个属性时,将先后使用 SuspendLayout 和 ResumeLayout 方法取消多个 Layout 事件。例如,通常先调用 SuspendLayout 方法,然后设置控件的 Size、Location、Anchor 或 Dock 属性,最后调用 ResumeLayout 方法以使更改生效。以下是MSDN里面的例子:
    ms-help://MS.VSCC/MS.MSDNVS.2052/cpref/html/frlrfSystemComponentModelDesignIComponentChangeServiceClassTopic.htm
      

  2.   

    public event LayoutEventHandler Layout;
    在控件应重新定位其子控件时发生。
    事件处理程序接收一个 LayoutEventArgs 类型的参数,它包含与此事件相关的数据。AffectedControl 获取受此更改影响的控件。 
    AffectedProperty 获取受此更改影响的属性。 备注:
    在添加或移除子控件,控件的边界改变,以及在发生其他可能影响控件的布局时,发生 Layout 事件。可以使用 SuspendLayout 和 ResumeLayout 方法取消布局事件。挂起布局允许在控件上执行多个操作,而不必为每次更改进行布局。例如,如果调整控件大小和移动控件,每次操作都会引发 Layout 事件。
    public void SuspendLayout();
    临时挂起控件的布局逻辑。备注:
    控件的布局逻辑被挂起,直到调用 ResumeLayout 方法为止。
    public void ResumeLayout(bool performLayout);
    恢复正常的布局逻辑。还可以强制对挂起的布局请求立即进行布局。参数 performLayout 
    若要执行挂起的布局请求,则为 true;否则为 false。 备注:
    如果有任何挂起的布局请求,那么调用 ResumeLayout 方法将强制立即进行布局。在 performLayout 参数设置为 true 时,如果有任何挂起的布局请求,则会立即进行布局。
    当调整控件的多个属性时,将先后使用 SuspendLayout 和 ResumeLayout 方法取消多个 Layout 事件。例如,通常先调用 SuspendLayout 方法,然后设置控件的 Size、Location、Anchor 或 Dock 属性,最后调用 ResumeLayout 方法以使更改生效。// 够详细了吧:)