Home c++ C++ Line drawn through HDC Lineto () flickering

C++ Line drawn through HDC Lineto () flickering

Author

Date

Category

When starting, the program must draw a short line along the middle of the screen (on top of all programs).
The line is drawn, everything is fine, but very flickering. How to solve the problem?

Here is the code:

# include & lt; iostream & gt;
#Include & lt; Windows.h & gt;
#Include & lt; Winuser.h & gt;
#Include & lt; stdio.h & gt;
Using Namespace STD;
INT MAIN () {
  int y = 0;
  int x = 0;
  int xs = 0;
  int ys = 0;
  Rect Desktop;
  Const Hwnd HDesktop = GetDesktopWindow ();
  GetWindowRect (HDESKTOP, & AMP; Desktop);
  x = desktop.right / 2;
  y = desktop.bottom / 2 - 2;
  xs = desktop.right / 2 - 2;
  ys = desktop.bottom / 2;
  HPEN PEN = CREATEPEN (PS_SOLID, 8, RGB (255, 0, 0));
  HDC dot = :: getdc (0);
  for (;) {
    SelectObject (Dot, Pen);
    Movetoex (Dot, X, Y, NULL);
    Lineto (DOT, X, Y + 4);
    SLEEP (10);
  }
}

Answer 1, Authority 100%

The cycle is not needed, you constantly redraw the line, so it flickers. It is enough to draw a line and stop the execution of the program. It is also important to free resources.

hpen op = (hpen) SelectObject (Dot, Pen);
// for (;)
{
  Movetoex (Dot, X, Y, NULL);
  Lineto (dot, x, y + 40); //
  // Sleep (10);
}
SelectObject (DOT, OP);
DELETEOBJECT (PEN);
ReleaseDC (0, DOT);
getchar ();

And of course, as soon as the redrawing occurs, the line will start. That is why it is better to create a transparent window and redraw a line on the WM_PAINT event window. In this case, the line will redraw when the application becomes active.

Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.

Recent questions