/****************************************************************************
**
** Copyright (C) 2020 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of the Qt VS Tools.
**
** $QT_BEGIN_LICENSE:GPL-EXCEPT$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3 as published by the Free Software
** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
**
** $QT_END_LICENSE$
**
****************************************************************************/
using System;
using System.Runtime.InteropServices;
using System.Text;
namespace QtVsTools
{
public static class NativeAPI
{
public const int GWL_STYLE = -16;
public const int GWL_EXSTYLE = -20;
public const int WS_VISIBLE = 0x10000000;
public const int WM_CLOSE = 0x10;
public const int WM_STYLECHANGED = 0x007D;
public const int WM_GETICON = 0x007F;
public const int WM_SETICON = 0x0080;
public const int ICON_SMALL = 0;
public const int GCL_HICON = -14;
public const int GCL_HICONSM = -34;
public const int SW_HIDE = 0;
public const int SW_SHOWMINNOACTIVE = 7;
public const int SW_RESTORE = 9;
[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool EnumThreadWindows(
uint dwThreadId,
EnumThreadWindowsCallback lpfn,
IntPtr lParam);
public delegate bool EnumThreadWindowsCallback(IntPtr hWnd, IntPtr lParam);
[DllImport("user32.dll", CharSet = CharSet.Unicode, EntryPoint = "GetWindowTextW")]
public static extern int GetWindowText(IntPtr hwnd, StringBuilder lpString, int maxCount);
public static string GetWindowCaption(IntPtr hwnd)
{
var caption = new StringBuilder(256);
if (GetWindowText(hwnd, caption, caption.Capacity) > 0)
return caption.ToString();
else
return string.Empty;
}
[DllImport("user32.dll")]
public static extern IntPtr SetParent(IntPtr hWndChild, IntPtr hWndNewParent);
[DllImport("user32.dll", EntryPoint = "GetWindowLongW", CharSet = CharSet.Unicode)]
public static extern int GetWindowLong(IntPtr hWnd, int nIndex);
[DllImport("user32.dll")]
public static extern int SetWindowLong(IntPtr hWnd, int nIndex, int dwNewLong);
[DllImport("user32.dll", EntryPoint = "GetClassLongW", CharSet = CharSet.Unicode)]
public static extern int GetClassLong(IntPtr hWnd, int nIndex);
[DllImport("user32.dll")]
public static extern bool MoveWindow(
IntPtr Handle,
int x, int y,
int w, int h,
bool repaint);
[DllImport("user32.dll")]
public static extern IntPtr SendMessage(IntPtr hWnd, int Msg, int wParam, int lParam);
[DllImport("user32.dll")]
public static extern IntPtr SendMessage(IntPtr hWnd, int Msg, int wParam, IntPtr lParam);
[DllImport("user32.dll")]
public static extern IntPtr SetForegroundWindow(IntPtr hWnd);
[DllImport("user32.dll")]
public static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);
[DllImport("shell32.dll", CharSet = CharSet.Auto)]
public static extern int SHGetFileInfo(
string pszPath,
int dwFileAttributes,
ref SHFILEINFO psfi,
int cbfileInfo,
SHGFI uFlags);
[DllImport("user32.dll", SetLastError = true)]
public static extern bool DestroyIcon(IntPtr hIcon);
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
public struct SHFILEINFO
{
/// Maximal Length of unmanaged Windows-Path-strings
public const int MAX_PATH = 260;
/// Maximal Length of unmanaged Typename
public const int MAX_TYPE = 80;
public SHFILEINFO(bool dummy = true)
{
hIcon = IntPtr.Zero;
iIcon = 0;
dwAttributes = 0;
szDisplayName = "";
szTypeName = "";
}
public IntPtr hIcon;
public int iIcon;
public uint dwAttributes;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = MAX_PATH)]
public string szDisplayName;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = MAX_TYPE)]
public string szTypeName;
};
[Flags]
public enum SHGFI : int
{
/// get icon
Icon = 0x000000100,
/// get display name
DisplayName = 0x000000200,
/// get type name
TypeName = 0x000000400,
/// get attributes
Attributes = 0x000000800,
/// get icon location
IconLocation = 0x000001000,
/// return exe type
ExeType = 0x000002000,
/// get system icon index
SysIconIndex = 0x000004000,
/// put a link overlay on icon
LinkOverlay = 0x000008000,
/// show icon in selected state
Selected = 0x000010000,
/// get only specified attributes
Attr_Specified = 0x000020000,
/// get large icon
LargeIcon = 0x000000000,
/// get small icon
SmallIcon = 0x000000001,
/// get open icon
OpenIcon = 0x000000002,
/// get shell size icon
ShellIconSize = 0x000000004,
/// pszPath is a pidl
PIDL = 0x000000008,
/// use passed dwFileAttribute
UseFileAttributes = 0x000000010,
/// apply the appropriate overlays
AddOverlays = 0x000000020,
/// Get the index of the overlay in the upper 8 bits of the iIcon
OverlayIndex = 0x000000040,
}
}
}