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
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
I would like to have a function to refresh the desktop like pressing "F5" does. I found many codes with Sendmessage and ToggleDesktopIcons on/off but none worked for me like manual hit of "F5" does. I saw also some topics here, but all with non-working solutions for this matter.
I am on Windows 7 64 Bit with IE 10 and use C# Net Framework 2.
I found also this code, but C# doesn't accept it, even if it seems to me as the right function.
I dunno what I need to change here. I would expect that the IDE would tell me what is my mistake here or what I need to correct. Can someone please correct me this function or suggest another function which is compatible to C#.
procedure RefreshDesktop2;
var
hDesktop: HWND;
begin
hDesktop := FindWindowEx(FindWindowEx(FindWindow('Progman', 'Program Manager'), 0,
'SHELLDLL_DefView', ''), 0, 'SysListView32', '');
PostMessage(hDesktop, WM_KEYDOWN, VK_F5, 0);
PostMessage(hDesktop, WM_KEYUP, VK_F5, 1 shl 31);
end;
Question:
How do I make the code above working in C# (translate in C#) or how looks a similar code in C# like.
Refreshing the desktop with its icons/settings like by pressing "F5" on a selected desktop icon is my goal. Several codes which I tried in similar questions brought me no result.
OK, I don't really understand your code, in fact you have to find the exact window to send the F5 keypress to it so that it will refresh the desktop. Here is the c# code (tested and worked like a charm:)
[DllImport("user32")]
private static extern int PostMessage(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam);
[DllImport("user32")]
private static extern IntPtr FindWindow(string className, string caption);
[DllImport("user32")]
private static extern IntPtr FindWindowEx(IntPtr parent, IntPtr startChild, string className, string caption);
public void RefreshDesktop(){
IntPtr d = FindWindow("Progman", "Program Manager");
d = FindWindowEx(d, IntPtr.Zero, "SHELLDLL_DefView", null);
d = FindWindowEx(d, IntPtr.Zero, "SysListView32", null);
PostMessage(d, 0x100, new IntPtr(0x74), IntPtr.Zero);//WM_KEYDOWN = 0x100 VK_F5 = 0x74
PostMessage(d, 0x101, new IntPtr(0x74), new IntPtr(1 << 31));//WM_KEYUP = 0x101
}
However I think there are still other choices for you to refresh the desktop programmatically, here is one of the links How to refresh the windows desktop programmatically (i.e. F5) from C#?
I made a simple C# program to display basic information about some machines we have. The program is full screen and refreshed every second. We have a dedicated PC and screen in the shop to display it constantly but some directors want it on their PC also, but appears as a wallpaper.
Please tell me if there is an easy way to set a program as a dynamic wallpaper, because I've found nothing.
I think the real solution would be to modify the program so that it runs minimized, and once in a while (like every 15 or 20 minute) it generates a image of the appearance when it is maximized, and then set that picture as the desktop wallpaper.
Does my solution make sense? How can I generate a picture from an hidden application?
Your solution makes sense.
You can change the background f:om C# with the following code but it only works with .bmp image types:
[DllImport("user32.dll", CharSet = CharSet.Auto)]
private static extern Int32 SystemParametersInfo(UInt32 uiAction, UInt32 uiParam, String pvParam, UInt32 fWinIni);
private static UInt32 SPI_SETDESKWALLPAPER = 20;
private static UInt32 SPIF_UPDATEINIFILE = 0x1;
private String imageFileName = "c:\\sample.bmp";
public void SetImage( string filename )
{
SystemParametersInfo(SPI_SETDESKWALLPAPER, 0, filename, SPIF_UPDATEINIFILE);
}
For info on creating a .bmp during runtime, you may read this forum post.