Ejemplo n.º 1
0
            /// <summary>
            ///  Once the classname and style bits have been set, this can be called to register the class.
            /// </summary>
            private unsafe void RegisterClass()
            {
                NativeMethods.WNDCLASS windowClass = new NativeMethods.WNDCLASS();

                string localClassName = _className;

                if (localClassName == null)
                {
                    // If we don't use a hollow brush here, Windows will "pre paint" us with COLOR_WINDOW which
                    // creates a little bit if flicker.  This happens even though we are overriding wm_erasebackgnd.
                    // Make this hollow to avoid all flicker.

                    windowClass.hbrBackground = Gdi32.GetStockObject(Gdi32.StockObject.HOLLOW_BRUSH);
                    windowClass.style         = _classStyle;

                    _defaultWindProc = DefaultWindowProc;
                    localClassName   = "Window." + Convert.ToString((int)_classStyle, 16);
                }
                else
                {
                    // A system defined Window class was specified, get its info

                    if (!UnsafeNativeMethods.GetClassInfoW(NativeMethods.NullHandleRef, _className, ref windowClass))
                    {
                        throw new Win32Exception(Marshal.GetLastWin32Error(), SR.InvalidWndClsName);
                    }

                    localClassName   = _className;
                    _defaultWindProc = windowClass.lpfnWndProc;
                }

                _windowClassName        = GetFullClassName(localClassName);
                _windProc               = new User32.WNDPROC(Callback);
                windowClass.lpfnWndProc = Marshal.GetFunctionPointerForDelegate(_windProc);
                windowClass.hInstance   = Kernel32.GetModuleHandleW(null);

                fixed(char *c = _windowClassName)
                {
                    windowClass.lpszClassName = c;

                    if (UnsafeNativeMethods.RegisterClassW(ref windowClass) == 0)
                    {
                        _windProc = null;
                        throw new Win32Exception();
                    }
                }
            }