internal static WindowClass Create(string className, User32.CS classStyle) { lock (s_wcInternalSyncObject) { WindowClass wc = s_cache; if (className is null) { // If we weren't given a class name, look for a window // that has the exact class style. while (wc != null && (wc._className != null || wc._classStyle != classStyle)) { wc = wc._next; } } else { while (wc != null && !className.Equals(wc._className)) { wc = wc._next; } } if (wc is null) { // Didn't find an existing class, create one and attatch it to // the end of the linked list. wc = new WindowClass(className, classStyle) { _next = s_cache }; s_cache = wc; } return(wc); } }
/// <summary> /// Constructor. /// </summary> /// <param name="className">Name, or default will be generated.</param> /// <param name="moduleInstance">Module to associate with the window. The entry assembly is the default.</param> /// <param name="backgroundBrush">Use (IntPtr)(-1) for no background brush.</param> /// <param name="icon">Use (IntPtr)(-1) for no icon.</param> /// <param name="cursor">Use (IntPtr)(-1) for no cursor.</param> /// <param name="menuName">Menu name, can not set with <paramref name="menuId"/>.</param> /// <param name="menuId">Menu id, can not set with <paramref name="menuName"/>.</param> public unsafe WindowClass( string className = default, IntPtr moduleInstance = default, User32.CS classStyle = User32.CS.HREDRAW | User32.CS.VREDRAW, IntPtr backgroundBrush = default, IntPtr icon = default, IntPtr cursor = default, string menuName = null, int menuId = 0, int classExtraBytes = 0, int windowExtraBytes = 0) { // Handle default values className ??= Guid.NewGuid().ToString(); if (backgroundBrush == default) { backgroundBrush = User32.GetSysColorBrush(COLOR_WINDOW); } else if (backgroundBrush == (IntPtr)(-1)) { backgroundBrush = default; } if (icon == default) { icon = LoadIconW(IntPtr.Zero, (IntPtr)IDI_APPLICATION); } else if (icon == (IntPtr)(-1)) { icon = default; } if (cursor == default) { cursor = User32.LoadCursorW(IntPtr.Zero, (IntPtr)User32.CursorResourceId.IDC_ARROW); } else if (cursor == (IntPtr)(-1)) { cursor = default; } if (moduleInstance == IntPtr.Zero) { Marshal.GetHINSTANCE(Assembly.GetCallingAssembly().Modules.First()); } if (menuId != 0 && menuName != null) { throw new ArgumentException($"Can't set both {nameof(menuName)} and {nameof(menuId)}."); } _windowProcedure = WNDPROC; ModuleInstance = moduleInstance; _className = className; _menuName = menuName ?? string.Empty; _wndClass = new User32.WNDCLASS { style = classStyle, lpfnWndProc = Marshal.GetFunctionPointerForDelegate(_windowProcedure), cbClsExtra = classExtraBytes, cbWndExtra = windowExtraBytes, hInstance = moduleInstance, hIcon = icon, hCursor = cursor, hbrBackground = backgroundBrush, lpszMenuName = (char *)menuId }; }
internal WindowClass(string className, User32.CS classStyle) { _className = className; _classStyle = classStyle; RegisterClass(); }