I have a form which I make click-through using the following function calls:
SetWindowLong(Handle, GWL_EXSTYLE, (IntPtr)(GetWindowLong(Handle, GWL_EXSTYLE) ^ WS_EX_LAYERED ^ WS_EX_TRANSPARENT));
SetLayeredWindowAttributes(Handle, 0, 0xFF, LWA_ALPHA);
This works fine, however when I try to fade that window using the System.Windows.Forms.Form.Opacity property I get the following exception:
System.ComponentModel.Win32Exception (0x80004005): The parameter is not valid
at System.Windows.Forms.Form.UpdateLayered()
at System.Windows.Forms.Form.set_Opacity(Double value)
How can I achieve both things at the same time?
The following works on a windows form
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
namespace WindowsFormsApplication30
{
public partial class Form1 : Form
{
[DllImport("user32.dll", SetLastError = true)]
static extern System.UInt32 GetWindowLong(IntPtr hWnd, int nIndex);
[DllImport("user32.dll", EntryPoint = "SetWindowLong")]
private static extern int SetWindowLong32(IntPtr hWnd, int nIndex, uint dwNewLong);
[DllImport("user32.dll", CharSet = CharSet.Ansi, SetLastError = true)]
static extern int SetLayeredWindowAttributes(IntPtr hWnd, int crKey, byte bAlpha, uint dwFlags);
public const int GWL_EXSTYLE = -20;
public const uint WS_EX_LAYERED = 0x80000;
public const uint LWA_ALPHA = 0x2;
public const uint LWA_COLORKEY = 0x1;
public Form1()
{
InitializeComponent();
IntPtr Handle = this.Handle;
UInt32 windowLong = GetWindowLong(Handle, GWL_EXSTYLE);
SetWindowLong32(Handle, GWL_EXSTYLE, (uint)(windowLong ^ WS_EX_LAYERED));
SetLayeredWindowAttributes(Handle, 0, 128, LWA_ALPHA);
}
}
}
Related
I have problems while I'm working where I'll pick up my laptop and since the screen is nearly the entire area of the laptops top portion, my hand ends up touching the screen and often minimizing or closing the program I'm working with. To remedy this, I made a small program I'm using to "disable" the touchscreen on my laptop, brought on by windows 10's lack of a disable option outside of device manager (on windows 7, you could just use the mouse settings - on win10 this option is gone).
The program works well by essentially identifying and dropping mouse inputs identified as touches, but has a weird quirk in that a few windows still receive touch inputs, such as Edge and Chrome. Most programs don't have this problem and the program usually correctly drops the touch inputs. It's not a huge deal, but I'm wondering why those windows still receive inputs when others do not.
I have attached the code (it's small), does anyone know why some programs still receive these inputs?
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.Diagnostics;
using System.Windows.Input;
namespace TouchDisable
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
Width = Screen.PrimaryScreen.Bounds.Width;
Height = Screen.PrimaryScreen.Bounds.Height;
_hookID = SetHook(_proc);
}
private void Form1_FormClosed(object sender, FormClosedEventArgs e)
{
UnhookWindowsHookEx(_hookID);
}
//------------------------------------------------------------------------------------------
private static LowLevelMouseProc _proc = HookCallback;
private static IntPtr _hookID = IntPtr.Zero;
private static IntPtr SetHook(LowLevelMouseProc proc)
{
using (Process curProcess = Process.GetCurrentProcess())
using (ProcessModule curModule = curProcess.MainModule)
{
return SetWindowsHookEx(WH_MOUSE_LL, proc,
GetModuleHandle(curModule.ModuleName), 0);
}
}
private delegate IntPtr LowLevelMouseProc(int nCode, IntPtr wParam, IntPtr lParam);
private static IntPtr HookCallback(int nCode, IntPtr wParam, IntPtr lParam)
{
if (IsTouch(lParam))
return new IntPtr(1);
else
return CallNextHookEx(_hookID, nCode, wParam, lParam);
}
private const int WH_MOUSE_LL = 14;
private enum MouseMessages
{
WM_LBUTTONDOWN = 0x0201,
WM_LBUTTONUP = 0x0202,
WM_MOUSEMOVE = 0x0200,
WM_MOUSEWHEEL = 0x020A,
WM_RBUTTONDOWN = 0x0204,
WM_RBUTTONUP = 0x0205
}
[StructLayout(LayoutKind.Sequential)]
private struct POINT
{
public int x;
public int y;
}
[StructLayout(LayoutKind.Sequential)]
private struct MSLLHOOKSTRUCT
{
public POINT pt;
public uint mouseData;
public uint flags;
public uint time;
public IntPtr dwExtraInfo;
}
[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
private static extern IntPtr SetWindowsHookEx(int idHook,
LowLevelMouseProc lpfn, IntPtr hMod, uint dwThreadId);
[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
private static extern bool UnhookWindowsHookEx(IntPtr hhk);
[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
private static extern IntPtr CallNextHookEx(IntPtr hhk, int nCode,
IntPtr wParam, IntPtr lParam);
[DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
private static extern IntPtr GetModuleHandle(string lpModuleName);
//------------------------------------------------------------------------------------------
const uint TOUCH_FLAG = 0xFF515700;
static bool IsTouch(IntPtr lParam)
{
MSLLHOOKSTRUCT hookData = (MSLLHOOKSTRUCT)Marshal.PtrToStructure(lParam,
typeof(MSLLHOOKSTRUCT));
uint extraInfo = (uint)hookData.dwExtraInfo.ToInt32();
if ((extraInfo & TOUCH_FLAG) == TOUCH_FLAG)
return true;
return false;
}
}
}
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
using System.Net;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Text.RegularExpressions;
using System.Drawing.Imaging;
namespace CaptureProcessWindow
{
public partial class Form1 : Form
{
[DllImport("user32.dll", SetLastError = true)]
static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
[DllImport("user32.dll", SetLastError = true)]
public static extern IntPtr SetParent(IntPtr hWndChild, IntPtr hWndNewParent);
[DllImport("user32.dll", SetLastError = true)]
static extern int GetWindowLong(IntPtr hWnd, int nIndex);
[DllImport("user32.dll")]
static extern int SetWindowLong(IntPtr hWnd, int nIndex, int dwNewLong);
[DllImport("user32.dll")]
public static extern long SetWindowPos(IntPtr hwnd, IntPtr hWndInsertAfter, long x, long y, long cx, long cy,
long wFlags);
[DllImport("user32.dll", SetLastError = true)]
static extern bool SetWindowPos(IntPtr hWnd, IntPtr hWndInsertAfter, int X, int Y, int cx, int cy, int uFlags);
[DllImport("user32.dll", CharSet = CharSet.Auto)]
public static extern IntPtr SendMessage(IntPtr hWnd, UInt32 Msg, int wParam, int lParam);
[DllImport("kernel32.dll")]
static extern bool CreateProcess(string lpApplicationName,
string lpCommandLine,
IntPtr lpProcessAttributes,
IntPtr lpThreadAttributes,
bool bInheritHandles,
uint dwCreationFlags,
IntPtr lpEnvironment,
string lpCurrentDirectory,
ref STARTUPINFO lpStartupInfo,
out PROCESS_INFORMATION lpProcessInformation);
public const int WM_SYSCOMMAND = 0x112;
public const int SC_MINIMIZE = 0xf020;
public const int SC_MAXIMIZE = 0xf030;
public struct PROCESS_INFORMATION
{
public IntPtr hProcess;
public IntPtr hThread;
public uint dwProcessId;
public uint dwThreadId;
}
public struct STARTUPINFO
{
public uint cb;
public string lpReserved;
public string lpDesktop;
public string lpTitle;
public uint dwX;
public uint dwY;
public uint dwXSize;
public uint dwYSize;
public uint dwXCountChars;
public uint dwYCountChars;
public uint dwFillAttribute;
public uint dwFlags;
public short wShowWindow;
public short cbReserved2;
public IntPtr lpReserved2;
public IntPtr hStdInput;
public IntPtr hStdOutput;
public IntPtr hStdError;
}
public struct SECURITY_ATTRIBUTES
{
public int length;
public IntPtr lpSecurityDescriptor;
public bool bInheritHandle;
}
public Form1()
{
InitializeComponent();
}
public void CaptureApplication(string _title)
{
string _wndcls = "ConsoleWindowClass";
STARTUPINFO si = new STARTUPINFO();
PROCESS_INFORMATION pi = new PROCESS_INFORMATION();
CreateProcess(_title, null, IntPtr.Zero, IntPtr.Zero, false, 0, IntPtr.Zero, null, ref si, out pi);
IntPtr _wndConsole = IntPtr.Zero;
for (int i = 0; i < 30; i++)
{
_wndConsole = FindWindow(_wndcls, _title);
if (_wndConsole == IntPtr.Zero)
{
System.Threading.Thread.Sleep(10);
continue;
}
break;
}
IntPtr value = SetParent(_wndConsole, this.pictureBox1.Handle);
int style = GetWindowLong(_wndConsole, -16);
style &= -12582913;
SetWindowLong(_wndConsole, -16, style);
SendMessage(_wndConsole, WM_SYSCOMMAND, SC_MAXIMIZE, 0);
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
Process[] processlist = Process.GetProcesses();
List<string> names = new List<string>();
foreach (Process process in processlist)
{
if (process.MainWindowTitle.Contains("Notepad"))
{
CaptureApplication(process.MainWindowTitle);
break;
}
else
{
string t = "";
}
}
}
}
}
When using a break point it stop on the line:
CaptureApplication(process.MainWindowTitle);
But it's not showing the window in the pictureBox.
I didn't check all the processes in the list but i checked like 20 and they are all empty in the MainWindowTitle ""
I checked now the notepad it's in the list in index 112 and it does have mainwindowtitle: MainWindowTitle = "New Text Document (11) - Notepad" but still it's not showing anything in the picturebox.
Maybe there is a way to capture the window of the process/s using the id number ?
Basically I would like to create a C# application (I'm using a standard WPF project opened in visual studio) that can detect when the user has scrolled the mouse wheel once either up or down, and to fire a mouse click at the current mouse screen position.
The Pseudo Code for the program I would give for what I want is
While the program is running
If the program detects a mouse scroll wheel up or scroll wheel down from the user
Perform a Single Left Click at the current mouse screen position
End If
End While
I do not know how to detect the mouse scroll wheel. I am using C# in a WPF Application, I have successfully been able to move the mouse cursor and perform a left click with the below code but I am having trouble figuring out how I can listen for mouse scroll wheel input and perform a function that will send the left mouse click when it receives the input. It will need to work even when the application does not have focus since the mouse clicks are sent to another application. Can anyone point me in the right direction to where I need to go to get this working.
Thank you. Current code is below.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace Clicky_Clicky
{
public partial class MainWindow : Window
{
[System.Runtime.InteropServices.DllImport("user32.dll")]
public static extern void mouse_event(int dwFlags, int dx, int dy, int cButtons, int dwExtraInfo);
[System.Runtime.InteropServices.DllImport("user32.dll")]
static extern bool SetCursorPos(int X, int Y);
public const int MOUSEEVENTF_LEFTDOWN = 0x02;
public const int MOUSEEVENTF_LEFTUP = 0x04;
public const int MOUSEEVENTF_RIGHTDOWN = 0x08;
public const int MOUSEEVENTF_RIGHTUP = 0x10;
public const int WM_MOUSEWHEEL = 0x020A;
public void MouseClick(int x, int y)
{
mouse_event(MOUSEEVENTF_LEFTDOWN, x, y, 0, 0);
mouse_event(MOUSEEVENTF_LEFTUP, x, y, 0, 0);
}
public MainWindow()
{
int x = 1400;//Set Mouse Pos X
int y = 340;//Set Mouse Pos Y
InitializeComponent();
SetCursorPos(x, y); //Move Mouse to position on screen designated by X and Y
MouseClick(x, y); //Perform a mouse click (mouse event down, mouse event up)
}
}
}
EDIT: Witha little more research it looks like the thing I want is a global hook for the mouse but I still have not been able to find a simple way to get what I want.
I was able to get a very simple answer for what I wanted to do using
http://blogs.msdn.com/b/toub/archive/2006/05/03/589468.aspx
Source code:
using Microsoft.Win32;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace Clicky_Clicky
{
public partial class MainWindow : Window
{
private static LowLevelMouseProc _proc = HookCallback;
private static IntPtr _hookID = IntPtr.Zero;
[System.Runtime.InteropServices.DllImport("user32.dll")]
public static extern void mouse_event(int dwFlags, int dx, int dy, int cButtons, int dwExtraInfo);
public MainWindow()
{
int x = 1400;//Set Mouse Pos X
int y = 340;//Set Mouse Pos Y
InitializeComponent();
_hookID = SetHook(_proc);
}
public const int MOUSEEVENTF_LEFTDOWN = 0x02;
public const int MOUSEEVENTF_LEFTUP = 0x04;
private static IntPtr SetHook(LowLevelMouseProc proc)
{
using (Process curProcess = Process.GetCurrentProcess())
using (ProcessModule curModule = curProcess.MainModule)
{
return SetWindowsHookEx(WH_MOUSE_LL, proc,
GetModuleHandle(curModule.ModuleName), 0);
}
}
private delegate IntPtr LowLevelMouseProc(int nCode, IntPtr wParam, IntPtr lParam);
public static void MouseClick(int x, int y)
{
mouse_event(MOUSEEVENTF_LEFTDOWN, x, y, 0, 0);
System.Threading.Thread.Sleep(33);
mouse_event(MOUSEEVENTF_LEFTUP, x, y, 0, 0);
}
private static IntPtr HookCallback(
int nCode, IntPtr wParam, IntPtr lParam)
{
if (nCode >= 0 &&
MouseMessages.WM_MOUSEWHEEL == (MouseMessages)wParam)
{
MSLLHOOKSTRUCT hookStruct = (MSLLHOOKSTRUCT)Marshal.PtrToStructure(lParam, typeof(MSLLHOOKSTRUCT));
Console.WriteLine(hookStruct.pt.x + ", " + hookStruct.pt.y);
MouseClick(hookStruct.pt.x, hookStruct.pt.y);
}
return CallNextHookEx(_hookID, nCode, wParam, lParam);
}
private const int WH_MOUSE_LL = 14;
private enum MouseMessages
{
WM_LBUTTONDOWN = 0x0201,
WM_LBUTTONUP = 0x0202,
WM_MOUSEMOVE = 0x0200,
WM_MOUSEWHEEL = 0x020A,
WM_RBUTTONDOWN = 0x0204,
WM_RBUTTONUP = 0x0205
}
[StructLayout(LayoutKind.Sequential)]
private struct POINT
{
public int x;
public int y;
}
[StructLayout(LayoutKind.Sequential)]
private struct MSLLHOOKSTRUCT
{
public POINT pt;
public uint mouseData;
public uint flags;
public uint time;
public IntPtr dwExtraInfo;
}
[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
private static extern IntPtr SetWindowsHookEx(int idHook,
LowLevelMouseProc lpfn, IntPtr hMod, uint dwThreadId);
[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
private static extern bool UnhookWindowsHookEx(IntPtr hhk);
[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
private static extern IntPtr CallNextHookEx(IntPtr hhk, int nCode,
IntPtr wParam, IntPtr lParam);
[DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
private static extern IntPtr GetModuleHandle(string lpModuleName);
}
}
I want be transparent the console, but I've a compilation error:
Transparency.cs(39,48): error CS0019: Operator '^' cannot be applied to operands of type 'System.IntPtr' and 'int'
using System;
using System.Runtime.InteropServices;
namespace Transparency
{
class Program
{
[DllImport("user32.dll")]
static extern int SetWindowLong(IntPtr hWnd, int nIndex, int dwNewLong);
[DllImport("user32.dll")]
static extern bool SetLayeredWindowAttributes(IntPtr hWnd, uint crKey,byte bAlpha, uint dwFlags);
[DllImport("user32.dll", SetLastError = true)]
internal static extern IntPtr GetWindowLong(IntPtr hWnd, int nIndex);
[DllImport("kernel32.dll", SetLastError = true)]
internal static extern IntPtr GetConsoleWindow();
static void Main(string[] args)
{
int GWL_EXSTYLE = -20;
int WS_EX_LAYERED = 0x80000;
uint LWA_ALPHA = 0x2;
//int LWA_COLORKEY = 0x1;
// Obtain our handle (hWnd)
IntPtr Handle = GetConsoleWindow();
SetWindowLong(Handle, GWL_EXSTYLE, GetWindowLong(Handle, GWL_EXSTYLE) ^ WS_EX_LAYERED);
// Opacity = 0.5 = (255/2)
SetLayeredWindowAttributes(Handle, 0, 128, LWA_ALPHA);
}
}
}
I think you need bitwise or which is the single pipe | as well as changing the return type for GetWindowLong to int. See pinvoke.net.
I've been trying to set the user and password for the IE proxies for a while without having good results, I try with WebProxy, WebClient and now i'm trying with InternetSetOption from Wininnet.dll, the idea behind this is to avoid the input of the user and password each time a user open a browser. Here is the code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Security.Principal;
using System.Runtime.InteropServices;
using System.Net;
using System.ComponentModel;
namespace Proxy.Core
{
public class ProxyImpersonator
{
private const int INTERNET_OPTION_PROXY_USERNAME = 43;
private const int INTERNET_OPTION_PROXY_PASSWORD = 44;
private const int INTERNET_OPEN_TYPE_PRECONFIG = 0; // use registry configuration
private const int INTERNET_OPEN_TYPE_DIRECT = 1; // direct to net
private const int INTERNET_OPEN_TYPE_PROXY = 3; // via named proxy
private const int INTERNET_OPEN_TYPE_PRECONFIG_WITH_NO_AUTOPROXY = 4; // prevent using java/script/INS
[DllImport("wininet.dll", SetLastError = true, CharSet = CharSet.Auto)]
public static extern bool InternetSetOption(IntPtr hInternet, int
dwOption, string lpBuffer, int dwBufferLength);
[DllImport("wininet.dll", SetLastError = true, CharSet = CharSet.Auto)]
static extern IntPtr InternetConnect(
IntPtr hInternet, string lpszServerName, short nServerPort,
string lpszUsername, string lpszPassword, int dwService,
int dwFlags, IntPtr dwContext);
[DllImport("wininet.dll", SetLastError = true, CharSet = CharSet.Auto)]
static extern IntPtr InternetOpen(
string lpszAgent, int dwAccessType, string lpszProxyName,
string lpszProxyBypass, int dwFlags);
public static void Impersonate(Proxy Config)
{
IntPtr hOpen = InternetOpen("Proxy Connection", INTERNET_OPEN_TYPE_PROXY,Config.Address.ToString(),String.Empty, 0);
IntPtr hInternet = InternetConnect(hOpen, Config.Address.URL, short.Parse(Config.Address.Port.ToString()), Config.UserID, Config.UserPwd, 3, 0, IntPtr.Zero);
if (!InternetSetOption(hInternet,INTERNET_OPTION_PROXY_USERNAME,Config.UserID,Config.UserID.Length+1))
{
throw new Win32Exception(Marshal.GetLastWin32Error());
}
if (!InternetSetOption(hInternet, INTERNET_OPTION_PROXY_PASSWORD, Config.UserPwd, Config.UserPwd.Length + 1))
{
throw new Win32Exception(Marshal.GetLastWin32Error());
}
}
}
}