Window management in C# - selenium

I am using selenium c# for web automation and I am having a problem arranging windows. See attached image of what am trying to achieve.

After a While, I found a workaround.
Every time a window is opened I get its handle by
[DllImport("user32.dll", SetLastError = true)]
private static extern IntPtr GetForegroundWindow();
and store it.
I get the size of the screen by:
int h = (int)Screen.PrimaryScreen.Bounds.Height;
int w = (int)Screen.PrimaryScreen.Bounds.Width;
I divide the screen and arrange the windows as needed using the function:
[DllImport("user32.dll", SetLastError = true)]
internal static extern bool MoveWindow(IntPtr hWnd, int X, int Y, int nWidth, int nHeight, bool bRepaint);
Results

Related

Silence detection in Kinect V2

I am using winmm.dll to record audio from kinect using below method :
[DllImport("winmm.dll", EntryPoint = "mciSendStringA", ExactSpelling = true, CharSet = CharSet.Ansi, SetLastError = true)]
private static extern int record(string lpstrCommand, string lpstrReturnString, int uReturnLength, int hwndCallback);
For now, I am using Stop button click to stops the recording but I want to stop Recording while detecting silence for sometimes.Because I want to remove "StopRecording" button and Stops the recording if the user doesn't speak for 10 seconds.

Third party app launched from MSI is displayed in background

I have an MSI, written with WiX, that calls a third party application as part of the setup process. I can get the app to execute, however it opens in the background, behind the installer. Is there any way to have the app appear in front of the installer?
The app in question requires elevated privileges, so running it from the Finish dialog is not an option.
Are you using an EXE command? I believe this custom action extension runs programs in the front. If not, you could always write your own.
How To: Run the Installed Application After Setup
I just recently figured the best way to do this. I pieced it together from multiple sources. This is for C# Custom action launching an exe. You can also just launch the exe by a Wix ExeCommand and use the custom action to bring it forward after manually finding the correct process.
[DllImport("user32.dll")]
static extern bool SetWindowPos(IntPtr hWnd, IntPtr hWndInsertAfter, int X, int Y, int cx, int cy, uint uFlags);
[DllImport("user32.dll")]
static extern IntPtr GetTopWindow(IntPtr hWnd);
static readonly IntPtr HWND_TOPMOST = new IntPtr(-1);
static readonly IntPtr HWND_NOTOPMOST = new IntPtr(-2);
const UInt32 SWP_NOSIZE = 0x0001;
const UInt32 SWP_NOMOVE = 0x0002;
const UInt32 SWP_SHOWWINDOW = 0x0040;
[CustomAction]
public static ActionResult BringExeForward(Session session)
{
ProcessStartInfo processInfo = new ProcessStartInfo("Application.exe");
Process bProcess = Process.Start(processInfo);
while (GetTopWindow((IntPtr)null) != bProcess.MainWindowHandle)
{
SetWindowPos(bProcess.MainWindowHandle, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE | SWP_SHOWWINDOW);
}
SetWindowPos(bProcess.MainWindowHandle, HWND_NOTOPMOST, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE | SWP_SHOWWINDOW);
return ActionResult.Success;
}

How to open autocad drawings in vb.net and run scripts?

I'm trying to open all .dwg files in a folder with AutoCad and run a previous written script.
In order to do that, I built the following code:
Dim myapp As New Autodesk.AutoCAD.Interop.AcadApplication
Dim docMgr As AutoCAD.Interop.AcadDocuments = myapp.Documents
docMgr.Open(File.FullName, False)
Can anyone help me understand why it just doesn't work?
First, I was getting that "RPC_E_CALL_REJECTED" error. But I inserted a handle to read the isQuiescent state and now I just run .Open when AutoCad is idle but stills, Visual Studio is returning me an error without any number.
The COM Details Exceptions is: -2147418113
Does anybody know the correct way to simple open an existing file and run a script in AutoCad? I don't know, I just followed the AutoDest instruction at their webpage and I thought that it would be easy :(
I always implement IMessageFilter when working with AutoCAD Interop objects based on Kean Walmsley's suggestion here: http://adndevblog.typepad.com/autocad/2012/05/cannot-instantiate-autocad-2010-from-an-external-net-application-after-installing-update-1.html
// IMessageFilter Interface
[ComImport, InterfaceType(ComInterfaceType.InterfaceIsIUnknown), Guid("00000016-0000-0000-C000-000000000046")]
public interface IMessageFilter
{
[PreserveSig]
int HandleInComingCall(int dwCallType, IntPtr hTaskCaller, int dwTickCount, IntPtr lpInterfaceInfo);
[PreserveSig]
int RetryRejectedCall(IntPtr hTaskCallee, int dwTickCount, int dwRejectType);
[PreserveSig]
int MessagePending(IntPtr hTaskCallee, int dwTickCount, int dwPendingType);
}
For a form, it would look something like this:
public partial class Form1 : Form, IMessageFilter
{
public Form1()
{
InitializeComponent();
CoRegisterMessageFilter(this, null);
}
int IMessageFilter.HandleInComingCall(int dwCallType, IntPtr hTaskCaller, int dwTickCount, IntPtr lpInterfaceInfo)
{
// SERVERCALL_ISHANDLED
return 0;
}
int IMessageFilter.RetryRejectedCall(IntPtr hTaskCallee, int dwTickCount, int dwRejectType)
{
// Retry in a second
return 1000;
}
int IMessageFilter.MessagePending(IntPtr hTaskCallee, int dwTickCount, int dwPendingType)
{
// PENDINGMSG_WAITNOPROCESS
return 1;
}
[DllImport("ole32.dll")]
private static extern int CoRegisterMessageFilter(IMessageFilter lpMessageFilter, IMessageFilter lplpMessageFilter);
...
}
It seems to be basically telling those COM exceptions to just shut up and wait for the AutoCAD COM object to be ready.

Maximizing windows underneath my form

I have a toolbar that is always on the top (topmost = true) but when i maximize other programs the top of their windows is hidden behind it. I want them to maximize BENEATH my toolbar so i can close/minimize them etc... Like an upside down taskbar, literally changing the screen working area...
Is this possible? I have seen it done in "Cairo Shell"
I don't think you achive by use TopMost property of Form. Instead you may need to use SetWindowPos() WIN32 API call
[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
static extern bool SetWindowPos(IntPtr hWnd, IntPtr hWndInsertAfter, int X, int Y, int cx, int cy, SetWindowPosFlags uFlags);
static readonly IntPtr HWND_TOPMOST = new IntPtr(-1);
If you want to reserve an area on the desktop for your program and make all other programs not use that area when maximized, then you have to register your app to Window's appbar list with the API SHAppBarMessage
I havent found any good code to do this in .Net but if you google it maybe you'll be lucky.
Here are one:
http://www.tek-tips.com/viewthread.cfm?qid=1202570&page=1

CF TreeView - Selected Item when control loses focus

I know that "HideSelection" property is missing in CF.
But i still need to handle situation when an item is selected that it remains selected (greyed) even when the control looses focus.
I have tried using this peace of code, but with no success. I get an exception in GetFocus() method and i dont know what im doing wrong.
Any help is much appreciated !
[DllImport("User32.dll")]
static extern IntPtr GetFocus();
[DllImport("User32.dll")]
static extern Int32 GetWindowLong(IntPtr hwnd, Int32 integer1);
[DllImport("User32.dll")]
static extern IntPtr SetWindowLong(IntPtr hwnd, Int32 integer1, int integer2);
private bool doneOnce;
protected override void OnGotFocus(System.EventArgs e)
{
base.OnGotFocus(e);
if (this.itemsTreeView != null)
{
this.itemsTreeView.Focus();
if (doneOnce == false)
{
doneOnce = true;
IntPtr hWnd = GetFocus();
Int32 lS = GetWindowLong(hWnd, -16);
lS = lS | 0x20;
SetWindowLong(hWnd, -16, lS);
}
}
}
I have put this code in my "code behind" view that holds TreeView control.
Windows CE uses coredll.dll instead of user32.dll.
Some functions are equal on both platforms, while some are not implemented for Compact Framework. I usually check http://www.pinvoke.net for the declares.