Exemplo n.º 1
0
 static public void ChangeProtection(IntPtr hWnd)
 {
     try
     {
         if (NativeCode.IsWindow(hWnd))
         {
             if (dwmDisabled == false)
             {
                 if ((int)NativeCode.GetProp(hWnd, "protect_affinity") == 1)
                 {
                     if (IsDWMEnabled() == false)
                     {
                         if ((int)NativeCode.GetProp(hWnd, "protect_change") == 0)  //Make sure that we try it only once!
                         {
                             NativeCode.SetProp(hWnd, "protect_change", (IntPtr)1); //Prop gets never removed, but who cares...
                             UnProtect(hWnd);
                             Protect(hWnd);
                         }
                     }
                 }
             }
         }
     }
     catch (System.Exception ex)
     {
         LogFile.Error("exception in ChangeProtection():", ex);
     }
 }
Exemplo n.º 2
0
        static public void UnProtect(IntPtr hWnd)
        {
            if (NativeCode.IsWindow(hWnd))
            {
                try
                {
                    if ((int)NativeCode.GetProp(hWnd, "protect_affinity") == 1)
                    {
                        NativeCode.SetWindowDisplayAffinity(hWnd, 0);
                        NativeCode.RemoveProp(hWnd, "protect_affinity");
                    }
                }
                catch (System.Exception ex)
                {
                    LogFile.Error("exception in UnProtect() while removing affinity:", ex);
                }


                try
                {
                    if ((int)NativeCode.GetProp(hWnd, "protect_layer_added") == 1)
                    {
                        NativeCode.SetWindowLong(hWnd, NativeCode.GWL_EXSTYLE, NativeCode.GetWindowLong(hWnd, NativeCode.GWL_EXSTYLE) & (~NativeCode.WS_EX_LAYERED));
                        NativeCode.RemoveProp(hWnd, "protect_layer_added");
                    }
                }
                catch (System.Exception ex)
                {
                    LogFile.Error("exception in UnProtect() while removing layered attribute:", ex);
                }


                try
                {
                    if ((int)NativeCode.GetProp(hWnd, "protect_mag") == 1)
                    {
                        if (NativeCode.IsWindow(NativeCode.GetProp(hWnd, "protect_mag_hwnd")))
                        {
                            NativeCode.DestroyWindow(NativeCode.GetProp(hWnd, "protect_mag_hwnd"));
                        }
                        NativeCode.RemoveProp(hWnd, "protect_mag");
                        NativeCode.RemoveProp(hWnd, "protect_mag_hwnd");
                    }
                }
                catch (System.Exception ex)
                {
                    LogFile.Error("exception in UnProtect() while removing magnification component:", ex);
                }
            }
            else
            {
                LogFile.Error("no vailid window handle forwarded to UnProtect()");
            }
        }
Exemplo n.º 3
0
 public void SetPlacement(int windowTop, int windowLeft, int windowWidth, int windowHeight, bool windowActivate)
 {
     if ((windowWidth != 0) && (windowHeight != 0))
     {
         this.Size     = new Size(windowWidth, windowHeight);
         this.Location = new Point(windowLeft, windowTop);
         NativeCode.ShowWindow(this.Handle, NativeCode.SW_RESTORE);
     }
     if (windowActivate)
     {
         NativeCode.SetForegroundWindow(this.Handle);
         NativeCode.AllowSetForegroundWindow(NativeCode.ASFW_ANY);
     }
 }
Exemplo n.º 4
0
        private void LoadXpsFromPipe(string pipeName)
        {
            int sz = (int)Program.PipeSize;

            if (sz <= 0)
            {
                throw new ArgumentException("Invalid pipe size " + Program.PipeSize.ToString());
            }

            SafeFileHandle pipeHandle = NativeCode.CreateFile(pipeName, NativeCode.GENERIC_READ, 0, IntPtr.Zero, NativeCode.OPEN_EXISTING, 0, IntPtr.Zero);

            if (pipeHandle.IsInvalid)
            {
                throw new IOException("pipe cannot be opened!");
            }

            FileStream fStream = new FileStream(pipeHandle, FileAccess.Read, 4096, false);
            //we must copy it to a memorystream, oterwise it does not work! (Length cannot be evaluated!)
            MemoryStream fMem = new MemoryStream();  //FileStream fMem = new FileStream(@"D:\test.out", FileMode.Create, FileAccess.Write, FileShare.ReadWrite, 1000, FileOptions.None);

            byte[] arr       = new byte[sz];
            int    offset    = 0;
            int    remaining = sz;
            int    read      = 0;
            int    start     = Environment.TickCount;

            while (remaining > 0)
            {
                read = fStream.Read(arr, offset, remaining); //Should we terminate when read returns 0 (end of stream)
                if (read > 0)
                {
                    start = Environment.TickCount; // reset timer
                }
                else
                {
                    if (Environment.TickCount > 25000) //Seit 10 sekunden konnte nichts gelesen werden -> abbrechen
                    {
                        LogFile.Error("Read timout reached, aborting...");
                        break;
                    }
                }
                LogFile.Debug("read " + read.ToString() + " bytes from pipe");
                offset    += read;
                remaining -= read;
            }
            fMem.Write(arr, 0, sz);
            fStream.Close();
            LoadXpsFromStream(fMem, @"pack://xpsstream.xps/");
        }
Exemplo n.º 5
0
        static public bool IsDWMEnabled()
        {
            bool dwmEnabled = false;

            try
            {
                NativeCode.DwmIsCompositionEnabled(out dwmEnabled);
            }
            catch (System.Exception ex)
            {
                //Windows XP...
                dwmEnabled = false;
                LogFile.Error("exception in IsDWMEnabled():", ex);
            }
            return(dwmEnabled);
        }
Exemplo n.º 6
0
        static public void Init()
        {
            try
            {
                NativeCode.RegisterHotKey(IntPtr.Zero, NativeCode.IDHOT_SNAPDESKTOP, 0, NativeCode.VK_SNAPSHOT);
                NativeCode.RegisterHotKey(IntPtr.Zero, NativeCode.IDHOT_SNAPWINDOW, NativeCode.MOD_ALT, NativeCode.VK_SNAPSHOT);
                NativeCode.RegisterHotKey(IntPtr.Zero, 0xC000, NativeCode.MOD_ALT | NativeCode.MOD_CONTROL, NativeCode.VK_SNAPSHOT);
                NativeCode.RegisterHotKey(IntPtr.Zero, 0xB000, NativeCode.MOD_CONTROL, NativeCode.VK_SNAPSHOT);
            }
            catch (System.Exception ex)
            {
                LogFile.Error("exception in Init() while registering hotkeys:", ex);
            }

            try
            {
                magInit = NativeCode.MagInitialize();
            }
            catch (System.Exception ex)
            {
                LogFile.Error("exception in Init() while initializing magnification component:", ex);
            }
        }
Exemplo n.º 7
0
        static public void Free()
        {
            try
            {
                if (dwmDisabled)
                {
                    NativeCode.DwmEnableComposition(NativeCode.DWM_EC_ENABLECOMPOSITION);
                    dwmDisabled = false;
                }
            }
            catch (System.Exception ex)
            {
                LogFile.Error("exception in Free() while enabling DWM:", ex);
            }

            try
            {
                NativeCode.UnregisterHotKey(IntPtr.Zero, NativeCode.IDHOT_SNAPDESKTOP);
                NativeCode.UnregisterHotKey(IntPtr.Zero, NativeCode.IDHOT_SNAPWINDOW);
                NativeCode.UnregisterHotKey(IntPtr.Zero, 0xB000);
                NativeCode.UnregisterHotKey(IntPtr.Zero, 0xC000);
            }
            catch (System.Exception ex)
            {
                LogFile.Error("exception in Free() while unregistering hotkey:", ex);
            }

            try
            {
                NativeCode.MagUninitialize();
                magInit = false;
            }
            catch (System.Exception ex)
            {
                LogFile.Error("exception in Free() while uninitalizing magnification component:", ex);
            }
        }
Exemplo n.º 8
0
 public void ToggleFullscreen()
 {
     if (isFullscreen)
     {
         try
         {
             if (Program.IsProtected)
             {
                 //Protected window cannot set style with FormBorderStyle.Sizable, because this seems to remove layered style!
                 NativeCode.SetWindowLong(this.Handle, NativeCode.GWL_STYLE, NativeCode.WS_VISIBLE | NativeCode.WS_OVERLAPPEDWINDOW | NativeCode.WS_MAXIMIZE);
                 NativeCode.ShowWindow(this.Handle, NativeCode.SW_SHOWNORMAL);
                 //this.WindowState = lastState;
                 this.Invalidate();
                 this.Refresh();
                 //NativeCode.RedrawWindow(this.Handle, IntPtr.Zero, IntPtr.Zero, NativeCode.RDW_FRAME | NativeCode.RDW_UPDATENOW | NativeCode.RDW_INVALIDATE);
                 //NativeCode.UpdateWindow(this.Handle);
                 isFullscreen = false;
             }
             else
             {
                 this.FormBorderStyle = FormBorderStyle.Sizable;
                 this.WindowState     = lastState;
                 isFullscreen         = false;
             }
         }
         catch (System.Exception ex)
         {
             LogFile.Error("Error in ToggleScreen() 1", ex);
         }
     }
     else
     {
         try
         {
             if (Program.IsProtected)
             {
                 //Protected window cannot set style with FormBorderStyle.Sizable, because this seems to remove layered style!
                 NativeCode.SetWindowLong(this.Handle, NativeCode.GWL_STYLE, NativeCode.WS_VISIBLE | NativeCode.WS_POPUP);
                 NativeCode.ShowWindow(this.Handle, NativeCode.SW_SHOWNORMAL); //Makes sure that the taskbar is not visible!
                 NativeCode.ShowWindow(this.Handle, NativeCode.SW_SHOWMAXIMIZED);
                 this.Invalidate();
                 this.Refresh();
                 //NativeCode.RedrawWindow(this.Handle, IntPtr.Zero, IntPtr.Zero, NativeCode.RDW_FRAME | NativeCode.RDW_UPDATENOW | NativeCode.RDW_INVALIDATE);
                 //NativeCode.UpdateWindow(this.Handle);
                 isFullscreen = true;
             }
             else
             {
                 lastState            = this.WindowState;
                 this.FormBorderStyle = FormBorderStyle.None;
                 this.WindowState     = FormWindowState.Normal;//Makes sure that the taskbar is not visible!
                 this.WindowState     = FormWindowState.Maximized;
                 isFullscreen         = true;
             }
         }
         catch (System.Exception ex)
         {
             LogFile.Error("Error in ToggleScreen() 2", ex);
         }
     }
 }
Exemplo n.º 9
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()");
            }
        }