SetWindowPos for a specific window

I have

        [DllImport("user32.dll", EntryPoint = "SetWindowPos")]
    public static extern IntPtr SetWindowPos(string hWnd, int hWndInsertAfter, int x, int Y, int cx, int cy, int wFlags);

My issue is I want to be able to move a specific window based on the text inside of my label.

        private void button1_Click(object sender, EventArgs e)
        const short SWP_NOSIZE = 1;
        const short SWP_NOZORDER = 0X4;
        const int SWP_SHOWWINDOW = 0x0040;

        Process[] processes = Process.GetProcesses();

        foreach (var process in processes)
            IntPtr handle = process.MainWindowHandle;
            string Text = handle.ToString();

            if (handle.ToString() == WindowTextBox.Text)
                SetWindowPos(Text, 0, 0, 0, 0, 0, SWP_NOZORDER | SWP_NOSIZE | SWP_SHOWWINDOW);

I knew this wouldn't work but wanted to try anyway, how else would I be able to move a window based on what is inside my WindowTextBox? (having IntPtr handle in the SetWindowPos(IntPtr hWnd, [...]) and just changing

SetWindowPos(Text, 0, 0, 0, 0, 0, SWP_NOZORDER | SWP_NOSIZE | SWP_SHOWWINDOW);


SetWindowPos(handle, 0, 0, 0, 0, 0, SWP_NOZORDER | SWP_NOSIZE | SWP_SHOWWINDOW);

doesn't work either.) Any suggestions?


Figured it out. I used

        static extern IntPtr FindWindow(string lpClassName, string lpWindowName);

to change the line to

IntPtr handle = FindWindow(null, WindowTextBox.Text);

and the if to

if (handle != IntPtr.Zero)

and it works just the way I want, thanks though!

Need Your Help

Java regular expression to extract parts of a string

java regex match

Consider a long string with the following format (the parentheses are not part of the actual text, just added here to show the group limits):

Correct KO binding to access objects within an array?

javascript knockout.js viewmodel single-page-application durandal

I've got the following code and I believe the ViewModel is structured correctly (I could be wrong) but I can't seem to access the values properly.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.