internal static void MeasureCharWidths(IntPtr hFont, out int[] charWidths, out NativeTextWin32.FontABC[] abcSizes) { //only in ascii range //current version charWidths = new int[MAX_CODEPOINT_NO + 1]; // MyWin32.SelectObject(win32MemDc.DC, hFont); unsafe { //see: https://msdn.microsoft.com/en-us/library/ms404377(v=vs.110).aspx //A code page contains 256 code points and is zero-based. //In most code pages, code points 0 through 127 represent the ASCII character set, //and code points 128 through 255 differ significantly between code pages abcSizes = new NativeTextWin32.FontABC[MAX_CODEPOINT_NO + 1]; fixed(NativeTextWin32.FontABC *abc = abcSizes) { NativeTextWin32.GetCharABCWidths(win32MemDc.DC, (uint)0, (uint)MAX_CODEPOINT_NO, abc); } for (int i = 0; i < (MAX_CODEPOINT_NO + 1); ++i) { charWidths[i] = abcSizes[i].Sum; } } }
public void Reset(int hPageNum, int vPageNum, int newWidth, int newHeight) { this.pageNumFlags = (hPageNum << 8) | vPageNum; this.ReleaseUnManagedResource(); this.ClearPreviousStoredValues(); var orgHdc = MyWin32.CreateCompatibleDC(IntPtr.Zero); System.Drawing.Bitmap bmp = new System.Drawing.Bitmap(newWidth, newHeight, System.Drawing.Imaging.PixelFormat.Format32bppArgb); hbmp = bmp.GetHbitmap(); MyWin32.SelectObject(orgHdc, hbmp); MyWin32.PatBlt(orgHdc, 0, 0, newWidth, newHeight, MyWin32.WHITENESS); MyWin32.SetBkMode(orgHdc, MyWin32._SetBkMode_TRANSPARENT); hFont = defaultHFont; MyWin32.SelectObject(orgHdc, hFont); currentClipRect = new System.Drawing.Rectangle(0, 0, newWidth, newHeight); MyWin32.SelectObject(orgHdc, hRgn); gx = System.Drawing.Graphics.FromHdc(orgHdc); this.originalHdc = orgHdc; gx.Clear(System.Drawing.Color.White); MyWin32.SetRectRgn(hRgn, 0, 0, newWidth, newHeight); left = hPageNum * newWidth; top = vPageNum * newHeight; right = left + newWidth; bottom = top + newHeight; #if DEBUG debug_resetCount++; #endif }
public override void DrawText(char[] buffer, Rectangle logicalTextBox, int textAlignment) { ReleaseHdc(); IntPtr gxdc = gx.GetHdc(); var clipRect = System.Drawing.Rectangle.Intersect(logicalTextBox.ToRect(), currentClipRect); clipRect.Offset(canvasOriginX, canvasOriginY); MyWin32.SetRectRgn(hRgn, clipRect.Left, clipRect.Top, clipRect.Right, clipRect.Bottom); MyWin32.SelectClipRgn(gxdc, hRgn); NativeTextWin32.TextOut(gxdc, CanvasOrgX + logicalTextBox.X, CanvasOrgY + logicalTextBox.Y, buffer, buffer.Length); MyWin32.SelectClipRgn(gxdc, IntPtr.Zero); gx.ReleaseHdc(); //ReleaseHdc(); //IntPtr gxdc = gx.GetHdc(); //MyWin32.SetViewportOrgEx(gxdc, CanvasOrgX, CanvasOrgY, IntPtr.Zero); //System.Drawing.Rectangle clipRect = // System.Drawing.Rectangle.Intersect(logicalTextBox.ToRect(), currentClipRect); //clipRect.Offset(CanvasOrgX, CanvasOrgY); //MyWin32.SetRectRgn(hRgn, clipRect.X, clipRect.Y, clipRect.Right, clipRect.Bottom); //MyWin32.SelectClipRgn(gxdc, hRgn); //NativeTextWin32.TextOut(gxdc, logicalTextBox.X, logicalTextBox.Y, buffer, buffer.Length); //MyWin32.SelectClipRgn(gxdc, IntPtr.Zero); //MyWin32.SetViewportOrgEx(gxdc, -CanvasOrgX, -CanvasOrgY, IntPtr.Zero); //gx.ReleaseHdc(); }
static WinGdiFont SetFont(RequestFont font) { WinGdiFont winFont = WinGdiFontSystem.GetWinGdiFont(font); MyWin32.SelectObject(win32MemDc.DC, winFont.CachedHFont()); return(winFont); }
public static void Main() { // Use interop to set a console control handler. MyWin32.HandlerRoutine hr = new MyWin32.HandlerRoutine(Handler); MyWin32.SetConsoleCtrlHandler(hr, true); // Give the user some time to raise a few events. Console.WriteLine("Waiting 30 seconds for console ctrl events..."); // The object hr is not referred to again. // The garbage collector can detect that the object has no // more managed references and might clean it up here while // the unmanaged SetConsoleCtrlHandler method is still using it. // Force a garbage collection to demonstrate how the hr // object will be handled. GC.Collect(); GC.WaitForPendingFinalizers(); GC.Collect(); Thread.Sleep(30000); // Display a message to the console when the unmanaged method // has finished its work. Console.WriteLine("Finished!"); // Call GC.KeepAlive(hr) at this point to maintain a reference to hr. // This will prevent the garbage collector from collecting the // object during the execution of the SetConsoleCtrlHandler method. GC.KeepAlive(hr); Console.Read(); }
public void MeasureCharWidths(IntPtr hFont, out int[] charWidths, out NativeTextWin32.FontABC[] abcSizes) { if (!isInit) { Init(); } //only in ascii range //current version charWidths = new int[256]; MyWin32.SelectObject(hdc, hFont); unsafe { abcSizes = new NativeTextWin32.FontABC[256]; fixed(NativeTextWin32.FontABC *abc = abcSizes) { NativeTextWin32.GetCharABCWidths(hdc, (uint)0, (uint)255, abc); } for (int i = 0; i < 161; i++) { charWidths[i] = abcSizes[i].Sum; } for (int i = 161; i < 255; i++) { charWidths[i] = abcSizes[i].Sum; } } }
public override void RenderTo(IntPtr destHdc, int sourceX, int sourceY, Rectangle destArea) { MyWin32.SetViewportOrgEx(win32MemDc.DC, CanvasOrgX, CanvasOrgY, IntPtr.Zero); MyWin32.BitBlt( destHdc, destArea.X, destArea.Y, destArea.Width, destArea.Height, //dest win32MemDc.DC, sourceX, sourceY, MyWin32.SRCCOPY); //src MyWin32.SetViewportOrgEx(win32MemDc.DC, -CanvasOrgX, -CanvasOrgY, IntPtr.Zero); }
static void Main(string[] args) { var handle = MyWin32.CreateToolhelp32Snapshot(new IntPtr(4), new IntPtr(System.Diagnostics.Process.GetCurrentProcess().Id)); Console.WriteLine(handle); Console.WriteLine(MyWin32.CloseHandle(handle)); Console.ReadLine(); }
void PrepareCharacterMapWhiteOnBlack(char[] buffer) { //----------------------------------------------------------------------- IntPtr gxdc = gx.GetHdc(); int len = buffer.Length; //draw each character int curX = 0; int curY = 0; //1. clear with white color, MyWin32.PatBlt(gxdc, 0, 0, width, height, MyWin32.BLACKNESS); //2. transparent background MyWin32.SetBkMode(gxdc, MyWin32._SetBkMode_TRANSPARENT); //3. white brush //set user font to dc MyWin32.SelectObject(gxdc, this.hFont); int rgb = ((255 & 0xFF) << 16 | (255 & 0xFF) << 8 | 255); MyWin32.SetTextColor(gxdc, rgb); //TODO:correct white text on black bg for subpixel rendering //when draw with subpixel rendering //on white bg -> red come first from left ,end with blue //on black bg -> blue come first from left, end with red int fontHeight = fontInfo.FontHeight; int maxLineHeight = fontHeight; for (int i = 0; i < len; ++i) { //----------------------------------------------------------------------- //measure string //and make simple character map //----------------------------------------------------------------------- //measure each character *** //not adjust kerning*** char c = buffer[i]; FontABC abcWidth = fontInfo.GetCharABCWidth(c); int glyphBoxWidth = Math.Abs(abcWidth.a) + (int)abcWidth.b + abcWidth.c; if (abcWidth.Sum + curX > this.width) { //start newline curX = 0; curY += maxLineHeight; maxLineHeight = fontHeight; } NativeTextWin32.TextOut(gxdc, curX, curY, new char[] { c }, 1); charMap.Add(c, new PixelFarm.Drawing.RectangleF(curX, curY, glyphBoxWidth, fontHeight)); curX += glyphBoxWidth; //move next } gx.ReleaseHdc(gxdc); //myTextBoardBmp = new Bitmap(width, height, new LazyGdiBitmapBufferProvider(this.textBoardBmp)); //myTextBoardBmp.InnerImage = GLBitmapTextureHelper.CreateBitmapTexture(this.textBoardBmp); }
public override void RenderTo(IntPtr destHdc, int sourceX, int sourceY, Rectangle destArea) { IntPtr gxdc = gx.GetHdc(); MyWin32.SetViewportOrgEx(gxdc, CanvasOrgX, CanvasOrgY, IntPtr.Zero); MyWin32.BitBlt(destHdc, destArea.X, destArea.Y, destArea.Width, destArea.Height, gxdc, sourceX, sourceY, MyWin32.SRCCOPY); MyWin32.SetViewportOrgEx(gxdc, -CanvasOrgX, -CanvasOrgY, IntPtr.Zero); gx.ReleaseHdc(); }
public int MeasureStringWidth(IntPtr hFont, char[] buffer, int length) { if (!isInit) { Init(); } MyWin32.SelectObject(this.hdc, hFont); NativeTextWin32.WIN32SIZE size; NativeTextWin32.GetTextExtentPoint32(hdc, buffer, length, out size); return(size.Width); }
public void Reuse(int hPageNum, int vPageNum) { this.pageNumFlags = (hPageNum << 8) | vPageNum; int w = this.Width; int h = this.Height; this.ClearPreviousStoredValues(); currentClipRect = new System.Drawing.Rectangle(0, 0, w, h); gx.Clear(System.Drawing.Color.White); MyWin32.SetRectRgn(hRgn, 0, 0, w, h); left = hPageNum * w; top = vPageNum * h; right = left + w; bottom = top + h; }
void PrepareCharacterMapBlackOnWhite(char[] buffer) { //----------------------------------------------------------------------- IntPtr gxdc = gx.GetHdc(); int len = buffer.Length; //draw each character int curX = 0; int curY = 0; //1. clear with white color, MyWin32.PatBlt(gxdc, 0, 0, width, height, MyWin32.WHITENESS); //2. transparent background MyWin32.SetBkMode(gxdc, MyWin32._SetBkMode_TRANSPARENT); //set user font to dc MyWin32.SelectObject(gxdc, this.hFont); int fontHeight = fontInfo.FontHeight; int maxLineHeight = fontHeight; for (int i = 0; i < len; ++i) { //----------------------------------------------------------------------- //measure string //and make simple character map //----------------------------------------------------------------------- //measure each character *** //not adjust kerning*** char c = buffer[i]; FontABC abcWidth = fontInfo.GetCharABCWidth(c); int glyphBoxWidth = Math.Abs(abcWidth.a) + (int)abcWidth.b + abcWidth.c; if (abcWidth.Sum + curX > this.width) { //start newline curX = 0; curY += maxLineHeight; maxLineHeight = fontHeight; } NativeTextWin32.TextOut(gxdc, curX, curY, new char[] { c }, 1); charMap.Add(c, new PixelFarm.Drawing.RectangleF(curX, curY, glyphBoxWidth, fontHeight)); curX += glyphBoxWidth; //move next } gx.ReleaseHdc(gxdc); //myTextBoardBmp = new Bitmap(width, height, new LazyGdiBitmapBufferProvider(this.textBoardBmp)); //myTextBoardBmp.InnerImage = GLBitmapTextureHelper.CreateBitmapTexture(this.textBoardBmp); }
//============================================== public override void DrawText(char[] buffer, int x, int y) { ReleaseHdc(); IntPtr gxdc = gx.GetHdc(); var clipRect = currentClipRect; clipRect.Offset(canvasOriginX, canvasOriginY); MyWin32.SetRectRgn(hRgn, clipRect.Left, clipRect.Top, clipRect.Right, clipRect.Bottom); MyWin32.SelectClipRgn(gxdc, hRgn); NativeTextWin32.TextOut(gxdc, CanvasOrgX + x, CanvasOrgY + y, buffer, buffer.Length); MyWin32.SelectClipRgn(gxdc, IntPtr.Zero); gx.ReleaseHdc(); }
void ReleaseUnManagedResource() { if (hRgn != IntPtr.Zero) { MyWin32.DeleteObject(hRgn); hRgn = IntPtr.Zero; } MyWin32.DeleteDC(originalHdc); originalHdc = IntPtr.Zero; MyWin32.DeleteObject(hbmp); hbmp = IntPtr.Zero; clipRectStack.Clear(); currentClipRect = new System.Drawing.Rectangle(0, 0, this.Width, this.Height); #if DEBUG debug_releaseCount++; #endif }
static IntPtr InitFont(string fontName, float emHeight, FontStyle style) { //see: MSDN, LOGFONT structure //https://msdn.microsoft.com/en-us/library/windows/desktop/dd145037(v=vs.85).aspx MyWin32.LOGFONT logFont = new MyWin32.LOGFONT(); MyWin32.SetFontName(ref logFont, fontName); logFont.lfHeight = -(int)PixelFarm.Drawing.RequestFont.ConvEmSizeInPointsToPixels(emHeight); //minus ** logFont.lfCharSet = 1; //default logFont.lfQuality = 0; //default MyWin32.LOGFONT_FontWeight weight = ((style & FontStyle.Bold) == FontStyle.Bold) ? MyWin32.LOGFONT_FontWeight.FW_BOLD : MyWin32.LOGFONT_FontWeight.FW_REGULAR; logFont.lfWeight = (int)weight; // logFont.lfItalic = (byte)(((style & FontStyle.Italic) == FontStyle.Italic) ? 1 : 0); return(MyWin32.CreateFontIndirect(ref logFont)); }
void CreateGraphicsFromNativeHdc(int width, int height) { //3. create original dc from memory dc and prepare background var orgHdc = MyWin32.CreateCompatibleDC(IntPtr.Zero); bgBmp = new System.Drawing.Bitmap(width, height, System.Drawing.Imaging.PixelFormat.Format32bppArgb); hbmp = bgBmp.GetHbitmap(); MyWin32.SelectObject(orgHdc, hbmp); MyWin32.PatBlt(orgHdc, 0, 0, width, height, MyWin32.WHITENESS); MyWin32.SetBkMode(orgHdc, MyWin32._SetBkMode_TRANSPARENT); //4. font hFont = MyWin32.SelectObject(orgHdc, hFont); //5. region hRgn = MyWin32.CreateRectRgn(0, 0, width, height); MyWin32.SelectObject(orgHdc, hRgn); //6. create graphics from hdc*** gx = System.Drawing.Graphics.FromHdc(orgHdc); this.originalHdc = orgHdc; }
public override void DrawText(char[] str, int startAt, int len, Rectangle logicalTextBox, int textAlignment) { #if DEBUG dbugCounter.dbugDrawStringCount++; #endif var color = this.CurrentTextColor; if (color.A == 255) { var clipRect = Rectangle.Intersect(logicalTextBox, new Rectangle(currentClipRect.Left, currentClipRect.Top, currentClipRect.Width, currentClipRect.Height)); clipRect.Offset(canvasOriginX, canvasOriginY); MyWin32.SetRectRgn(hRgn, clipRect.Left, clipRect.Top, clipRect.Right, clipRect.Bottom); MyWin32.SelectClipRgn(tempDc, hRgn); unsafe { fixed(char *startAddr = &str[0]) { NativeTextWin32.TextOutUnsafe(tempDc, (int)logicalTextBox.X + canvasOriginX, (int)logicalTextBox.Y + canvasOriginY, (startAddr + startAt), len); } } MyWin32.SelectClipRgn(tempDc, IntPtr.Zero); #if DEBUG //NativeTextWin32.dbugDrawTextOrigin(tempDc, // logicalTextBox.X + canvasOriginX, // logicalTextBox.Y + canvasOriginY); #endif } else { //translucent / transparent text InitHdc(); var intersectRect = Rectangle.Intersect(logicalTextBox, new Rectangle(currentClipRect.Left, currentClipRect.Top, currentClipRect.Width, currentClipRect.Height)); intersectRect.Offset(canvasOriginX, canvasOriginY); MyWin32.SetRectRgn(hRgn, intersectRect.Left, intersectRect.Top, intersectRect.Right, intersectRect.Bottom); MyWin32.SelectClipRgn(tempDc, hRgn); unsafe { fixed(char *startAddr = &str[0]) { NativeTextWin32.TextOutUnsafe(tempDc, logicalTextBox.X + canvasOriginX, logicalTextBox.Y + canvasOriginY, (startAddr + startAt), len); } } #if DEBUG //NativeTextWin32.dbugDrawTextOrigin(tempDc, // logicalTextBox.X + canvasOriginX, // logicalTextBox.Y + canvasOriginY); #endif } }
protected override void OnDispose() { //TODO: review here MyWin32.DeleteObject(hfont); hfont = IntPtr.Zero; }