public void Clipping() { IVwGraphicsWin32 vwGraphics = VwGraphicsWin32Class.Create(); Assert.IsNotNull(vwGraphics); using (var gr = new GraphicsObjectFromImage()) { // start with a blue background. using (var blueBrush = new SolidBrush(Color.Blue)) { gr.Graphics.FillRectangle(blueBrush, new Rectangle(0, 0, 1000, 1000)); // Check that filling with a blue brush worked. Assert.IsTrue(ColorCompare(gr.Bitmap.GetPixel(500, 500), Color.Blue)); // // Check that drawing using a VwGraphics works. // vwGraphics.Initialize(gr.Graphics.GetHdc()); vwGraphics.PushClipRect(new Utils.Rect(0, 0, 1000, 1000)); vwGraphics.BackColor = ConvertToVwGraphicsColor(Color.Red); vwGraphics.DrawRectangle(0, 0, 1000, 1000); vwGraphics.PopClipRect(); vwGraphics.ReleaseDC(); gr.Graphics.ReleaseHdc(); gr.Graphics.Flush(); // Check that drawing a red rectangle using the VwGraphics Interface worked Assert.IsTrue(ColorCompare(gr.Bitmap.GetPixel(500, 500), Color.Red)); ///// // Check that VwGraphics doesn't draw outside its clip rect. ///// vwGraphics.Initialize(gr.Graphics.GetHdc()); // make the clip rect not include the area we are going to draw to. vwGraphics.PushClipRect(new Utils.Rect(100, 100, 200, 200)); // attempt to draw off the clip rect. vwGraphics.BackColor = ConvertToVwGraphicsColor(Color.Green); vwGraphics.DrawRectangle(400, 400, 600, 600); vwGraphics.PopClipRect(); vwGraphics.ReleaseDC(); gr.Graphics.ReleaseHdc(); gr.Graphics.Flush(); // Check that the green rectangle didn't appear on screen. Assert.IsTrue(!ColorCompare(gr.Bitmap.GetPixel(500, 500), Color.Green)); } } }
/// -------------------------------------------------------------------------------- /// <summary> /// Initializes a new instance of the <see cref="T:HoldDummyGraphics"/> class. /// </summary> /// <param name="fontName">Name of the font.</param> /// <param name="fBold">if set to <c>true</c> [f bold].</param> /// <param name="fItalic">if set to <c>true</c> [f italic].</param> /// <param name="ctrl">The parent control</param> /// -------------------------------------------------------------------------------- public HoldDummyGraphics(string fontName, bool fBold, bool fItalic, Control ctrl) { // Make a VwGraphics and initialize it. IVwGraphicsWin32 vwGraphics32 = VwGraphicsWin32Class.Create(); m_vwGraphics = vwGraphics32; m_graphics = ctrl.CreateGraphics(); m_hdc = m_graphics.GetHdc(); ((IVwGraphicsWin32)m_vwGraphics).Initialize(m_hdc); // Select our font into it. var chrp = new LgCharRenderProps(); chrp.szFaceName = new ushort[32]; for (int ich = 0; ich < fontName.Length; ++ich) { if (ich < 32) { chrp.szFaceName[ich] = fontName[ich]; } } if (fontName.Length < 32) { chrp.szFaceName[fontName.Length] = 0; } else { chrp.szFaceName[31] = 0; } chrp.ttvBold = (int)(fBold ? FwTextToggleVal.kttvForceOn : FwTextToggleVal.kttvOff); chrp.ttvItalic = (int)(fItalic ? FwTextToggleVal.kttvForceOn : FwTextToggleVal.kttvOff); m_vwGraphics.SetupGraphics(ref chrp); }
/// <summary> /// See C++ documentation /// </summary> public void DrawTheRootAt(IVwRootBox prootb, IntPtr hdc, Rect rcpDraw, uint bkclr, bool fDrawSel, IVwGraphics pvg, Rect rcSrc, Rect rcDst, int ysTop, int dysHeight) { IVwGraphicsWin32 qvg32 = VwGraphicsWin32Class.Create(); using (Graphics screen = Graphics.FromHdc(hdc)) { Rectangle rcp = rcpDraw; Rectangle rcFill = new Rect(0, 0, rcp.Width, rcp.Height); using (var memoryBuffer = new MemoryBuffer(rcp.Width, rcp.Height)) { memoryBuffer.Graphics.FillRectangle(new SolidBrush(ColorUtil.ConvertBGRtoColor(bkclr)), rcFill); qvg32.Initialize(memoryBuffer.Graphics.GetHdc()); qvg32.XUnitsPerInch = rcDst.right - rcDst.left; qvg32.YUnitsPerInch = rcDst.bottom - rcDst.top; try { prootb.DrawRoot2(qvg32, rcSrc, rcDst, fDrawSel, ysTop, dysHeight); } catch (Exception) { qvg32.ReleaseDC(); throw; } screen.DrawImageUnscaled(memoryBuffer.Bitmap, rcp); qvg32.ReleaseDC(); } } }
public void GetTextExtentWithEmptyString() { IVwGraphicsWin32 vwGraphics = VwGraphicsWin32Class.Create(); Assert.IsNotNull(vwGraphics); using (var gr = new GraphicsObjectFromImage()) { const int areaWidth = 1000; const int areaHeight = 1000; vwGraphics.Initialize(gr.Graphics.GetHdc()); vwGraphics.PushClipRect(new Utils.Rect(0, 0, areaWidth, areaHeight)); vwGraphics.ForeColor = ConvertToVwGraphicsColor(Color.Black); int extentX; int extentY; vwGraphics.GetTextExtent(0, String.Empty, out extentX, out extentY); Assert.That(extentX == 0, "extentX should equal 0"); Assert.That(extentY > 0, "extentY should be greater than 0"); vwGraphics.PopClipRect(); vwGraphics.ReleaseDC(); gr.Graphics.ReleaseHdc(); } }
/// <summary> /// Draw the text using the FieldWorks IVwGraphicsWin32 interface. /// </summary> /// <remarks>Only used with Mono</remarks> private void DrawFwText(Graphics graphics, Rectangle cellBounds, string text, DataGridViewCellStyle cellStyle, FwTextBoxColumn col) { Debug.Assert(Platform.IsMono, "This method is only needed on Mono"); if (String.IsNullOrEmpty(text)) { return; } IntPtr hdc = graphics.GetHdc(); IVwGraphicsWin32 vg = VwGraphicsWin32Class.Create(); // actually VwGraphicsCairo try { vg.Initialize(hdc); var renderProps = GetRenderProps(cellStyle, col); vg.SetupGraphics(ref renderProps); int x; int y; GetLocationForText(cellBounds, (col.TextBoxControl.RightToLeft == RightToLeft.Yes), vg, text, out x, out y); vg.PushClipRect(cellBounds); vg.DrawText(x, y, text.Length, text, 0); vg.PopClipRect(); } finally { vg.ReleaseDC(); graphics.ReleaseHdc(hdc); } }
private void pd_PrintPage(object sender, PrintPageEventArgs e) { // If the print rootsite hasn't been initialized yet, do so now. if (m_rootb == null) { Init(e); if (m_rootb == null || !m_morePagesToPrint) { e.HasMorePages = false; return; } } // Initialize the IVwGraphics with the hDC from the .Net graphics object. IVwGraphics vwGraphics = VwGraphicsWin32Class.Create(); IntPtr hdc = e.Graphics.GetHdc(); ((IVwGraphicsWin32)vwGraphics).Initialize(hdc); m_vwPrintContext.SetGraphics(vwGraphics); // Print the next page m_rootb.PrintSinglePage(m_vwPrintContext, m_nextPageToPrint); // Release these things here or bad things will happen. vwGraphics.ReleaseDC(); if (hdc != IntPtr.Zero) { e.Graphics.ReleaseHdc(hdc); } // If more lines exist, print another page. Advance(); e.HasMorePages = m_morePagesToPrint; }
protected void ProcessInitialize(IEnumerable <string> arguments) { string handle = arguments.Skip(1).First(); if (VwGraphicsLookup.Keys.Contains(handle)) { var bitmap = new Bitmap(1000, 1000); Graphics gr = Graphics.FromImage(bitmap); VwGraphicsLookup[handle].Initialize(gr.GetHdc()); GraphicsLookup[handle] = gr; BitmapLookup[handle] = bitmap; } else { IVwGraphicsWin32 vwGraphics = VwGraphicsWin32Class.Create(); var bitmap = new Bitmap(1000, 1000); Graphics gr = Graphics.FromImage(bitmap); vwGraphics.Initialize(gr.GetHdc()); VwGraphicsLookup.Add(handle, vwGraphics); GraphicsLookup[handle] = gr; BitmapLookup[handle] = bitmap; m_vwGraphics32 = vwGraphics; } }
/// ------------------------------------------------------------------------------------ /// <summary> /// /// </summary> /// <param name="e"></param> /// ------------------------------------------------------------------------------------ private void Init(PrintPageEventArgs e) { #if false long x1 = System.DateTime.Now.Ticks; #endif // Set these now because the Graphics object will be locked below. m_rcDst = m_rcSrc = new Rect(0, 0, (int)e.Graphics.DpiX, (int)e.Graphics.DpiY); m_dxpAvailWidth = PixelsFrom100ths(e.MarginBounds.Width, (int)e.Graphics.DpiX); // Create and initialize a print context. m_vwPrintContext = VwPrintContextWin32Class.Create(); // TODO: When we provide a way for the user to specify the nFirstPageNo (i.e. the // first argument to SetPagePrintInfo), then change the arguments to // SetPagePrintInfo. m_vwPrintContext.SetPagePrintInfo(1, 1, 65535, 1, false); SetMargins(e); IVwGraphics vwGraphics = VwGraphicsWin32Class.Create(); IntPtr hdc = IntPtr.Zero; try { // Get the printer's hdc and use it to initialize other stuff. hdc = e.Graphics.GetHdc(); ((IVwGraphicsWin32)vwGraphics).Initialize(hdc); m_vwPrintContext.SetGraphics(vwGraphics); // Make a rootbox for printing and initialize it. m_rootb = VwRootBoxClass.Create(); m_rootb.SetSite(this); m_rootb.DataAccess = m_sda; m_rootb.SetRootObject(m_hvo, m_vc, m_frags, m_styleSheet); m_rootb.InitializePrinting(m_vwPrintContext); m_totalNumberOfPages = m_rootb.GetTotalPrintPages(m_vwPrintContext, null); m_psettings = e.PageSettings.PrinterSettings; SetPrintRange(); } catch { m_rootb = null; string errorMsg = ResourceHelper.GetResourceString("kstidErrorCreatingPrintRootSite"); // TODO: Add the help file and url when we have a help file. MessageBox.Show(errorMsg, "", MessageBoxButtons.OK, MessageBoxIcon.Error); } finally { if (hdc != IntPtr.Zero) { vwGraphics.ReleaseDC(); e.Graphics.ReleaseHdc(hdc); } } #if false long x2 = System.DateTime.Now.Ticks; Debug.WriteLine("PrintRootSite.Init() took " + DeltaTime(x1, x2) + " seconds."); #endif }
/// ----------------------------------------------------------------------------------- /// <summary>Member FwXYFromGlyphPoint</summary> /// <param name='chw'>chw</param> /// <param name='nPoint'>nPoint</param> /// <param name='_xRet'>_xRet</param> /// <param name='_yRet'>_yRet</param> /// ----------------------------------------------------------------------------------- public void FwXYFromGlyphPoint(int chw, int nPoint, out int _xRet, out int _yRet) { Debug.Assert(m_bitmapGraphics != null); IVwGraphics vwGraphics = VwGraphicsWin32Class.Create(); IntPtr hdc = m_bitmapGraphics.GetHdc(); ((IVwGraphicsWin32)vwGraphics).FwInitialize(hdc); vwGraphics.FwXYFromGlyphPoint(chw, nPoint, out _xRet, out _yRet); m_bitmapGraphics.ReleaseHdc(hdc); }
public void SimpleCreationAndRelease() { IVwGraphicsWin32 vwGraphics = VwGraphicsWin32Class.Create(); Assert.IsNotNull(vwGraphics); using (var gr = new GraphicsObjectFromImage()) { vwGraphics.Initialize(gr.Graphics.GetHdc()); vwGraphics.ReleaseDC(); } }
/// ----------------------------------------------------------------------------------- /// <summary>Member FwGetFontDataRgch</summary> /// <param name='nTableId'>nTableId</param> /// <param name='_cbTableSz'>_cbTableSz</param> /// <param name='_rgch'>_rgch</param> /// <param name='cchMax'>cchMax</param> /// ----------------------------------------------------------------------------------- public void FwGetFontDataRgch(int nTableId, out int _cbTableSz, [MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof(ArrayPtrMarshaler))] ArrayPtr _rgch, int cchMax) { Debug.Assert(m_bitmapGraphics != null); IVwGraphics vwGraphics = VwGraphicsWin32Class.Create(); IntPtr hdc = m_bitmapGraphics.GetHdc(); ((IVwGraphicsWin32)vwGraphics).FwInitialize(hdc); vwGraphics.FwGetFontDataRgch(nTableId, out _cbTableSz, _rgch, cchMax); m_bitmapGraphics.ReleaseHdc(hdc); }
/// ----------------------------------------------------------------------------------- /// <summary>Member FwGetFontData</summary> /// <param name='nTableId'>nTableId</param> /// <param name='_cbTableSz'>_cbTableSz</param> /// <returns>A System.String</returns> /// ----------------------------------------------------------------------------------- public string FwGetFontData(int nTableId, out int _cbTableSz) { Debug.Assert(m_bitmapGraphics != null); IVwGraphics vwGraphics = VwGraphicsWin32Class.Create(); IntPtr hdc = m_bitmapGraphics.GetHdc(); ((IVwGraphicsWin32)vwGraphics).FwInitialize(hdc); string fontData = vwGraphics.FwGetFontData(nTableId, out _cbTableSz); m_bitmapGraphics.ReleaseHdc(hdc); return(fontData); }
/// ----------------------------------------------------------------------------------- /// <summary>Member FwGetGlyphMetrics</summary> /// <param name='chw'>chw</param> /// <param name='_sBoundingWidth'>_sBoundingWidth</param> /// <param name='_yBoundingHeight'>_yBoundingHeight</param> /// <param name='_xBoundingX'>_xBoundingX</param> /// <param name='_yBoundingY'>_yBoundingY</param> /// <param name='_xAdvanceX'>_xAdvanceX</param> /// <param name='_yAdvanceY'>_yAdvanceY</param> /// ----------------------------------------------------------------------------------- public void FwGetGlyphMetrics(int chw, out int _sBoundingWidth, out int _yBoundingHeight, out int _xBoundingX, out int _yBoundingY, out int _xAdvanceX, out int _yAdvanceY) { Debug.Assert(m_bitmapGraphics != null); IVwGraphics vwGraphics = VwGraphicsWin32Class.Create(); IntPtr hdc = m_bitmapGraphics.GetHdc(); ((IVwGraphicsWin32)vwGraphics).FwInitialize(hdc); vwGraphics.FwGetGlyphMetrics(chw, out _sBoundingWidth, out _yBoundingHeight, out _xBoundingX, out _yBoundingY, out _xAdvanceX, out _yAdvanceY); m_bitmapGraphics.ReleaseHdc(hdc); }
/// <summary> /// Draws the Root into a memory buffer, then rotates it 90deg clockwise while drawing /// the memory buffer to the screen. /// See C++ documentation for more info. /// </summary> public void DrawTheRootRotated(IVwRootBox rootb, IntPtr hdc, Rect rcpDraw, uint bkclr, bool fDrawSel, IVwRootSite vrs, int nHow) { IVwGraphicsWin32 qvg32 = VwGraphicsWin32Class.Create(); Rectangle rcp = new Rectangle(rcpDraw.top, rcpDraw.left, rcpDraw.bottom, rcpDraw.right); Rectangle rcFill = new Rect(0, 0, rcp.Width, rcp.Height); using (Graphics screen = Graphics.FromHdc(hdc)) using (var memoryBuffer = new MemoryBuffer(rcp.Width, rcp.Height)) { memoryBuffer.Graphics.FillRectangle(new SolidBrush(ColorUtil.ConvertBGRtoColor(bkclr)), rcFill); qvg32.Initialize(memoryBuffer.Graphics.GetHdc()); IVwGraphics qvgDummy = null; try { Rect rcDst, rcSrc; vrs.GetGraphics(rootb, out qvgDummy, out rcSrc, out rcDst); Rectangle temp = rcDst; temp.Offset(-rcp.Left, -rcp.Top); rcDst = temp; qvg32.XUnitsPerInch = qvgDummy.XUnitsPerInch; qvg32.YUnitsPerInch = qvgDummy.YUnitsPerInch; rootb.DrawRoot(qvg32, rcSrc, rcDst, fDrawSel); vrs.ReleaseGraphics(rootb, qvgDummy); qvgDummy = null; } catch (Exception) { if (qvgDummy != null) { vrs.ReleaseGraphics(rootb, qvgDummy); } qvg32.ReleaseDC(); throw; } Point[] rgptTransform = new Point[3]; rgptTransform[0] = new Point(rcpDraw.right, rcpDraw.top); // upper left of actual drawing maps to top right of rotated drawing rgptTransform[1] = new Point(rcpDraw.right, rcpDraw.bottom); // upper right of actual drawing maps to bottom right of rotated drawing. rgptTransform[2] = new Point(rcpDraw.left, rcpDraw.top); // bottom left of actual drawing maps to top left of rotated drawing. screen.DrawImage((Image)memoryBuffer.Bitmap, rgptTransform, new Rectangle(0, 0, rcp.Width, rcp.Height), GraphicsUnit.Pixel); qvg32.ReleaseDC(); } }
/// ------------------------------------------------------------------------------------ /// <summary> /// Make sure the graphics object has a DC. If it already has, increment a count, /// so we know when to really free the DC. /// </summary> /// <param name="dpix">The DPI in the x direction.</param> /// <param name="dpiy">The DPI in the y direction.</param> /// <param name="zoom">The zoom percentage.</param> /// ------------------------------------------------------------------------------------ private void Init(int dpix, int dpiy, float zoom) { CheckDisposed(); if (m_cactInitGraphics == 0) { // We are asking for a VwGraphics but haven't been given a DC. Make one. // (ReleaseHdc is called in Uninit!) // TODO: we might want a graphics appropriate for our printer. m_graphics = m_parent.CreateGraphics(); if (m_vwGraphics == null) { m_vwGraphics = VwGraphicsWin32Class.Create(); } if (m_graphics != null) { if (dpix <= 0) { dpix = (int)(m_graphics.DpiX * zoom); } if (dpiy <= 0) { dpiy = (int)(m_graphics.DpiY * zoom); } IntPtr hdc = m_graphics.GetHdc(); m_vwGraphics.Initialize(hdc); } else { // we think m_graphics should never be null. But it has happened (see e.g. LTB-708). // So provide a desperate fall-back. if (dpix <= 0) { dpix = 96; } if (dpiy <= 0) { dpiy = 96; } SIL.Utils.Logger.WriteEvent(String.Format("WARNING: failed to create m_graphics in GraphicsManager.Init({0}, {1}, {2})", dpix, dpiy, zoom)); } m_vwGraphics.XUnitsPerInch = dpix; m_vwGraphics.YUnitsPerInch = dpiy; } m_cactInitGraphics++; }
public void LargeRectangles() { // EB/2011-02-08: this test is failing for me on Linux. Not sure if it should work // or what (see FWNX-449). Test is failing in FillRectangle similar to the failure // in the following comment // https://www.jira.insitehome.org/browse/FWNX-449?focusedCommentId=108054&page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel#comment-108054 IVwGraphicsWin32 vwGraphics = VwGraphicsWin32Class.Create(); Assert.IsNotNull(vwGraphics); const int width = 1241; const int height = 56080; // something bigger than MAX_IMAGE_SIZE (32767) using (var gr = new GraphicsObjectFromImage(width, height)) { // start with a blue background. using (var blueBrush = new SolidBrush(Color.Blue)) { gr.Graphics.FillRectangle(blueBrush, new Rectangle(0, 0, width, height)); // Check that filling with a blue brush worked. Assert.IsTrue(ColorCompare(gr.Bitmap.GetPixel(width - 1, height - 1), Color.Blue)); ///// // Check that drawing using a VwGraphics works. //// vwGraphics.Initialize(gr.Graphics.GetHdc()); vwGraphics.PushClipRect(new Utils.Rect(0, 0, width, height)); vwGraphics.BackColor = ConvertToVwGraphicsColor(Color.Red); vwGraphics.DrawRectangle(0, 0, width, height); vwGraphics.PopClipRect(); vwGraphics.ReleaseDC(); gr.Graphics.ReleaseHdc(); gr.Graphics.Flush(); // Check that drawing a red rectangle using the VwGraphics Interface worked Assert.IsTrue(ColorCompare(gr.Bitmap.GetPixel(width - 1, height - 1), Color.Red)); } } }
internal void TestGetTextExtentHelper(string testString) { IVwGraphicsWin32 vwGraphics = VwGraphicsWin32Class.Create(); Assert.IsNotNull(vwGraphics); using (var gr = new GraphicsObjectFromImage()) { const int areaWidth = 500; const int areaHeight = 500; // start with a White background. using (var blueBrush = new SolidBrush(Color.White)) { gr.Graphics.FillRectangle(blueBrush, new Rectangle(0, 0, areaWidth, areaHeight)); Assert.AreEqual(-1, SearchForBottomMostNonWhitePixel(gr.Bitmap, areaWidth, areaHeight), "Should all be white #1"); Assert.AreEqual(-1, SearchForRightMostNonWhitePixel(gr.Bitmap, areaWidth, areaHeight), "Should all be white #2"); vwGraphics.Initialize(gr.Graphics.GetHdc()); vwGraphics.PushClipRect(new Utils.Rect(0, 0, areaWidth, areaHeight)); vwGraphics.ForeColor = ConvertToVwGraphicsColor(Color.Black); int extentX; int extentY; vwGraphics.GetTextExtent(testString.Length, testString, out extentX, out extentY); Assert.That(extentX > 0, "extentX should be greater than 0"); Assert.That(extentY > 0, "extentY should be greater than 0"); vwGraphics.DrawText(0, 0, testString.Length, testString, 0); vwGraphics.PopClipRect(); vwGraphics.ReleaseDC(); gr.Graphics.ReleaseHdc(); gr.Graphics.Flush(); Assert.That(SearchForBottomMostNonWhitePixel(gr.Bitmap, areaWidth, areaHeight) <= extentY, String.Format("Should be <= {0}", extentY)); Assert.That(SearchForRightMostNonWhitePixel(gr.Bitmap, areaWidth, areaHeight) <= extentX, String.Format("Should be <= {0}", extentX)); } } }
public void ComplexClipping() { IVwGraphicsWin32 vwGraphics = VwGraphicsWin32Class.Create(); Assert.IsNotNull(vwGraphics); using (var gr = new GraphicsObjectFromImage()) { vwGraphics.Initialize(gr.Graphics.GetHdc()); // Test on a single push int left, top, right, bottom; vwGraphics.PushClipRect(new Utils.Rect(50, 60, 500, 510)); vwGraphics.GetClipRect(out left, out top, out right, out bottom); Assert.AreEqual(50, left, "Left doesn't match"); Assert.AreEqual(60, top, "Top doesn't match"); Assert.AreEqual(500, right, "Right doesn't match"); Assert.AreEqual(510, bottom, "Bottom doesn't match"); // Test on a second push vwGraphics.PushClipRect(new Utils.Rect(1, 1, 300, 310)); vwGraphics.GetClipRect(out left, out top, out right, out bottom); Assert.AreEqual(50, left, "Left doesn't match"); Assert.AreEqual(60, top, "Top doesn't match"); Assert.AreEqual(300, right, "Right doesn't match"); Assert.AreEqual(310, bottom, "Bottom doesn't match"); vwGraphics.PopClipRect(); vwGraphics.GetClipRect(out left, out top, out right, out bottom); Assert.AreEqual(50, left, "Left doesn't match"); Assert.AreEqual(60, top, "Top doesn't match"); Assert.AreEqual(500, right, "Right doesn't match"); Assert.AreEqual(510, bottom, "Bottom doesn't match"); vwGraphics.PopClipRect(); vwGraphics.GetClipRect(out left, out top, out right, out bottom); Assert.AreEqual(0, left, "Left doesn't match"); Assert.AreEqual(0, top, "Top doesn't match"); Assert.AreEqual(1000, right, "Right doesn't match"); Assert.AreEqual(1000, bottom, "Bottom doesn't match"); vwGraphics.ReleaseDC(); gr.Graphics.ReleaseHdc(); } }
public void TextClipping() { const string longString = "abcdefghijklmnopqrstuvwzyzabcdefghijklmnopqrstuvwzyzabcdefghijklmnopqrstuvwzyz"; IVwGraphicsWin32 vwGraphics = VwGraphicsWin32Class.Create(); Assert.IsNotNull(vwGraphics); using (var gr = new GraphicsObjectFromImage()) { const int areaWidth = 500; const int areaHeight = 500; const int clipLeft = 300; const int clipRight = 400; // start with a White background. using (var blueBrush = new SolidBrush(Color.White)) { gr.Graphics.FillRectangle(blueBrush, new Rectangle(0, 0, areaWidth, areaHeight)); Assert.AreEqual(-1, SearchForBottomMostNonWhitePixel(gr.Bitmap, areaWidth, areaHeight), "Should all be white #1"); Assert.AreEqual(-1, SearchForRightMostNonWhitePixel(gr.Bitmap, areaWidth, areaHeight), "Should all be white #2"); vwGraphics.Initialize(gr.Graphics.GetHdc()); vwGraphics.PushClipRect(new Utils.Rect(clipLeft, 0, clipRight, areaHeight)); vwGraphics.ForeColor = ConvertToVwGraphicsColor(Color.Black); vwGraphics.DrawText(0, 0, longString.Length, longString, 0); vwGraphics.PopClipRect(); vwGraphics.ReleaseDC(); gr.Graphics.ReleaseHdc(); gr.Graphics.Flush(); Assert.That(SearchForLeftMostNonWhitePixel(gr.Bitmap, areaWidth, areaHeight) >= clipLeft, String.Format("Should be >= {0}", clipLeft)); Assert.That(SearchForRightMostNonWhitePixel(gr.Bitmap, areaWidth, areaHeight) <= clipRight, String.Format("Should be <= {0}", clipRight)); } } }
public void GetClipRect() { IVwGraphicsWin32 vwGraphics = VwGraphicsWin32Class.Create(); Assert.IsNotNull(vwGraphics); using (var gr = new GraphicsObjectFromImage()) { vwGraphics.Initialize(gr.Graphics.GetHdc()); var rect1 = new Utils.Rect(0, 0, 1000, 1000); var rect2 = new Utils.Rect(500, 500, 700, 700); vwGraphics.PushClipRect(rect1); int left, top, right, bottom; vwGraphics.GetClipRect(out left, out top, out right, out bottom); Assert.IsTrue(left == rect1.left, "First push failed: left"); Assert.IsTrue(right == rect1.right, "First push failed: right"); Assert.IsTrue(top == rect1.top, "First push failed: top"); Assert.IsTrue(bottom == rect1.bottom, "First push failed: bottom"); // try a second rectangle vwGraphics.PushClipRect(rect2); vwGraphics.GetClipRect(out left, out top, out right, out bottom); Assert.IsTrue(left == rect2.left, "Second push failed: left"); Assert.IsTrue(right == rect2.right, "Second push failed: right"); Assert.IsTrue(top == rect2.top, "Second push failed: top"); Assert.IsTrue(bottom == rect2.bottom, "Second push failed: bottom"); vwGraphics.PopClipRect(); vwGraphics.PopClipRect(); vwGraphics.ReleaseDC(); gr.Graphics.ReleaseHdc(); } }
/// <summary> /// Make one with the specified control and Graphics (typically passed in to a Paint method). The Graphics will not be disposed. /// </summary> public GraphicsManager(Control c, Graphics g) { m_graphics = g; if (g == null) { m_graphics = c.CreateGraphics(); m_shouldDisposeGraphics = true; } m_vwGraphics = VwGraphicsWin32Class.Create(); double zoom = 1.0; // eventually an argument, probably m_dpiX = m_graphics.DpiX; m_dpiY = m_graphics.DpiY; int dpix = (int)(m_dpiX * zoom); int dpiy = (int)(m_dpiY * zoom); IntPtr hdc = m_graphics.GetHdc(); m_vwGraphics.Initialize(hdc); m_vwGraphics.XUnitsPerInch = dpix; m_vwGraphics.YUnitsPerInch = dpiy; m_vwGraphics.BackColor = (int)FwTextColor.kclrTransparent; }
public void SetClipRect() { IVwGraphicsWin32 vwGraphics = VwGraphicsWin32Class.Create(); Assert.IsNotNull(vwGraphics); using (var gr = new GraphicsObjectFromImage()) { vwGraphics.Initialize(gr.Graphics.GetHdc()); int left, top, right, bottom; var rect = new Utils.Rect(50, 25, 1000, 1000); vwGraphics.SetClipRect(ref rect); vwGraphics.GetClipRect(out left, out top, out right, out bottom); Assert.AreEqual(50, left, "Left doesn't match"); Assert.AreEqual(25, top, "Top doesn't match"); Assert.AreEqual(1000, right, "Right doesn't match"); Assert.AreEqual(1000, bottom, "Bottom doesn't match"); } }
/// <summary> /// See C++ documentation /// </summary> public void DrawTheRoot(IVwRootBox prootb, IntPtr hdc, Rect rcpDraw, uint bkclr, bool fDrawSel, IVwRootSite pvrs) { IVwSynchronizer synchronizer = prootb.Synchronizer; if (synchronizer != null) { try { if (synchronizer.IsExpandingLazyItems) { return; } } catch (Exception e) { Console.WriteLine("call to IsExpandingLazyItems caused exceptionException e ={0}", e); } } IVwGraphicsWin32 qvg = VwGraphicsWin32Class.Create(); Rectangle rcp = rcpDraw; using (Graphics screen = Graphics.FromHdc(hdc)) using (var memoryBuffer = new MemoryBuffer(rcp.Width, rcp.Height)) { memoryBuffer.Graphics.FillRectangle(new SolidBrush(ColorUtil.ConvertBGRtoColor(bkclr)), 0, 0, rcp.Width, rcp.Height); qvg.Initialize(memoryBuffer.Graphics.GetHdc()); VwPrepDrawResult xpdr = VwPrepDrawResult.kxpdrAdjust; IVwGraphics qvgDummy = null; try { Rect rcDst, rcSrc; while (xpdr == VwPrepDrawResult.kxpdrAdjust) { pvrs.GetGraphics(prootb, out qvgDummy, out rcSrc, out rcDst); Rectangle temp = rcDst; temp.Offset(-rcp.Left, -rcp.Top); rcDst = temp; qvg.XUnitsPerInch = qvgDummy.XUnitsPerInch; qvg.YUnitsPerInch = qvgDummy.YUnitsPerInch; xpdr = prootb.PrepareToDraw(qvg, rcSrc, rcDst); pvrs.ReleaseGraphics(prootb, qvgDummy); qvgDummy = null; } if (xpdr != VwPrepDrawResult.kxpdrInvalidate) { pvrs.GetGraphics(prootb, out qvgDummy, out rcSrc, out rcDst); Rectangle temp = rcDst; temp.Offset(-rcp.Left, -rcp.Top); rcDst = temp; qvg.XUnitsPerInch = qvgDummy.XUnitsPerInch; qvg.YUnitsPerInch = qvgDummy.YUnitsPerInch; try { prootb.DrawRoot(qvg, rcSrc, rcDst, fDrawSel); } catch (Exception e) { Console.WriteLine("DrawRoot e = {0} qvg = {1} rcSrc = {2} rcDst = {3} fDrawSel = {4}", e, qvg, rcSrc, rcDst, fDrawSel); } pvrs.ReleaseGraphics(prootb, qvgDummy); qvgDummy = null; } } catch (Exception) { if (qvgDummy != null) { pvrs.ReleaseGraphics(prootb, qvgDummy); } qvg.ReleaseDC(); throw; } if (xpdr != VwPrepDrawResult.kxpdrInvalidate) { screen.DrawImageUnscaled(memoryBuffer.Bitmap, rcp.Left, rcp.Top, rcp.Width, rcp.Height); } qvg.ReleaseDC(); } }
private void Init(PrintPageEventArgs e) { #if false long x1 = System.DateTime.Now.Ticks; #endif // Set these now because the Graphics object will be locked below. m_rcDst = m_rcSrc = new Rect(0, 0, (int)e.Graphics.DpiX, (int)e.Graphics.DpiY); int dpix; if (MiscUtils.IsUnix) { dpix = 72; } else { dpix = (int)e.Graphics.DpiX; } m_dxpAvailWidth = PixelsFrom100ths(e.MarginBounds.Width, dpix); // Create and initialize a print context. m_vwPrintContext = VwPrintContextWin32Class.Create(); // TODO: When we provide a way for the user to specify the nFirstPageNo (i.e. the // first argument to SetPagePrintInfo), then change the arguments to // SetPagePrintInfo. m_vwPrintContext.SetPagePrintInfo(1, 1, 65535, 1, false); SetMargins(e); IVwGraphics vwGraphics = VwGraphicsWin32Class.Create(); IntPtr hdc = IntPtr.Zero; try { // Get the printer's hdc and use it to initialize other stuff. hdc = e.Graphics.GetHdc(); ((IVwGraphicsWin32)vwGraphics).Initialize(hdc); m_vwPrintContext.SetGraphics(vwGraphics); // Make a rootbox for printing and initialize it. m_rootb = VwRootBoxClass.Create(); m_rootb.SetSite(this); m_rootb.DataAccess = m_sda; m_rootb.SetRootObject(m_hvo, m_vc, m_frags, m_styleSheet); m_rootb.InitializePrinting(m_vwPrintContext); m_totalNumberOfPages = m_rootb.GetTotalPrintPages(m_vwPrintContext); m_psettings = e.PageSettings.PrinterSettings; SetPrintRange(); } catch (Exception ex) { m_rootb = null; throw new ContinuableErrorException("An error has occurred during the setup required for printing.", ex); } finally { if (hdc != IntPtr.Zero) { vwGraphics.ReleaseDC(); e.Graphics.ReleaseHdc(hdc); } } #if false long x2 = System.DateTime.Now.Ticks; Debug.WriteLine("PrintRootSite.Init() took " + DeltaTime(x1, x2) + " seconds."); #endif }