/// <summary> /// Requests the specified window to draw itself to a Bitmap /// </summary> /// <param name="handle">The handle of the window to draw</param> /// <param name="clientArea">If the client area should be drawn or the whole window.</param> /// <remarks>This method only works on windows that handle WM_PRINT. It allows for capturing non-visible and minimized applications.</remarks> /// <returns>An image with the window drawn onto it.</returns> public static Bitmap Print(IntPtr hWnd, bool clientArea) { NativeStructs.RECT r = new NativeStructs.RECT(); NativeMethods.GetWindowRect(hWnd, ref r); Rectangle rect = r; Bitmap bmp = new Bitmap(rect.Width, rect.Height); using (Graphics g = Graphics.FromImage(bmp)) { IntPtr hdc = g.GetHdc(); NativeMethods.PrintWindow(hWnd, hdc, 0); g.ReleaseHdc(hdc); } if (clientArea) { NativeMethods.GetClientRect(hWnd, ref r); Point pt = Point.Empty.ClientToScreen(hWnd); Rectangle client = new Rectangle(pt, ((Rectangle)r).Size); Bitmap crop = new Bitmap(client.Width, client.Height); using (Graphics g = Graphics.FromImage(crop)) { int titleHeight = client.Top - rect.Top; g.DrawImage(bmp, new Point((client.Width - rect.Width) / 2, -titleHeight)); } bmp.Dispose(); bmp = crop; } return(bmp); }
/// <summary> /// Function: Convert Rectangle to NativeStructs.RECT /// Author : Jerry Xu /// Date : 2008-8-2 /// </summary> /// <param name="source">Rectangle</param> /// <returns>NativeStructs.RECT</returns> private static NativeStructs.RECT ConvertRectangleToRECT(Rectangle source) { NativeStructs.RECT target = new NativeStructs.RECT(); target.left = source.Left; target.top = source.Top; target.bottom = source.Right; target.right = source.Bottom; return(target); }
private static bool EnumMonitorCallback(IntPtr hMonitor, IntPtr hdc, ref NativeStructs.RECT rect, IntPtr dwData) { User32.MONITORINFOEX monitorInfo = new User32.MONITORINFOEX(); monitorInfo.cbSize = Marshal.SizeOf(monitorInfo); if (!User32.GetMonitorInfo(hMonitor, ref monitorInfo)) { throw new Win32Exception(); } _monitorInfo.Add(monitorInfo.szDevice, monitorInfo); _monitorHandle.Add(monitorInfo.szDevice, hMonitor); return(true); }
public void Highlight() { NativeStructs.RECT windowRect = new NativeStructs.RECT(0, 0, 0, 0); NativeMethods.GetWindowRect(handle, ref windowRect); IntPtr windowDC = NativeMethods.GetWindowDC(handle); if (windowDC != IntPtr.Zero) { Graphics graph = Graphics.FromHdc(windowDC, handle); graph.DrawRectangle(highlightPen, 1, 1, windowRect.Width - 2, windowRect.Height - 2); graph.Dispose(); NativeMethods.ReleaseDC(handle, windowDC); } }
ScreenshotData TakeScreenshotCore(NativeInterfaces.IViewObject rpViewObject) { const int BitCount = 24; var rEmbedElement = rpViewObject as HTMLEmbed; var rWidth = rEmbedElement.clientWidth; var rHeight = rEmbedElement.clientHeight; var rScreenDC = NativeMethods.User32.GetDC(IntPtr.Zero); var rHDC = NativeMethods.Gdi32.CreateCompatibleDC(rScreenDC); var rInfo = new NativeStructs.BITMAPINFO(); rInfo.bmiHeader.biSize = Marshal.SizeOf(typeof(NativeStructs.BITMAPINFOHEADER)); rInfo.bmiHeader.biWidth = rWidth; rInfo.bmiHeader.biHeight = rHeight; rInfo.bmiHeader.biBitCount = BitCount; rInfo.bmiHeader.biPlanes = 1; IntPtr rBits; var rHBitmap = NativeMethods.Gdi32.CreateDIBSection(rHDC, ref rInfo, 0, out rBits, IntPtr.Zero, 0); var rOldObject = NativeMethods.Gdi32.SelectObject(rHDC, rHBitmap); var rTargetDevice = new NativeStructs.DVTARGETDEVICE() { tdSize = 0 }; var rRect = new NativeStructs.RECT(0, 0, rWidth, rHeight); var rEmptyRect = default(NativeStructs.RECT); rpViewObject.Draw(1, 0, IntPtr.Zero, ref rTargetDevice, IntPtr.Zero, rHDC, ref rRect, ref rEmptyRect, IntPtr.Zero, IntPtr.Zero); var rResult = new ScreenshotData(rWidth, rHeight, BitCount); var rPixels = new byte[rWidth * rHeight * 3]; Marshal.Copy(rBits, rPixels, 0, rPixels.Length); rResult.BitmapData = rPixels; NativeMethods.Gdi32.SelectObject(rHDC, rOldObject); NativeMethods.Gdi32.DeleteObject(rHBitmap); NativeMethods.Gdi32.DeleteDC(rHDC); NativeMethods.User32.ReleaseDC(IntPtr.Zero, rScreenDC); return(rResult); }
/// <summary> /// Processes incoming Windows Messages. /// </summary> /// <param name="m">The Windows <see cref="T:System.Windows.Forms.Message" /> to process.</param> protected override void WndProc(ref Message m) { if (!this.DesignMode) { switch (m.Msg) { //Remove NoClientArea case NativeConstants.WM_NCCALCSIZE: if (m.WParam == IntPtr.Zero) { NativeStructs.RECT rect = (NativeStructs.RECT)m.GetLParam(typeof(NativeStructs.RECT)); Rectangle drawingRect = rect.ToRectangle(); drawingRect.Inflate(8, 8); Marshal.StructureToPtr(new NativeStructs.RECT(drawingRect), m.LParam, true); } else { NativeStructs.NCCALCSIZE_PARAMS calcSizeParams = (NativeStructs.NCCALCSIZE_PARAMS)m.GetLParam(typeof(NativeStructs.NCCALCSIZE_PARAMS)); Rectangle drawingRect = calcSizeParams.rgrc0.ToRectangle(); drawingRect.Inflate(8, 8); calcSizeParams.rgrc0 = new NativeStructs.RECT(drawingRect); Marshal.StructureToPtr(calcSizeParams, m.LParam, true); } m.Result = IntPtr.Zero; break; //Prevent activation of the nonclientarea case NativeConstants.WM_NCACTIVATE: m.Result = IntPtr.Zero; return; } base.WndProc(ref m); switch (m.Msg) { case NativeConstants.WM_SIZE: m.Result = IntPtr.Zero; return; } } else { base.WndProc(ref m); } }
/// <summary> /// /// </summary> /// <returns></returns> public static NativeStructs.MONITORINFOEX[] GetAllMonitors(out NativeStructs.MONITORINFOEX primaryMonitor) { NativeStructs.RECT empty = NativeStructs.RECT.Empty; NativeStructs.MONITORINFOEX[] monitorArray = null; lock (_globalLock) { _currentMonitors = new List <NativeStructs.MONITORINFOEX>();; NativeMethods.EnumDisplayMonitors(IntPtr.Zero, null, new NativeMethods.MonitorEnumProc(MultiMonitorHelper.MonitorEnumProcCallback), IntPtr.Zero); if (_currentMonitors.Count <= 0) { throw new InvalidOperationException("No Monitor found."); } monitorArray = _currentMonitors.ToArray(); primaryMonitor = _primaryMonitor; } return(monitorArray); }
private int HookProc(IntPtr hdlg, int msg, int wParam, int lParam) { switch ((NativeEnums.WindowMessages)msg) { case NativeEnums.WindowMessages.WM_INITDIALOG: Rectangle rcScreen = m_activeScreen.Bounds; NativeStructs.RECT rcDialog = new NativeStructs.RECT(); IntPtr parent = NativeMethods.GetParent(hdlg); NativeMethods.GetWindowRect(parent, ref rcDialog); IntPtr fileComboWindow = NativeMethods.GetDlgItem(parent, 0x470); for (int i = 0; i < m_formats.Length; i++) { NativeMethods.SendMessage(fileComboWindow, (int)NativeEnums.ComboboxControlMessages.CB_ADDSTRING, 0, m_formats[i]); } NativeMethods.SendMessage(fileComboWindow, (int)NativeEnums.ComboboxControlMessages.CB_SETCURSEL, 0, 0); m_comboFormatHandle = fileComboWindow;//CreateComboBox("combobox_builder", fontHandle, m_formats, 2, point.x, point.y + 8, aboveRect.right - point.x, 100, parent); break; case NativeEnums.WindowMessages.WM_DESTROY: break; case NativeEnums.WindowMessages.WM_NOTIFY: // The following line throws unknown exception // NativeStructs.OFNotify notify = (NativeStructs.OFNotify)NativeMethods.PtrToStructure(new IntPtr(lParam), typeof(NativeStructs.OFNotify)); //if (notify.hdr.code == CDN_FILEOK) if (m_comboFormatHandle != IntPtr.Zero) { int index = NativeMethods.SendMessage(m_comboFormatHandle, (int)NativeEnums.ComboboxControlMessages.CB_GETCURSEL, 0, 0); m_format = m_formats[index]; } break; } return(0); }
protected override void WndProc(ref Message m) { switch ((NativeEnums.WM)m.Msg) { case NativeEnums.WM.NCCALCSIZE: //Crop the form to remove the windows borders if (m.WParam.Equals(IntPtr.Zero)) { NativeStructs.RECT rc = (NativeStructs.RECT)m.GetLParam(typeof(NativeStructs.RECT)); Rectangle r = CropForm(rc); Marshal.StructureToPtr(new NativeStructs.RECT(r), m.LParam, true); } else { NativeStructs.NCCALCSIZE_PARAMS csp = (NativeStructs.NCCALCSIZE_PARAMS)m.GetLParam(typeof(NativeStructs.NCCALCSIZE_PARAMS)); Rectangle r = CropForm(csp.rgrc0); csp.rgrc0 = new NativeStructs.RECT(r); Marshal.StructureToPtr(csp, m.LParam, true); } m.Result = IntPtr.Zero; return; case NativeEnums.WM.SIZE: if (this.WindowState != lastState) { lastState = this.WindowState; if (this.WindowState == FormWindowState.Maximized) { OverrideSize = this.RestoreBounds.Size; } NativeMethods.SetWindowPos(this.Handle, IntPtr.Zero, 0, 0, 0, 0, NativeEnums.SWP.FRAMECHANGED | NativeEnums.SWP.NOACTIVATE | NativeEnums.SWP.NOMOVE | NativeEnums.SWP.NOSIZE | NativeEnums.SWP.NOZORDER); this.Refresh(); } break; } base.WndProc(ref m); }
/// <summary> /// Function: Strech text /// Author : Jerry Xu /// Date : 2008-8-2 /// </summary> /// <param name="g">Graphics:destination graphics</param> /// <param name="drawText">string:destination text</param> /// <param name="font">Font</param> /// <param name="sourceBounds">source rectangle</param> /// <param name="antiAliasing">bool</param> private static void StrechText(Graphics g, string drawText, Font font, Color foreColor, Color backColor, Rectangle sourceBounds, float zoom, bool antiAliasing) { //Define InPtr(point) IntPtr destDC = IntPtr.Zero; //IntPtr destDC1 = IntPtr.Zero; IntPtr dcMemery = IntPtr.Zero; IntPtr pbitmap = IntPtr.Zero; IntPtr hFont = IntPtr.Zero; //Define destination bound Rectangle destBounds; try { //Destionation DC destDC = g.GetHdc(); //destDC1 = g.GetHdc(); //Temp DC dcMemery = SafeNativeMethods.CreateCompatibleDC(destDC); //Init font //font = new Font("宋体", 14.0f, FontStyle.Regular, GraphicsUnit.Pixel); //Set font to temp DC hFont = font.ToHfont(); SafeNativeMethods.SelectObject(dcMemery, hFont); //Create image and get image DC pbitmap = SafeNativeMethods.CreateCompatibleBitmap(destDC, sourceBounds.Width, sourceBounds.Height); //Set image DC to temp DC SafeNativeMethods.SelectObject(dcMemery, pbitmap); //Set fore color NativeStructs.RGBQUAD rgb = new NativeStructs.RGBQUAD(); rgb.rgbBlue = foreColor.B; rgb.rgbRed = foreColor.R; rgb.rgbGreen = foreColor.G; SafeNativeMethods.SetTextColor(dcMemery, rgb); //Set back color NativeStructs.RGBQUAD rgbBG = new NativeStructs.RGBQUAD(); rgbBG.rgbBlue = backColor.B; rgbBG.rgbRed = backColor.R; rgbBG.rgbGreen = backColor.G; SafeNativeMethods.SetBkColor(dcMemery, rgbBG); //Draw text to temp Rectangle NativeStructs.RECT rect = ConvertRectangleToRECT(sourceBounds); SafeNativeMethods.DrawTextW(dcMemery, drawText, drawText.Length, ref rect, NativeConstants.DT_CENTER); //Zoom Rectangle destBounds = new Rectangle(sourceBounds.X, sourceBounds.Y, (int)(sourceBounds.Width * zoom), (int)(sourceBounds.Height * zoom)); //Draw temp Rectangle to zoom Rectangle SafeNativeMethods.StretchBlt(destDC, destBounds.X, destBounds.Y, destBounds.Width, destBounds.Height, dcMemery, sourceBounds.X, sourceBounds.Y, sourceBounds.Width, sourceBounds.Height, NativeConstants.SRCCOPY); //SafeNativeMethods.TransparentBlt(destDC1, destBounds.X, destBounds.Y, destBounds.Width, destBounds.Height, destDC, sourceBounds.X, sourceBounds.Y, sourceBounds.Width, sourceBounds.Height, rgbBG); } catch { } finally { //Disponse resource if (pbitmap != IntPtr.Zero) { SafeNativeMethods.SelectObject(dcMemery, pbitmap); pbitmap = IntPtr.Zero; } if (dcMemery != IntPtr.Zero) { SafeNativeMethods.SelectObject(destDC, dcMemery); dcMemery = IntPtr.Zero; } if (hFont != IntPtr.Zero) { SafeNativeMethods.DeleteObject(hFont); hFont = IntPtr.Zero; } if (destDC != IntPtr.Zero) { g.ReleaseHdc(destDC); destDC = IntPtr.Zero; } //if (destDC1 != IntPtr.Zero) //{ // g.ReleaseHdc(destDC1); // destDC1 = IntPtr.Zero; //} } }
private int HookProc(IntPtr hdlg, int msg, int wParam, int lParam) { switch ((NativeEnums.WindowMessages)msg) { case NativeEnums.WindowMessages.WM_INITDIALOG: Rectangle rcScreen = m_activeScreen.Bounds; NativeStructs.RECT rcDialog = new NativeStructs.RECT(); IntPtr parent = NativeMethods.GetParent(hdlg); NativeMethods.GetWindowRect(parent, ref rcDialog); IntPtr fileComboWindow = NativeMethods.GetDlgItem(parent, 0x470); for (int i = 0; i < m_formats.Length; i++) NativeMethods.SendMessage(fileComboWindow, (int)NativeEnums.ComboboxControlMessages.CB_ADDSTRING, 0, m_formats[i]); NativeMethods.SendMessage(fileComboWindow, (int)NativeEnums.ComboboxControlMessages.CB_SETCURSEL, 0, 0); m_comboFormatHandle = fileComboWindow;//CreateComboBox("combobox_builder", fontHandle, m_formats, 2, point.x, point.y + 8, aboveRect.right - point.x, 100, parent); break; case NativeEnums.WindowMessages.WM_DESTROY: break; case NativeEnums.WindowMessages.WM_NOTIFY: // The following line throws unknown exception // NativeStructs.OFNotify notify = (NativeStructs.OFNotify)NativeMethods.PtrToStructure(new IntPtr(lParam), typeof(NativeStructs.OFNotify)); //if (notify.hdr.code == CDN_FILEOK) if (m_comboFormatHandle != IntPtr.Zero) { int index = NativeMethods.SendMessage(m_comboFormatHandle, (int)NativeEnums.ComboboxControlMessages.CB_GETCURSEL, 0, 0); m_format = m_formats[index]; } break; } return 0; }
ScreenshotData TakeScreenshotCore(NativeInterfaces.IViewObject rpViewObject) { const int BitCount = 24; var rEmbedElement = rpViewObject as HTMLEmbed; var rWidth = rEmbedElement.clientWidth; var rHeight = rEmbedElement.clientHeight; var rScreenDC = NativeMethods.User32.GetDC(IntPtr.Zero); var rHDC = NativeMethods.Gdi32.CreateCompatibleDC(rScreenDC); var rInfo = new NativeStructs.BITMAPINFO(); rInfo.bmiHeader.biSize = Marshal.SizeOf(typeof(NativeStructs.BITMAPINFOHEADER)); rInfo.bmiHeader.biWidth = rWidth; rInfo.bmiHeader.biHeight = rHeight; rInfo.bmiHeader.biBitCount = BitCount; rInfo.bmiHeader.biPlanes = 1; IntPtr rBits; var rHBitmap = NativeMethods.Gdi32.CreateDIBSection(rHDC, ref rInfo, 0, out rBits, IntPtr.Zero, 0); var rOldObject = NativeMethods.Gdi32.SelectObject(rHDC, rHBitmap); var rTargetDevice = new NativeStructs.DVTARGETDEVICE() { tdSize = 0 }; var rRect = new NativeStructs.RECT(0, 0, rWidth, rHeight); var rEmptyRect = default(NativeStructs.RECT); rpViewObject.Draw(1, 0, IntPtr.Zero, ref rTargetDevice, IntPtr.Zero, rHDC, ref rRect, ref rEmptyRect, IntPtr.Zero, IntPtr.Zero); var rResult = new ScreenshotData(rWidth, rHeight, BitCount); var rPixels = new byte[rWidth * rHeight * 3]; Marshal.Copy(rBits, rPixels, 0, rPixels.Length); rResult.BitmapData = rPixels; NativeMethods.Gdi32.SelectObject(rHDC, rOldObject); NativeMethods.Gdi32.DeleteObject(rHBitmap); NativeMethods.Gdi32.DeleteDC(rHDC); NativeMethods.User32.ReleaseDC(IntPtr.Zero, rScreenDC); return rResult; }
public extern static int FillRect( IntPtr hDC, ref NativeStructs.RECT lprc, IntPtr hbr);
public static extern int DrawTextW( IntPtr hdc, string lpString, int nCount, ref NativeStructs.RECT lpRect, uint uFormat);
private void DrawFunction(PaintEventArgs e) { //Define font //Define string string drawText = "ABC"; //Define XY Point pt = new Point(0, 0); //Define anti bool antiAliasing = false; //Define Smoothing FontSmoothing smoothType = FontSmoothing.Sharp; //Fonts.DrawText(e.Graphics, font, "A", new Point(0, 0), false, FontSmoothing.Sharp); // Panel handle IntPtr destDC = IntPtr.Zero; IntPtr hOldObject = IntPtr.Zero; IntPtr dcMemery = IntPtr.Zero; IntPtr pbitmap = IntPtr.Zero; //IntPtr pTemp = IntPtr.Zero; Graphics g = e.Graphics; NativeStructs.RECT destBounds = new NativeStructs.RECT(); destBounds.left = 0; destBounds.top = 0; destBounds.bottom = 20; destBounds.right = 20; destDC = g.GetHdc(); //pTemp = SafeNativeMethods.CreateCompatibleDC(destDC); dcMemery = SafeNativeMethods.CreateCompatibleDC(destDC); // Font Font font = new Font("宋体", 10.0f, FontStyle.Regular, GraphicsUnit.Pixel); IntPtr hFont = IntPtr.Zero; hFont = Fonts.CreateFontObject(font, antiAliasing); SafeNativeMethods.SelectObject(dcMemery, hFont); pbitmap = SafeNativeMethods.CreateCompatibleBitmap(destDC, 20, 20); SafeNativeMethods.SelectObject(dcMemery, pbitmap); NativeStructs.RGBQUAD rgb = new NativeStructs.RGBQUAD(); rgb.rgbBlue = 255; NativeStructs.RGBQUAD rgbBG = new NativeStructs.RGBQUAD(); rgbBG.rgbBlue = 0; rgbBG.rgbRed = 0; rgbBG.rgbGreen = 0; //Graphics gSource = new Graphics(); //gSource. //IntPtr gPtr = gSource.GetHdc(); //gPtr = dcMemery; //gSource SafeNativeMethods.SetTextColor(dcMemery, rgb); SafeNativeMethods.SetBkColor(dcMemery, rgbBG); SafeNativeMethods.DrawTextW(dcMemery, drawText, drawText.Length, ref destBounds, NativeConstants.DT_CENTER); //SafeNativeMethods.TransparentBlt(pTemp, destBounds.left, destBounds.top, destBounds.right, destBounds.bottom, dcMemery, destBounds.left, destBounds.top, destBounds.right, destBounds.bottom, 0); SafeNativeMethods.StretchBlt(destDC, 0, 0, 20 * 10, 20 * 10, dcMemery, 0, 0, 20, 20, NativeConstants.SRCCOPY); //dcMemery. //pbitmap = bit.GetHbitmap(); //dcMemery = SafeNativeMethods.CreateCompatibleBitmap(pdc, 20, 20); //SafeNativeMethods. //hFont = CreateFontObject(font, antiAliasing); //hOldObject = SafeNativeMethods.SelectObject(pdc, hFont); //SafeNativeMethods.CreateCompatibleDC(pdc); //uint result1 = SafeNativeMethods.StretchBlt( // 0, // 0, // 20, // 20, // pdc, // 0, // 0, // 20 * 1000, // 20 * 1000, // NativeConstants.SRCCOPY); }