情報アイランド

「情報を制する者は世界を制す」をモットーに様々な情報を提供することを目指すブログです。現在はプログラミング関連情報が多めですが、投資関連情報も取り扱っていきたいです。

C#でWIN32API step8 ここまでのまとめ(その1)

step1:ウィンドウの表示

step2:タスクトレイアイコンを表示する

step3:タスクトレイアイコンにコンテキストメニューを追加する

step4:タスクトレイアイコンにバルーンチップを表示する

step5:タスクトレイアイコンにフライアウトウィンドウを追加する(その1)

step6:タスクトレイアイコンにフライアウトウィンドウを追加する(その2)

step7:タスクトレイアイコンにカスタムツールチップを表示する

ここまで作ってきたプログラムのまとめとして、全ソースコードを示します。

private static IntPtr hinstance = Process.GetCurrentProcess().Handle;
private static IntPtr hicon = WIN32.Window.ExtractIcon(hinstance, "up0669.ico", 0);

private static IntPtr hflyoutwnd = IntPtr.Zero;

[STAThread]
public static void Main(string[] args)
{
    WIN32.Window.WNDCLASSEX wndclassex = new WIN32.Window.WNDCLASSEX();
    wndclassex.cbSize = (uint)Marshal.SizeOf(wndclassex);
    wndclassex.style = WIN32.Window.CS.CS_HREDRAW | WIN32.Window.CS.CS_VREDRAW;
    wndclassex.lpfnWndProc = WndProc;
    wndclassex.cbClsExtra = 0;
    wndclassex.cbWndExtra = 0;
    wndclassex.hInstance = hinstance;
    wndclassex.hIcon = hicon;
    wndclassex.hCursor = WIN32.Window.LoadCursor(IntPtr.Zero, WIN32.Window.IDC.IDC_ARROW);
    wndclassex.hbrBackground = IntPtr.Zero;
    wndclassex.lpszMenuName = null;
    wndclassex.lpszClassName = "anttn";
    wndclassex.hIconSm = IntPtr.Zero;

    WIN32.Window.RegisterClassEx(ref wndclassex);

    IntPtr hWnd = WIN32.Window.CreateWindowEx(0, wndclassex.lpszClassName, "∀In++n", WIN32.Window.WS.WS_OVERLAPPEDWINDOW, WIN32.Window.CW_USEDEFAULT, WIN32.Window.CW_USEDEFAULT, 800, 600, IntPtr.Zero, IntPtr.Zero, wndclassex.hInstance, IntPtr.Zero);

    WIN32.Window.ShowWindow(hWnd, WIN32.Window.SW.SW_SHOW);
    WIN32.Window.UpdateWindow(hWnd);

    wndclassex.lpfnWndProc = FlyoutWndProc;
    wndclassex.hIcon = IntPtr.Zero;
    wndclassex.lpszClassName = "anttnFlyout";

    WIN32.Window.RegisterClassEx(ref wndclassex);

    WIN32.Window.MSG msg = new WIN32.Window.MSG();
    while (WIN32.Window.GetMessage(out msg, IntPtr.Zero, 0, 0) != 0)
    {
        WIN32.Window.TranslateMessage(ref msg);
        WIN32.Window.DispatchMessage(ref msg);
    }
}

private static IntPtr WndProc(IntPtr hWnd, uint uMsg, IntPtr wParam, IntPtr lParam)
{
    switch (uMsg)
    {
        case WIN32.Window.WM_CREATE:
            {
                WIN32.NotifyIcon.NOTIFYICONDATA notifyicondata = new WIN32.NotifyIcon.NOTIFYICONDATA();
                notifyicondata.cbSize = (uint)Marshal.SizeOf(notifyicondata);
                notifyicondata.hWnd = hWnd;
                notifyicondata.uID = 0;
                notifyicondata.uFlags = WIN32.NotifyIcon.NIF.NIF_MESSAGE | WIN32.NotifyIcon.NIF.NIF_ICON | WIN32.NotifyIcon.NIF.NIF_TIP;
                notifyicondata.uCallbackMessage = WIN32.Window.WM_APP;
                notifyicondata.hIcon = hicon;
                notifyicondata.szTip = "∀In++n 0.0.0.0";

                WIN32.NotifyIcon.Shell_NotifyIcon(WIN32.NotifyIcon.NIM.NIM_ADD, ref notifyicondata);

                notifyicondata.uVersion = WIN32.NotifyIcon.NOTIFYICON_VERSION_4;

                WIN32.NotifyIcon.Shell_NotifyIcon(WIN32.NotifyIcon.NIM.NIM_SETVERSION, ref notifyicondata);

                break;
            }
        case WIN32.Window.WM_DESTROY:
            {
                WIN32.NotifyIcon.NOTIFYICONDATA notifyicondata = new WIN32.NotifyIcon.NOTIFYICONDATA();
                notifyicondata.cbSize = (uint)Marshal.SizeOf(notifyicondata);
                notifyicondata.hWnd = hWnd;
                notifyicondata.uID = 0;

                WIN32.NotifyIcon.Shell_NotifyIcon(WIN32.NotifyIcon.NIM.NIM_DELETE, ref notifyicondata);

                WIN32.Window.PostQuitMessage(0);

                break;
            }
        case WIN32.Window.WM_COMMAND:
            switch ((uint)WIN32.Common.LOWORD(wParam))
            {
                case 0:
                    WIN32.NotifyIcon.NOTIFYICONDATA notifyicondata = new WIN32.NotifyIcon.NOTIFYICONDATA();
                    notifyicondata.cbSize = (uint)Marshal.SizeOf(notifyicondata);
                    notifyicondata.hWnd = hWnd;
                    notifyicondata.uID = 0;
                    notifyicondata.uFlags = WIN32.NotifyIcon.NIF.NIF_INFO;
                    notifyicondata.szInfo = "バoル?ー[ン?チ`ッbプvをd表\uinput2示|し?EてAい?‘まUす?E。B";
                    notifyicondata.szInfoTitle = "バoル?ー[ン?チ`ッbプvをd表\uinput2示|す?Eるe";
                    notifyicondata.dwInfoFlags = WIN32.NotifyIcon.NIIF.NIIF_INFO | WIN32.NotifyIcon.NIIF.NIIF_RESPECT_QUIET_TIME;

                    WIN32.NotifyIcon.Shell_NotifyIcon(WIN32.NotifyIcon.NIM.NIM_MODIFY, ref notifyicondata);

                    break;
                case 1:
                    WIN32.Window.DestroyWindow(hWnd);

                    break;
            }
            break;
        case WIN32.Window.WM_APP:
            switch ((uint)WIN32.Common.LOWORD(lParam))
            {
                case WIN32.Window.WM_CONTEXTMENU:
                    WIN32.Window.SetForegroundWindow(hWnd);

                    IntPtr hMenu = WIN32.Menu.CreatePopupMenu();

                    WIN32.Menu.MENUITEMINFO menuiteminfo = new WIN32.Menu.MENUITEMINFO();
                    menuiteminfo.cbSize = (uint)Marshal.SizeOf(menuiteminfo);
                    menuiteminfo.fMask = WIN32.Menu.MIIM.MIIM_STRING | WIN32.Menu.MIIM.MIIM_ID;
                    menuiteminfo.wID = 0;
                    menuiteminfo.dwTypeData = "バoル?ー[ン?チ`ッbプvをd表\uinput2示|す?Eるe";
                    menuiteminfo.cch = (uint)menuiteminfo.dwTypeData.Length;

                    WIN32.Menu.InsertMenuItem(hMenu, 0, true, ref menuiteminfo);

                    menuiteminfo.fMask = WIN32.Menu.MIIM.MIIM_FTYPE;
                    menuiteminfo.fType = WIN32.Menu.MFT.MFT_SEPARATOR;

                    WIN32.Menu.InsertMenuItem(hMenu, 1, true, ref menuiteminfo);

                    menuiteminfo.fMask = WIN32.Menu.MIIM.MIIM_STRING | WIN32.Menu.MIIM.MIIM_ID;
                    menuiteminfo.wID = 1;
                    menuiteminfo.dwTypeData = "終I了1";
                    menuiteminfo.cch = (uint)menuiteminfo.dwTypeData.Length;

                    WIN32.Menu.InsertMenuItem(hMenu, 2, true, ref menuiteminfo);

                    WIN32.Menu.TrackPopupMenuEx(hMenu, WIN32.Menu.TPM.TPM_LEFTALIGN, WIN32.Common.LOWORD(wParam), WIN32.Common.HIWORD(wParam), hWnd, IntPtr.Zero);
                    WIN32.Menu.DestroyMenu(hMenu);

                    break;
                case WIN32.NotifyIcon.NIN_SELECT:
                    WIN32.Window.SetForegroundWindow(hWnd);

                    break;
                case WIN32.NotifyIcon.NIN_BALLOONTIMEOUT:
                    {
                        WIN32.NotifyIcon.NOTIFYICONDATA notifyicondata = new WIN32.NotifyIcon.NOTIFYICONDATA();
                        notifyicondata.cbSize = (uint)Marshal.SizeOf(notifyicondata);
                        notifyicondata.hWnd = hWnd;
                        notifyicondata.uID = 0;
                        notifyicondata.uFlags = WIN32.NotifyIcon.NIF.NIF_SHOWTIP;

                        WIN32.NotifyIcon.Shell_NotifyIcon(WIN32.NotifyIcon.NIM.NIM_MODIFY, ref notifyicondata);

                        break;
                    }
                case WIN32.NotifyIcon.NIN_BALLOONUSERCLICK:
                    {
                        WIN32.NotifyIcon.NOTIFYICONDATA notifyicondata = new WIN32.NotifyIcon.NOTIFYICONDATA();
                        notifyicondata.cbSize = (uint)Marshal.SizeOf(notifyicondata);
                        notifyicondata.hWnd = hWnd;
                        notifyicondata.uID = 0;
                        notifyicondata.uFlags = WIN32.NotifyIcon.NIF.NIF_SHOWTIP;

                        WIN32.NotifyIcon.Shell_NotifyIcon(WIN32.NotifyIcon.NIM.NIM_MODIFY, ref notifyicondata);

                        WIN32.DialogBox.MessageBox(hWnd, "バoル?ー[ン?チ`ッbプvがaクNリ?ッbクNさ3れeまUし?Eた?。B", "∀In++n", WIN32.DialogBox.MessageBoxOptions.Ok | WIN32.DialogBox.MessageBoxOptions.IconInformation);

                        break;
                    }
                case WIN32.NotifyIcon.NIN_POPUPOPEN:
                    WIN32.Window.RECT rectWindow = new WIN32.Window.RECT();
                    rectWindow.left = 0;
                    rectWindow.top = 0;
                    rectWindow.right = 256;
                    rectWindow.bottom = 256;
                    WIN32.Window.AdjustWindowRectEx(ref rectWindow, WIN32.Window.WS.WS_POPUP | WIN32.Window.WS.WS_THICKFRAME, false, WIN32.Window.WS_EX.WS_EX_TOOLWINDOW);

                    WIN32.Window.RECT rectNotifyIcon = new WIN32.Window.RECT();
                    WIN32.NotifyIcon.NOTIFYICONIDENTIFIER notifyiconidentifier = new WIN32.NotifyIcon.NOTIFYICONIDENTIFIER();
                    notifyiconidentifier.cbSize = (uint)Marshal.SizeOf(notifyiconidentifier);
                    notifyiconidentifier.hWnd = hWnd;
                    notifyiconidentifier.uID = 0;
                    WIN32.NotifyIcon.Shell_NotifyIconGetRect(ref notifyiconidentifier, out rectNotifyIcon);

                    WIN32.Window.POINT point = new WIN32.Window.POINT();
                    point.x = (rectNotifyIcon.left + rectNotifyIcon.right) / 2;
                    point.y = (rectNotifyIcon.top + rectNotifyIcon.bottom) / 2;

                    WIN32.Window.SIZE size = new WIN32.Window.SIZE();
                    size.cx = rectWindow.right - rectWindow.left;
                    size.cy = rectWindow.bottom - rectWindow.top;

                    WIN32.Window.CalculatePopupWindowPosition(ref point, ref size, WIN32.Menu.TPM.TPM_CENTERALIGN | WIN32.Menu.TPM.TPM_VCENTERALIGN | WIN32.Menu.TPM.TPM_VERTICAL | WIN32.Menu.TPM.TPM_WORKAREA, ref rectNotifyIcon, out rectWindow);

                    IntPtr hFlyoutWnd = hflyoutwnd = WIN32.Window.CreateWindowEx(WIN32.Window.WS_EX.WS_EX_TOOLWINDOW, "anttnFlyout", null, WIN32.Window.WS.WS_POPUP | WIN32.Window.WS.WS_THICKFRAME, rectWindow.left, rectWindow.top, rectWindow.right - rectWindow.left, rectWindow.bottom - rectWindow.top, IntPtr.Zero, IntPtr.Zero, hinstance, IntPtr.Zero);

                    WIN32.Window.ShowWindow(hFlyoutWnd, WIN32.Window.SW.SW_SHOW);
                    WIN32.Window.UpdateWindow(hFlyoutWnd);
                    WIN32.Window.SetForegroundWindow(hFlyoutWnd);

                    break;
                case WIN32.NotifyIcon.NIN_POPUPCLOSE:
                    WIN32.Window.DestroyWindow(hflyoutwnd);

                    break;
            }
            break;
        default:
            return WIN32.Window.DefWindowProc(hWnd, uMsg, wParam, lParam);
    }

    return IntPtr.Zero;
}

private static IntPtr FlyoutWndProc(IntPtr hWnd, uint uMsg, IntPtr wParam, IntPtr lParam)
{
    switch (uMsg)
    {
        case WIN32.Window.WM_ACTIVATE:
            if (WIN32.Common.LOWORD(wParam) == WIN32.Window.WA_INACTIVE)
                WIN32.Window.DestroyWindow(hWnd);
            break;
        default:
            return WIN32.Window.DefWindowProc(hWnd, uMsg, wParam, lParam);
    }

    return IntPtr.Zero;
}
using System;
using System.Runtime.InteropServices;

namespace Anttn.WIN32
{
    public static class Common
    {
        [DllImport("user32.dll")]
        public static extern uint GetDoubleClickTime();

        [DllImport("user32.dll")]
        public static extern uint SetTimer(IntPtr hWnd, uint nIDEvent, uint uElapse, TimerProc lpTimerFunc);

        [DllImport("user32.dll")]
        public static extern bool KillTimer(IntPtr hWnd, uint uIDEvent);

        public delegate void TimerProc(IntPtr hWnd, uint uMsg, uint nIDEvent, uint dwTime);

        public static int LOWORD(IntPtr dwValue)
        {
            return (dwValue.ToInt32() & 0xFFFF);
        }

        public static int HIWORD(IntPtr dwValue)
        {
            return (dwValue.ToInt32() >> 16) & 0xFFFF;
        }
    }

    public static partial class Window
    {
        [DllImport("user32.dll")]
        public static extern IntPtr LoadCursor(IntPtr hInstance, IDC cursor);

        [DllImport("shell32.dll")]
        public static extern IntPtr ExtractIcon(IntPtr hInst, string lpszExeFileName, uint nIconIndex);

        [DllImport("user32.dll")]
        [return: MarshalAs(UnmanagedType.U2)]
        public static extern short RegisterClassEx([In] ref WNDCLASSEX lpwcx);

        [DllImport("user32.dll", CharSet = CharSet.Auto)]
        public static extern IntPtr CreateWindowEx(WS_EX dwExStyle, string lpClassName, string lpWindowName, WS dwStyle, int x, int y, int nWidth, int nHeight, IntPtr hWndParent, IntPtr hMenu, IntPtr hInstance, IntPtr lpParam);

        [DllImport("user32.dll")]
        public static extern bool ShowWindow(IntPtr hWnd, SW nCmdShow);

        [DllImport("user32.dll")]
        public static extern bool UpdateWindow(IntPtr hWnd);

        [DllImport("user32.dll")]
        public static extern int GetMessage(out MSG lpMsg, IntPtr hWnd, uint wMsgFilterMin, uint wMsgFilterMax);

        [DllImport("user32.dll")]
        public static extern int TranslateMessage([In] ref MSG lpMsg);

        [DllImport("user32.dll")]
        public static extern IntPtr DispatchMessage([In] ref MSG lpMsg);

        [DllImport("user32.dll")]
        public static extern void PostQuitMessage(int nExitCode);

        [DllImport("user32.dll")]
        public static extern IntPtr DefWindowProc(IntPtr hWnd, uint uMsg, IntPtr wParam, IntPtr lParam);

        [DllImport("user32.dll")]
        public static extern bool DestroyWindow(IntPtr hWnd);

        [DllImport("user32.dll")]
        public static extern bool SetForegroundWindow(IntPtr hWnd);

        [DllImport("user32.dll")]
        public static extern bool AdjustWindowRectEx([In, Out] ref RECT lpRect, WS dwStyle, bool bMenu, WS_EX dwExStyle);

        [DllImport("user32.dll")]
        public static extern bool CalculatePopupWindowPosition([In] ref POINT anchorPoint, [In] ref SIZE windowSize, Menu.TPM flags, [In] ref RECT excludeRect, out RECT popupWindowPosition);

        [DllImport("user32.dll")]
        public static extern bool IsWindowVisible(IntPtr hWnd);

        public delegate IntPtr WNDPROC(IntPtr hWnd, uint uMsg, IntPtr wParam, IntPtr lParam);

        [StructLayout(LayoutKind.Sequential)]
        public struct WNDCLASSEX
        {
            public uint cbSize;
            public CS style;
            public WNDPROC lpfnWndProc;
            public int cbClsExtra;
            public int cbWndExtra;
            public IntPtr hInstance;
            public IntPtr hIcon;
            public IntPtr hCursor;
            public IntPtr hbrBackground;
            public string lpszMenuName;
            public string lpszClassName;
            public IntPtr hIconSm;
        }

        [StructLayout(LayoutKind.Sequential)]
        public struct MSG
        {
            public IntPtr hwnd;
            public uint message;
            public IntPtr wParam;
            public IntPtr lParam;
            public uint time;
            public POINT pt;
        }

        [StructLayout(LayoutKind.Sequential)]
        public struct POINT
        {
            public int x;
            public int y;
        }

        [StructLayout(LayoutKind.Sequential)]
        public struct SIZE
        {
            public int cx;
            public int cy;
        }

        [StructLayout(LayoutKind.Sequential)]
        public struct RECT
        {
            public int left;
            public int top;
            public int right;
            public int bottom;
        }

        public enum IDC : uint
        {
            IDC_APPSTARTING = 0x7f8a,
            IDC_ARROW = 0x7f00,
            IDC_CROSS = 0x7f03,
            IDC_HAND = 0x7f89,
            IDC_HELP = 0x7f8b,
            IDC_IBEAM = 0x7f01,
            IDC_ICON = 0x7f81,
            IDC_NO = 0x7f88,
            IDC_SIZE = 0x7f80,
            IDC_SIZEALL = 0x7f86,
            IDC_SIZENESW = 0x7f83,
            IDC_SIZENS = 0x7f85,
            IDC_SIZENWSE = 0x7f82,
            IDC_SIZEWE = 0x7f84,
            IDC_UPARROW = 0x7f04,
            IDC_WAIT = 0x7f02
        }

        [Flags]
        public enum CS : uint
        {
            CS_VREDRAW = 0x0001,
            CS_HREDRAW = 0x0002,
            CS_DBLCLKS = 0x0008,
            CS_OWNDC = 0x0020,
            CS_CLASSDC = 0x0040,
            CS_PARENTDC = 0x0080,
            CS_NOCLOSE = 0x0200,
            CS_SAVEBITS = 0x0800,
            CS_BYTEALIGNCLIENT = 0x1000,
            CS_BYTEALIGNWINDOW = 0x2000,
            CS_GLOBALCLASS = 0x4000,
            CS_IME = 0x00010000,
            CS_DROPSHADOW = 0x00020000
        }

        [Flags]
        public enum WS : uint
        {
            WS_OVERLAPPED = 0x00000000,
            WS_POPUP = 0x80000000,
            WS_CHILD = 0x40000000,
            WS_MINIMIZE = 0x20000000,
            WS_VISIBLE = 0x10000000,
            WS_DISABLED = 0x08000000,
            WS_CLIPSIBLINGS = 0x04000000,
            WS_CLIPCHILDREN = 0x02000000,
            WS_MAXIMIZE = 0x01000000,
            WS_CAPTION = 0x00C00000,
            WS_BORDER = 0x00800000,
            WS_DLGFRAME = 0x00400000,
            WS_VSCROLL = 0x00200000,
            WS_HSCROLL = 0x00100000,
            WS_SYSMENU = 0x00080000,
            WS_THICKFRAME = 0x00040000,
            WS_GROUP = 0x00020000,
            WS_TABSTOP = 0x00010000,
            WS_MINIMIZEBOX = 0x00020000,
            WS_MAXIMIZEBOX = 0x00010000,
            WS_TILED = WS_OVERLAPPED,
            WS_ICONIC = WS_MINIMIZE,
            WS_SIZEBOX = WS_THICKFRAME,
            WS_TILEDWINDOW = WS_OVERLAPPEDWINDOW,
            WS_OVERLAPPEDWINDOW = WS_OVERLAPPED | WS_CAPTION | WS_SYSMENU | WS_THICKFRAME | WS_MINIMIZEBOX | WS_MAXIMIZEBOX,
            WS_POPUPWINDOW = WS_POPUP | WS_BORDER | WS_SYSMENU,
            WS_CHILDWINDOW = WS_CHILD
        }

        [Flags]
        public enum WS_EX : uint
        {
            WS_EX_ACCEPTFILES = 0x00000010,
            WS_EX_APPWINDOW = 0x00040000,
            WS_EX_CLIENTEDGE = 0x00000200,
            WS_EX_COMPOSITED = 0x02000000,
            WS_EX_CONTEXTHELP = 0x00000400,
            WS_EX_CONTROLPARENT = 0x00010000,
            WS_EX_DLGMODALFRAME = 0x00000001,
            WS_EX_LAYERED = 0x00080000,
            WS_EX_LAYOUTRTL = 0x00400000,
            WS_EX_LEFT = 0x00000000,
            WS_EX_LEFTSCROLLBAR = 0x00004000,
            WS_EX_LTRREADING = 0x00000000,
            WS_EX_MDICHILD = 0x00000040,
            WS_EX_NOACTIVATE = 0x08000000,
            WS_EX_NOINHERITLAYOUT = 0x00100000,
            WS_EX_NOPARENTNOTIFY = 0x00000004,
            WS_EX_OVERLAPPEDWINDOW = WS_EX_WINDOWEDGE | WS_EX_CLIENTEDGE,
            WS_EX_PALETTEWINDOW = WS_EX_WINDOWEDGE | WS_EX_TOOLWINDOW | WS_EX_TOPMOST,
            WS_EX_RIGHT = 0x00001000,
            WS_EX_RIGHTSCROLLBAR = 0x00000000,
            WS_EX_RTLREADING = 0x00002000,
            WS_EX_STATICEDGE = 0x00020000,
            WS_EX_TOOLWINDOW = 0x00000080,
            WS_EX_TOPMOST = 0x00000008,
            WS_EX_TRANSPARENT = 0x00000020,
            WS_EX_WINDOWEDGE = 0x00000100
        }

        public enum SW : uint
        {
            SW_HIDE = 0,
            SW_SHOWNORMAL = 1,
            SW_NORMAL = 1,
            SW_SHOWMINIMIZED = 2,
            SW_SHOWMAXIMIZED = 3,
            SW_MAXIMIZE = 3,
            SW_SHOWNOACTIVATE = 4,
            SW_SHOW = 5,
            SW_MINIMIZE = 6,
            SW_SHOWMINNOACTIVE = 7,
            SW_SHOWNA = 8,
            SW_RESTORE = 9,
            SW_SHOWDEFAULT = 10,
            SW_FORCEMINIMIZE = 11,
            SW_MAX = 11,
        }

        public const int CW_USEDEFAULT = unchecked((int)0x80000000);

        public const int WA_INACTIVE = 0;
        public const int WA_ACTIVE = 1;
        public const int WA_CLICKACTIVE = 2;

        public const uint WM_CREATE = 0x0001;
        public const uint WM_DESTROY = 0x0002;
        public const uint WM_ACTIVATE = 0x0006;
        public const uint WM_CONTEXTMENU = 0x007B;
        public const uint WM_COMMAND = 0x0111;
        public const uint WM_TIMER = 0x0113;
        public const uint WM_APP = 0x8000;
    }

    public static partial class NotifyIcon
    {
        [DllImport("shell32.dll")]
        public static extern bool Shell_NotifyIcon(NIM dwMessage, [In] ref NOTIFYICONDATA lpdata);

        [DllImport("shell32.dll")]
        public static extern int Shell_NotifyIconGetRect([In] ref NOTIFYICONIDENTIFIER identifier, out Window.RECT iconLocation);

        [StructLayout(LayoutKind.Sequential)]
        public struct NOTIFYICONDATA
        {
            public uint cbSize;
            public IntPtr hWnd;
            public uint uID;
            public NIF uFlags;
            public uint uCallbackMessage;
            public IntPtr hIcon;
            [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 128)]
            public string szTip;
            public NIS dwState;
            public NIS dwStateMask;
            [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 256)]
            public string szInfo;
            public uint uVersion;
            [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 64)]
            public string szInfoTitle;
            public NIIF dwInfoFlags;
            public Guid guidItem;
            public IntPtr hBalloonIcon;
        }

        [StructLayout(LayoutKind.Sequential)]
        public struct NOTIFYICONIDENTIFIER
        {
            public uint cbSize;
            public IntPtr hWnd;
            public uint uID;
            public Guid guidItem;
        }

        [Flags]
        public enum NIF : uint
        {
            NIF_MESSAGE = 0x00000001,
            NIF_ICON = 0x00000002,
            NIF_TIP = 0x00000004,
            NIF_STATE = 0x00000008,
            NIF_INFO = 0x00000010,
            NIF_GUID = 0x00000020,
            NIF_REALTIME = 0x00000040,
            NIF_SHOWTIP = 0x00000080,
        }

        [Flags]
        public enum NIS : uint
        {
            NIS_HIDDEN = 0x00000001,
            NIS_SHAREDICON = 0x00000002,
        }

        [Flags]
        public enum NIIF : uint
        {
            NIIF_NONE = 0x00000000,
            NIIF_INFO = 0x00000001,
            NIIF_WARNING = 0x00000002,
            NIIF_ERROR = 0x00000003,
            NIIF_USER = 0x00000004,
            NIIF_NOSOUND = 0x00000010,
            NIIF_LARGE_ICON = 0x00000020,
            NIIF_RESPECT_QUIET_TIME = 0x00000080,
            NIIF_ICON_MASK = 0x0000000F,
        }

        public enum NIM : uint
        {
            NIM_ADD = 0x00000000,
            NIM_MODIFY = 0x00000001,
            NIM_DELETE = 0x00000002,
            NIM_SETFOCUS = 0x00000003,
            NIM_SETVERSION = 0x00000004,
        }

        public const uint NOTIFYICON_VERSION = 3;
        public const uint NOTIFYICON_VERSION_4 = 4;

        public const uint NIN_SELECT = 0x0400;
        public const uint NIN_BALLOONTIMEOUT = 0x0404;
        public const uint NIN_BALLOONUSERCLICK = 0x0405;
        public const uint NIN_POPUPOPEN = 0x0406;
        public const uint NIN_POPUPCLOSE = 0x0407;
    }

    public static partial class Menu
    {
        [DllImport("user32.dll")]
        public static extern IntPtr CreatePopupMenu();

        [DllImport("user32.dll")]
        public static extern bool InsertMenuItem(IntPtr hMenu, uint uItem, bool fByPosition, [In] ref MENUITEMINFO lpmii);

        [DllImport("user32.dll")]
        public static extern bool TrackPopupMenuEx(IntPtr hmenu, TPM fuFlags, int x, int y, IntPtr hwnd, IntPtr lptpm);

        [DllImport("user32.dll")]
        public static extern bool DestroyMenu(IntPtr hMenu);

        [StructLayout(LayoutKind.Sequential)]
        public struct MENUITEMINFO
        {
            public uint cbSize;
            public MIIM fMask;
            public MFT fType;
            public MFS fState;
            public uint wID;
            public IntPtr hSubMenu;
            public IntPtr hbmpChecked;
            public IntPtr hbmpUnchecked;
            public IntPtr dwItemData;
            public string dwTypeData;
            public uint cch;
            public IntPtr hbmpItem;
        }

        [Flags]
        public enum MIIM : uint
        {
            MIIM_STATE = 0x00000001,
            MIIM_ID = 0x00000002,
            MIIM_SUBMENU = 0x00000004,
            MIIM_CHECKMARKS = 0x00000008,
            MIIM_TYPE = 0x00000010,
            MIIM_DATA = 0x00000020,
            MIIM_STRING = 0x00000040,
            MIIM_BITMAP = 0x00000080,
            MIIM_FTYPE = 0x00000100,
        }

        [Flags]
        public enum MFT : uint
        {
            MFT_STRING = 0x00000000,
            MFT_BITMAP = 0x00000004,
            MFT_MENUBARBREAK = 0x00000020,
            MFT_MENUBREAK = 0x00000040,
            MFT_OWNERDRAW = 0x00000100,
            MFT_RADIOCHECK = 0x00000200,
            MFT_SEPARATOR = 0x00000800,
            MFT_RIGHTORDER = 0x00002000,
            MFT_RIGHTJUSTIFY = 0x00004000,
        }

        [Flags]
        public enum MFS : uint
        {
            MFS_GRAYED = 0x00000003,
            MFS_DISABLED = 0x00000003,
            MFS_CHECKED = 0x00000008,
            MFS_HILITE = 0x00000080,
            MFS_ENABLED = 0x00000000,
            MFS_UNCHECKED = 0x00000000,
            MFS_UNHILITE = 0x00000000,
            MFS_DEFAULT = 0x00001000,
        }

        [Flags]
        public enum TPM : uint
        {
            TPM_LEFTBUTTON = 0x00000000,
            TPM_RECURSE = 0x00000001,
            TPM_RIGHTBUTTON = 0x00000002,
            TPM_LEFTALIGN = 0x00000000,
            TPM_CENTERALIGN = 0x00000004,
            TPM_RIGHTALIGN = 0x00000008,
            TPM_TOPALIGN = 0x00000000,
            TPM_VCENTERALIGN = 0x00000010,
            TPM_BOTTOMALIGN = 0x00000020,
            TPM_HORIZONTAL = 0x00000000,
            TPM_VERTICAL = 0x00000040,
            TPM_NONOTIFY = 0x00000080,
            TPM_RETURNCMD = 0x00000100,
            TPM_HORPOSANIMATION = 0x00000400,
            TPM_HORNEGANIMATION = 0x00000800,
            TPM_VERPOSANIMATION = 0x00001000,
            TPM_VERNEGANIMATION = 0x00002000,
            TPM_NOANIMATION = 0x00004000,
            TPM_LAYOUTRTL = 0x00008000,
            TPM_WORKAREA = 0x00010000,
        }
    }

    public static partial class DialogBox
    {
        [DllImport("user32.dll", CharSet = CharSet.Auto)]
        public static extern MessageBoxResult MessageBox(IntPtr hWnd, string text, string caption, MessageBoxOptions options);

        [Flags]
        public enum MessageBoxOptions : uint
        {
            Ok = 0x000000,
            OkCancel = 0x000001,
            AbortRetryIgnore = 0x000002,
            YesNoCancel = 0x000003,
            YesNo = 0x000004,
            RetryCancel = 0x000005,
            CancelTryContinue = 0x000006,

            IconHand = 0x000010,
            IconQuestion = 0x000020,
            IconExclamation = 0x000030,
            IconAsterisk = 0x000040,
            UserIcon = 0x000080,

            IconWarning = IconExclamation,
            IconError = IconHand,
            IconInformation = IconAsterisk,
            IconStop = IconHand,

            DefButton1 = 0x000000,
            DefButton2 = 0x000100,
            DefButton3 = 0x000200,
            DefButton4 = 0x000300,

            ApplicationModal = 0x000000,
            SystemModal = 0x001000,
            TaskModal = 0x002000,

            Help = 0x004000,
            NoFocus = 0x008000,

            SetForeground = 0x010000,
            DefaultDesktopOnly = 0x020000,
            Topmost = 0x040000,
            Right = 0x080000,
            RTLReading = 0x100000,
        }

        public enum MessageBoxResult : uint
        {
            Ok = 1,
            Cancel,
            Abort,
            Retry,
            Ignore,
            Yes,
            No,
            Close,
            Help,
            TryAgain,
            Continue,
            Timeout = 32000
        }
    }
}
pizyumi
プログラミング歴19年のベテランプログラマー。業務システム全般何でも作れます。現在はWeb系の技術を勉強中。
スポンサーリンク

-C#, WIN32API