问题
I can't solve this problem. I get an error:
The name 'hWnd' does not exist in the current context
It sounds very easy and probably is... sorry for asking so obvious questions.
Here's my code:
public static IntPtr WinGetHandle(string wName)
{
foreach (Process pList in Process.GetProcesses())
{
if (pList.MainWindowTitle.Contains(wName))
{
IntPtr hWnd = pList.MainWindowHandle;
}
}
return hWnd;
}
I tried with many different ways and each fails. Thanks in advance.
回答1:
Don't forget you're declaring you hWnd
inside the loop - which means it's only visible inside the loop. What happens if the window title doesn't exist? If you want to do it with a for
you should declare it outside your loop, set it inside the loop then return it...
IntPtr hWnd = IntPtr.Zero;
foreach (Process pList in Process.GetProcesses())
{
if (pList.MainWindowTitle.Contains(wName))
{
hWnd = pList.MainWindowHandle;
}
}
return hWnd; //Should contain the handle but may be zero if the title doesn't match
回答2:
Because you are declaring hWnd
inside the if block, it is inaccessible to the return statement which is outside it. See http://www.blackwasp.co.uk/CSharpVariableScopes.aspx for clarification.
The code you've provided can be fixed by moving the declaration of the hWnd variable:
public static IntPtr WinGetHandle(string wName)
{
IntPtr hwnd = IntPtr.Zero;
foreach (Process pList in Process.GetProcesses())
{
if (pList.MainWindowTitle.Contains(wName))
{
hWnd = pList.MainWindowHandle;
}
}
return hWnd;
}
回答3:
As an option to solve this problem:
[DllImport("user32.dll")]
private static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
public IntPtr GetHandleWindow(string title)
{
return FindWindow(null, title);
}
回答4:
hWnd
is declared in the foreach
loop. Its context is inside foeach
loop. To get its value declare it outside foreach
loop.
Use it like this,
public static IntPtr WinGetHandle(string wName){
IntPtr hWnd = NULL;
foreach (Process pList in Process.GetProcesses())
if (pList.MainWindowTitle.Contains(wName))
hWnd = pList.MainWindowHandle;
return hWnd;
}
回答5:
Coming several years late to this but, as others have mentioned, the scope of hWnd
is only in the foreach
loop.
However it's worth noting that, assuming you're doing nothing else with the function, then there are two issues with the answers others have provided:
- The variable
hWnd
is actually unnecessary since it's only being for one thing (as the variable for thereturn
) - The
foreach
loop is inefficient as, even after you've found a match, you continue to search the rest of the processes. In actual fact, it'll return the last process it finds that matches.
Assuming that you don't want to match the last process (point #2), then this is a cleaner and more efficient function:
public static IntPtr WinGetHandle(string wName)
{
foreach (Process pList in Process.GetProcesses())
if (pList.MainWindowTitle.Contains(wName))
return pList.MainWindowHandle;
return IntPtr.Zero;
}
回答6:
#include <windows.h>
#using <System.dll>
using namespace System;
using namespace System::Diagnostics;
using namespace System::ComponentModel;
// get window handle from part of window title
public static IntPtr WinGetHandle(string wName)
{
IntPtr hwnd = IntPtr.Zero;
foreach (Process pList in Process.GetProcesses())
{
if (pList.MainWindowTitle.Contains(wName))
{
hWnd = pList.MainWindowHandle;
return hWnd;
}
}
return hWnd;
}
// get window handle from exact window title
[DllImport("user32.dll")]
private static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
public IntPtr GetHandleWindow(string title)
{
return FindWindow(null, title);
}
来源:https://stackoverflow.com/questions/13547639/return-window-handle-by-its-name-title