Advertisement
Guest User

Untitled

a guest
Nov 6th, 2011
351
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.27 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Windows.Forms;
  9. using System.Runtime.InteropServices;
  10. using System.Diagnostics;
  11.  
  12. namespace WindowsFormsApplication1
  13. {
  14.     public partial class Form1 : Form
  15.     {
  16.         // For Windows Mobile, replace user32.dll with coredll.dll
  17.         [DllImport("user32.dll", SetLastError = true)]
  18.         static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
  19.  
  20.         // Find window by Caption only. Note you must pass IntPtr.Zero as the first parameter.
  21.  
  22.         [DllImport("user32.dll", EntryPoint = "FindWindow", SetLastError = true)]
  23.         static extern IntPtr FindWindowByCaption(IntPtr ZeroOnly, string lpWindowName);
  24.         [DllImport("user32.dll", SetLastError = true)]
  25. static extern int GetWindowLong(IntPtr hWnd, int nIndex);
  26.  
  27. //sets bigflags that control the windows styles
  28. [DllImport("user32.dll")]
  29. static extern int SetWindowLong(IntPtr hWnd, int nIndex, int dwNewLong);
  30.         [DllImport("user32.dll")]
  31.     public static extern IntPtr GetDC(IntPtr hWnd);
  32.  
  33. //changes flags that modify attributes of the
  34. //layered window such as alpha(opacity)
  35. [DllImport("user32.dll")]
  36. static extern bool SetLayeredWindowAttributes(IntPtr hwnd, uint crKey,
  37.  byte bAlpha, uint dwFlags);
  38.         // You can also call FindWindow(default(string), lpWindowName) or FindWindow((string)null, lpWindowName)
  39. [DllImport("user32.dll", CharSet = CharSet.Auto, ExactSpelling = true)]
  40. public static extern IntPtr GetDesktopWindow();
  41. [DllImport("user32.dll")]
  42. public static extern IntPtr SetParent(
  43.      IntPtr hWndChild,      // handle to <strong class="highlight">window</strong>
  44.      IntPtr hWndNewParent   // new parent <strong class="highlight">window</strong>
  45.    );
  46. public const byte AC_SRC_OVER = 0x00;
  47. public const byte AC_SRC_ALPHA = 0x01;
  48. public const Int32 ULW_ALPHA = 0x00000002;
  49. private const short GWL_EXSTYLE = -20;
  50. private const short LWA_ALPHA = 2;
  51. private const short LWA_COLORKEY = 1;
  52. private const int WS_EX_LAYERED = 0x80000;
  53. [StructLayout(LayoutKind.Sequential, Pack = 1)]
  54. public struct BLENDFUNCTION
  55. {
  56.     public byte BlendOp;
  57.     public byte BlendFlags;
  58.     public byte SourceConstantAlpha;
  59.     public byte AlphaFormat;
  60. }
  61. [DllImport("user32.dll", ExactSpelling = true, SetLastError = true)]
  62. public static extern bool UpdateLayeredWindow(IntPtr hwnd, IntPtr hdcDst, ref Point pptDst, ref Size psize, IntPtr hdcSrc, ref Point pprSrc, Int32 crKey, ref BLENDFUNCTION pblend, Int32 dwFlags);
  63.             public Form1()
  64.         {
  65.             InitializeComponent();
  66.         }
  67.  
  68.         private void Form1_Load(object sender, EventArgs e)
  69.         {
  70.         }
  71.  
  72.         private void button1_Click(object sender, EventArgs e)
  73.         {
  74.             Size size = new Size();
  75.             size.Height = 1000;
  76.             size.Width = 1000;
  77.             IntPtr memDc;
  78.             IntPtr screenDc;
  79.             Point pointSource = new Point(50, 50);
  80.             Point topPos = new Point(0, 500);
  81.             IntPtr foundWindow = new IntPtr(0);
  82.             API.BLENDFUNCTION blend = new API.BLENDFUNCTION();
  83.             Process[] processes = Process.GetProcessesByName("notepad");
  84.             foreach (Process p in processes)
  85.             {
  86.                 foundWindow = p.MainWindowHandle;
  87.             }
  88.  
  89.             memDc = API.GetDC(foundWindow);
  90.             screenDc = API.GetDC(foundWindow);
  91.             SetWindowLong(foundWindow, GWL_EXSTYLE, GetWindowLong(foundWindow, GWL_EXSTYLE) ^ WS_EX_LAYERED);
  92.             API.UpdateLayeredWindow(foundWindow, screenDc, ref topPos, ref size, memDc, ref pointSource, 0, ref blend, 0x00000002);
  93.         }
  94.     }
  95.     internal class API
  96.     {
  97.         public const byte AC_SRC_OVER = 0x00;
  98.         public const byte AC_SRC_ALPHA = 0x01;
  99.         public const Int32 ULW_ALPHA = 0x00000002;
  100.  
  101.         [StructLayout(LayoutKind.Sequential, Pack = 1)]
  102.         public struct BLENDFUNCTION
  103.         {
  104.             public byte BlendOp;
  105.             public byte BlendFlags;
  106.             public byte SourceConstantAlpha;
  107.             public byte AlphaFormat;
  108.         }
  109.  
  110.         [DllImport("user32.dll", ExactSpelling = true, SetLastError = true)]
  111.         public static extern bool UpdateLayeredWindow(IntPtr hwnd, IntPtr hdcDst, ref Point pptDst, ref Size psize, IntPtr hdcSrc, ref Point pprSrc, Int32 crKey, ref BLENDFUNCTION pblend, Int32 dwFlags);
  112.  
  113.  
  114.         [DllImport("user32.dll", ExactSpelling = true, SetLastError = true)]
  115.         public static extern IntPtr GetDC(IntPtr hWnd);
  116.  
  117.         [DllImport("gdi32.dll", ExactSpelling = true, SetLastError = true)]
  118.         public static extern IntPtr CreateCompatibleDC(IntPtr hDC);
  119.  
  120.         [DllImport("user32.dll", ExactSpelling = true)]
  121.         public static extern int ReleaseDC(IntPtr hWnd, IntPtr hDC);
  122.  
  123.         [DllImport("gdi32.dll", ExactSpelling = true, SetLastError = true)]
  124.         public static extern bool DeleteDC(IntPtr hdc);
  125.  
  126.  
  127.         [DllImport("gdi32.dll", ExactSpelling = true)]
  128.         public static extern IntPtr SelectObject(IntPtr hDC, IntPtr hObject);
  129.  
  130.         [DllImport("gdi32.dll", ExactSpelling = true, SetLastError = true)]
  131.         public static extern bool DeleteObject(IntPtr hObject);
  132.     }
  133. }
  134.  
  135.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement