protected internal bool ClickControl(HasControlInputCmdletBase cmdlet,
IUiElement element,
ClickSettings settings)
{
bool result = false;
if (-1000000 == settings.RelativeX) {
settings.RelativeX = Preferences.ClickOnControlByCoordX;
}
if (-1000000 == settings.RelativeY) {
settings.RelativeY = Preferences.ClickOnControlByCoordY;
}
IUiElement whereToClick =
element;
IUiElement whereTheHandle =
whereToClick;
// 20140312
// if (whereToClick.Current.NativeWindowHandle == 0) {
if (whereToClick.GetCurrent().NativeWindowHandle == 0) {
whereTheHandle =
whereToClick.GetAncestorWithHandle();
// 20140312
// if (whereTheHandle.Current.NativeWindowHandle == 0) {
if (whereTheHandle.GetCurrent().NativeWindowHandle == 0) {
WriteError(
this,
"The handle of this control equals to zero",
"ZeroHandle",
ErrorCategory.InvalidArgument,
true);
// TODO: WriteError(...)
} else {
// WriteVerbose(cmdlet,
// "the control with a handle is " +
// whereTheHandle.Current.Name);
// WriteVerbose(cmdlet,
// "its handle is " +
// whereTheHandle.Current.NativeWindowHandle.ToString());
// WriteVerbose(cmdlet,
// "its rectangle is " +
// whereTheHandle.Current.BoundingRectangle.ToString());
}
}
int x = 0;
int y = 0;
// these x and y are window-related coordinates
if (settings.RelativeX != 0 && settings.RelativeY != 0) {
// 20140312
// x = settings.RelativeX + (int)whereToClick.Current.BoundingRectangle.X;
// y = settings.RelativeY + (int)whereToClick.Current.BoundingRectangle.Y;
x = settings.RelativeX + (int)whereToClick.GetCurrent().BoundingRectangle.X;
y = settings.RelativeY + (int)whereToClick.GetCurrent().BoundingRectangle.Y;
} else {
// these x and y are for the SetCursorPos call
// they are screen coordinates
// 20140312
// x = (int)whereToClick.Current.BoundingRectangle.X +
// ((int)whereToClick.Current.BoundingRectangle.Width / 2); // + 3;
// y = (int)whereToClick.Current.BoundingRectangle.Y +
// ((int)whereToClick.Current.BoundingRectangle.Height / 2); // + 3;
x = (int)whereToClick.GetCurrent().BoundingRectangle.X +
((int)whereToClick.GetCurrent().BoundingRectangle.Width / 2); // + 3;
y = (int)whereToClick.GetCurrent().BoundingRectangle.Y +
((int)whereToClick.GetCurrent().BoundingRectangle.Height / 2); // + 3;
}
// if the -X and -Y paramters are supplied (only for SetCursorPos)
// PostMessage's (click) second parameter
uint uDown = 0;
uint uUp = 0;
// these relative coordinates for SendMessage/PostMessage
// 20140312
// settings.RelativeX = x - (int)whereTheHandle.Current.BoundingRectangle.X;
// settings.RelativeY = y - (int)whereTheHandle.Current.BoundingRectangle.Y;
settings.RelativeX = x - (int)whereTheHandle.GetCurrent().BoundingRectangle.X;
settings.RelativeY = y - (int)whereTheHandle.GetCurrent().BoundingRectangle.Y;
// PostMessage's (click) third and fourth paramters (the third'll be reasigned later)
IntPtr wParamDown = IntPtr.Zero;
IntPtr wParamUp = IntPtr.Zero;
var lParam =
new IntPtr(((new IntPtr(settings.RelativeX)).ToInt32() & 0xFFFF) +
(((new IntPtr(settings.RelativeY)).ToInt32() & 0xFFFF) << 16));
/*
IntPtr lParam =
new IntPtr(((new IntPtr(settings.RelativeX)).ToInt32() & 0xFFFF) +
(((new IntPtr(settings.RelativeY)).ToInt32() & 0xFFFF) << 16));
*/
// PostMessage's (keydown/keyup) fourth parameter
const uint uCtrlDown = 0x401D;
const uint uCtrlUp = 0xC01D;
const uint uShiftDown = 0x402A;
const uint uShiftUp = 0xC02A;
IntPtr lParamKeyDown = IntPtr.Zero;
IntPtr lParamKeyUp = IntPtr.Zero;
if (settings.Ctrl) {
lParamKeyDown =
new IntPtr(((new IntPtr(0x0001)).ToInt32() & 0xFFFF) +
(((new IntPtr(uCtrlDown)).ToInt32() & 0xFFFF) << 16));
lParamKeyUp =
new IntPtr(((new IntPtr(0x0001)).ToInt32() & 0xFFFF) +
(((new IntPtr(uCtrlUp)).ToInt32() & 0xFFFF) << 16));
}
if (settings.Shift) {
lParamKeyDown =
new IntPtr(((new IntPtr(0x0001)).ToInt32() & 0xFFFF) +
(((new IntPtr(uShiftDown)).ToInt32() & 0xFFFF) << 16));
lParamKeyUp =
new IntPtr(((new IntPtr(0x0001)).ToInt32() & 0xFFFF) +
(((new IntPtr(uShiftUp)).ToInt32() & 0xFFFF) << 16));
}
// PostMessage's (activate) third parameter
uint ulAct = 0;
uint uhAct = 0;
uint mask = 0;
if (settings.Ctrl) {
mask |= NativeMethods.MK_CONTROL;
}
if (settings.Shift) {
mask |= NativeMethods.MK_SHIFT;
}
if (settings.RightClick && !settings.DoubleClick) {
uhAct = uDown = NativeMethods.WM_RBUTTONDOWN;
uUp = NativeMethods.WM_RBUTTONUP;
wParamDown = new IntPtr(NativeMethods.MK_RBUTTON | mask);
wParamUp = new IntPtr(mask);
ulAct = NativeMethods.MK_RBUTTON;
} else if (settings.RightClick && settings.DoubleClick) {
uhAct = uDown = NativeMethods.WM_RBUTTONDBLCLK;
uUp = NativeMethods.WM_RBUTTONUP;
wParamDown = new IntPtr(NativeMethods.MK_RBUTTON | mask);
wParamUp = new IntPtr(mask);
ulAct = NativeMethods.MK_RBUTTON;
} else if (settings.MidClick && !settings.DoubleClick) {
uhAct = uDown = NativeMethods.WM_MBUTTONDOWN;
uUp = NativeMethods.WM_MBUTTONUP;
wParamDown = new IntPtr(NativeMethods.MK_MBUTTON | mask);
wParamUp = new IntPtr(mask);
ulAct = NativeMethods.MK_MBUTTON;
} else if (settings.MidClick && settings.DoubleClick) {
uhAct = uDown = NativeMethods.WM_MBUTTONDBLCLK;
uUp = NativeMethods.WM_MBUTTONUP;
wParamDown = new IntPtr(NativeMethods.MK_MBUTTON | mask);
wParamUp = new IntPtr(mask);
ulAct = NativeMethods.MK_MBUTTON;
} else if (settings.DoubleClick) {
uhAct = uDown = NativeMethods.WM_LBUTTONDBLCLK;
uUp = NativeMethods.WM_LBUTTONUP;
wParamDown = new IntPtr(NativeMethods.MK_LBUTTON | mask);
wParamUp = new IntPtr(mask);
ulAct = NativeMethods.MK_LBUTTON;
} else {
uhAct = uDown = NativeMethods.WM_LBUTTONDOWN;
uUp = NativeMethods.WM_LBUTTONUP;
wParamDown = new IntPtr(NativeMethods.MK_LBUTTON | mask);
wParamUp = new IntPtr(mask);
ulAct = NativeMethods.MK_LBUTTON;
}
// 20140312
// var handle = new IntPtr(whereTheHandle.Current.NativeWindowHandle);
var handle = new IntPtr(whereTheHandle.GetCurrent().NativeWindowHandle);
/*
IntPtr handle =
new IntPtr(whereTheHandle.Current.NativeWindowHandle);
*/
try {
whereTheHandle.SetFocus();
}
catch { }
bool setCursorPosResult =
NativeMethods.SetCursorPos(x, y);
// WriteVerbose(cmdlet, "SetCursorPos result = " + setCursorPosResult.ToString());
Thread.Sleep(Preferences.OnClickDelay);
// trying to heal context menu clicks
Process windowProcess =
Process.GetProcessById(
// 20140312
// whereTheHandle.Current.ProcessId);
whereTheHandle.GetCurrent().ProcessId);
if (windowProcess != null) {
IntPtr mainWindow =
windowProcess.MainWindowHandle;
if (mainWindow != IntPtr.Zero) {
var lParam2 =
new IntPtr(((new IntPtr(ulAct)).ToInt32() & 0xFFFF) +
(((new IntPtr(uhAct)).ToInt32() & 0xFFFF) << 16));
/*
IntPtr lParam2 =
new IntPtr(((new IntPtr(ulAct)).ToInt32() & 0xFFFF) +
(((new IntPtr(uhAct)).ToInt32() & 0xFFFF) << 16));
*/
bool res0 =
NativeMethods.PostMessage1(handle, NativeMethods.WM_MOUSEACTIVATE,
mainWindow, lParam2);
// WriteVerbose(this, "WM_MOUSEACTIVATE is sent");
}
}
if (settings.Ctrl) {
// press the control key
NativeMethods.keybd_event((byte)NativeMethods.VK_LCONTROL, 0x45, NativeMethods.KEYEVENTF_EXTENDEDKEY | 0, 0);
// WriteVerbose(this, " the Control button has been pressed");
}
if (settings.Shift) {
// press the settings.Shift key
NativeMethods.keybd_event((byte)NativeMethods.VK_LSHIFT, 0x45, NativeMethods.KEYEVENTF_EXTENDEDKEY | 0, 0);
// WriteVerbose(this, " the Shift button has been pressed");
}
// // 20120620 for Home Tab
bool res1 = NativeMethods.PostMessage1(handle, uDown, wParamDown, lParam);
int interval = settings.DoubleClickInterval / 2;
if (settings.DoubleClick) {
Thread.Sleep(interval);
}
// MouseMove
if (settings.RightClick || settings.DoubleClick) {
bool resMM = NativeMethods.PostMessage1(handle, NativeMethods.WM_MOUSEMOVE, wParamDown, lParam);
}
// 20131125
if (settings.DoubleClick) {
Thread.Sleep(interval);
}
// // 20120620 for Home Tab
bool res2 = NativeMethods.PostMessage1(handle, uUp, wParamUp, lParam);
if (!settings.inSequence) {
if (settings.Ctrl) {
// release the control key
NativeMethods.keybd_event((byte)NativeMethods.VK_LCONTROL, 0x45, NativeMethods.KEYEVENTF_EXTENDEDKEY | NativeMethods.KEYEVENTF_KEYUP, 0);
// WriteVerbose(this, " the Control button has been released");
}
if (settings.Shift) {
// release the settings.Shift key
NativeMethods.keybd_event((byte)NativeMethods.VK_LSHIFT, 0x45, NativeMethods.KEYEVENTF_EXTENDEDKEY | NativeMethods.KEYEVENTF_KEYUP, 0);
// WriteVerbose(this, " the Shift button has been released");
}
}
// WriteVerbose(cmdlet,
// // // 20120620 for Home Tab
// // 20130312
// "PostMessage " + uDown.ToString() +
// //"SendMessage " + uDown.ToString() +
// " result = " + res1.ToString());
// WriteVerbose(cmdlet,
// // // 20120620 for Home Tab
// // 20130312
// "PostMessage " + uUp.ToString() +
// //"SendMessage " + uUp.ToString() +
// " result = " + res2.ToString());
// if (!res1 && !res2) {
if (res1 && res2) {
result = true;
} else {
result = false;
}
return result;
}