Click here to Skip to main content
15,885,365 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want to make fast moving lines motion on the top left of the screen (to keep myself alert), I managed to draw the lines, but I want to remove the previous one (or ones if a smoother motion), how to hide the previous lines from the desktop or whatever is on it (refreshing this part of the screen may help).

What I have tried:

[DllImport("User32.dll")]
public static extern IntPtr GetDC(IntPtr hwnd);
[DllImport("User32.dll")]
public static extern void ReleaseDC(IntPtr hwnd, IntPtr dc);
//Get a Graphics object for the entire screen and draw a rectangle with it:

int currentLine = 0, totalNumOfLines=12;

Point point = new Point(60, 60);

private void FormSettings_Load(object sender, EventArgs e)
{
    desktopPtr = GetDC(IntPtr.Zero);
    g = Graphics.FromHdc(desktopPtr);
}

IntPtr desktopPtr;

Graphics g;

private void FormSettings_FormClosed(object sender, FormClosedEventArgs e)
{
    g.Dispose();
    ReleaseDC(IntPtr.Zero, desktopPtr);
}

private void timerDraw_Tick(object sender, EventArgs e)
{
    g.Dispose();
    ReleaseDC(IntPtr.Zero, desktopPtr);

    desktopPtr = GetDC(IntPtr.Zero);
    g = Graphics.FromHdc(desktopPtr);

    currentLine++;

    if (currentLine == totalNumOfLines) currentLine = 0;

    g.DrawLine(new Pen(Color.Yellow, 2), point.X, point.Y
        , point.X + (int)(50 * Math.Cos((currentLine / (double)totalNumOfLines) * Math.PI * 2))
        , point.Y + (int)(50 * Math.Sin((currentLine / (double)totalNumOfLines) * Math.PI * 2)));
}
Posted
Updated 8-May-19 15:37pm
Comments
[no name] 8-May-19 20:11pm    
I use a rotating taiji, but that's to slow me down.

https://social.msdn.microsoft.com/Forums/vstudio/en-US/8de8b6e9-94ef-4e7d-950a-19ec68715982/how-to-continously-spin-or-rotate-an-image?forum=wpf
john1990_1 8-May-19 20:15pm    
Thx, but I only understand C#, I didn't understand the link at all!
[no name] 8-May-19 22:40pm    
It's C#, WPF, XAML ... that was the XAML part. Drop it in a WPF window. Live a little.

1 solution

If your code is drawing the lines, you have to constantly update the positions of the endpoints of the lines and redraw the lines using these new values.

There is really no such thing as "erasing" those lines. You have to draw the area under where you drew your lines and then draw your new lines on top of them. Your look would look something like this, assuming a single line and drawing in a small box, say 20 x 20 pixels:
Initialize your line endpoint values, say 0,0 and 20,20.
Grab a snapshot of the 20x20 box you're going to draw your line in.
Start your timer to trigger drawing the next "frame".

Timer loop:
Draw the snapshot you took back to the original position to "erase" any line.
Draw your line.
Update the positions of the line endpoints for the next frame.
Done - on the next timer "tick", your code start the "timer loop" all over again.
 
Share this answer
 
Comments
john1990_1 8-May-19 21:51pm    
Thx a lot, this is problematic because what was behind the drawn line may change, and drawing the drawing of the saved snapshot is slow in C# .Net VS 2017 for a 50X50 pixels I think.
Dave Kreskowiak 8-May-19 23:04pm    
Slow? Depends on how you're capturing the image and drawing it back.
john1990_1 9-May-19 12:00pm    
Maybe I make a borderless form with "TopMost=true;", with TransparentKey to the same as its Backolor, but I'm afraid the user would lose mouse presses if he presses on the line/form?
Dave Kreskowiak 9-May-19 14:34pm    
You could, but as soon as the user hits Win-D, your form gets minimized.

Also, TopMost is transient. The second another app sets a window to TopMost, yours is no longer at the top of the z-order.

Oh! And Color.Transparent is NOT transparent.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900