Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
guyrleech committed Feb 12, 2022
2 parents c31dc05 + c894b81 commit 80a256f
Show file tree
Hide file tree
Showing 2 changed files with 136 additions and 0 deletions.
20 changes: 20 additions & 0 deletions Excel Formatting Macro.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@

' Intended to be called by Quick Access Toolbar button when column with date/time imported from csv
'
' Save in "Personal Macro Workbook" (%AppData%\Excel\XLSTART\Personal.xlsb) to autoload

' @guyrleech 2022/01/26

Public Sub FormatAsDateTime()
Dim myRange As Range
Set myRange = Selection
myRange.NumberFormat = "dd/mm/yyyy hh:mm:ss.000"
End Sub


Public Sub FormatAsTime()
Dim myRange As Range
Set myRange = Selection
myRange.NumberFormat = "hh:mm:ss.000"
End Sub

116 changes: 116 additions & 0 deletions Window Helper Functions.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
<#
.SYNOPSIS
Dot source this module and call the P/Invoke members as required
.EXAMPLE
. 'Z:\Guys Scripts\Scripts\Window Helper Functions.ps1'
[PInvoke.Win32.UserInput]::IdleTime
.NOTES
Modification# History:
@guyrleech 2022-01-19 First standalone public release
#>

## Borrowed from http://stackoverflow.com/a/15846912 and adapted
Add-Type @'
using System;
using System.Text;
using System.Diagnostics;
using System.Runtime.InteropServices;
namespace PInvoke.Win32
{
public static class UserInput
{
[DllImport("user32.dll", SetLastError=true)]
public static extern uint GetWindowThreadProcessId(IntPtr hWnd, out uint lpdwProcessId);
[DllImport("user32.dll")]
public static extern IntPtr GetTopWindow(IntPtr hWnd);
[DllImport("user32.dll", SetLastError = true)]
public static extern IntPtr GetWindow(IntPtr hWnd, uint uCmd);
public enum GetWindow_Cmd : uint {
GW_HWNDFIRST = 0,
GW_HWNDLAST = 1,
GW_HWNDNEXT = 2,
GW_HWNDPREV = 3,
GW_OWNER = 4,
GW_CHILD = 5,
GW_ENABLEDPOPUP = 6
}
[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
public static extern int GetWindowText(IntPtr hWnd, StringBuilder lpString, int nMaxCount);
[DllImport("user32.dll", SetLastError=true, CharSet=CharSet.Auto)]
public static extern int GetWindowTextLength(IntPtr hWnd);
[DllImport("user32.dll")]
public static extern IntPtr FindWindow(IntPtr sClassName, String sAppName);
[DllImport("user32.dll")]
public static extern bool IsWindowVisible(IntPtr hWnd);
[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool GetWindowRect(IntPtr hWnd, out RECT lpRect);
public struct RECT
{
public int Left;
public int Top;
public int Right;
public int Bottom;
}
[DllImport("user32.dll")]
public static extern IntPtr GetForegroundWindow();
[DllImport("user32.dll", SetLastError=true)]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool SetForegroundWindow(IntPtr hWnd);
[DllImport("user32.dll")]
public static extern int GetWindowThreadProcessId(IntPtr hWnd, out int lpdwProcessId);
[DllImport("user32.dll", SetLastError=false)]
private static extern bool GetLastInputInfo(ref LASTINPUTINFO plii);
[StructLayout(LayoutKind.Sequential)]
private struct LASTINPUTINFO
{
public uint cbSize;
public int dwTime;
}
public static DateTime LastInput
{
get
{
DateTime bootTime = DateTime.UtcNow.AddMilliseconds(-Environment.TickCount);
DateTime lastInput = bootTime.AddMilliseconds(LastInputTicks);
return lastInput;
}
}
public static TimeSpan IdleTime
{
get
{
return DateTime.UtcNow.Subtract(LastInput);
}
}
public static int LastInputTicks
{
get
{
LASTINPUTINFO lii = new LASTINPUTINFO();
lii.cbSize = (uint)Marshal.SizeOf(typeof(LASTINPUTINFO));
GetLastInputInfo(ref lii);
return lii.dwTime;
}
}
}
}
'@

0 comments on commit 80a256f

Please sign in to comment.