C#拖放OXO [英] C# Drag and Drop OXO
本文介绍了C#拖放OXO的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
嘿,
我需要制作一个C#项目,允许您使用拖放功能来播放OXO.了解代码显示了我到目前为止所拥有的.我有2个问题.
1.图片框阵列始终位于拖动的对象的前面.
2.当我停止单击鼠标按钮(MouseUp事件处理程序)时,不会将拖动的对象放到图片框中.
有人知道答案吗?
Hey,
I need to make a C# project wich allows you to play OXO using Drag and Drop. Understanding code shows what i have till now. i have 2 problems.
1. The array of pictureboxes keeps getting in front of the dragged object.
2. When I stop clicking the mousebutton (MouseUp eventhandler) it won''t put my dragged object into a picturebox.
Someone knows an answer?
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApplication2
{
public partial class Form1 : Form
{
public Form1()
{
//PictureBox MyX = new PictureBox();
//PictureBox MyY = new PictureBox();
InitializeComponent();
MyX.MouseDown +=new MouseEventHandler(MyX_MouseDown);
MyX.MouseMove +=new MouseEventHandler(MyX_MouseMove);
Point pX = new Point(230, 316);
Point pY = new Point(20, 316);
Bitmap X = new Bitmap("X.png");
Bitmap Y = new Bitmap("O.jpg");
MyX.BackgroundImage = X;
MyX.BackgroundImageLayout = ImageLayout.Stretch;
MyY.BackgroundImage = Y;
MyY.BackgroundImageLayout = ImageLayout.Stretch;
//
int kolommen = 4;
int rijen = 4;
int aantalElementen = (kolommen * rijen) + 1;
PictureBox[] Boxes = new PictureBox[aantalElementen];
int tellerIndex = 1;
for (int tellerRij = 1; tellerRij <= rijen; tellerRij++)
{
for (int tellerKol = 1; tellerKol <= kolommen; tellerKol++)
{
Boxes[tellerIndex] = new PictureBox();
Boxes[tellerIndex].Name = "Picturebox" + tellerIndex.ToString();
Point positie = new Point((-50 + (tellerKol * 70)), (-50 + (tellerRij * 70)));
Boxes[tellerIndex].Width = 60;
Boxes[tellerIndex].BorderStyle = BorderStyle.FixedSingle;
Boxes[tellerIndex].Height = 60;
Boxes[tellerIndex].Location = positie;
Boxes[tellerIndex].AllowDrop = true;
tellerIndex++;
}
}
MyX.BorderStyle = BorderStyle.Fixed3D;
MyX.Height = 60;
MyX.Width = 60;
MyX.Image = X;
MyX.SizeMode = PictureBoxSizeMode.StretchImage;
MyY.BorderStyle = BorderStyle.Fixed3D;
MyY.Height = 60;
MyY.Width = 60;
MyY.Image = Y;
MyY.SizeMode = PictureBoxSizeMode.StretchImage;
MyX.Location = pX;
MyY.Location = pY;
this.Controls.AddRange(Boxes);
this.Controls.Add(MyX);
this.Controls.Add(MyY);
}
bool isDragging = false;
int CurrentX, CurrentY;
private void MyX_MouseMove(object sender, MouseEventArgs e)
{
if (isDragging == true)
{
MyX.Top = MyX.Top + (e.Y - CurrentY);
MyX.Left = MyX.Left + (e.X - CurrentX);
}
}
private void MyX_MouseDown(object sender, MouseEventArgs e)
{
isDragging = true;
CurrentX = e.X;
CurrentY = e.Y;
}
}
}
[edit]插入代码块以保留格式,较小的拼写-OriginalGriff [/edit]
[edit]Code block inserted to preserve formatting, minor spelling - OriginalGriff[/edit]
推荐答案
一些注意事项...
您还需要实现一个MouseUp
事件.目前,没有什么可以告诉应用程序拖动已停止.这意味着,第一次按下鼠标按钮时,isDragging
将始终为true
.
您可能还想考虑不要移动MyX
或MyY
(尽管为什么将其命名为MyY
而不是MyO
毫无意义). (作为FYI,也使用c,命名约定是变量以小写字母开头,而类型或类以大写字母开头.)但是,这样做的方式还是需要MouseUp
事件来重置您要拖动的PictureBox
的位置.
如果要确保在其他控件之上绘制MyX
,请尝试在开始拖动时像MyX.BringToFront();
一样调用方法BringToFront
.
您应该真正使用Google"c#拖放".这样做的方式很好,但是c#的创建者已经在控件中实现了内置函数来处理拖放.
a couple of notes...
you need to implement aMouseUp
event as well. Right now, there''s nothing to tell the app that dragging has stopped. That means that once you push the mouse button down the first time,isDragging
will always betrue
.
You also probably want to consider not movingMyX
orMyY
(though why you named itMyY
instead ofMyO
makes no sense). (Also as an FYI, with c, the naming conventions are that variables start with a lower case while types or classes start with uppercase.) But the way you''re doing it, again, you need aMouseUp
event to reset the position of thePictureBox
that you are dragging.
If you want to make sure thatMyX
is being drawn on top of the other controls, try calling the methodBringToFront
as inMyX.BringToFront();
when you start the dragging.
You should really google "c# drag and drop". The way that you''re doing it is fine, but the creators of c# have implemented built-in functions in the controls to handle the drag drop.
对于拖动,您需要更改所拖动项目的Z顺序...
您需要使用Contains相对于控件数组检查"mouseDoneMoving"位置,以获取索引(控件)并手动对控件属性进行修改.
希望对您有所帮助...:confused:
For the dragging you will need to change the Z order for the dragged item...
You need to check the "mouseDoneMoving" position against your controls array using Contains to get the index(control) back and do the modification manually to that controls properties.
Hope that was helpfull...:confused:
这篇关于C#拖放OXO的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文