miranda/Speak/Speak/Interop/WinApi.cs
2013-05-08 21:39:27 +00:00

357 lines
15 KiB
C#

using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using System.Text;
using Speak.Interfaces;
using Speak.RichEdit;
namespace Speak.Interop
{
internal class WinApi
{
public static int SMTO_ABORTIFHUNG = 0x2;
public static int WM_HTML_GETOBJECT = RegisterWindowMessage("WM_HTML_GETOBJECT");
public const int WM_USER = 0x0400;
public static int GWL_STYLE = -16;
public const int NM_CLICK = -2;
public const int WM_HSCROLL = -16;
public static IntPtr HWND_DESKTOP = IntPtr.Zero;
public const Int32 PSM_CHANGED = WM_USER + 104;
public const Int32 PSN_APPLY = ((0 - 200) - 2);
public const Int32 MK_CONTROL = 0x8;
public const int WM_MOUSEMOVE = 0x200;
public const int WM_LBUTTONDOWN = 0x201;
public const int WM_LBUTTONUP = 0x202;
public const int WM_RBUTTONDOWN = 0x204;
public const int WM_RBUTTONUP = 0x205;
public const int WM_MBUTTONDOWN = 0x207;
public const int WM_MBUTTONUP = 0x208;
public const int WM_LBUTTONDBLCLK = 0x203;
public const int WM_MBUTTONDBLCLK = 0x209;
public const int WM_RBUTTONDBLCLK = 0x206;
public const int WM_KEYUP = 0x101;
public const int WM_KEYDOWN = 0x100;
public const int WM_SYSKEYDOWN = 0x104;
public const int UM_CHECKSTATECHANGE = WM_USER + 100;
public const int WM_VSCROLL = 0x115;
public const int WM_UPDATE = 0x401;
public const int WM_RESIZE = 0x0085;
public const int WM_PAINT = 0x0F;
public const int WM_SETTEXT = 0xC;
public const int WM_NOTIFY = 0x4E;
public const int WM_GETTEXT = 13;
public const int WM_GETTEXTLENGTH = 14;
public const int WM_INITDIALOG = 0x0110;
public const int WM_COMMAND = 0x0111;
public const int WM_SETFOCUS = 0x0007;
public const int WM_DESTROY = 0x2;
public const int BM_GETCHECK = 0x00F0;
public const int BM_SETCHECK = 0x00F1;
public const int BST_UNCHECKED = 0;
public const int BST_CHECKED = 1;
public const int BST_INDETERMINATE = 2;
public const int BST_PUSHED = 4;
public const int BST_FOCUS = 8;
public const int CB_ADDSTRING = 0x0143;
public const int CB_SETCURSEL = 0x014E;
public const int CB_GETCURSEL = 0x0147;
public const int CB_GETLBTEXT = 0x0148;
public const int CB_SELECTSTRING = 0x014D;
public const int CB_GETLBTEXTLEN = 0x0149;
public const int CB_RESETCONTENT = 0x014B;
public const int CBN_SELCHANGE = 1;
public const int LVM_FIRST = 0x1000;
public const int LVM_INSERTITEM = LVM_FIRST + 7;
public const int LVIF_TEXT = 0x0001;
public const int LVIF_IMAGE = 0x0002;
public const int LVIF_PARAM = 0x0004;
public const int LVIF_INDENT = 0x0010;
public const int ILC_MASK = 0x0001;
public const int ILC_COLOR32 = 0x0020;
public const int BN_CLICKED = 0;
public const int EN_CHANGE = 0x300;
public const int STM_SETICON = 368;
public const uint ILD_NORMAL = 0;
public const int COLOR_WINDOW = 5; //http://msdn.microsoft.com/en-us/library/windows/desktop/ms724371(v=vs.85).aspx
public const int COLOR_WINDOWTEXT = 8;
public const int TBM_SETRANGE = WM_USER + 6;
public const int TBM_SETPOS = WM_USER + 5;
public const int TBM_GETPOS = WM_USER;
[DllImport("user32.dll")]
public static extern int GetSysColor(int XAmount);
[DllImport("user32.dll")]
public static extern IntPtr SetWindowLong(IntPtr hWnd, int nIndex, int dwNewLong);
[DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)]
public static extern int SendMessage(IntPtr hWnd, Int32 Msg, IntPtr wParam, IntPtr lParam);
[DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)]
public static extern int SendMessage(IntPtr hWnd, Int32 Msg, Int32 wParam, Int32 lParam);
[DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)]
public static extern int SendMessage(IntPtr hWnd, Int32 Msg, Int64 wParam, Int64 lParam);
[DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)]
public static extern int SendMessage(IntPtr hWnd, Int32 Msg, Int32 wParam, IntPtr lParam);
[DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)]
public static extern int SendMessage(IntPtr hWnd, Int32 Msg, Int32 wParam, ref POINTL lParam);
[DllImport("user32.dll", EntryPoint = "SendMessageA", /*CharSet = CharSet.Auto, */SetLastError = true)]
public static extern int SendMessageA(IntPtr hwnd, int msg, int wParam, IntPtr lParam);
[return: MarshalAs(UnmanagedType.Bool)]
[DllImport("user32.dll", SetLastError = true)]
public static extern bool PostMessage(IntPtr hWnd, UInt32 Msg, Int32 wParam, Int32 lParam);
[return: MarshalAs(UnmanagedType.Bool)]
[DllImport("user32.dll", SetLastError = true)]
public static extern bool PostMessage(IntPtr hWnd, UInt32 Msg, Int32 wParam, IntPtr lParam);
[DllImport("user32.dll")]
public static extern bool ScrollWindow(IntPtr hWnd, int XAmount, int YAmount, IntPtr lpRect, IntPtr wpRect);
[DllImport("user32.dll")]
public static extern bool DestroyIcon(int hIcon);
[DllImport("comctl32.dll")]
public static extern bool ImageList_Destroy(int himl);
[DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)]
public static extern int GetClassName(IntPtr hWnd, StringBuilder lpClassName, int nMaxCount);
[DllImport("user32.dll", EntryPoint = "SendMessageTimeoutA")]
public static extern int SendMessageTimeout(IntPtr hwnd, int msg, int wParam, int lParam, int fuFlags, int uTimeout, out int lpdwResult);
[DllImport("user32.dll", SetLastError = true)]
public static extern int GetWindowLong(IntPtr hWnd, int nIndex);
[DllImport("OLEACC.dll")]
public static extern int ObjectFromLresult(int lResult, ref Guid riid, int wParam, ref IHTMLDocument2 ppvObject);
[DllImport("user32.dll", EntryPoint = "RegisterWindowMessageA")]
public static extern int RegisterWindowMessage(string lpString);
[DllImport("user32.dll")]
public static extern bool ClientToScreen(IntPtr hwnd, ref POINT lpPoint);
[DllImport("user32.dll")]
public static extern bool ScreenToClient(IntPtr hwnd, ref POINT lpPoint);
[DllImport("user32.dll")]
public static extern IntPtr GetDlgItem(IntPtr hDlg, int nIDDlgItem);
[DllImport("user32.dll")]
public static extern Int32 SendDlgItemMessage(IntPtr hDlg, int nIDDlgItem, uint Msg, Int32 wParam, Int32 lParam);
[DllImport("user32.dll")]
public static extern Int32 SendDlgItemMessage(IntPtr hDlg, int nIDDlgItem, uint Msg, Int32 wParam, IntPtr lParam);
[DllImport("user32.dll")]
public static extern bool EnableWindow(IntPtr hWnd, bool bEnable);
[DllImport("user32.dll")]
public static extern bool ShowWindow(IntPtr hWnd, Int32 nCmdShow);
[DllImport("comctl32.dll", SetLastError = true)]
public static extern IntPtr ImageList_Create(int cx, int cy, uint flags, int cInitial, int cGrow);
[DllImport("comctl32.dll", SetLastError = true)]
public static extern Int32 ImageList_ReplaceIcon(IntPtr imageList, Int32 iconIndex, IntPtr hIcon);
[DllImport("comctl32.dll", SetLastError = true)]
public static extern Int32 ImageList_GetIcon(IntPtr imageList, Int32 iconIndex, uint flags);
[DllImport("user32.dll")]
public static extern uint GetMessagePos();
[DllImport("user32.dll")]
public static extern Int32 GetFocus();
[DllImport("user32.dll")]
public static extern Int32 SetFocus(IntPtr hWnd);
[DllImport("user32.dll")]
public static extern IntPtr GetParent(IntPtr hWnd);
[DllImport("user32.dll", SetLastError = true)]
public static extern int MapWindowPoints(IntPtr hwndFrom, IntPtr hwndTo, ref POINT lpPoints, [MarshalAs(UnmanagedType.U4)] int cPoints);
[DllImport("kernel32", CharSet = CharSet.Ansi, ExactSpelling = true, SetLastError = true)]
public static extern IntPtr GetProcAddress(IntPtr hModule, string procName);
[DllImport("user32")]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool EnumChildWindows(IntPtr window, EnumWindowProc callback, IntPtr i);
public delegate bool EnumWindowProc(IntPtr hWnd, IntPtr parameter);
public static IntPtr MakeLParam(int wLow, int wHigh)
{
return (IntPtr)(((short)wHigh << 16) | (wLow & 0xffff));
}
public static List<IntPtr> GetChildWindows(IntPtr parent)
{
List<IntPtr> result = new List<IntPtr>();
GCHandle listHandle = GCHandle.Alloc(result);
try
{
EnumWindowProc childProc = EnumWindow;
EnumChildWindows(parent, childProc, GCHandle.ToIntPtr(listHandle));
}
finally
{
if (listHandle.IsAllocated)
listHandle.Free();
}
return result;
}
public static string GetWindowName(IntPtr window)
{
StringBuilder className = new StringBuilder(100);
GetClassName(window, className, className.Capacity);
return className.ToString();
}
private static bool EnumWindow(IntPtr handle, IntPtr pointer)
{
GCHandle gch = GCHandle.FromIntPtr(pointer);
List<IntPtr> list = gch.Target as List<IntPtr>;
if (list == null)
{
throw new InvalidCastException("GCHandle Target could not be cast as List<IntPtr>");
}
list.Add(handle);
// You can modify this to check to see if you want to cancel the operation, then return a null here
return true;
}
public static IntPtr GetLogWindow(IntPtr parent, string className)
{
List<IntPtr> childs = GetChildWindows(parent);
for (int i = 0, iCount = childs.Count; i < iCount; i++)
{
#if DEBUG
System.Diagnostics.Debug.WriteLine("[" + childs[i].ToString("X") + "] " + GetWindowName(childs[i]));
#endif
if (GetWindowName(childs[i]).StartsWith(className))
return childs[i];
}
return IntPtr.Zero;
}
// sciver 2.8.1.1+ fix
public static IntPtr GetLogWindowByStyles(IntPtr parent, string className)
{
List<IntPtr> childs = GetChildWindows(parent);
for (int i = 0, iCount = childs.Count; i < iCount; i++)
{
if (GetWindowName(childs[i]).StartsWith(className))
{
Int32 exStyles = GetWindowLong(childs[i], GWL_STYLE);
if (exStyles != 0 && ((exStyles & REConstants.ES_READONLY) != 0) && ((exStyles & REConstants.ES_AUTOVSCROLL) != 0))
return childs[i];
}
}
return IntPtr.Zero;
}
}
[StructLayout(LayoutKind.Sequential)]
public struct NMHDR
{
public IntPtr handleFrom;
public Int32 idFrom;
public Int32 code;
}
public struct WindowShowStyle
{
/// <summary>Hides the window and activates another window.</summary>
public const Int32 SW_HIDE = 0;
/// <summary>Activates and displays a window. If the window is minimized
/// or maximized, the system restores it to its original size and
/// position. An application should specify this flag when displaying
/// the window for the first time.</summary>
public const Int32 SW_SHOWNORMAL = 1;
/// <summary>Activates the window and displays it as a minimized window.</summary>
public const Int32 SW_SHOWMINIMIZED = 2;
/// <summary>Activates the window and displays it as a maximized window.</summary>
public const Int32 SW_SHOWMAXIMIZED = 3;
/// <summary>Maximizes the specified window.</summary>
public const Int32 SW_MAXIMIZE = 3;
/// <summary>Displays a window in its most recent size and position.
/// This value is similar to "ShowNormal", except the window is not
/// actived.</summary>
public const Int32 SW_SHOWNOACTIVATE = 4;
/// <summary>Activates the window and displays it in its current size
/// and position.</summary>
public const Int32 SW_SHOW = 5;
/// <summary>Minimizes the specified window and activates the next
/// top-level window in the Z order.</summary>
public const Int32 SW_MINIMIZE = 6;
/// <summary>Displays the window as a minimized window. This value is
/// similar to "ShowMinimized", except the window is not activated.</summary>
public const Int32 SW_SHOWMINNOACTIVE = 7;
/// <summary>Displays the window in its current size and position. This
/// value is similar to "Show", except the window is not activated.</summary>
public const Int32 SW_SHOWNA = 8;
/// <summary>Activates and displays the window. If the window is
/// minimized or maximized, the system restores it to its original size
/// and position. An application should specify this flag when restoring
/// a minimized window.</summary>
public const Int32 SW_RESTORE = 9;
/// <summary>Sets the show state based on the SW_ value specified in the
/// STARTUPINFO structure passed to the CreateProcess function by the
/// program that started the application.</summary>
/// <remarks>See SW_SHOWDEFAULT</remarks>
public const Int32 ShowDefault = 10;
/// <summary>Windows 2000/XP: Minimizes a window, even if the thread
/// that owns the window is hung. This flag should only be used when
/// minimizing windows from a different thread.</summary>
public const Int32 SW_FORCEMINIMIZE = 11;
}
[ComImport]
[Guid("6d5140c1-7436-11ce-8034-00aa006009fa")]
[InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
interface IServiceProvider
{
void QueryService(ref Guid guidService, ref Guid riid, [MarshalAs(UnmanagedType.Interface)] out object ppvObject);
}
}