问题
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