C# Moving PictureBox over other PictureBoxes with transparent background

ぃ、小莉子 提交于 2019-12-25 07:27:31

问题


My goal is to make checkers game , where user can move checkers on the board.

The board I have made is from DataGridView class .

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.

The problem is that the "transparent" effect is actually relative to parent control, and the parent of all checkers is the DataGridView.

So the result is when there is 2 checkers overlapping there is no transparent effect .

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.

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


回答1:


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. So 4 images which can be separate or combined on a spritesheet. 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.




回答2:


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://stackoverflow.com/questions/37714679/c-sharp-moving-picturebox-over-other-pictureboxes-with-transparent-background

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!