我正在制作一款游戏,用户可以在其中使用图像创建自己的赛道。
我想让人们双击图片框(表单 A),然后在不同的表单(表单 B)上重新创建图像。如果用户随后想要将 2 张图像(图像 a 和 b)放在一起,则被拖动的图像(图像 a)将附加到图像 b。
我已经尝试使自定义控件继承自 PictureBox,但由于错误,该控件无法放在表单上。
using Microsoft.Xna.Framework;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Microsoft.Xna;
using System.Drawing;
using System.Drawing.Drawing2D;
using Rectangle = Microsoft.Xna.Framework.Rectangle;
using Color = System.Drawing.Color;
namespace _2D_Racer
{
public class Component_Control : PictureBox
{
Color defaultColor = Color.Transparent;
Color hoverColor = Color.FromArgb(10,100,10);
public Rectangle outline;
//Creating Properties
public struct ComponentSide
{
public Rectangle sideRect; // can be used to see if a snap point is contained with an area
public bool active; // can be used to see if the side should be used or not
public Vector2 snapPoint; // can be used to see if location is contained within an area
}
ComponentSide starboard, port, bow, stern;
List<ComponentSide> sidesList = new List<ComponentSide>();
//Allowing properties to be returned
public Rectangle GetStarBoardRectangle
{
get
{
return starboard.sideRect;
}
}
public Rectangle GetPortRectangle
{
get
{
return port.sideRect;
}
}
public Rectangle GetBowRectangle
{
get
{
return bow.sideRect;
}
}
public Rectangle GetSternRectangle
{
get
{
return stern.sideRect;
}
}
//Methods
public Component_Control(Image _image, bool starBoardActive, bool portActive, bool bowActive, bool sternActive)
{
Image = _image;
outline = new Rectangle(0, 0, Image.Width, Image.Height);
starboard.sideRect = new Rectangle(0, 0, 10, Image.Width);
starboard.snapPoint = new Vector2(starboard.sideRect.X + Image.Width + 10, starboard.sideRect.Y - starboard.sideRect.Y / 2);
starboard.active = starBoardActive;
port.sideRect = new Rectangle(0, 0, 10, Image.Width);
port.snapPoint = new Vector2(port.sideRect.X - Image.Width - 10, port.sideRect.Y - port.sideRect.Y / 2);
port.active = portActive;
bow.sideRect = new Rectangle(0, 0, 10, Image.Width);
bow.snapPoint = new Vector2(bow.sideRect.X + Image.Width + 10, bow.sideRect.Y / 2);
bow.active = bowActive;
stern.sideRect = new Rectangle(0, 0, 10, Image.Width);
stern.snapPoint = new Vector2(stern.sideRect.X + Image.Width + 10, stern.sideRect.Y / 2);
stern.active = sternActive;
}
public void Update(GameTime gameTime)
{
base.Update();
}
//events
protected override void OnMouseClick(MouseEventArgs e)
{
base.OnMouseClick(e);
Cursor = Cursors.Cross;
Location = Cursor.Position;
foreach (ComponentSide sidea in sidesList) // this instance
{
if (sidea.active == true)//this instance
{
foreach(Component_Control component_Control in Globals.componentControlsList) //getting other instances
{
foreach (ComponentSide sidez in component_Control.sidesList) //sides in other other instances
if (sidea.sideRect.Contains(sidez.snapPoint)) //checks if Item has been placed inside the snap region
{
//sidea.
}
}
}
}
}
protected override void OnMouseUp(MouseEventArgs e)
{
base.OnMouseUp(e);
Cursor = Cursors.Default;
}
protected override void OnMouseEnter(EventArgs e)
{
base.OnMouseEnter(e);
BorderStyle = BorderStyle.FixedSingle;
}
protected override void OnMouseLeave(EventArgs e)
{
base.OnMouseLeave(e);
BorderStyle = BorderStyle.None;
}
}
}
还有其他解决方案吗?还是我应该继续尝试使自定义控件工作?