Why does overriding OnPaint() have no noticeable effect?

Reborn Xu
protected override void OnPaint(PaintEventArgs e)
{
    // Nothing here...
}

I wrote a class that inherits System.Windows.Forms.Form. I overrode the method OnPaint and do nothing in it, as shown above. I expected to see nothing appear on the screen when when I run the code, but the window appeared as usual.

I made sure that nothing got painted, yet it looks completely normal. Why did this happen?

Hans Passant

The Form.OnPaint() method already does no painting at all. So you didn't change anything, the only thing you did is preventing the Paint event from getting fired by not calling base.OnPaint(). Not very useful of course.

You forgot about the other method that paints, the OnPaintBackground() method. It draws the background of the window as set by the BackColor property, an image if you've set the BackgroundImage property. OnPaint() and any Paint event handler draw on top of that. The fundamental cause of flicker.

Trying to channel why you are doing this: no, this is not the proper way to achieve transparency. An unpainted window shows random pixels, whatever happened to be present in the video frame buffer before the window was created. Which looks like transparency, but only for a very short time. Messed up when the user resizes or moves the window. Windows are fundamentally opaque. The proper way is to use the TransparencyKey property, it enables a mixer that combines the pixels of multiple windows. Also the way the Opacity property works.

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

Why does a Redux reducer have to be side-effect free?

分類Dev

Why does Await within "override async void OnPaint" throw OutOfMemoryException or ArgumentException?

分類Dev

Why does adding a tokenbf_v2 index to my Clickhouse table not have any effect

分類Dev

Does launchMode in manifest have any effect for service?

分類Dev

Why does '/' have an '..' entry?

分類Dev

Why does Docker have a daemon?

分類Dev

What effect does the ruby regular expression option `n` have?

分類Dev

Does putting a command in single quotes have any effect in a bash script?

分類Dev

What effect does Optimisation in GCC compiler have on overflow conditions

分類Dev

What effect does #define X X have in C?

分類Dev

why does class cast does not take effect in java ternay operator

分類Dev

Why my a element tag does not effect the links? Right syntax?

分類Dev

Why does C++ not have reflection?

分類Dev

Why does C++ not have reflection?

分類Dev

Why does C++ not have reflection?

分類Dev

Why std::vector does not have a release method?

分類Dev

Why does my viewmodel name have to be 'model'?

分類Dev

Why does the Javascript EventLoop have different result?

分類Dev

Why does __new__ not have classmethod decorator

分類Dev

Why does Windows have multiple Desktop views?

分類Dev

Why does Chrome have two encoding settings?

分類Dev

Does the -O0 compiler flag have the same effect as the volatile keyword in C?

分類Dev

Why does npm install say I have unmet dependencies?

分類Dev

Why does pthread_cond_wait have spurious wakeups?

分類Dev

Why does the escape key have a delay in Python curses?

分類Dev

Why does numpy have a corresponding function for many ndarray methods?

分類Dev

Why does AVPlayerView not have a member named "player" in Swift

分類Dev

Why does Rust have both call by value and call by reference?

分類Dev

Why does Box<trait> have a different size than Box<struct>?

Related 関連記事

  1. 1

    Why does a Redux reducer have to be side-effect free?

  2. 2

    Why does Await within "override async void OnPaint" throw OutOfMemoryException or ArgumentException?

  3. 3

    Why does adding a tokenbf_v2 index to my Clickhouse table not have any effect

  4. 4

    Does launchMode in manifest have any effect for service?

  5. 5

    Why does '/' have an '..' entry?

  6. 6

    Why does Docker have a daemon?

  7. 7

    What effect does the ruby regular expression option `n` have?

  8. 8

    Does putting a command in single quotes have any effect in a bash script?

  9. 9

    What effect does Optimisation in GCC compiler have on overflow conditions

  10. 10

    What effect does #define X X have in C?

  11. 11

    why does class cast does not take effect in java ternay operator

  12. 12

    Why my a element tag does not effect the links? Right syntax?

  13. 13

    Why does C++ not have reflection?

  14. 14

    Why does C++ not have reflection?

  15. 15

    Why does C++ not have reflection?

  16. 16

    Why std::vector does not have a release method?

  17. 17

    Why does my viewmodel name have to be 'model'?

  18. 18

    Why does the Javascript EventLoop have different result?

  19. 19

    Why does __new__ not have classmethod decorator

  20. 20

    Why does Windows have multiple Desktop views?

  21. 21

    Why does Chrome have two encoding settings?

  22. 22

    Does the -O0 compiler flag have the same effect as the volatile keyword in C?

  23. 23

    Why does npm install say I have unmet dependencies?

  24. 24

    Why does pthread_cond_wait have spurious wakeups?

  25. 25

    Why does the escape key have a delay in Python curses?

  26. 26

    Why does numpy have a corresponding function for many ndarray methods?

  27. 27

    Why does AVPlayerView not have a member named "player" in Swift

  28. 28

    Why does Rust have both call by value and call by reference?

  29. 29

    Why does Box<trait> have a different size than Box<struct>?

ホットタグ

アーカイブ