简体   繁体   English

C#将PictureBox移到其他具有透明背景的PictureBox上

[英]C# Moving PictureBox over other PictureBoxes with transparent background

My goal is to make checkers game , where user can move checkers on the board. 我的目标是制作跳棋游戏,用户可以在其中移动跳棋。

The board I have made is from DataGridView class . 我制作的板来自DataGridView类。

Right now what am i doing is creating picture boxes for each checker with transparent background and when the user moves the selected checker i am setting him BringToFront. 现在,我正在为具有透明背景的每个检查器创建图片框,并且当用户移动选定的检查器时,我将其设置为BringToFront。

The problem is that the "transparent" effect is actually relative to parent control, and the parent of all checkers is the DataGridView. 问题在于“透明”效果实际上是相对于父控件而言的,所有检查器的父元素都是DataGridView。

So the result is when there is 2 checkers overlapping there is no transparent effect . 因此,结果是当2个棋子重叠时,没有透明效果。

Example of my problem image 我的问题图片示例

I have got some solution but its takes a lot of memory and all freezes even on thread which is not in UI thread. 我有一些解决方案,但它占用大量内存,即使在UI线程中没有的线程上,所有冻结。

My solution was to have one big panel (parent of all checkers) that its background is build of client screenshot and changes while moving the checker. 我的解决方案是要有一个大面板(所有检查器的父级),其背景是客户端屏幕快照的构建,并在移动检查器时进行更改。

What is the correct way of doing thing like that ? 做这样的事情的正确方法是什么? I was looking and trying solutions on the net from the mooring ): Please help ! 我一直在寻找并尝试通过系泊设备在网上找到解决方案):请帮助!

Edit : 编辑:

Almost done : 差不多完成了:

https://youtu.be/nyhfIOzqeY8 https://youtu.be/nyhfIOzqeY8

If you are going to use pictures I wouldn't use transparency. 如果您要使用图片,则不会使用透明度。 Transparency is slower in every application. 在每个应用程序中,透明度都较慢。

Let's think for a minute about an alternate solution. 让我们思考一下替代解决方案。 How many states can a square be? 一个正方形可以有多少个州? Primary colored square can be empty, or have a red or black piece on top. 原色正方形可以为空,或者顶部为红色或黑色。 The alternate colored square never has a piece on it. 备用彩色正方形永远不会有碎片。

That is a total of 4 states. 总共有4个州。 So 4 images which can be separate or combined on a spritesheet. 因此可以在Spritesheet上分离或组合4张图像。 Then you just need to switch the picture depending on its state. 然后,您只需要根据图片的状态切换图片即可。 Primary empty, primary with red, primary with black, or alternate colored square. 主色为空,主色为红色,主色为黑色或其他彩色正方形。

If you want a fancy border you can use an image for that behind or beside if you build it piece by piece. 如果您想要精美的边框,则可以逐个构建图像,以在其后面或旁边使用图像。

So if anyone in the future will need help with this issue . 因此,如果将来有人需要此问题的帮助。

The idea is to have another control panel over all the other panels (game board and checkers). 想法是在所有其他面板(游戏板和检查器)上拥有另一个控制面板。

Then each time we would like to move an checker we take a screenshot from the game board without the moving checker and putting this screenshot to be the image of the top most panel. 然后,每次我们想要移动一个检查器时,我们都会从游戏板上截取一个屏幕截图,而无需移动该检查器,并将此屏幕截图作为最顶部面板的图像。

Now we change the parent of the moving checker to be the top most panel and now we are moving the checker upon this panel with transparent background. 现在我们将移动检查器的父级更改为最上面的面板,现在我们将检查器移动到具有透明背景的该面板上。

https://youtu.be/PUEyZPhPTlc https://youtu.be/PUEyZPhPTlc

(: (:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM