I recently tried those 2 functions:
[DllImport("user32.dll")]
public static extern IntPtr FindWindow(string lpClassName,string lpWindowName);
[DllImport("user32.dll")]
public static extern IntPtr FindWindowEx(int hwndParent,int hwndChildAfter,String lpszClass, String lpszWindow);
..so far I received only IntPtrs, I know(at least I think) that this returned int is the process ID of the Window, now I want to get window's control text and click on button, how to do that?
I can't find anything around...
Related
Is unity standalone player for Windows a Windows.Form? If so, how can we access its handle?
if(Form.ActiveForm != null)
{
m_form = Form.ActiveForm;
Debug.Log("m_form.Name");
}
I am using the Mono version of System.Windows.Forms.dll. I am trying the above code to access the Form handle but it always returns null even when window is active, which makes me doubt that standalone windows build is not a Windows.Form.
I basically need to access the toolbar so that I can change its color (Not title, for title we already have a workaround that doesn't require accessing the Form instance). Also, I need to set minimum size for the window. Any solution will be helpful.
You can always p/invoke winapi , so in order to get the handle of the current active window use this :
[DllImport("user32.dll")]
static extern IntPtr GetActiveWindow();
And in order to move / adjust the position of the window u can use one of these :
SetWindowPos, MoveWindow and AdjustWindowRectEx
and btw to get a the window's handle ..
[DllImport("user32.dll", EntryPoint="FindWindow", SetLastError = true)]
static extern IntPtr FindWindowByCaption(IntPtr ZeroOnly, string lpWindowName);
or :
[DllImport("user32.dll", SetLastError = true)]
static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
I am attempting to open an existing word document and have the MS Word window be brought to the front. I have used the following code.
var app = new Application{ Visible = true};
app.Documents.Open(path);
app.Activate();
This opens the document in word and on my windows 7 box brings word 2013 to the foreground. On an end user machine running windows 8.1 and office 2010 it opens the document but does not bring Word to the front.
Are there different/missing steps needed for windows 8 or office 2010?
Thanks
Try Microsoft.VisualBasic.Interaction.AppActivate
Microsoft.VisualBasic.Interaction.AppActivate(app.Caption);
If this doesn't work, then try SetForegroundWindow API like this:
[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
static extern bool SetForegroundWindow(IntPtr hWnd);
[DllImport("user32.dll", SetLastError = true)]
static extern System.IntPtr FindWindow(string lpClassName, string lpWindowName);
private void ShowForeground(Word.Application app)
{
IntPtr handler = FindWindow(null, app.Caption);
SetForegroundWindow(handler);
}
I have a hotkey window application in C# and I want all the text from the focused window of other application on pressing hotkey like notepad, browser, command window(cmd), Turbo c++, Pascal etc.
So Is it possible?
If any one have idea please help me with code example.
I have attach screen shot. I want to read text from this window. On pressing hotkey I want to read text "This is my test text".
There is a GetWindowText() in user32 API,
but if you need to get text from a control in another process, GetWindowText() won't work.
You have to use SendMessage() with WM_GETTEXT instead:
const UInt32 WM_GETTEXT = 0x000D;
const UInt32 WM_GETTEXTLENGTH = 0x000E;
[DllImport("user32.dll", CharSet = CharSet.Auto)]
static extern IntPtr SendMessage(IntPtr hWnd, UInt32 Msg, IntPtr wParam, IntPtr lParam);
[DllImport("user32.dll", CharSet = CharSet.Auto)]
static extern IntPtr SendMessage(IntPtr hWnd, UInt32 Msg, IntPtr wParam, StringBuilder lParam);
static string GetWindowTextRaw(IntPtr hwnd)
{
// Allocate string length
int length = (int)SendMessage(hwnd, WM_GETTEXTLENGTH, IntPtr.Zero, IntPtr.Zero);
StringBuilder sb = new StringBuilder(length + 1);
// Get window text
SendMessage(hwnd, WM_GETTEXT, (IntPtr)sb.Capacity, sb);
return sb.ToString();
}
Application that call themselves "Screen Reader" (for visually impaired people) do that kind of things, sort of.
They use the old MSSA (Microsoft Active Accessibility) APIs and/or the new UIAutomation APIs.
With the two APIs, if you have a "Main Window" HWND, you can then browse the tree of the componants making the app. You can then retrieve properties, such as "Text" or "Name" and so on.
If the application doesn't support Accessive technologies, you fall back on case by case solutions, which means eventually awful hacks (as APIs hooking) or more regular methods (as DLL injection and use of the JNI Invocation API in the JAVA case).
Its is not directly possible through C#,
Still microsoft provides with WMI services which can utilized to get at max information on the machine and processes. Kindly check MSDN
You can download WMI tool from here and possible check Win32 classes and methods, you may find useful information for your requirement
oh i have been trying to make a program which looks to see if a program in this case chrome is running then switching to it, i have been trying to follow a similar thread (thread one, thread two) unfortunately I'm having issues with the handle as it seems to like switching to windows explorer (file manager)
[DllImport("User32.dll")]
static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
[DllImport("User32.dll")]
static extern int SetForegroundWindow(IntPtr hWnd);
IntPtr ptrFF = FindWindow(null, "chrome");
SetForegroundWindow(ptrFF);
Process proc = Process.GetProcessesByName("firefox")[0];
IntPtr ptrFF = proc.Handle;
SetForegroundWindow(ptrFF);
these are the two bits of code i have been playing with, however i only got the second one to actuly do somthing, using a basic console aplication i tested weather it was actuly finding the right prosess when i uses "chrome" as the name and this appeared to be the case
test code:
var proc = Process.GetProcessesByName("chrome")[0];
Console.WriteLine(proc.ToString());
The Output of the Writeline is "System.Diagnostics.Prosess (chrome)"
if i printed Process.GetProcessesByName("chrome")[0].Handle;
it comes out as 572
but using:
var proc = Process.GetProcessesByName("chrome")[0].Handle;
Console.WriteLine(proc.ToString());
SetForegroundWindow(proc);
doesn't set the current window to chrome nor give me any errors
I have a project where I want to display a combo to show dropdown list of directory listing like dialog boxes does.
there is a Win32 API
int DlgDirListComboBox(
LPTSTR lpPathSpec,
int nIDComboBox,
int nIDStaticPath,
UINT nFileType
);
and its c# version (thanks to pInvoke.net)
[DllImport("user32.dll")]
static extern int DlgDirListComboBox(IntPtr hDlg, StringBuilder lpPathSpec,
int nIDComboBox, int nIDStaticPath, uint uFiletype);
but I can't figure out what value should I pass in for nIDComboBox parameter (I tried and handle do not work here!)
nIDComboBox should be the Win32 Control ID of the combobox. You can get it with another P/Invoke call by passing the control's Handle:
[DllImport("user32.dll")]
static extern int GetDlgCtrlID(IntPtr hWnd);
But you can also list the directories without resorting to P/Invoke by using Directory.GetDirectories