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);
Related
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 am using Screen.PrimaryScreen.WorkingArea to place a popup window in the lower right corner of the primary screen. Normally, this works as it is intented to, but it has some issues when the application is being run on
a computer that is connected to different screens with different resolutions (like a laptop being moved between offices).
a computer that is an RDP server, which is connected to from clients with different resolutions (like a home computer being connected to from different offices).
It looks like the values of WorkingArea is not always up-to-date after having changed the resolution multiple (?) times in one of the above mentioned scenarios, which makes it difficult to correctly position the popup window.
Is there a way to force Screen.PrimaryScreen.WorkingArea to refresh, or is there any other ways of getting the screen resolution in a Windows Forms application that can be used instead?
You could try Pinvoke
[DllImport("user32.dll", SetLastError = false)]
static extern IntPtr GetDesktopWindow();
[DllImport("user32.dll", SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
static extern bool GetWindowRect(IntPtr hWnd, ref RECT lpRect);
RECT scBounds = new RECT();
GetWindowRect(GetDesktopWindow(), ref scBounds);
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 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...
I am trying to disable all visual effects programmatically in a Windows Forms App. Other than an extense list of registry values to be changed, i found this option, but I cant seem to get it to work.
Searched in pinvoke.net and on MSDN for a more specific answer, but I could not find one.
So, heres the data I have:
Info about SPI_SETUIEFFECTS
SPI_SETUIEFFECTS
0x103F
Enables or disables UI effects. Set the pvParam parameter to TRUE to enable all UI effects or FALSE to disable all UI effects.
I need to pass it a bool as the pvParam, which seems to be an exception to the general rule... ok, then I try declaring it as:
[DllImport("user32.dll", SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
static extern bool SystemParametersInfo(uint uiAction, uint uiParam, bool pvParam, uint fWinIni);
now, to call it:
const uint SPI_SETUIEFFECTS = 0x103F;
const uint SPIF_SENDCHANGE = 0x02;
bool result = SystemParametersInfo(SPI_SETUIEFFECTS, 0, false, SPIF_SENDCHANGE);
And it builds, runs... and does nothing. No error, no change. "result" is set to true.
What am I missing here?
This code
[DllImport("user32.dll", SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
static extern bool SystemParametersInfo(
uint uiAction,
uint uiParam,
bool pvParam,
uint fWinIni
);
....
bool enabled = false;
SystemParametersInfo(SPI_SETUIEFFECTS, 0, enabled, 0);
works perfectly. I tested by looking at the drop-down effect of a combo box. When enabled is false then the combo's list appears instantly. When enabled is true then the combo's list slides down.
Most likely you are looking at a control which is not painted by the system and that control is ignoring this option.