private static void glBuildFont() { IntPtr font; IntPtr oldfont; fontbase = Gl.glGenLists(96); font = Gdi.CreateFont( -10, // Height Of Font 0, // Width Of Font 0, // Angle Of Escapement 0, // Orientation Angle Gdi.FW_BOLD, // Font Weight false, // Italic false, // Underline false, // Strikeout Gdi.ANSI_CHARSET, // Character Set Identifier Gdi.OUT_TT_PRECIS, // Output Precision Gdi.CLIP_DEFAULT_PRECIS, // Clipping Precision Gdi.ANTIALIASED_QUALITY, // Output Quality Gdi.FF_DONTCARE | Gdi.DEFAULT_PITCH, // Family And Pitch "Verdana"); // Font Name oldfont = Gdi.SelectObject(hDC, font); // Selects The Font We Want Wgl.wglUseFontBitmaps(hDC, 32, 96, fontbase); // Builds 96 Characters Starting At Character 32 Gdi.SelectObject(hDC, oldfont); // Selects The Font We Want Gdi.DeleteObject(font); // Delete The Font }
// --- Private Static Methods --- #region BuildFont() /// <summary> /// Builds our bitmap font. /// </summary> private static void BuildFont() { Gdi.GLYPHMETRICSFLOAT[] gmf = new Gdi.GLYPHMETRICSFLOAT[256]; // Address Buffer For Font Storage IntPtr font; // Windows Font ID fontbase = Gl.glGenLists(256); // Storage For 256 Characters font = Gdi.CreateFont( // Create The Font -12, // Height Of Font 0, // Width Of Font 0, // Angle Of Escapement 0, // Orientation Angle Gdi.FW_BOLD, // Font Weight false, // Italic false, // Underline false, // Strikeout Gdi.SYMBOL_CHARSET, // Character Set Identifier Gdi.OUT_TT_PRECIS, // Output Precision Gdi.CLIP_DEFAULT_PRECIS, // Clipping Precision Gdi.ANTIALIASED_QUALITY, // Output Quality Gdi.FF_DONTCARE | Gdi.DEFAULT_PITCH, // Family And Pitch "Wingdings"); // Font Name Gdi.SelectObject(hDC, font); // Selects The Font We Created Wgl.wglUseFontOutlinesA( hDC, // Select The Current DC 0, // Starting Character 255, // Number Of Display Lists To Build fontbase, // Starting Display Lists 0.1f, // Deviation From The True Outlines 0.2f, // Font Thickness In The Z Direction Wgl.WGL_FONT_POLYGONS, // Use Polygons, Not Lines gmf); // Address Of Buffer To Recieve Data }
public void Load(string FontFamily, int Size) { IntPtr font; IntPtr oldfont; fontbase = Gl.glGenLists(257); font = Gdi.CreateFont( -Size, 0, 0, 0, Gdi.FW_BOLD, false, false, false, Gdi.ANSI_CHARSET, Gdi.OUT_TT_PRECIS, Gdi.CLIP_DEFAULT_PRECIS, Gdi.ANTIALIASED_QUALITY, Gdi.FF_DONTCARE | Gdi.DEFAULT_PITCH, FontFamily ); IntPtr hDC = Wgl.wglGetCurrentDC(); oldfont = Gdi.SelectObject(hDC, font); Wgl.wglUseFontBitmapsA(hDC, 0, 256, fontbase); Gdi.SelectObject(hDC, oldfont); Gdi.DeleteObject(font); }
// Overriding the callback method will allow us to provide our own custom behavior protected override LResult WindowProcedure(WindowHandle window, MessageType message, WParam wParam, LParam lParam) { switch (message) { // The Paint message is sent when the Window contents need drawn. case MessageType.Paint: // Drawing is done in a Device Context by calling BeginPaint(). When the // DeviceContext is disposed it will call EndPaint(). using (DeviceContext dc = window.BeginPaint()) { Rectangle client = window.GetClientRectangle(); // The default font is really small on modern PCs, so we'll take the extra step // to select a font into our device context before drawing the text. using FontHandle font = Gdi.CreateFont( height: client.Height / 5, family: FontFamilyType.Swiss); dc.SelectObject(font); // Draw the given text in the middle of the client area of the Window. dc.DrawText( "Hello, .NET Core!", client, TextFormat.SingleLine | TextFormat.Center | TextFormat.VerticallyCenter); // Put the system font back as we're going to dispose our font dc.SelectObject(StockFont.System); } // Return 0 to indicate we've handled the message return(0); } // Let the base class handle any other messages return(base.WindowProcedure(window, message, wParam, lParam)); }
protected override LResult WindowProcedure(WindowHandle window, MessageType message, WParam wParam, LParam lParam) { switch (message) { case MessageType.InputLanguageChange: dwCharSet = (CharacterSet)(uint)wParam; goto case MessageType.Create; case MessageType.Create: using (DeviceContext dc = window.GetDeviceContext()) { using (FontHandle font = Gdi.CreateFont(0, 0, 0, 0, FontWeight.DoNotCare, false, false, false, dwCharSet, OutputPrecision.Default, ClippingPrecision.Default, Quality.Default, FontPitch.FixedPitch, FontFamilyType.DoNotCare, null)) { dc.SelectObject(font); dc.GetTextMetrics(out TextMetrics tm); cxChar = tm.AverageCharWidth; cyChar = tm.Height; dc.SelectObject(StockFont.System); } } goto CalculateSize; case MessageType.Size: cxClient = lParam.LowWord; cyClient = lParam.HighWord; CalculateSize: // calculate window size in characters cxBuffer = Math.Max(1, cxClient / cxChar); cyBuffer = Math.Max(1, cyClient / cyChar); // allocate memory for buffer and clear it pBuffer = new char[cxBuffer, cyBuffer]; // set caret to upper left corner xCaret = 0; yCaret = 0; if (window == Windows.GetFocus()) { Windows.SetCaretPosition(new Point(xCaret * cxChar, yCaret * cyChar)); } window.Invalidate(true); return(0); case MessageType.SetFocus: // create and show the caret window.CreateCaret(default, new Size(cxChar, cyChar)); Windows.SetCaretPosition(new Point(xCaret * cxChar, yCaret * cyChar)); window.ShowCaret(); return(0); case MessageType.KillFocus: // hide and destroy the caret window.HideCaret(); Windows.DestroyCaret(); return(0); case MessageType.KeyDown: switch ((VirtualKey)wParam) { case VirtualKey.Home: xCaret = 0; break; case VirtualKey.End: xCaret = cxBuffer - 1; break; case VirtualKey.Prior: yCaret = 0; break; case VirtualKey.Next: yCaret = cyBuffer - 1; break; case VirtualKey.Left: xCaret = Math.Max(xCaret - 1, 0); break; case VirtualKey.Right: xCaret = Math.Min(xCaret + 1, cxBuffer - 1); break; case VirtualKey.Up: yCaret = Math.Max(yCaret - 1, 0); break; case VirtualKey.Down: yCaret = Math.Min(yCaret + 1, cyBuffer - 1); break; case VirtualKey.Delete: for (int x = xCaret; x < cxBuffer - 1; x++) { pBuffer[x, yCaret] = pBuffer[x + 1, yCaret]; } pBuffer[cxBuffer - 1, yCaret] = ' '; window.HideCaret(); using (DeviceContext dc = window.GetDeviceContext()) { using (FontHandle font = Gdi.CreateFont(0, 0, 0, 0, FontWeight.DoNotCare, false, false, false, dwCharSet, OutputPrecision.Default, ClippingPrecision.Default, Quality.Default, FontPitch.FixedPitch, FontFamilyType.DoNotCare, null)) { dc.SelectObject(font); unsafe { fixed(char *c = &pBuffer[xCaret, yCaret]) dc.TextOut( new Point(xCaret * cxChar, yCaret * cyChar), new ReadOnlySpan <char>(c, cxBuffer - xCaret)); } dc.SelectObject(StockFont.System); } window.ShowCaret(); } break; } Windows.SetCaretPosition(new Point(xCaret * cxChar, yCaret * cyChar)); return(0); case MessageType.Char: for (int i = 0; i < lParam.LowWord; i++) { switch ((char)wParam) { case '\b': // backspace if (xCaret > 0) { xCaret--; window.SendMessage(MessageType.KeyDown, (uint)VirtualKey.Delete, 1); } break; case '\t': // tab do { window.SendMessage(MessageType.Char, ' ', 1); } while (xCaret % 8 != 0); break; case '\n': // line feed if (++yCaret == cyBuffer) { yCaret = 0; } break; case '\r': // carriage return xCaret = 0; if (++yCaret == cyBuffer) { yCaret = 0; } break; case '\x1B': // escape for (int y = 0; y < cyBuffer; y++) { for (int x = 0; x < cxBuffer; x++) { pBuffer[x, y] = ' '; } } xCaret = 0; yCaret = 0; window.Invalidate(false); break; default: // character codes pBuffer[xCaret, yCaret] = (char)wParam; window.HideCaret(); using (DeviceContext dc = window.GetDeviceContext()) { using (FontHandle font = Gdi.CreateFont(0, 0, 0, 0, FontWeight.DoNotCare, false, false, false, dwCharSet, OutputPrecision.Default, ClippingPrecision.Default, Quality.Default, FontPitch.FixedPitch, FontFamilyType.DoNotCare, null)) { dc.SelectObject(font); unsafe { fixed(char *c = &pBuffer[xCaret, yCaret]) dc.TextOut( new Point(xCaret * cxChar, yCaret * cyChar), new ReadOnlySpan <char>(c, 1)); } dc.SelectObject(StockFont.System); } window.ShowCaret(); } if (++xCaret == cxBuffer) { xCaret = 0; if (++yCaret == cyBuffer) { yCaret = 0; } } break; } } Windows.SetCaretPosition(new Point(xCaret * cxChar, yCaret * cyChar)); return(0); case MessageType.Paint: using (DeviceContext dc = window.BeginPaint()) { using (FontHandle font = Gdi.CreateFont(0, 0, 0, 0, FontWeight.DoNotCare, false, false, false, dwCharSet, OutputPrecision.Default, ClippingPrecision.Default, Quality.Default, FontPitch.FixedPitch, FontFamilyType.DoNotCare, null)) { dc.SelectObject(font); unsafe { for (int y = 0; y < cyBuffer; y++) fixed(char *c = &pBuffer[0, y]) dc.TextOut(new Point(0, y * cyChar), new ReadOnlySpan <char>(c, cxBuffer)); } dc.SelectObject(StockFont.System); } } return(0); } return(base.WindowProcedure(window, message, wParam, lParam)); }