[ Pobierz całość w formacie PDF ]
.Text = "Put SuperGlo on clipboard";b.Location = new Point(10, 40);b.Width = 100;b.Click += new EventHandler(OnPutSuperGlo);PictureBox pb = new PictureBox();pb.Image = Image.FromFile(@".\superglo.jpg");pb.SizeMode = PictureBoxSizeMode.AutoSize;pb.Location = new Point(220, 100);pb.MouseDown +=new MouseEventHandler(OnBeginDrag);Panel floor = new Panel();floor.BorderStyle = BorderStyle.Fixed3D;floor.Location = new Point(10, 80);Label f = new Label();f.Text = "Floor";floor.Controls.Add(f);floor.AllowDrop = true;floor.DragEnter +=new DragEventHandler(OnDragEnterFloorWax);floor.DragDrop +=new DragEventHandler(OnDropFloorWax);Panel dessert = new Panel();dessert.BorderStyle = BorderStyle.Fixed3D;dessert.Location = new Point(300, 80);Label d = new Label();d.Text = "Dessert";dessert.Controls.Add(d);dessert.AllowDrop = true;dessert.DragEnter +=new DragEventHandler(OnDragEnterDessertTopping);dessert.DragDrop +=new DragEventHandler(OnDropDessertTopping);TextBox textTarget = new TextBox();textTarget.Width = 400;textTarget.Location = new Point(120, 250);textTarget.AllowDrop = true;textTarget.DragEnter +=new DragEventHandler(OnDragEnterText);textTarget.DragDrop +=new DragEventHandler(OnDropText);Controls.AddRange(new Control[]{b, pb, floor, dessert, textTarget});}private void OnPutSuperGlo(object s, EventArgs a){SuperGlo superglo = new SuperGlo();SuperGloMover mover = new SuperGloMover();mover.SetData(superglo);Clipboard.SetDataObject(mover);}private void OnBeginDrag(object s, MouseEventArgs args) {SuperGloMover sgm = new SuperGloMover();sgm.SetData(new SuperGlo());((Control) s).DoDragDrop(sgm, DragDropEffects.Copy );}private void OnDragEnterFloorWax(object s, DragEventArgs args) {if (args.Data.GetDataPresent("FloorWax")) {args.Effect = DragDropEffects.Copy;}}private void OnDropFloorWax(object s, DragEventArgs args) {FloorWax f =(FloorWax) args.Data.GetData("FloorWax");f.Polish();}private void OnDragEnterDessertTopping(object s, DragEventArgs args) {if(args.Data.GetDataPresent("DessertTopping")) {args.Effect = DragDropEffects.Copy;}}private void OnDropDessertTopping(object s, DragEventArgs args) {DessertTopping d =(DessertTopping) args.Data.GetData("DessertTopping");d.Eat();}private void OnDragEnterText(object s, DragEventArgs args) {if (args.Data.GetDataPresent("Text")) {args.Effect = DragDropEffects.Copy;}}private void OnDropText(object sender, DragEventArgs args) {string s = (string) args.Data.GetData("Text");((Control)sender).Text = s;}public static void Main() {Application.Run(new DragAndDropDemo());}}//:~When run, this application looks like this:If you click on the image, you can drag it to either of the two Panels, theTextEdit display, or into a text editor such as Microsoft Word.As you drag,the mouse cursor will change to indicate whether or not the SuperGlo can bedropped.If you drop the SuperGlo on the “Floor” panel you’ll see one messageon the console, if on the “Dessert” panel another, and if in the TextEdit boxor in another application, the promotional message will be pasted.If you clickon the button, you can put a new SuperGlo on the Clipboard.The DragAndDropDemo( ) constructor should have no surprises until we add aMouseEventHandler on for the MouseDown event of the PictureBox control.Thatevent will trigger the creation of a new SuperGloMover (as will the pressing ofthe Button).The Panels both have their AllowDrop property set to true, so when the mouseenters them while dragging an object, the mouse will give visual feedback thata drop target is being traversed
[ Pobierz całość w formacie PDF ]