Exemplo n.º 1
0
        static public void Protect(IntPtr hWnd)
        {
            bool isProtected = false;

            if (NativeCode.IsWindow(hWnd))
            {
                if (IsDWMEnabled())
                {
                    try
                    {
                        isProtected = NativeCode.SetWindowDisplayAffinity(hWnd, NativeCode.WDA_MONITOR);
                        if (isProtected)
                        {
                            NativeCode.SetProp(hWnd, "protect_affinity", (IntPtr)1);
                        }
                    }
                    catch (System.Exception ex)
                    {
                        //Possibly Windows Vista
                        LogFile.Warn("exception in Protect() while setting affinity:" + ex.Message);
                    }
                }

                if (isProtected == false)
                {
                    try
                    {
                        //Disable DWM, that it cannot be enabled while the application is running
                        NativeCode.DwmEnableComposition(NativeCode.DWM_EC_DISABLECOMPOSITION);
                        dwmDisabled = true;
                    }
                    catch (System.Exception ex)
                    {
                        //Possibly Windows XP...
                        LogFile.Warn("exception in Protect() while disabling DWM:" + ex.Message);
                    }


                    try
                    {
                        if ((NativeCode.GetWindowLong(hWnd, NativeCode.GWL_EXSTYLE) & NativeCode.WS_EX_LAYERED) == 0)
                        {
                            NativeCode.SetProp(hWnd, "protect_layer_added", (IntPtr)1);
                        }
                        NativeCode.SetWindowLong(hWnd, NativeCode.GWL_EXSTYLE, NativeCode.GetWindowLong(hWnd, NativeCode.GWL_EXSTYLE) | NativeCode.WS_EX_LAYERED);
                        NativeCode.SetLayeredWindowAttributes(hWnd, 0, 255, NativeCode.LWA_ALPHA);
                    }
                    catch (System.Exception ex)
                    {
                        LogFile.Error("exception in Protect() while setting layered window:", ex);
                    }


                    try
                    {
                        if (magInit)
                        {
                            IntPtr magWnd = NativeCode.CreateWindow(0, NativeCode.WC_MAGNIFIER, "", NativeCode.WS_CHILD, 0, 0, 1, 1, hWnd, IntPtr.Zero, IntPtr.Zero, IntPtr.Zero);
                            NativeCode.SetProp(hWnd, "protect_mag", (IntPtr)1);
                            NativeCode.SetProp(hWnd, "protect_mag_hwnd", magWnd);
                        }
                    }
                    catch (System.Exception ex)
                    {
                        LogFile.Error("exception in Protect() while creating maginification window:", ex);
                    }


                    try
                    {
                        NativeCode.RedrawWindow(hWnd, IntPtr.Zero, IntPtr.Zero, NativeCode.RDW_FRAME | NativeCode.RDW_UPDATENOW | NativeCode.RDW_INVALIDATE);
                        NativeCode.UpdateWindow(hWnd);
                    }
                    catch (System.Exception ex)
                    {
                        LogFile.Error("exception in Protect() while refreshing window:", ex);
                    }
                }
            }
            else
            {
                LogFile.Error("no vailid window handle forwarded to Protect()");
            }
        }