/// <summary> /// for use with DcRenderTarget (though it would also work with HwndRenderTarget if GDI interop was not used) /// </summary> /// <param name="e"></param> protected override void OnPaint(PaintEventArgs e) { if (!IsInitialized || DesignMode) { e.Graphics.DrawString("D2DShapesControl", Font, foregroundBrush, (float)Width / 2, (float)Height / 2); } else { lock (renderSyncObject) { if (renderMode == RenderModes.DCRenderTarget || renderMode == RenderModes.BitmapRenderTargetOnPaint) { if (dcRenderTarget == null) { return; } CalculateFPS(); if (renderMode == RenderModes.DCRenderTarget) { //render scene directly to DC dcRenderTarget.BindDC(e.Graphics.GetHdc(), new Rect(Left, Top, Right, Bottom)); RenderScene(dcRenderTarget); e.Graphics.ReleaseHdc(); e.Graphics.DrawString(string.Format("OnPaint({0}) direct DC draw", DateTime.Now), Font, Brushes.White, 0, 2); e.Graphics.DrawString(string.Format("OnPaint({0}) direct DC draw", DateTime.Now), Font, Brushes.Black, 1, 2); } else if (renderMode == RenderModes.BitmapRenderTargetOnPaint) { //draw bitmap cache of the shapes to DC dcRenderTarget.BindDC(e.Graphics.GetHdc(), new Rect(Left, Top, Right, Bottom)); dcRenderTarget.BeginDraw(); dcRenderTarget.DrawBitmap(bitmapRenderTarget.Bitmap, 1.0f, BitmapInterpolationMode.NearestNeighbor, new RectF(0, 0, Width, Height)); dcRenderTarget.EndDraw(); e.Graphics.ReleaseHdc(); e.Graphics.DrawString(string.Format("OnPaint({0}) DC DrawBitmap", DateTime.Now), Font, Brushes.White, 0, 2); e.Graphics.DrawString(string.Format("OnPaint({0}) DC DrawBitmap", DateTime.Now), Font, Brushes.Black, 1, 2); } } } } }
void FontEnumComboBox_DrawItem(object sender, DrawItemEventArgs e) { //initialize the DC Render Target and a brush before first use InitializeRenderTarget(); //draw the background of the combo item e.DrawBackground(); //set section of the DC to draw on var subRect = new Rect( e.Bounds.Left, e.Bounds.Top, e.Bounds.Right, e.Bounds.Bottom); //bind the render target with the DC dcRenderTarget.BindDC(e.Graphics.GetHdc(), subRect); //draw the text using D2D/DWrite dcRenderTarget.BeginDraw(); var fontName = (string)Items[e.Index]; //if ((e.State & DrawItemState.Selected & ~DrawItemState.NoFocusRect) != DrawItemState.None) dcRenderTarget.DrawTextLayout( new Point2F(5, (e.Bounds.Height - layouts[fontName].Metrics.Height) / 2), layouts[fontName], brush, DrawTextOptions.Clip); dcRenderTarget.EndDraw(); //release the DC e.Graphics.ReleaseHdc(); //drow focus rect for a focused item e.DrawFocusRectangle(); }