internal void PerformPaint(VisualControlBase textBox, Graphics g, PI.RECT rect, SolidBrush backBrush) { g.SmoothingMode = SmoothingMode.HighQuality; g.TextRenderingHint = TextRenderingHint.AntiAlias; // Define the string formatting requirements var stringFormat = new StringFormat { Trimming = StringTrimming.None, LineAlignment = StringAlignment.Near }; stringFormat.Alignment = GetContentShortTextH(PaletteState.Normal) switch { PaletteRelativeAlign.Near => textBox.RightToLeft == RightToLeft.Yes ? StringAlignment.Far : StringAlignment.Near, PaletteRelativeAlign.Far => textBox.RightToLeft == RightToLeft.Yes ? StringAlignment.Near : StringAlignment.Far, PaletteRelativeAlign.Center => StringAlignment.Center, _ => StringAlignment.Near }; // This is most applicable to the multi-line controls stringFormat.LineAlignment = GetContentShortTextV(PaletteState.Normal) switch { PaletteRelativeAlign.Near => StringAlignment.Near, //PaletteRelativeAlign.Center => StringAlignment.Center, PaletteRelativeAlign.Far => StringAlignment.Far, _ => StringAlignment.Center }; // Use the correct prefix setting stringFormat.HotkeyPrefix = HotkeyPrefix.None; Rectangle layoutRectangle = Rectangle.FromLTRB(rect.left, rect.top, rect.right, rect.bottom); // Draw entire client area in the background color g.FillRectangle(backBrush, layoutRectangle); var padding = GetContentPadding(PaletteState.Normal); if (!padding.Equals(CommonHelper.InheritPadding)) { layoutRectangle.X += padding.Left; layoutRectangle.Y += padding.Top; layoutRectangle.Width -= padding.Left + padding.Right; layoutRectangle.Height -= padding.Top + padding.Bottom; } using Font font = GetContentShortTextNewFont(PaletteState.Normal); using SolidBrush foreBrush = new(GetContentShortTextColor1(PaletteState.Normal)); var drawText = string.IsNullOrEmpty(CueHintText) ? textBox.Text : CueHintText; g.DrawString(drawText, font, foreBrush, layoutRectangle, stringFormat); }
public bool IsOverlapped() { if (_blurValues.BlurWhenFocusLost || !PI.IsWindowVisible(_parentForm.Handle) ) { return(false); } IntPtr hWnd = _parentForm.Handle; // The set is used to make calling GetWindow in a loop stable by checking if we have already // visited the window returned by GetWindow. This avoids the possibility of an infinite loop. HashSet <IntPtr> visited = new HashSet <IntPtr> { hWnd }; Form activeForm = Form.ActiveForm; if (activeForm != null) { visited.Add(activeForm.Handle); } visited.Add(_visualBlur.Handle); PI.RECT thisRect = new PI.RECT(); PI.GetWindowRect(hWnd, ref thisRect); while ((hWnd = PI.GetWindow(hWnd, PI.GetWindowType.GW_HWNDPREV)) != IntPtr.Zero && !visited.Contains(hWnd)) { visited.Add(hWnd); PI.RECT testRect = new PI.RECT(); if (PI.IsWindowVisible(hWnd) && PI.GetWindowRect(hWnd, ref testRect) && PI.IntersectRect(out _, ref thisRect, ref testRect) ) { return(true); } } return(false); }
/// <summary> /// Draw text without a glowing background, for use on a composition element. /// </summary> /// <param name="g">Graphics reference.</param> /// <param name="text">Text to be drawn.</param> /// <param name="font">Font to use for text.</param> /// <param name="bounds">Bounding area for the text.</param> /// <param name="state">State of the source element.</param> /// <param name="color"><see cref="Color"/> of the text.</param> /// <param name="copyBackground">Should existing background be copied into the bitmap.</param> /// <param name="sf">StringFormat of the memento.</param> public static void DrawCompositionText(Graphics g, string text, Font font, Rectangle bounds, PaletteState state, Color color, bool copyBackground, StringFormat sf) { // Get the hDC for the graphics instance and create a memory DC IntPtr gDC = g.GetHdc(); try { IntPtr mDC = PI.CreateCompatibleDC(gDC); PI.BITMAPINFO bmi = new PI.BITMAPINFO { biWidth = bounds.Width, biHeight = -(bounds.Height), biCompression = 0, biBitCount = 32, biPlanes = 1 }; bmi.biSize = (uint)Marshal.SizeOf(bmi); // Create a device independent bitmap and select into the memory DC IntPtr hDIB = PI.CreateDIBSection(gDC, ref bmi, 0, out _, IntPtr.Zero, 0); PI.SelectObject(mDC, hDIB); if (copyBackground) { // Copy existing background into the bitmap PI.BitBlt(mDC, 0, 0, bounds.Width, bounds.Height, gDC, bounds.X, bounds.Y, 0x00CC0020); } // Select the font for use when drawing IntPtr hFont = font.ToHfont(); PI.SelectObject(mDC, hFont); // Get renderer for the correct state VisualStyleRenderer renderer = new VisualStyleRenderer(state == PaletteState.Normal ? VisualStyleElement.Window.Caption.Active : VisualStyleElement.Window.Caption.Inactive); // Create structures needed for theme drawing call PI.RECT textBounds = new PI.RECT { left = 0, top = 0, right = (bounds.Right - bounds.Left), bottom = (bounds.Bottom - bounds.Top) }; PI.DTTOPTS dttOpts = new PI.DTTOPTS { dwSize = Marshal.SizeOf(typeof(PI.DTTOPTS)), dwFlags = PI.DTT_COMPOSITED | PI.DTT_TEXTCOLOR, crText = ColorTranslator.ToWin32(color) }; // Always draw text centered TextFormatFlags textFormat = TextFormatFlags.SingleLine | TextFormatFlags.HorizontalCenter | TextFormatFlags.VerticalCenter; ////Seb | TextFormatFlags.EndEllipsis; // Perform actual drawing //PI.DrawThemeTextEx(renderer.Handle, // mDC, 0, 0, // text, -1, (int)StringFormatToFlags(sf), // ref textBounds, ref dttOpts); PI.DrawThemeTextEx(renderer.Handle, mDC, 0, 0, text, -1, (int)textFormat, ref textBounds, ref dttOpts); // Copy to foreground PI.BitBlt(gDC, bounds.Left, bounds.Top, bounds.Width, bounds.Height, mDC, 0, 0, 0x00CC0020); // Dispose of allocated objects PI.DeleteObject(hFont); PI.DeleteObject(hDIB); PI.DeleteDC(mDC); } catch { // ignored } finally { // Must remember to release the hDC g.ReleaseHdc(gDC); } }
private void RenderOnComposition(RenderContext context) { // Convert the clipping rectangle from floating to int version RectangleF rectClipF = context.Graphics.ClipBounds; Rectangle rectClip = new Rectangle((int)rectClipF.X, (int)rectClipF.Y, (int)rectClipF.Width, (int)rectClipF.Height); // No point drawing unless some of the client fits into the clipping area if (rectClip.IntersectsWith(ClientRectangle)) { // Get the hDC for the graphics instance and create a memory DC IntPtr gDC = context.Graphics.GetHdc(); IntPtr mDC = PI.CreateCompatibleDC(gDC); PI.BITMAPINFO bmi = new PI.BITMAPINFO(); bmi.biSize = Marshal.SizeOf(bmi); bmi.biWidth = ClientWidth; bmi.biHeight = -ClientHeight; bmi.biCompression = 0; bmi.biBitCount = 32; bmi.biPlanes = 1; // Create a device independant bitmp and select into the memory DC IntPtr hDIB = PI.CreateDIBSection(gDC, bmi, 0, 0, IntPtr.Zero, 0); PI.SelectObject(mDC, hDIB); // To call the renderer we need to convert from Win32 HDC to Graphics object using (Graphics bitmapG = Graphics.FromHdc(mDC)) { Rectangle renderClientRect = new Rectangle(0, 0, ClientWidth, ClientHeight); // Create new render context that uses the bitmap graphics instance using (RenderContext bitmapContext = new RenderContext(context.Control, bitmapG, renderClientRect, context.Renderer)) { // Finally we get the renderer to draw the background for the bitmap _mementoBack = context.Renderer.RenderRibbon.DrawRibbonTabContextTitle(_ribbon.RibbonShape, bitmapContext, renderClientRect, _ribbon.StateCommon.RibbonGeneral, this, _mementoBack); } } // Select the font for use when drawing IntPtr hFont = _contentProvider.GetContentShortTextFont(State).ToHfont(); PI.SelectObject(mDC, hFont); // Get renderer for the correct state VisualStyleRenderer renderer = new VisualStyleRenderer(VisualStyleElement.Window.Caption.Active); // Create structures needed for theme drawing call PI.RECT textBounds = new PI.RECT(); textBounds.left = TEXT_SIDE_GAP_COMPOSITION; textBounds.top = 0; textBounds.right = ClientWidth - (TEXT_SIDE_GAP_COMPOSITION * 2); textBounds.bottom = ClientHeight; PI.DTTOPTS dttOpts = new PI.DTTOPTS(); dttOpts.dwSize = Marshal.SizeOf(typeof(PI.DTTOPTS)); dttOpts.dwFlags = PI.DTT_COMPOSITED | PI.DTT_GLOWSIZE | PI.DTT_TEXTCOLOR; dttOpts.crText = ColorTranslator.ToWin32(SystemColors.ActiveCaptionText); dttOpts.iGlowSize = (_ribbon.Enabled ? 12 : 2); // Always draw text centered TextFormatFlags textFormat = TextFormatFlags.SingleLine | TextFormatFlags.HorizontalCenter | TextFormatFlags.VerticalCenter | TextFormatFlags.EndEllipsis; // Perform actual drawing PI.DrawThemeTextEx(renderer.Handle, mDC, 0, 0, GetShortText(), -1, (int)textFormat, ref textBounds, ref dttOpts); // Copy to foreground PI.BitBlt(gDC, ClientLocation.X, ClientLocation.Y, ClientWidth, ClientHeight, mDC, 0, 0, 0x00CC0020); // Dispose of allocated objects PI.DeleteObject(hFont); PI.DeleteObject(hDIB); PI.DeleteDC(mDC); // Must remember to release the hDC context.Graphics.ReleaseHdc(gDC); } }