本文整理汇总了C#中System.Windows.Forms.IDeviceContext.GetHdc方法的典型用法代码示例。如果您正苦于以下问题:C# IDeviceContext.GetHdc方法的具体用法?C# IDeviceContext.GetHdc怎么用?C# IDeviceContext.GetHdc使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Windows.Forms.IDeviceContext
的用法示例。
在下文中一共展示了IDeviceContext.GetHdc方法的14个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: DrawText
/// <include file='doc\TextRenderer.uex' path='docs/doc[@for="TextRenderer.DrawText1"]/*' />
public static void DrawText(IDeviceContext dc, string text, Font font, Point pt, Color foreColor, Color backColor)
{
if (dc == null)
{
throw new ArgumentNullException("dc");
}
WindowsFontQuality fontQuality = WindowsFont.WindowsFontQualityFromTextRenderingHint(dc as Graphics);
IntPtr hdc = dc.GetHdc();
try
{
using( WindowsGraphics wg = WindowsGraphics.FromHdc( hdc ))
{
using (WindowsFont wf = WindowsGraphicsCacheManager.GetWindowsFont(font, fontQuality)) {
wg.DrawText(text, wf, pt, foreColor, backColor);
}
}
}
finally
{
dc.ReleaseHdc();
}
}
示例2: WindowsGraphicsWrapper
public WindowsGraphicsWrapper( IDeviceContext idc, TextFormatFlags flags)
{
if( idc is Graphics )
{
ApplyGraphicsProperties properties = ApplyGraphicsProperties.None;
if( (flags & TextFormatFlags.PreserveGraphicsClipping) != 0)
{
properties |= ApplyGraphicsProperties.Clipping;
}
if( (flags & TextFormatFlags.PreserveGraphicsTranslateTransform) != 0)
{
properties |= ApplyGraphicsProperties.TranslateTransform;
}
// Create the WindowsGraphics from the Grahpics object only if Graphics properties need
// to be reapplied to the DC wrapped by the WindowsGraphics.
if( properties != ApplyGraphicsProperties.None )
{
this.wg = WindowsGraphics.FromGraphics( idc as Graphics, properties);
}
}
else
{
// If passed-in IDeviceContext object is a WindowsGraphics we can use it directly.
this.wg = idc as WindowsGraphics;
if( this.wg != null )
{
// In this case we cache the idc to compare it against the wg in the Dispose method to avoid
// disposing of the wg.
this.idc = idc;
}
}
if( this.wg == null )
{
// The IDeviceContext object is not a WindowsGraphics, or it is a custom IDeviceContext, or
// it is a Graphics object but we did not need to re-apply Graphics propertiesto the hdc.
// So create the WindowsGraphics from the hdc directly.
// Cache the IDC so the hdc can be released on dispose.
this.idc = idc;
this.wg = WindowsGraphics.FromHdc( idc.GetHdc() );
}
// Set text padding on the WindowsGraphics (if any).
if( (flags & TextFormatFlags.LeftAndRightPadding) != 0 )
{
wg.TextPadding = TextPaddingOptions.LeftAndRightPadding;
}
else if ((flags & TextFormatFlags.NoPadding) != 0 )
{
wg.TextPadding = TextPaddingOptions.NoPadding;
}
// else wg.TextPadding = TextPaddingOptions.GlyphOverhangPadding - the default value.
}
示例3: WindowsGraphicsWrapper
public WindowsGraphicsWrapper(IDeviceContext idc, TextFormatFlags flags)
{
if (idc is Graphics)
{
ApplyGraphicsProperties none = ApplyGraphicsProperties.None;
if ((flags & TextFormatFlags.PreserveGraphicsClipping) != TextFormatFlags.Default)
{
none |= ApplyGraphicsProperties.Clipping;
}
if ((flags & TextFormatFlags.PreserveGraphicsTranslateTransform) != TextFormatFlags.Default)
{
none |= ApplyGraphicsProperties.TranslateTransform;
}
if (none != ApplyGraphicsProperties.None)
{
this.wg = System.Windows.Forms.Internal.WindowsGraphics.FromGraphics(idc as Graphics, none);
}
}
else
{
this.wg = idc as System.Windows.Forms.Internal.WindowsGraphics;
if (this.wg != null)
{
this.idc = idc;
}
}
if (this.wg == null)
{
this.idc = idc;
this.wg = System.Windows.Forms.Internal.WindowsGraphics.FromHdc(idc.GetHdc());
}
if ((flags & TextFormatFlags.LeftAndRightPadding) != TextFormatFlags.Default)
{
this.wg.TextPadding = TextPaddingOptions.LeftAndRightPadding;
}
else if ((flags & TextFormatFlags.NoPadding) != TextFormatFlags.Default)
{
this.wg.TextPadding = TextPaddingOptions.NoPadding;
}
}
示例4: DrawText
public static void DrawText(IDeviceContext dc, string text, Font font, Rectangle bounds, Color foreColor)
{
if (dc == null)
{
throw new ArgumentNullException("dc");
}
WindowsFontQuality fontQuality = WindowsFont.WindowsFontQualityFromTextRenderingHint(dc as Graphics);
IntPtr hdc = dc.GetHdc();
try
{
using (WindowsGraphics graphics = WindowsGraphics.FromHdc(hdc))
{
using (WindowsFont font2 = WindowsGraphicsCacheManager.GetWindowsFont(font, fontQuality))
{
graphics.DrawText(text, font2, bounds, foreColor);
}
}
}
finally
{
dc.ReleaseHdc();
}
}
示例5: CopyPixels
internal static void CopyPixels(IntPtr sourceHwnd, IDeviceContext targetDC, Point sourceLocation, Point destinationLocation, Size blockRegionSize, CopyPixelOperation copyPixelOperation)
{
int width = blockRegionSize.Width;
int height = blockRegionSize.Height;
DeviceContext context = DeviceContext.FromHwnd(sourceHwnd);
HandleRef hDC = new HandleRef(null, targetDC.GetHdc());
HandleRef hSrcDC = new HandleRef(null, context.Hdc);
try
{
if (!System.Windows.Forms.SafeNativeMethods.BitBlt(hDC, destinationLocation.X, destinationLocation.Y, width, height, hSrcDC, sourceLocation.X, sourceLocation.Y, (int) copyPixelOperation))
{
throw new Win32Exception();
}
}
finally
{
targetDC.ReleaseHdc();
context.Dispose();
}
}
示例6: MeasureTextInternal
internal static Size MeasureTextInternal (IDeviceContext dc, string text, Font font, Size proposedSize, TextFormatFlags flags, bool useMeasureString)
{
if (!useMeasureString && !XplatUI.RunningOnUnix) {
// Tell DrawText to calculate size instead of draw
flags |= (TextFormatFlags)1024; // DT_CALCRECT
IntPtr hdc = dc.GetHdc ();
XplatUIWin32.RECT r = XplatUIWin32.RECT.FromRectangle (new Rectangle (Point.Empty, proposedSize));
IntPtr prevobj;
if (font != null) {
prevobj = SelectObject (hdc, font.ToHfont ());
Win32DrawText (hdc, text, text.Length, ref r, (int)flags);
prevobj = SelectObject (hdc, prevobj);
DeleteObject (prevobj);
}
else {
Win32DrawText (hdc, text, text.Length, ref r, (int)flags);
}
dc.ReleaseHdc ();
// Really, I am just making something up here, which as far as I can tell, MS
// just makes something up as well. This will require lots of tweaking to match MS. :(
Size retval = r.ToRectangle ().Size;
if (retval.Width > 0 && (flags & TextFormatFlags.NoPadding) == 0) {
retval.Width += 6;
retval.Width += (int)retval.Height / 8;
}
return retval;
}
else {
StringFormat sf = FlagsToStringFormat (flags);
Size retval;
if (dc is Graphics)
retval = (dc as Graphics).MeasureString (text, font, proposedSize.Width == 0 ? Int32.MaxValue : proposedSize.Width, sf).ToSize ();
else
retval = TextRenderer.MeasureString (text, font, proposedSize.Width == 0 ? Int32.MaxValue : proposedSize.Width, sf).ToSize ();
if (retval.Width > 0 && (flags & TextFormatFlags.NoPadding) == 0)
retval.Width += 9;
return retval;
}
}
示例7: DrawTextInternal
internal static void DrawTextInternal (IDeviceContext dc, string text, Font font, Rectangle bounds, Color foreColor, Color backColor, TextFormatFlags flags, bool useDrawString)
{
if (dc == null)
throw new ArgumentNullException ("dc");
if (text == null || text.Length == 0)
return;
// We use MS GDI API's unless told not to, or we aren't on Windows
if (!useDrawString && !XplatUI.RunningOnUnix) {
if ((flags & TextFormatFlags.VerticalCenter) == TextFormatFlags.VerticalCenter || (flags & TextFormatFlags.Bottom) == TextFormatFlags.Bottom)
flags |= TextFormatFlags.SingleLine;
// Calculate the text bounds (there is often padding added)
Rectangle new_bounds = PadRectangle (bounds, flags);
new_bounds.Offset ((int)(dc as Graphics).Transform.OffsetX, (int)(dc as Graphics).Transform.OffsetY);
IntPtr hdc = IntPtr.Zero;
bool clear_clip_region = false;
// If we need to use the graphics clipping region, add it to our hdc
if ((flags & TextFormatFlags.PreserveGraphicsClipping) == TextFormatFlags.PreserveGraphicsClipping) {
Graphics graphics = (Graphics)dc;
Region clip_region = graphics.Clip;
if (!clip_region.IsInfinite (graphics)) {
IntPtr hrgn = clip_region.GetHrgn (graphics);
hdc = dc.GetHdc ();
SelectClipRgn (hdc, hrgn);
DeleteObject (hrgn);
clear_clip_region = true;
}
}
if (hdc == IntPtr.Zero)
hdc = dc.GetHdc ();
// Set the fore color
if (foreColor != Color.Empty)
SetTextColor (hdc, ColorTranslator.ToWin32 (foreColor));
// Set the back color
if (backColor != Color.Transparent && backColor != Color.Empty) {
SetBkMode (hdc, 2); //1-Transparent, 2-Opaque
SetBkColor (hdc, ColorTranslator.ToWin32 (backColor));
}
else {
SetBkMode (hdc, 1); //1-Transparent, 2-Opaque
}
XplatUIWin32.RECT r = XplatUIWin32.RECT.FromRectangle (new_bounds);
IntPtr prevobj;
if (font != null) {
prevobj = SelectObject (hdc, font.ToHfont ());
Win32DrawText (hdc, text, text.Length, ref r, (int)flags);
prevobj = SelectObject (hdc, prevobj);
DeleteObject (prevobj);
}
else {
Win32DrawText (hdc, text, text.Length, ref r, (int)flags);
}
if (clear_clip_region)
SelectClipRgn (hdc, IntPtr.Zero);
dc.ReleaseHdc ();
}
// Use Graphics.DrawString as a fallback method
else {
Graphics g;
IntPtr hdc = IntPtr.Zero;
if (dc is Graphics)
g = (Graphics)dc;
else {
hdc = dc.GetHdc ();
g = Graphics.FromHdc (hdc);
}
StringFormat sf = FlagsToStringFormat (flags);
Rectangle new_bounds = PadDrawStringRectangle (bounds, flags);
g.DrawString (text, font, ThemeEngine.Current.ResPool.GetSolidBrush (foreColor), new_bounds, sf);
if (!(dc is Graphics)) {
g.Dispose ();
dc.ReleaseHdc ();
}
}
}
示例8: GetThemeMargins
private Padding GetThemeMargins(IDeviceContext dc, MarginTypes marginType)
{
MARGINS margins;
try
{
IntPtr hDC = dc.GetHdc();
if (0 == GetThemeMargins(_renderer.Handle, hDC, _renderer.Part, _renderer.State, (int) marginType, IntPtr.Zero, out margins))
return new Padding(margins.cxLeftWidth, margins.cyTopHeight, margins.cxRightWidth, margins.cyBottomHeight);
return new Padding(0);
}
finally
{
dc.ReleaseHdc();
}
}
示例9: GetThemeMargins
private Padding GetThemeMargins(IDeviceContext dc,
MarginProperty marginType)
{
NativeMethods.MARGINS margins;
try {
var hDC = dc.GetHdc();
var rv = NativeMethods.GetThemeMargins(
renderer.Handle,
hDC,
renderer.Part,
renderer.State,
(int)marginType,
IntPtr.Zero,
out margins);
if (rv == 0) {
return new Padding(
margins.cxLeftWidth,
margins.cyTopHeight,
margins.cxRightWidth,
margins.cyBottomHeight);
}
return new Padding(0);
}
catch (Exception) {
return renderer.GetMargins(dc, marginType);
}
finally {
dc.ReleaseHdc();
}
}
示例10: DrawCompositedText
/// <summary>
/// Draws composited text onto the glass area of a form.
/// </summary>
/// <param name="dc">The <see cref="IDeviceContext"/> onto which the composited text should be drawn.</param>
/// <param name="text">The text to draw.</param>
/// <param name="font">The <see cref="Font"/> to apply to the drawn text.</param>
/// <param name="bounds">The <see cref="Rectangle" /> that represents the bounds of the text.</param>
/// <param name="padding">The <see cref="Padding"/> around the text; necessary to allow space for the glow effect.</param>
/// <param name="foreColor">The <see cref="Color" /> to apply to the drawn text.</param>
/// <param name="textFormat">A bitwise combination of the <see cref="TextFormatFlags" /> values.</param>
/// <param name="glowSize">Specifies the size of a glow that will be drawn on the background prior to any text being drawn.</param>
/// <remarks>
/// <para>
/// Do not use this method to draw text on non-glass areas of a form.
/// </para>
/// </remarks>
/// <exception cref="NotSupportedException">The current operating system does not support glass, or the Desktop Window Manager is not enabled.</exception>
/// <exception cref="ArgumentNullException"><paramref name="dc"/>, <paramref name="text"/> or <paramref name="font"/> is <see langword="null"/>.</exception>
public static void DrawCompositedText(IDeviceContext dc, string text, Font font, Rectangle bounds, Padding padding, Color foreColor, int glowSize, TextFormatFlags textFormat)
{
if( !IsDwmCompositionEnabled )
throw new NotSupportedException(Properties.Resources.GlassNotSupportedError);
if( dc == null )
throw new ArgumentNullException("dc");
if( text == null )
throw new ArgumentNullException("text");
if( font == null )
throw new ArgumentNullException("font");
IntPtr primaryHdc = dc.GetHdc();
try
{
using( SafeDeviceHandle memoryHdc = NativeMethods.CreateCompatibleDC(primaryHdc) )
using( SafeGDIHandle fontHandle = new SafeGDIHandle(font.ToHfont(), true) )
using( SafeGDIHandle dib = NativeMethods.CreateDib(bounds, primaryHdc, memoryHdc) )
{
NativeMethods.SelectObject(memoryHdc, fontHandle);
// Draw glowing text
System.Windows.Forms.VisualStyles.VisualStyleRenderer renderer = new System.Windows.Forms.VisualStyles.VisualStyleRenderer(System.Windows.Forms.VisualStyles.VisualStyleElement.Window.Caption.Active);
NativeMethods.DTTOPTS dttOpts = new NativeMethods.DTTOPTS();
dttOpts.dwSize = Marshal.SizeOf(typeof(NativeMethods.DTTOPTS));
dttOpts.dwFlags = NativeMethods.DrawThemeTextFlags.Composited | NativeMethods.DrawThemeTextFlags.GlowSize | NativeMethods.DrawThemeTextFlags.TextColor;
dttOpts.crText = ColorTranslator.ToWin32(foreColor);
dttOpts.iGlowSize = glowSize;
NativeMethods.RECT textBounds = new NativeMethods.RECT(padding.Left, padding.Top, bounds.Width - padding.Right, bounds.Height - padding.Bottom);
NativeMethods.DrawThemeTextEx(renderer.Handle, memoryHdc, 0, 0, text, text.Length, (int)textFormat, ref textBounds, ref dttOpts);
// Copy to foreground
const int SRCCOPY = 0x00CC0020;
NativeMethods.BitBlt(primaryHdc, bounds.Left, bounds.Top, bounds.Width, bounds.Height, memoryHdc, 0, 0, SRCCOPY);
}
}
finally
{
dc.ReleaseHdc();
}
}
示例11: MeasureCompositedText
/// <summary>
/// Provides the size, in pixels, of the specified text.
/// </summary>
/// <param name="dc">The device context in which to measure the text.</param>
/// <param name="text">The text to measure.</param>
/// <param name="font">The <see cref="Font"/> to apply to the measured text.</param>
/// <param name="textFormat">A bitwise combination of the <see cref="TextFormatFlags" /> values.</param>
/// <returns>The <see cref="Size"/>, in pixels, of <paramref name="text"/> drawn with the specified <paramref name="font"/> and format.</returns>
/// <exception cref="NotSupportedException">The current operating system does not support glass, or the Desktop Window Manager is not enabled.</exception>
/// <exception cref="ArgumentNullException"><paramref name="dc"/>, <paramref name="text"/> or <paramref name="font"/> is <see langword="null"/>.</exception>
public static Size MeasureCompositedText(IDeviceContext dc, string text, Font font, TextFormatFlags textFormat)
{
if( !IsDwmCompositionEnabled )
throw new NotSupportedException(Properties.Resources.GlassNotSupportedError);
if( dc == null )
throw new ArgumentNullException("dc");
if( text == null )
throw new ArgumentNullException("text");
if( font == null )
throw new ArgumentNullException("font");
IntPtr primaryHdc = dc.GetHdc();
try
{
Rectangle bounds = new Rectangle(0, 0, int.MaxValue, int.MaxValue);
using( SafeDeviceHandle memoryHdc = NativeMethods.CreateCompatibleDC(primaryHdc) )
using( SafeGDIHandle fontHandle = new SafeGDIHandle(font.ToHfont(), true) )
using( SafeGDIHandle dib = NativeMethods.CreateDib(bounds, primaryHdc, memoryHdc) )
{
NativeMethods.SelectObject(memoryHdc, fontHandle);
System.Windows.Forms.VisualStyles.VisualStyleRenderer renderer = new System.Windows.Forms.VisualStyles.VisualStyleRenderer(System.Windows.Forms.VisualStyles.VisualStyleElement.Window.Caption.Active);
NativeMethods.RECT bounds2 = new NativeMethods.RECT(bounds);
NativeMethods.RECT rect;
NativeMethods.GetThemeTextExtent(renderer.Handle, memoryHdc, 0, 0, text, text.Length, (int)textFormat, ref bounds2, out rect);
return new Size(rect.Right - rect.Left, rect.Bottom - rect.Top);
}
}
finally
{
dc.ReleaseHdc();
}
}
示例12: GetThemeMargins
private Padding GetThemeMargins(IDeviceContext dc, MarginTypes marginType) {
Padding padding;
try {
NativeMethods.MARGINS margins;
IntPtr hdc = dc.GetHdc();
if (
NativeMethods.GetThemeMargins(renderer.Handle, hdc, renderer.Part, renderer.State, (int) marginType, IntPtr.Zero,
out margins) == 0) {
return new Padding(margins.cxLeftWidth, margins.cyTopHeight, margins.cxRightWidth, margins.cyBottomHeight);
}
padding = new Padding(0);
}
finally {
dc.ReleaseHdc();
}
return padding;
}
示例13: MeasureText
public static Size MeasureText(IDeviceContext dc, string text, Font font, Size proposedSize)
{
Size size;
if (dc == null)
{
throw new ArgumentNullException("dc");
}
if (string.IsNullOrEmpty(text))
{
return Size.Empty;
}
WindowsFontQuality fontQuality = WindowsFont.WindowsFontQualityFromTextRenderingHint(dc as Graphics);
IntPtr hdc = dc.GetHdc();
try
{
using (WindowsGraphics graphics = WindowsGraphics.FromHdc(hdc))
{
using (WindowsFont font2 = WindowsGraphicsCacheManager.GetWindowsFont(font, fontQuality))
{
size = graphics.MeasureText(text, font2, proposedSize);
}
}
}
finally
{
dc.ReleaseHdc();
}
return size;
}
示例14: CopyPixels
// roughly the same code as in Graphics.cs
internal static void CopyPixels(IntPtr sourceHwnd, IDeviceContext targetDC, Point sourceLocation, Point destinationLocation, Size blockRegionSize, CopyPixelOperation copyPixelOperation) {
int destWidth = blockRegionSize.Width;
int destHeight = blockRegionSize.Height;
DeviceContext dc = DeviceContext.FromHwnd(sourceHwnd);
HandleRef targetHDC = new HandleRef( null, targetDC.GetHdc());
HandleRef screenHDC = new HandleRef( null, dc.Hdc );
try {
bool result = SafeNativeMethods.BitBlt(targetHDC, destinationLocation.X, destinationLocation.Y, destWidth, destHeight,
screenHDC,
sourceLocation.X, sourceLocation.Y, (int) copyPixelOperation);
//a zero result indicates a win32 exception has been thrown
if (!result) {
throw new Win32Exception();
}
}
finally {
targetDC.ReleaseHdc();
dc.Dispose();
}
}