1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105 |
- using System;
- using System.Drawing;
- using System.Runtime.InteropServices;
- using System.Text;
- using System.Windows.Forms;
- namespace Microsoft.Win32
- {
-
-
-
- public static partial class Util
- {
-
-
-
-
- public static void BeginUpdate(IntPtr hWnd)
- {
- UnsafeNativeMethods.SendMessage(hWnd, NativeMethods.WM_SETREDRAW, 0, 0);
- }
-
-
-
-
- public static void EndUpdate(IntPtr hWnd)
- {
- UnsafeNativeMethods.SendMessage(hWnd, NativeMethods.WM_SETREDRAW, 1, 0);
- }
-
-
-
-
- public static void BeginDrag(IntPtr hWnd)
- {
- UnsafeNativeMethods.ReleaseCapture();
- UnsafeNativeMethods.SendMessage(hWnd, NativeMethods.WM_SYSCOMMAND, NativeMethods.SC_MOVE | NativeMethods.HTCAPTION, 0);
- }
-
-
-
-
-
- public static void PostMouseDown(IntPtr hWnd, Point pt)
- {
- IntPtr lParam = Util.MAKELPARAM(pt.X, pt.Y);
- UnsafeNativeMethods.PostMessage(hWnd, NativeMethods.WM_MOUSEMOVE, IntPtr.Zero, lParam);
- UnsafeNativeMethods.PostMessage(hWnd, NativeMethods.WM_LBUTTONDOWN, IntPtr.Zero, lParam);
- }
-
-
-
-
-
- public static void PostMouseUp(IntPtr hWnd, Point pt)
- {
- IntPtr lParam = Util.MAKELPARAM(pt.X, pt.Y);
- UnsafeNativeMethods.PostMessage(hWnd, NativeMethods.WM_LBUTTONUP, IntPtr.Zero, lParam);
- }
-
-
-
-
-
- public static void PostMouseClick(IntPtr hWnd, Point pt)
- {
- IntPtr lParam = Util.MAKELPARAM(pt.X, pt.Y);
- UnsafeNativeMethods.PostMessage(hWnd, NativeMethods.WM_MOUSEMOVE, IntPtr.Zero, lParam);
- UnsafeNativeMethods.PostMessage(hWnd, NativeMethods.WM_LBUTTONDOWN, IntPtr.Zero, lParam);
- UnsafeNativeMethods.PostMessage(hWnd, NativeMethods.WM_LBUTTONUP, IntPtr.Zero, lParam);
- }
-
-
-
-
-
- public static void SendMouseDown(IntPtr hWnd, Point pt)
- {
- IntPtr lParam = Util.MAKELPARAM(pt.X, pt.Y);
- UnsafeNativeMethods.SendMessage(hWnd, NativeMethods.WM_MOUSEMOVE, IntPtr.Zero, lParam);
- UnsafeNativeMethods.SendMessage(hWnd, NativeMethods.WM_LBUTTONDOWN, IntPtr.Zero, lParam);
- }
-
-
-
-
-
- public static void SendMouseUp(IntPtr hWnd, Point pt)
- {
- IntPtr lParam = Util.MAKELPARAM(pt.X, pt.Y);
- UnsafeNativeMethods.SendMessage(hWnd, NativeMethods.WM_LBUTTONUP, IntPtr.Zero, lParam);
- }
-
-
-
-
-
- public static void SendMouseClick(IntPtr hWnd, Point pt)
- {
- IntPtr lParam = Util.MAKELPARAM(pt.X, pt.Y);
- UnsafeNativeMethods.SendMessage(hWnd, NativeMethods.WM_MOUSEMOVE, IntPtr.Zero, lParam);
- UnsafeNativeMethods.SendMessage(hWnd, NativeMethods.WM_LBUTTONDOWN, IntPtr.Zero, lParam);
- UnsafeNativeMethods.SendMessage(hWnd, NativeMethods.WM_LBUTTONUP, IntPtr.Zero, lParam);
- }
-
-
-
-
- public static void SendKeyDown(short vKey)
- {
- NativeMethods.INPUT[] input = new NativeMethods.INPUT[1];
- input[0].type = NativeMethods.INPUT_KEYBOARD;
- input[0].ki.wVk = vKey;
- input[0].ki.time = UnsafeNativeMethods.GetTickCount();
- UnsafeNativeMethods.SendInput((uint)input.Length, input, Marshal.SizeOf(input[0]));
- }
-
-
-
-
- public static void SendKeyUp(short vKey)
- {
- NativeMethods.INPUT[] input = new NativeMethods.INPUT[1];
- input[0].type = NativeMethods.INPUT_KEYBOARD;
- input[0].ki.wVk = vKey;
- input[0].ki.dwFlags = NativeMethods.KEYEVENTF_KEYUP;
- input[0].ki.time = UnsafeNativeMethods.GetTickCount();
- UnsafeNativeMethods.SendInput((uint)input.Length, input, Marshal.SizeOf(input[0]));
- }
-
-
-
-
- public static void SendKeyClick(short vKey)
- {
- SendKeyDown(vKey);
- SendKeyUp(vKey);
- }
-
-
-
-
- public static void ShowCursor(bool visible)
- {
- if (visible)
- {
- while (UnsafeNativeMethods.ShowCursor(true) < 0) { }
- }
- else
- {
- while (UnsafeNativeMethods.ShowCursor(false) >= 0) { }
- }
- }
-
-
-
- public static bool GetCursorVisible()
- {
- UnsafeNativeMethods.ShowCursor(false);
- return UnsafeNativeMethods.ShowCursor(true) >= 0;
- }
-
-
-
-
-
- public static IntPtr GetOwner(IntPtr hWnd)
- {
- return UnsafeNativeMethods.GetWindow(hWnd, NativeMethods.GW_OWNER);
- }
-
-
-
-
-
- public static void SetOwner(IntPtr hWnd, IntPtr hWndNewOwner)
- {
- UnsafeNativeMethods.SetWindowLong(hWnd, NativeMethods.GWL_HWNDPARENT, (int)hWndNewOwner);
- }
-
-
-
-
-
- public static IntPtr GetParent(IntPtr hWnd)
- {
- return UnsafeNativeMethods.GetAncestor(hWnd, NativeMethods.GA_PARENT);
- }
-
-
-
-
-
- public static void SetParent(IntPtr hWnd, IntPtr hWndNewParent)
- {
- UnsafeNativeMethods.SetParent(hWnd, hWndNewParent);
- }
-
-
-
-
-
- public static int GetScrollBars(IntPtr hWnd)
- {
- int wndStyle = UnsafeNativeMethods.GetWindowLong(hWnd, NativeMethods.GWL_STYLE);
- bool hsVisible = (wndStyle & NativeMethods.WS_HSCROLL) != 0;
- bool vsVisible = (wndStyle & NativeMethods.WS_VSCROLL) != 0;
- if (hsVisible)
- return vsVisible ? 3 : 1;
- else
- return vsVisible ? 2 : 0;
- }
-
-
-
-
-
- public static bool GetLeftScrollBar(IntPtr hWnd)
- {
- int wndExStyle = UnsafeNativeMethods.GetWindowLong(hWnd, NativeMethods.GWL_EXSTYLE);
- return (wndExStyle & NativeMethods.WS_EX_LEFTSCROLLBAR) != 0;
- }
-
-
-
-
-
- public static int GetBorderWidth(IntPtr hWnd)
- {
- int wndExStyle = UnsafeNativeMethods.GetWindowLong(hWnd, NativeMethods.GWL_EXSTYLE);
- if ((wndExStyle & NativeMethods.WS_EX_STATICEDGE) != 0)
- return 3;
- else if ((wndExStyle & NativeMethods.WS_EX_WINDOWEDGE) != 0)
- return 2;
- else if ((wndExStyle & NativeMethods.WS_EX_CLIENTEDGE) != 0)
- return 2;
- else if ((UnsafeNativeMethods.GetWindowLong(hWnd, NativeMethods.GWL_STYLE) & NativeMethods.WS_BORDER) != 0)
- return 1;
- else
- return 0;
- }
-
-
-
-
-
-
- public static IntPtr GetHRgn(IntPtr hWnd, Region region)
- {
- using (Graphics g = Graphics.FromHwndInternal(hWnd))
- {
- return region.GetHrgn(g);
- }
- }
-
-
-
-
-
- public static NativeMethods.RECT GetClientRect(IntPtr hWnd)
- {
- NativeMethods.RECT wndRect = new NativeMethods.RECT();
- NativeMethods.RECT clientRect = new NativeMethods.RECT();
- UnsafeNativeMethods.GetWindowRect(hWnd, ref wndRect);
- UnsafeNativeMethods.GetClientRect(hWnd, ref clientRect);
- UnsafeNativeMethods.MapWindowPoints(hWnd, NativeMethods.HWND_DESKTOP, ref clientRect, 2);
-
- clientRect.left -= wndRect.left;
- clientRect.top -= wndRect.top;
- clientRect.right -= wndRect.left;
- clientRect.bottom -= wndRect.top;
-
- return clientRect;
- }
-
-
-
-
-
-
- public static bool FlashWindow(IntPtr hWnd, int count)
- {
- NativeMethods.FLASHWINFO fwi = new NativeMethods.FLASHWINFO();
- fwi.cbSize = Marshal.SizeOf(fwi);
- fwi.hwnd = hWnd;
- fwi.dwFlags = NativeMethods.FLASHW_TRAY;
- fwi.uCount = count;
- fwi.dwTimeout = 0;
- return UnsafeNativeMethods.FlashWindowEx(ref fwi);
- }
- #region 扩展
-
-
-
-
-
- public static void BringToFront(string lpClassName, string lpWindowName)
- {
- IntPtr hWnd = UnsafeNativeMethods.FindWindow(lpClassName, lpWindowName);
- if (hWnd != IntPtr.Zero)
- UnsafeNativeMethods.SetWindowPos(hWnd, NativeMethods.HWND_TOP, 0, 0, 0, 0, NativeMethods.SWP_NOSIZE | NativeMethods.SWP_NOMOVE);
- }
-
-
-
-
-
- public static void SendToBack(string lpClassName, string lpWindowName)
- {
- IntPtr hWnd = UnsafeNativeMethods.FindWindow(lpClassName, lpWindowName);
- if (hWnd != IntPtr.Zero)
- UnsafeNativeMethods.SetWindowPos(hWnd, NativeMethods.HWND_BOTTOM, 0, 0, 0, 0, NativeMethods.SWP_NOSIZE | NativeMethods.SWP_NOMOVE);
- }
-
-
-
-
-
-
- public static void SendCopyData(string lpWindowName, int flag, string data)
- {
- IntPtr hWnd = UnsafeNativeMethods.FindWindow(null, lpWindowName);
- if (hWnd == IntPtr.Zero)
- return;
- byte[] arr = Encoding.UTF8.GetBytes(data);
- NativeMethods.COPYDATASTRUCT cds = new NativeMethods.COPYDATASTRUCT();
- cds.dwData = flag;
- cds.cbData = arr.Length + 1;
- cds.lpData = data;
- UnsafeNativeMethods.SendMessage(hWnd, NativeMethods.WM_COPYDATA, IntPtr.Zero, ref cds);
- }
-
-
-
-
- public static void CloseWindow(string lpWindowName)
- {
- IntPtr hWnd = UnsafeNativeMethods.FindWindow(null, lpWindowName);
- if (hWnd == IntPtr.Zero)
- return;
- UnsafeNativeMethods.PostMessage(hWnd, NativeMethods.WM_CLOSE, IntPtr.Zero, IntPtr.Zero);
- }
-
-
-
-
-
- public static void ShowWindow(string lpWindowName, int nCmdShow)
- {
- IntPtr hWnd = UnsafeNativeMethods.FindWindow(null, lpWindowName);
- if (hWnd == IntPtr.Zero)
- return;
- UnsafeNativeMethods.ShowWindow(hWnd, nCmdShow);
- }
-
-
-
-
- public static void SetTopMost(IntPtr hWnd)
- {
- try
- {
- UnsafeNativeMethods.SetWindowPos(hWnd, NativeMethods.HWND_TOPMOST, 0, 0, 0, 0, NativeMethods.SWP_NOSIZE | NativeMethods.SWP_NOMOVE | NativeMethods.SWP_NOACTIVATE);
- }
- catch
- {
- }
- }
-
-
-
-
- public static void SetNoTopMost(IntPtr hWnd)
- {
- try
- {
- UnsafeNativeMethods.SetWindowPos(hWnd, NativeMethods.HWND_NOTOPMOST, 0, 0, 0, 0, NativeMethods.SWP_NOSIZE | NativeMethods.SWP_NOMOVE | NativeMethods.SWP_NOACTIVATE);
- }
- catch
- {
- }
- }
-
-
-
-
-
- public static void SetOwner(Control child, string lpParentWindowName)
- {
- IntPtr hWndNewParent = UnsafeNativeMethods.FindWindow(null, lpParentWindowName);
- if (hWndNewParent != IntPtr.Zero)
- SetOwner(child.Handle, hWndNewParent);
- }
-
-
-
-
-
- public static void SetOwner(string lpChildWindowName, Control parent)
- {
- IntPtr hWndChild = UnsafeNativeMethods.FindWindow(null, lpChildWindowName);
- if (hWndChild != IntPtr.Zero)
- SetOwner(hWndChild, parent.Handle);
- }
-
-
-
-
-
-
- public static void PostMessage(string lpWindowName, int msg, int lParam)
- {
- IntPtr hWnd = UnsafeNativeMethods.FindWindow(null, lpWindowName);
- if (hWnd == IntPtr.Zero)
- return;
- UnsafeNativeMethods.PostMessage(hWnd, msg, IntPtr.Zero, (IntPtr)lParam);
- }
-
-
-
-
-
-
-
- public static void PostMessage(string lpWindowName, int msg, int wParam, int lParam)
- {
- IntPtr hWnd = UnsafeNativeMethods.FindWindow(null, lpWindowName);
- if (hWnd == IntPtr.Zero)
- return;
- UnsafeNativeMethods.PostMessage(hWnd, msg, (IntPtr)wParam, (IntPtr)lParam);
- }
-
-
-
-
-
-
- public static void PostMessage(IntPtr hWnd, int msg, int lParam)
- {
- if (hWnd == IntPtr.Zero)
- return;
- UnsafeNativeMethods.PostMessage(hWnd, msg, IntPtr.Zero, (IntPtr)lParam);
- }
-
-
-
-
-
-
-
- public static void PostMessage(IntPtr hWnd, int msg, int wParam, int lParam)
- {
- if (hWnd == IntPtr.Zero)
- return;
- UnsafeNativeMethods.PostMessage(hWnd, msg, (IntPtr)wParam, (IntPtr)lParam);
- }
- #endregion
- #region 窗口
-
-
-
-
-
- public static Point GetMousePosition(IntPtr lParam)
- {
- return new Point(GET_X_LPARAM(lParam), GET_Y_LPARAM(lParam));
- }
-
-
-
-
-
- public static Point GetBottomRight(IntPtr hWnd)
- {
- NativeMethods.RECT lpRect = new NativeMethods.RECT();
- UnsafeNativeMethods.GetWindowRect(hWnd, ref lpRect);
- NativeMethods.POINT pt = new NativeMethods.POINT(lpRect.bottom, lpRect.right);
-
- IntPtr hWndParent = GetParent(hWnd);
- if (hWndParent != IntPtr.Zero)
- UnsafeNativeMethods.MapWindowPoints(NativeMethods.HWND_DESKTOP, hWndParent, ref pt, 1);
- return new Point(pt.x, pt.y);
- }
-
-
-
-
-
- public static bool GetTopLevel(IntPtr hWnd)
- {
- int dwStyle = UnsafeNativeMethods.GetWindowLong(hWnd, NativeMethods.GWL_STYLE);
- return ((dwStyle & NativeMethods.WS_CHILD) == 0);
- }
-
-
-
-
-
- public static bool GetIsHandleCreated(IntPtr hWnd)
- {
- return (hWnd != IntPtr.Zero);
- }
-
-
-
-
-
- public static bool GetVisible(IntPtr hWnd)
- {
- return UnsafeNativeMethods.IsWindowVisible(hWnd);
- }
-
-
-
-
-
- public static void SetVisible(IntPtr hWnd, bool value)
- {
- UnsafeNativeMethods.SetWindowPos(hWnd, IntPtr.Zero, 0, 0, 0, 0, NativeMethods.SWP_NOSIZE | NativeMethods.SWP_NOMOVE | NativeMethods.SWP_NOZORDER | NativeMethods.SWP_NOACTIVATE | (value ? NativeMethods.SWP_SHOWWINDOW : NativeMethods.SWP_HIDEWINDOW));
- }
-
-
-
-
-
- public static bool GetEnabled(IntPtr hWnd)
- {
- return UnsafeNativeMethods.IsWindowEnabled(hWnd);
- }
-
-
-
-
-
- public static void SetEnabled(IntPtr hWnd, bool value)
- {
- UnsafeNativeMethods.EnableWindow(hWnd, value);
- }
-
-
-
-
-
- public static bool GetFocused(IntPtr hWnd)
- {
- return (GetIsHandleCreated(hWnd) && (UnsafeNativeMethods.GetFocus() == hWnd));
- }
-
-
-
-
-
- public static bool GetCanFocus(IntPtr hWnd)
- {
- if (!GetIsHandleCreated(hWnd))
- return false;
- return UnsafeNativeMethods.IsWindowVisible(hWnd) && UnsafeNativeMethods.IsWindowEnabled(hWnd);
- }
-
-
-
-
-
- public static bool GetContainsFocus(IntPtr hWnd)
- {
- if (!GetIsHandleCreated(hWnd))
- return false;
- IntPtr focus = UnsafeNativeMethods.GetFocus();
- if (focus == IntPtr.Zero)
- return false;
- return ((focus == hWnd) || UnsafeNativeMethods.IsChild(hWnd, focus));
- }
-
-
-
-
-
- public static bool GetCapture(IntPtr hWnd)
- {
- return (GetIsHandleCreated(hWnd) && (UnsafeNativeMethods.GetCapture() == hWnd));
- }
-
-
-
-
-
- public static void SetCapture(IntPtr hWnd, bool value)
- {
- if (value)
- UnsafeNativeMethods.SetCapture(hWnd);
- else
- UnsafeNativeMethods.ReleaseCapture();
- }
-
-
-
-
-
- public static Point GetLocation(IntPtr hWnd)
- {
- NativeMethods.RECT lpRect = new NativeMethods.RECT();
- UnsafeNativeMethods.GetWindowRect(hWnd, ref lpRect);
- NativeMethods.POINT pt = new NativeMethods.POINT(lpRect.left, lpRect.top);
-
- IntPtr hWndParent = GetParent(hWnd);
- if (hWndParent != IntPtr.Zero)
- UnsafeNativeMethods.MapWindowPoints(NativeMethods.HWND_DESKTOP, hWndParent, ref pt, 1);
- return new Point(pt.x, pt.y);
- }
-
-
-
-
-
- public static void SetLocation(IntPtr hWnd, Point value)
- {
- UnsafeNativeMethods.SetWindowPos(hWnd, IntPtr.Zero, value.X, value.Y, 0, 0, NativeMethods.SWP_NOSIZE | NativeMethods.SWP_NOZORDER | NativeMethods.SWP_NOACTIVATE);
- }
-
-
-
-
-
- public static Size GetSize(IntPtr hWnd)
- {
- NativeMethods.RECT lpRect = new NativeMethods.RECT();
- UnsafeNativeMethods.GetWindowRect(hWnd, ref lpRect);
- return lpRect.Size;
- }
-
-
-
-
-
- public static void SetSize(IntPtr hWnd, Size value)
- {
- UnsafeNativeMethods.SetWindowPos(hWnd, IntPtr.Zero, 0, 0, value.Width, value.Height, NativeMethods.SWP_NOMOVE | NativeMethods.SWP_NOZORDER | NativeMethods.SWP_NOACTIVATE);
- }
-
-
-
-
-
- public static Rectangle GetBounds(IntPtr hWnd)
- {
- NativeMethods.RECT lpRect = new NativeMethods.RECT();
- UnsafeNativeMethods.GetWindowRect(hWnd, ref lpRect);
-
- IntPtr hWndParent = GetParent(hWnd);
- if (hWndParent != IntPtr.Zero)
- UnsafeNativeMethods.MapWindowPoints(NativeMethods.HWND_DESKTOP, hWndParent, ref lpRect, 2);
- return lpRect.ToRectangle();
- }
-
-
-
-
-
- public static void SetBounds(IntPtr hWnd, Rectangle value)
- {
- UnsafeNativeMethods.SetWindowPos(hWnd, IntPtr.Zero, value.X, value.Y, value.Width, value.Height, NativeMethods.SWP_NOZORDER | NativeMethods.SWP_NOACTIVATE);
- }
-
-
-
-
-
- public static int GetLeft(IntPtr hWnd)
- {
- return GetLocation(hWnd).X;
- }
-
-
-
-
-
- public static void SetLeft(IntPtr hWnd, int value)
- {
- Point pt = GetLocation(hWnd);
- pt.X = value;
- SetLocation(hWnd, pt);
- }
-
-
-
-
-
- public static int GetTop(IntPtr hWnd)
- {
- return GetLocation(hWnd).Y;
- }
-
-
-
-
-
- public static void SetTop(IntPtr hWnd, int value)
- {
- Point pt = GetLocation(hWnd);
- pt.Y = value;
- SetLocation(hWnd, pt);
- }
-
-
-
-
-
- public static int GetRight(IntPtr hWnd)
- {
- return GetBottomRight(hWnd).X;
- }
-
-
-
-
-
- public static int GetBottom(IntPtr hWnd)
- {
- return GetBottomRight(hWnd).Y;
- }
-
-
-
-
-
- public static int GetWidth(IntPtr hWnd)
- {
- return GetSize(hWnd).Width;
- }
-
-
-
-
-
- public static void SetWidth(IntPtr hWnd, int value)
- {
- Size sz = GetSize(hWnd);
- sz.Width = value;
- SetSize(hWnd, sz);
- }
-
-
-
-
-
- public static int GetHeight(IntPtr hWnd)
- {
- return GetSize(hWnd).Height;
- }
-
-
-
-
-
- public static void SetHeight(IntPtr hWnd, int value)
- {
- Size sz = GetSize(hWnd);
- sz.Height = value;
- SetSize(hWnd, sz);
- }
-
-
-
-
-
- public static Size GetClientSize(IntPtr hWnd)
- {
- NativeMethods.RECT lpRect = new NativeMethods.RECT();
- UnsafeNativeMethods.GetClientRect(hWnd, ref lpRect);
- return lpRect.Size;
- }
-
-
-
-
-
- public static void SetClientSize(IntPtr hWnd, Size value)
- {
- NativeMethods.RECT lpRect = new NativeMethods.RECT(0, 0, value.Width, value.Height);
- int dwStyle = UnsafeNativeMethods.GetWindowLong(hWnd, NativeMethods.GWL_STYLE);
- int dwExStyle = UnsafeNativeMethods.GetWindowLong(hWnd, NativeMethods.GWL_EXSTYLE);
- UnsafeNativeMethods.AdjustWindowRectEx(ref lpRect, dwStyle, false, dwExStyle);
- SetSize(hWnd, lpRect.Size);
- }
-
-
-
-
-
- public static Rectangle GetClientRectangle(IntPtr hWnd)
- {
- NativeMethods.RECT lpRect = new NativeMethods.RECT();
- UnsafeNativeMethods.GetClientRect(hWnd, ref lpRect);
- return lpRect.ToRectangle();
- }
-
-
-
-
-
- public static string GetText(IntPtr hWnd)
- {
- if (!GetIsHandleCreated(hWnd))
- return string.Empty;
- int windowTextLength = UnsafeNativeMethods.GetWindowTextLength(hWnd);
- if (UnsafeNativeMethods.GetSystemMetrics(NativeMethods.SM_DBCSENABLED) != 0)
- windowTextLength = (windowTextLength * 2) + 1;
- StringBuilder lpString = new StringBuilder(windowTextLength + 1);
- UnsafeNativeMethods.GetWindowText(hWnd, lpString, lpString.Capacity);
- return lpString.ToString();
- }
-
-
-
-
-
- public static void SetText(IntPtr hWnd, string value)
- {
- if (GetIsHandleCreated(hWnd))
- return;
- UnsafeNativeMethods.SetWindowText(hWnd, value);
- }
-
-
-
-
- public static void ResetText(IntPtr hWnd)
- {
- SetText(hWnd, string.Empty);
- }
-
-
-
-
- public static void Show(IntPtr hWnd)
- {
- SetVisible(hWnd, true);
- }
-
-
-
-
- public static void Hide(IntPtr hWnd)
- {
- SetVisible(hWnd, false);
- }
-
-
-
-
- public static void BringToFront(IntPtr hWnd)
- {
- UnsafeNativeMethods.SetWindowPos(hWnd, NativeMethods.HWND_TOP, 0, 0, 0, 0, NativeMethods.SWP_NOSIZE | NativeMethods.SWP_NOMOVE);
- }
-
-
-
-
- public static void SendToBack(IntPtr hWnd)
- {
- UnsafeNativeMethods.SetWindowPos(hWnd, NativeMethods.HWND_BOTTOM, 0, 0, 0, 0, NativeMethods.SWP_NOSIZE | NativeMethods.SWP_NOMOVE);
- }
-
-
-
-
-
- public static bool Focus(IntPtr hWnd)
- {
- if (GetCanFocus(hWnd))
- UnsafeNativeMethods.SetFocus(hWnd);
- return GetFocused(hWnd);
- }
-
-
-
-
-
- public static Graphics CreateGraphics(IntPtr hWnd)
- {
- return Graphics.FromHwndInternal(hWnd);
- }
-
-
-
-
-
-
- public static Point PointToClient(IntPtr hWnd, Point p)
- {
- NativeMethods.POINT pt = new NativeMethods.POINT(p.X, p.Y);
- UnsafeNativeMethods.MapWindowPoints(NativeMethods.HWND_DESKTOP, hWnd, ref pt, 1);
- return new Point(pt.x, pt.y);
- }
-
-
-
-
-
-
- public static Point PointToScreen(IntPtr hWnd, Point p)
- {
- NativeMethods.POINT pt = new NativeMethods.POINT(p.X, p.Y);
- UnsafeNativeMethods.MapWindowPoints(hWnd, NativeMethods.HWND_DESKTOP, ref pt, 1);
- return new Point(pt.x, pt.y);
- }
-
-
-
-
-
-
- public static Rectangle RectangleToClient(IntPtr hWnd, Rectangle r)
- {
- NativeMethods.RECT rect = new NativeMethods.RECT(r);
- UnsafeNativeMethods.MapWindowPoints(NativeMethods.HWND_DESKTOP, hWnd, ref rect, 2);
- return rect.ToRectangle();
- }
-
-
-
-
-
-
- public static Rectangle RectangleToScreen(IntPtr hWnd, Rectangle r)
- {
- NativeMethods.RECT rect = new NativeMethods.RECT(r);
- UnsafeNativeMethods.MapWindowPoints(hWnd, NativeMethods.HWND_DESKTOP, ref rect, 2);
- return rect.ToRectangle();
- }
-
-
-
-
- public static void Invalidate(IntPtr hWnd)
- {
- Invalidate(hWnd, false);
- }
-
-
-
-
-
- public static void Invalidate(IntPtr hWnd, Rectangle rc)
- {
- Invalidate(hWnd, rc, false);
- }
-
-
-
-
-
- public static void Invalidate(IntPtr hWnd, Region region)
- {
- Invalidate(hWnd, region, false);
- }
-
-
-
-
-
- public static void Invalidate(IntPtr hWnd, bool invalidateChildren)
- {
- if (GetIsHandleCreated(hWnd))
- {
- if (invalidateChildren)
- {
- UnsafeNativeMethods.RedrawWindow(hWnd, IntPtr.Zero, IntPtr.Zero, NativeMethods.RDW_ALLCHILDREN | NativeMethods.RDW_INVALIDATE);
- }
- else
- {
- UnsafeNativeMethods.InvalidateRect(hWnd, IntPtr.Zero, false);
- }
- }
- }
-
-
-
-
-
-
- public static void Invalidate(IntPtr hWnd, Rectangle rc, bool invalidateChildren)
- {
- if (rc.IsEmpty)
- {
- Invalidate(hWnd, invalidateChildren);
- }
- else if (GetIsHandleCreated(hWnd))
- {
- if (invalidateChildren)
- {
- NativeMethods.RECT rcUpdate = NativeMethods.RECT.FromXYWH(rc.X, rc.Y, rc.Width, rc.Height);
- UnsafeNativeMethods.RedrawWindow(hWnd, ref rcUpdate, IntPtr.Zero, NativeMethods.RDW_ALLCHILDREN | NativeMethods.RDW_INVALIDATE);
- }
- else
- {
- NativeMethods.RECT rect = NativeMethods.RECT.FromXYWH(rc.X, rc.Y, rc.Width, rc.Height);
- UnsafeNativeMethods.InvalidateRect(hWnd, ref rect, false);
- }
- }
- }
-
-
-
-
-
-
- public static void Invalidate(IntPtr hWnd, Region region, bool invalidateChildren)
- {
- if (region == null)
- {
- Invalidate(hWnd, invalidateChildren);
- }
- else if (GetIsHandleCreated(hWnd))
- {
- IntPtr hRgn = GetHRgn(hWnd, region);
- try
- {
- if (invalidateChildren)
- {
- UnsafeNativeMethods.RedrawWindow(hWnd, IntPtr.Zero, hRgn, NativeMethods.RDW_ALLCHILDREN | NativeMethods.RDW_INVALIDATE);
- }
- else
- {
- UnsafeNativeMethods.InvalidateRgn(hWnd, hRgn, false);
- }
- }
- finally
- {
- UnsafeNativeMethods.DeleteObject(hRgn);
- }
- }
- }
-
-
-
-
- public static void Update(IntPtr hWnd)
- {
- UnsafeNativeMethods.UpdateWindow(hWnd);
- }
-
-
-
-
- public static void Refresh(IntPtr hWnd)
- {
- Invalidate(hWnd, true);
- Update(hWnd);
- }
- #endregion
- }
- }
|