question

Bob-0347 avatar image
0 Votes"
Bob-0347 asked RobCaplan edited

is it possible to make a the graophics class of a winform control to be static

I would like to begin designing simple 2D games. So I thought I would start off with something similar to whack a mole. However, instead of using one mole, I would like to add multiple instances of the same flash card. For example, using a star for instance, a star can always be set to yellow, and always be a 5 pointed star. The appearance of the image is always the same, but each star can have there own unique location and be different in size. As a first attempt, I would think, based on these criteria's, the image could be a static member, but the location and its size will need to be dynamic during runtime that is set by a random generator. However, I have a dilemma. First and foremost, I love the ease of Winform mouse events, since you do not need to loop through every control to find which control the player has clicked on. Once you register a mouse event to each object, Winform does the rest in finding which control has been clicked on. Despite the benefits with mouse event handling, I have read that using Winform controls are not your best choice for graphic animation, but I still feel like it is still possible if I could only some how make the image of the control to be static. I tried looking at the Monogame Framework, but so far, for what I understand, you need to loop through each sprite before finding which sprite was clicked on, and I think looping through each rectangle would be just as slow. So is it still possible to create control, where the image is static?

dotnet-csharpwindows-forms
· 4
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

You should use more commons tags, such :

windows-forms
dotnet-visual-basic
dotnet-csharp

0 Votes 0 ·

thanks for the tip henrihan. I just added windows-forms and dotnet-csharp tags

0 Votes 0 ·

It did not seem to work : there is still only : dotnet-gamedev

0 Votes 0 ·

@ henrihan-8261:
it should updated, because I can see all three tags. Is there still a problem

0 Votes 0 ·
cheong00 avatar image
0 Votes"
cheong00 answered cheong00 edited

WinForm controls (with the exception of possibly Dialogs) should generally be created with form and destroyed with it.

You'll want to create a static state-storage variable to store the game items and their state for your game, and the whether you need to show the UI, you recreates the UI according to this state-storage variable instead.

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

DanielZhang-MSFT avatar image
0 Votes"
DanielZhang-MSFT answered

Hi Bob-0347,
It is useless to create a graphics object using the "static" keyword, because this is the only way to provide a device context for the Graphics object or the handle to be drawn on it is done by calling its constructor.
More details please refer to this docuemnt.
I suggest you use it included in the using block, otherwise you need tomake sure to discard it after each set of drawing operations.

 using(Graphics x = this.CreateGraphics())
     {
         // draw here...
     }

Best Regards,
Daniel Zhang


If the response is helpful, please click "Accept Answer" and upvote it.

Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Bob-0347 avatar image
0 Votes"
Bob-0347 answered

....wow! I did not expect my question to be answered in the way it did. I think I need to step back in a much broader spectrum. So let me try clearing up a few things. At first, I thought I could focus more on my game and not worry about mouse event handling. So once I found out that the .Net Winform library has this mythology, I thought this would of been an easy language to use and learn. However, I quickly also found out, that Winforms has a slow draw call rate. That is why I thought, if I could somehow harness a bounding box, that is not drawable, I could then use mouse event handling and hold the shared image, its location, and size in another class. This way, I would only need one image component per image, which should allow my game to do 1 draw call per different image, but have x^n power of bounding boxes.

So technically, I am left in the dark, and I am not sure which way to go. Any suggestion would be much appreciated.

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Castorix31 avatar image
0 Votes"
Castorix31 answered

I quickly also found out, that Winforms has a slow draw call rate

For games, you must use Direct2D or DirectX (for 3D, more complex) to use Hardware Acceleration
(I had posted some simples animations samples in .NET like VB_Direct2D_Collision.zip which show that animations are very smooth)
GDI+ or GDI are too slow for animations
I use P/Invoke for Direct2D interfaces, but you can also use wrapper DLLs, like Win2D


5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.