電子書籍の厳選無料作品が豊富!

C#初心者です。以下のC#のコードでfirefoxのウインドウを移動しようとしましたが、できませんでした。"MozillaWindowClass"を変更すればメモ帳を移動できることは確認しました。UWSCならfirefoxのウインドウを移動できることも確認しています。C#でfirefoxのウインドウを移動する方法を教えてください。

using System;
using System.Windows.Forms;
using System.Runtime.InteropServices;

namespace WindowsFormsApplication2
{
public partial class Form1 : Form
{
[DllImport("User32.dll", EntryPoint = "FindWindow")]
public static extern IntPtr FindWindow(String lpClassName, String lpWindowName);

public Form1()
{
InitializeComponent();
}

[System.Runtime.InteropServices.DllImport("user32.dll")]
private static extern int MoveWindow(IntPtr hwnd, int x, int y,
int nWidth, int nHeight, int bRepaint);


private void Form1_Load(object sender, EventArgs e)
{
IntPtr hwnd1 = FindWindow("MozillaWindowClass", null);

MoveWindow(hwnd1, 0, 0, 500, 700, 1);

}
}
}

A 回答 (1件)

FindWindowで引数lpWindowNameをNULLとして与えると lpClassNameに該当するすべてのウィンドウの中で初めて見つかったものを返します


FireFox自体が 何かしらのダミーを作っているようで 実際操作したいもの以外のハンドルを取得しているのが原因のようですよ

対策として lpWindowNameを指定してやるか EnumWindowsで列挙しながら GetClassNameでFireFoxか特定し
その上で GetWindowTextなどで 対象かどうか確かめてはいかがでしょう
    • good
    • 0
この回答へのお礼

ありがとうございます。
教えていただいた方法を試してみようと思いましたが、EnumWindowsをよく理解できず、
調査中です。

お礼日時:2017/08/02 22:50

お探しのQ&Aが見つからない時は、教えて!gooで質問しましょう!