Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Linq;
- using System.Text;
- using System.Windows.Forms;
- using System.Runtime.InteropServices;
- using System.Diagnostics;
- namespace WindowsFormsApplication1
- {
- public partial class Form1 : Form
- {
- // For Windows Mobile, replace user32.dll with coredll.dll
- [DllImport("user32.dll", SetLastError = true)]
- static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
- // Find window by Caption only. Note you must pass IntPtr.Zero as the first parameter.
- [DllImport("user32.dll", EntryPoint = "FindWindow", SetLastError = true)]
- static extern IntPtr FindWindowByCaption(IntPtr ZeroOnly, string lpWindowName);
- [DllImport("user32.dll", SetLastError = true)]
- static extern int GetWindowLong(IntPtr hWnd, int nIndex);
- //sets bigflags that control the windows styles
- [DllImport("user32.dll")]
- static extern int SetWindowLong(IntPtr hWnd, int nIndex, int dwNewLong);
- [DllImport("user32.dll")]
- public static extern IntPtr GetDC(IntPtr hWnd);
- //changes flags that modify attributes of the
- //layered window such as alpha(opacity)
- [DllImport("user32.dll")]
- static extern bool SetLayeredWindowAttributes(IntPtr hwnd, uint crKey,
- byte bAlpha, uint dwFlags);
- // You can also call FindWindow(default(string), lpWindowName) or FindWindow((string)null, lpWindowName)
- [DllImport("user32.dll", CharSet = CharSet.Auto, ExactSpelling = true)]
- public static extern IntPtr GetDesktopWindow();
- [DllImport("user32.dll")]
- public static extern IntPtr SetParent(
- IntPtr hWndChild, // handle to <strong class="highlight">window</strong>
- IntPtr hWndNewParent // new parent <strong class="highlight">window</strong>
- );
- public const byte AC_SRC_OVER = 0x00;
- public const byte AC_SRC_ALPHA = 0x01;
- public const Int32 ULW_ALPHA = 0x00000002;
- private const short GWL_EXSTYLE = -20;
- private const short LWA_ALPHA = 2;
- private const short LWA_COLORKEY = 1;
- private const int WS_EX_LAYERED = 0x80000;
- [StructLayout(LayoutKind.Sequential, Pack = 1)]
- public struct BLENDFUNCTION
- {
- public byte BlendOp;
- public byte BlendFlags;
- public byte SourceConstantAlpha;
- public byte AlphaFormat;
- }
- [DllImport("user32.dll", ExactSpelling = true, SetLastError = true)]
- 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);
- public Form1()
- {
- InitializeComponent();
- }
- private void Form1_Load(object sender, EventArgs e)
- {
- }
- private void button1_Click(object sender, EventArgs e)
- {
- Size size = new Size();
- size.Height = 1000;
- size.Width = 1000;
- IntPtr memDc;
- IntPtr screenDc;
- Point pointSource = new Point(50, 50);
- Point topPos = new Point(0, 500);
- IntPtr foundWindow = new IntPtr(0);
- API.BLENDFUNCTION blend = new API.BLENDFUNCTION();
- Process[] processes = Process.GetProcessesByName("notepad");
- foreach (Process p in processes)
- {
- foundWindow = p.MainWindowHandle;
- }
- memDc = API.GetDC(foundWindow);
- screenDc = API.GetDC(foundWindow);
- SetWindowLong(foundWindow, GWL_EXSTYLE, GetWindowLong(foundWindow, GWL_EXSTYLE) ^ WS_EX_LAYERED);
- API.UpdateLayeredWindow(foundWindow, screenDc, ref topPos, ref size, memDc, ref pointSource, 0, ref blend, 0x00000002);
- }
- }
- internal class API
- {
- public const byte AC_SRC_OVER = 0x00;
- public const byte AC_SRC_ALPHA = 0x01;
- public const Int32 ULW_ALPHA = 0x00000002;
- [StructLayout(LayoutKind.Sequential, Pack = 1)]
- public struct BLENDFUNCTION
- {
- public byte BlendOp;
- public byte BlendFlags;
- public byte SourceConstantAlpha;
- public byte AlphaFormat;
- }
- [DllImport("user32.dll", ExactSpelling = true, SetLastError = true)]
- 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);
- [DllImport("user32.dll", ExactSpelling = true, SetLastError = true)]
- public static extern IntPtr GetDC(IntPtr hWnd);
- [DllImport("gdi32.dll", ExactSpelling = true, SetLastError = true)]
- public static extern IntPtr CreateCompatibleDC(IntPtr hDC);
- [DllImport("user32.dll", ExactSpelling = true)]
- public static extern int ReleaseDC(IntPtr hWnd, IntPtr hDC);
- [DllImport("gdi32.dll", ExactSpelling = true, SetLastError = true)]
- public static extern bool DeleteDC(IntPtr hdc);
- [DllImport("gdi32.dll", ExactSpelling = true)]
- public static extern IntPtr SelectObject(IntPtr hDC, IntPtr hObject);
- [DllImport("gdi32.dll", ExactSpelling = true, SetLastError = true)]
- public static extern bool DeleteObject(IntPtr hObject);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement