質問

A Visual C# form includes a function to create a blank new Microsoft Document when a specific button is pressed.Once the user has clicked on this button ,I need to check if a new Microsoft Document is already created and still activate before creating a new word document,hence one document is opened at a time.

I'm trying to accomplish this using the code below:-

[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
static extern bool  IsWindow(IntPtr hWnd);

This will return a bool.I do not know how to check the output?Which parameter will I check and what will be the corresponding syntax?The problem that might arise is that the user can open a notepad and this will be the activate window,hence I need to be specific since it only applies to a word documents.Normally,when a new document is create the default name is Document1,but the user has an option of saving and changing the name.

役に立ちましたか?

解決

IsWindow is not too useful for what you want (you don't have hWnd, the handle of the window). FindWindow would be much more helpful; but the problem is that it (and other equivalent WinAPI calls) takes the name of the given window as argument and, for MS Word, this is the name of the document (what I guess that you don't know).

A simple and reliable approach would be looking at the running processes:

System.Diagnostics.Process[] Procs = System.Diagnostics.Process.GetProcessesByName("winword");
if (Procs.Length >= 1)
{
    //There is an opened Word document
} 

他のヒント

you can check Process check running process and get handle id.

System.Diagnostics.Process

Process[] allProcesses = Process.GetProcesses();
processName = <Todo: check process_name in the array for word document>
Regex r = WildcardToRegex(processName);

matching = allProcesses.Where((p) =>
            {
                try
                {
                    return r.IsMatch(p.MainModule.FileName);
                }
                catch
                {
                    return false;
                }
            }).Select((p) => p.Id);

Once you found the process_name and Process ID, you can check if there is a new process(WordDocument in your case) running.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top