本文介绍了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]
推荐答案
这篇关于C#拖放OXO的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!