-4

I'm using this function in a Timer:

IntPtr Handle = FindWindow(null, "Notepad");

if (radioButton1.Checked)
{
    using (Graphics g = Graphics.FromHwnd(Handle))
    {
        Pen PN = new Pen(pictureBox2.BackColor, (Convert.ToInt32(numericUpDown2.Value)));
        g.DrawLine(PN, 961, 520, 961, 560);
        g.DrawLine(PN, 985, 540, 935, 540);
        g.Dispose();
    }
}

But the draw is blinking alot even if i set the timer interval to 1

Jonathan Hall
  • 75,165
  • 16
  • 143
  • 189
sdhp442
  • 1
  • 2
  • _even if i set the timer interval to 1_ Huh? Meaning _even if I run it as often as I can_ ??? (Interval is in ms !) – TaW May 21 '19 at 12:06
  • @TaW Yeh.. got any solution ? – sdhp442 May 21 '19 at 12:22
  • For a slower blink rate set Interval to larger value. 1000 is one second. - Also don't dispose what is created in a using clause! – TaW May 21 '19 at 12:33
  • @TaW Slower interval is actually even worse It's not even drawing it properly – sdhp442 May 21 '19 at 12:40
  • Not sure if your approach will work. [See here](https://stackoverflow.com/questions/815667/how-to-draw-graphics-text-on-top-of-another-application) – TaW May 21 '19 at 12:58
  • What is that you actually want to do here? I doubt you just want to paint a cross in a fixed position over Notepad's client area. – Jimi May 21 '19 at 13:11

1 Answers1

-1

Your code will only get the main window's handle.

Most applications have other controls that cover that clientarea.

Instead of drawing onto the clientarea you need to get the the handle of Notepad's textarea.

You can test this by

  • testing the handle value. If it is 0 you only got the desktop. This probably what you have been seeing.

  • If you test it with a form of your own it will show your cross if and only if the drawing area is not covered by other controls..

It is also a good idea to enumerate the processes to get at the correct titles. Here is an example I found in another post:

public static IntPtr WinGetHandle(string wName)
{
    IntPtr hWnd = IntPtr.Zero;
    foreach (Process pList in Process.GetProcesses())
    {
        if (pList.MainWindowTitle.Contains(wName))
        {
            hWnd = pList.MainWindowHandle;
        }
        Console.WriteLine(pList.MainWindowTitle);
    }
    return hWnd;
}

It will take some more trial and error code to find a suitable subhandle of the application's surface..

TaW
  • 53,122
  • 8
  • 69
  • 111
  • Still can't figure out how to fix the blinking draw it's working better when i use "for ( ; ; )", but if i use that i can't go back to the form anymore – sdhp442 May 21 '19 at 17:19
  • What rate do you aim to get? Also: Are you claiming that your code actually draws onto Notepad?? (Test: Move Notepade around; will it still get the cross at the same spot??) – TaW May 21 '19 at 18:01