views:

347

answers:

2

Hello there, i saw a method called Control.FromHandle wich (should) give you the access to it. Now, i wanted to try it using this code

    [DllImport("user32.dll", SetLastError = true)]
    static extern IntPtr FindWindow(string lpClassName, string lpWindowName);

    // Find window by Caption only. Note you must pass IntPtr.Zero as the first parameter.

    [DllImport("user32.dll", EntryPoint = "FindWindow", SetLastError = true)]
    static extern IntPtr FindWindowByCaption(IntPtr ZeroOnly, string lpWindowName);

    [DllImport("user32.dll")]
    private static extern IntPtr GetDC(IntPtr hwnd);
    [DllImport("user32.dll")]
    private static extern bool ReleaseDC(IntPtr hwnd, IntPtr hdc);

    public Form1()
    {
        InitializeComponent();
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        IntPtr ptr = FindWindowByCaption(IntPtr.Zero, "Download");
        Control f = Control.FromHandle(ptr);
        f.Text = "Something";
    }

but it won't, obviously, work. I checked personally that the handle is correct... but the method returns a null control. Any explaining?

+1  A: 

This method only works if the handle you pass in actually is a Control in your application.

jdv
Oh, i see... that means i'll search for another method :D
FBSC
A: 

In your particular case, since you just want to set the text, call SetWindowText from user32.dll

Ben Voigt
i wanted to control the window, so that method doesn't fits :)
FBSC
control how? Move or resize it? Call MoveWindow or SetWindowPoshttp://msdn.microsoft.com/en-us/library/ms633534(VS.85).aspxhttp://msdn.microsoft.com/en-us/library/ms633545(VS.85).aspxMost other interactions will require calling SendMessage or PostMessage (this is how SetWindowText works, actually).What are you trying to do to the window? Since it's a Win32 window, not a .NET window, you'll have to use Win32 APIs to control it.
Ben Voigt