0

I have a problem with screen flickering. I read some other topics on this case, but there are solutions that didn't work for me, I think that's because I don't know what exactly causes the problem.

My screen is has a large number of controls, maybe this is what causes the problem. I'll try to describe it as best, as I can.

  1. First of all, I am using WinForms.

  2. I am making a video game, so the screen should be maximized all the time.

  3. To allow stretching all the controls I am using TablePanels, one large that is docked to fill the whole form and a few smaller that also dock fill the large Table cells. In smaller cells of those tables, Buttons are docked fill.

  4. To show the background drawn buttons, I made control buttons completely transparent. It needs to stay that way.

  5. The screen flickers white at positions of TableLayoutPanels borders.

  6. The screen flickers when a mouse enters the position of a Button, any Button, no matter where it is located.

  7. For now, only one element changes actively during gameplay - a Label. When mouse enters the field of button, this label shows what this button does. For example if I enter the area of "Use" button, the label displays the word "USE".

  8. I haven't tried that yet, but I must implement, that some images of button will change or become transparent, or lose transparency during game-play. Like there could be one image for closed cupboard, but when player opens it, another image of open cupboard appears. I think I know how to do it, all I want is to prevent flickering.

If you suggest using some code (and I expect it will be needed), please specify where I should put it.

Yogi
  • 9,174
  • 2
  • 46
  • 61
EvgenieT
  • 209
  • 1
  • 4
  • 16
  • 1
    Time to learn and move to WPF – AEonAX Oct 19 '15 at 07:06
  • 1
    The only thing i can think of is to enable double buffering : https://msdn.microsoft.com/en-us/library/3t7htc9c%28v=vs.110%29.aspx – Fabjan Oct 19 '15 at 07:22
  • I read about double buffering, but where I have to put it? I don't have in properties window. At which event I should put it? – EvgenieT Oct 19 '15 at 07:34
  • @EvgenieT Flickering occurs as result of multiple redraw of form and its controls. Quote from MSDN : *"When double buffering is enabled, all paint operations are first rendered to a memory buffer instead of the drawing surface on the screen. After all paint operations are completed, the memory buffer is copied directly to the drawing surface associated with it."* You can try to enable it on your main form – Fabjan Oct 19 '15 at 07:43
  • @Fabjan Sorry that I ask again. I read about double buffering and tried to put those lines: DoubleBuffered = true; SetStyle(ControlStyles.OptimizedDoubleBuffer, true); to main form Load. But it didn't help. – EvgenieT Oct 19 '15 at 07:48
  • This article may help you : https://social.msdn.microsoft.com/Forums/windows/en-US/aaed00ce-4bc9-424e-8c05-c30213171c2c/flickerfree-painting?forum=winforms – Fabjan Oct 19 '15 at 07:59
  • @Fabjan I'll read and try it at evening, when I am home. – EvgenieT Oct 19 '15 at 08:13
  • I used the suggestion from the link, but it only partly helped. Now I have much less flickering. However it does happen, when I move from one button to another abruptly, less then one second. Then, I tried setting a timer, that will turn menu bar invisible for a few milliseconds. It reduced flickering even more, but still not completely. Also, that works with menu, but won't be of much help with images I can't make invisible, even for short time. What other solutions are there? I don't know how that will be in code, but maybe something like "don't redraw graphics until some time passed". – EvgenieT Oct 19 '15 at 15:55
  • Or, if its possible, to reduce mouse movement speed. When I move my mouse slowly, there is no flickering. Is there a way to force my application to use slower mouse speed? – EvgenieT Oct 19 '15 at 15:55

1 Answers1

0

It seems problem was solved. I'll answer my question myself, in case someone else needs it.

The problem was caused by one of labels, although I can't guess why. I am using a number of TableLayoutPanels, one of them fills the whole form. It has a number of rows, each of them is also a TableLayoutPanel, or just a Panel. Flickering appeared when I tried to put a label inside main TableLayoutPanel by its own, and not in a sub panel. When I put a Panel inside main Table, and label inside it, the majority of flickering was gone.

To remove it completely, I used both recommendations just in case: 1) DoubleBuffering: Here (answer from Fabjan) 2) Article also recommended by Fabian

After that, only a few minor glitches remained. I did a few tests and it seems that the last glitches happened when mouse left the screen, then returned back inside. My game screen is maximized, so it took some time to check. To fix it, I used an easy command:

"Cursor.Clip = this.Bounds;" on FormLoad

I learned it from another question from this site: Here

Unfortunately, I didn't understand, why misplaced label was behind flickering, but since the problem was solved, I decided to write it here, in case someone else needs it.

Community
  • 1
  • 1
EvgenieT
  • 209
  • 1
  • 4
  • 16