Tag Archives: winforms
Monitor for clipboard changes using AddClipboardFormatListener
Microsoft has added a new windows function to help monitor when the data in the clipboard has been changed. The new function is called AddClipboardFormatListener but sadly it is only available for Windows Vista and higher. If you are looking for a method that will work for earlier versions of Windows take a look at Monitor clipboard in C#.
The principle is the same with the older method. We need to add our window to the clipboard format listener list so it can receive the WM_CLIPBOARDUPDATE message.
In order to do that we need to pinvoke the AddClipboardFormatListener
and RemoveClipboardFormatListener
.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | /// <summary> /// Places the given window in the system-maintained clipboard format listener list. /// </summary> [DllImport("user32.dll", SetLastError = true)] [return: MarshalAs(UnmanagedType.Bool)] static extern bool AddClipboardFormatListener(IntPtr hwnd); /// <summary> /// Removes the given window from the system-maintained clipboard format listener list. /// </summary> [DllImport("user32.dll", SetLastError = true)] [return: MarshalAs(UnmanagedType.Bool)] static extern bool RemoveClipboardFormatListener(IntPtr hwnd); /// <summary> /// Sent when the contents of the clipboard have changed. /// </summary> private const int WM_CLIPBOARDUPDATE = 0x031D; |
Posted in C#.
Tagged AddClipboardFormatListener, C#, csharp, RemoveClipboardFormatListener, snippet, winforms, WM_CLIPBOARDUPDATE
How to get your IP in C#
There are two ways of getting your IP address in C#. One of them is by using the methods provided by the .NET framework and the other one by using a third party api.
What is the difference you might ask. The answer is fairly simple. There are two kinds of IP’s, internal IP’s and external IP’s. Sadly the .NET framework methods are not able to retrieve the external IP if the computer is not directly connected to the internet (no router or any other similar devices). Since this limitation exists there is only one option if you wish to get the external IP, and this is by using an online service.
I’ve constructed two methods below. One uses only the .NET framework methods but is not 100% accurate when trying to retrieve the external IP (requires the system to be directly connected to the internet). The second method uses an online service which should have fairly accurate results as long as the service is online and the computer is not behind a proxy.
Method 1
LINQ approach
1 2 3 4 5 6 | IPAddress localAddress = Dns.GetHostEntry(Dns.GetHostName()).AddressList.FirstOrDefault(ip => ip.AddressFamily == AddressFamily.InterNetwork); if (localAddress != null) { // do something with localAddress } |
Non-LINQ approach (for .NET framework versions before 3.5)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | IPAddress localAddress = null; foreach (IPAddress address in Dns.GetHostEntry(Dns.GetHostName()).AddressList) { if (address.AddressFamily == AddressFamily.InterNetwork) { localAddress = address; break; } } if (localAddress != null) { // do something with localAddress } |
Method 2
1 2 3 4 5 6 7 8 9 10 | string ipAddress; using (WebClient wc = new WebClient()) { ipAddress = wc.DownloadString("http://icanhazip.com/"); } if (!string.IsNullOrEmpty(ipAddress)) { // do something with ipAddress } |
Turn monitor on/off in C#
The following snippet will allow you to change your monitor’s state to either off/on or standby mode. Unlike other methods this one works on Windows 7 as well (tested under Windows 7 64bit).
The first step is to include in your class the following code:
1 2 3 4 5 6 7 8 9 10 11 12 | [DllImport("user32.dll")] static extern IntPtr SendMessage(IntPtr hWnd, int Msg, int wParam, int lParam); private int SC_MONITORPOWER = 0xF170; private int WM_SYSCOMMAND = 0x0112; enum MonitorState { ON = -1, OFF = 2, STANDBY = 1 } |
This will allow us to send a WM_SYSCOMMAND
message using SendMessage
to alter the state of the monitor.
Finally, add the method which we will be calling when we want to change the monitor’s state:
1 2 3 4 | public void SetMonitorState(MonitorState state) { SendMessage(this.Handle, WM_SYSCOMMAND, SC_MONITORPOWER, (int)state); } |
Simply call the SetMonitorState
method with the desirable state you want to change your monitor’s state to.
Usage:
1 | SetMonitorState(MonitorState.OFF); |
Keep in mind that the SC_MONITORPOWER
commands supports devices that have power-saving features, so depending on the monitor’s brand/drivers/firmware results might vary.
Posted in C#.
Tagged C#, csharp, HWND_BROADCAST, monitor, SC_MONITORPOWER, snippet, winforms, WM_SYSCOMMAND
Check if user has administartor rights
In numerous cases it is essential to know if the user has administrator rights or not. A good example for this would be when we want to decide if we want to write a value in HKEY_LOCAL_MACHINE
which requires administrator rights or HKEY_CURRENT_USER
which doesn’t.
The following method will return true if the application is running under administration rights or false if its not.
1 2 3 4 5 6 | public static bool IsUserAdministrator() { WindowsIdentity user = WindowsIdentity.GetCurrent(); WindowsPrincipal principal = new WindowsPrincipal(user); return principal.IsInRole(WindowsBuiltInRole.Administrator); } |