SetWindowPos for a specific window

被刻印的时光 ゝ 提交于 2020-01-06 07:56:39

问题


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);

to

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

doesn't work either.) Any suggestions?


回答1:


Figured it out. I used

[DllImport("user32.dll")]
        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!



来源:https://stackoverflow.com/questions/12340373/setwindowpos-for-a-specific-window

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!