Example #1
0
        public WindowFactory(string existingClassName, string targetClassName,
                             IntPtr srcInstanceHandle, IntPtr targetInstanceHandle,
                             ClassInfoMutator mutator)
        {
            WindowClassExBlittable classInfo;

            if (!User32Methods.GetClassInfoEx(srcInstanceHandle, existingClassName, out classInfo))
            {
                throw new Exception("Class is not registered - " + existingClassName);
            }

            var className   = targetClassName ?? Guid.NewGuid().ToString();
            var ciClassName = Marshal.SystemDefaultCharSize == 1
                ? Marshal.StringToHGlobalAnsi(className)
                : Marshal.StringToHGlobalUni(className);

            classInfo.Size           = (uint)Marshal.SizeOf <WindowClassExBlittable>();
            classInfo.ClassName      = ciClassName;
            classInfo.InstanceHandle = targetInstanceHandle;

            try
            {
                mutator?.Invoke(ref classInfo);

                this.ClassName       = className;
                this.InstanceHandle  = classInfo.InstanceHandle;
                this.m_windowProc    = Marshal.GetDelegateForFunctionPointer <WindowProc>(classInfo.WindowProc);
                classInfo.WindowProc = Marshal.GetFunctionPointerForDelegate <WindowProc>(this.ClassInitializerProc);
                this.RegisterClass(ref classInfo);
            }
            finally {
                Marshal.FreeHGlobal(ciClassName);
            }
        }