Struct DeleteDCGuard

Source
pub struct DeleteDCGuard { /* private fields */ }
Available on crate features kernel and gdi only.
Expand description

RAII implementation for HDC which automatically calls DeleteDC when the object goes out of scope.

Implementations§

Source§

impl DeleteDCGuard

Source

pub const unsafe fn new(handle: HDC) -> Self

Constructs the guard by taking ownership of the handle.

This method can be used as an escape hatch to interoperate with other libraries.

§Safety

Be sure the handle must be freed with the specified function at the end of scope.

Source

pub fn leak(&mut self) -> HDC

Ejects the underlying handle, leaving a Handle::INVALID in its place.

Since the internal handle will be invalidated, the destructor will not run. It’s your responsability to run it, otherwise you’ll cause a resource leak.

Methods from Deref<Target = HDC>§

Source

pub fn AbortDoc(&self) -> SysResult<()>

AbortDoc function.

Source

pub fn AbortPath(&self) -> SysResult<()>

AborthPath function.

Source

pub fn AlphaBlend( &self, origin_dest: RECT, hdc_src: &HDC, origin_src: RECT, ftn: &BLENDFUNCTION, ) -> SysResult<()>

AlphaBlend function.

Source

pub fn AngleArc( &self, center: POINT, radius: u32, start_angle: f32, sweep_angle: f32, ) -> SysResult<()>

AngleArc function.

Source

pub fn Arc( &self, bound: RECT, radial_start: POINT, radial_end: POINT, ) -> SysResult<()>

Arc function.

Source

pub fn ArcTo( &self, bound: RECT, radial_start: POINT, radial_end: POINT, ) -> SysResult<()>

ArcTo function.

Source

pub fn BeginPath(&self) -> SysResult<()>

BeginPath function.

Source

pub fn BitBlt( &self, dest_pos: POINT, sz: SIZE, hdc_src: &HDC, src_src: POINT, rop: ROP, ) -> SysResult<()>

BitBlt function.

Source

pub fn CancelDC(&self) -> SysResult<()>

CancelDC function.

Source

pub fn ChoosePixelFormat(&self, pfd: &PIXELFORMATDESCRIPTOR) -> SysResult<i32>

Source

pub fn Chord( &self, bounds: RECT, start_radial: POINT, end_radial: POINT, ) -> SysResult<()>

Chord function.

Source

pub fn CloseFigure(&self) -> SysResult<()>

CloseFigure function.

Source

pub fn CreateCompatibleBitmap( &self, cx: i32, cy: i32, ) -> SysResult<DeleteObjectGuard<HBITMAP>>

Source

pub fn CreateCompatibleDC(&self) -> SysResult<DeleteDCGuard>

Source

pub fn CreateHalftonePalette(&self) -> SysResult<DeleteObjectPaletteGuard>

Source

pub fn DescribePixelFormat( &self, index: i32, ) -> SysResult<PIXELFORMATDESCRIPTOR>

Source

pub fn Ellipse(&self, bound: RECT) -> SysResult<()>

Ellipse function.

Source

pub fn EndPath(&self) -> SysResult<()>

EndPath function.

Source

pub fn ExcludeClipRect(&self, rc: RECT) -> SysResult<REGION>

ExcludeClipRect function.

Source

pub fn FillPath(&self) -> SysResult<()>

FillPath function.

Source

pub fn FillRect(&self, rc: RECT, hbr: &HBRUSH) -> SysResult<()>

FillRect function.

Source

pub fn FillRgn(&self, rgn: &HRGN, brush: &HBRUSH) -> SysResult<()>

FillRgn function.

Source

pub fn FlattenPath(&self) -> SysResult<()>

FlattenPath function.

Source

pub fn FrameRgn( &self, rgn: &HRGN, brush: &HBRUSH, w: i32, h: i32, ) -> SysResult<()>

FrameRgn function.

Source

pub fn GetBkColor(&self) -> SysResult<COLORREF>

GetBkColor function.

Source

pub fn GetBkMode(&self) -> SysResult<BKMODE>

GetBkMode function.

Source

pub fn GetCurrentObject(&self, kind: CUR_OBJ) -> SysResult<CurObj>

GetCurrentObject function.

§Examples
use winsafe::{self as w, prelude::*, co};

let hdc: w::HDC; // initialized somewhere

let obj = hdc.GetCurrentObject(co::CUR_OBJ::BRUSH)?;
let w::CurObj::Brush(hbrush) = obj else { unreachable!() };

println!("HBRUSH: {}", hbrush);
w::SysResult::Ok(())
Source

pub fn GetCurrentPositionEx(&self) -> SysResult<POINT>

Source

pub fn GetDCBrushColor(&self) -> SysResult<COLORREF>

GetDCBrushColor function.

Source

pub fn GetDCPenColor(&self) -> SysResult<COLORREF>

GetDCPenColor function.

Source

pub fn GetDeviceCaps(&self, index: GDC) -> i32

GetDeviceCaps function.

Source

pub unsafe fn GetDIBits( &self, hbm: &HBITMAP, first_scan_line: u32, num_scan_lines: u32, bmp_data_buf: Option<&mut [u8]>, bmi: &mut BITMAPINFO, usage: DIB, ) -> SysResult<i32>

GetDIBits function.

§Safety

If bmpDataBuf is smaller than needed, you’ll have a buffer overflow.

§Examples

Taking a screenshot and saving to file:

use winsafe::{self as w, prelude::*, co};

let cx_screen = w::GetSystemMetrics(co::SM::CXSCREEN);
let cy_screen = w::GetSystemMetrics(co::SM::CYSCREEN);

let hdc_screen = w::HWND::DESKTOP.GetDC()?;
let hbmp = hdc_screen.CreateCompatibleBitmap(cx_screen, cy_screen)?;
let hdc_mem = hdc_screen.CreateCompatibleDC()?;
let _hbmp_guard = hdc_mem.SelectObject(&*hbmp)?;

hdc_mem.BitBlt(
    w::POINT::new(),
    w::SIZE::with(cx_screen, cy_screen),
    &hdc_screen,
    w::POINT::new(),
    co::ROP::SRCCOPY,
)?;

let bmp_obj = hbmp.GetObject()?;

let mut bi = w::BITMAPINFO::default();
bi.bmiHeader.biWidth = cx_screen;
bi.bmiHeader.biHeight = cy_screen;
bi.bmiHeader.biPlanes = 1;
bi.bmiHeader.biBitCount = 32;
bi.bmiHeader.biCompression = co::BI::RGB;

let bmp_size = (bmp_obj.bmWidth * (bi.bmiHeader.biBitCount as i32) + 31)
    / 32 * 4 * bmp_obj.bmHeight;
let mut data_buf = vec![0u8; bmp_size as _];

unsafe {
    hdc_screen.GetDIBits(
        &hbmp,
        0,
        cy_screen as _,
        Some(&mut data_buf),
        &mut bi,
        co::DIB::RGB_COLORS,
    )?;
}

let mut bfh = w::BITMAPFILEHEADER::default();
bfh.bfOffBits = (std::mem::size_of::<w::BITMAPFILEHEADER>()
    + std::mem::size_of::<w::BITMAPINFOHEADER>()) as _;
bfh.bfSize = bfh.bfOffBits + (bmp_size as u32);

let fo = w::File::open("C:\\Temp\\foo.bmp", w::FileAccess::OpenOrCreateRW)?;
fo.write(bfh.serialize())?;
fo.write(bi.bmiHeader.serialize())?;
fo.write(&data_buf)?;
Source

pub fn GetPixelFormat(&self) -> SysResult<i32>

GetPixelFormat function.

Source

pub fn GetStretchBltMode(&self) -> SysResult<STRETCH_MODE>

Source

pub fn GetTextColor(&self) -> SysResult<COLORREF>

GetTextColor function.

Source

pub fn GetTextExtentPoint32(&self, text: &str) -> SysResult<SIZE>

Source

pub fn GetTextFace(&self) -> SysResult<String>

GetTextFace function.

Source

pub fn GetTextMetrics(&self) -> SysResult<TEXTMETRIC>

GetTextMetrics function.

Source

pub fn GetViewportExtEx(&self) -> SysResult<SIZE>

GetViewportExtEx function.

Source

pub fn GetViewportOrgEx(&self) -> SysResult<POINT>

GetViewportOrgEx function.

Source

pub fn GetWindowExtEx(&self) -> SysResult<SIZE>

GetWindowExtEx function.

Source

pub fn GetWindowOrgEx(&self) -> SysResult<POINT>

GetWindowOrgEx function.

Source

pub fn HiMetricToPixel(&self, x: i32, y: i32) -> (i32, i32)

AtlHiMetricToPixel function.

Converts HIMETRIC units to pixels. The inverse operation is HDC::PixelToHiMetric.

Source

pub fn IntersectClipRect(&self, rc: RECT) -> SysResult<()>

Source

pub fn InvertRgn(&self, hrgn: &HRGN) -> SysResult<()>

InvertRgn function.

Source

pub fn LineTo(&self, x: i32, y: i32) -> SysResult<()>

LineTo function.

Source

pub fn MaskBlt( &self, dest_top_left: POINT, sz: SIZE, hdc_src: &HDC, src_top_left: POINT, hbm_mask: &HBITMAP, mask_offset: POINT, rop: ROP, ) -> SysResult<()>

MaskBlt function.

Source

pub fn MoveToEx(&self, x: i32, y: i32, pt: Option<&mut POINT>) -> SysResult<()>

MoveToEx function.

Source

pub fn PaintRgn(&self, hrgn: &HRGN) -> SysResult<()>

PaintRgn function.

Source

pub fn PatBlt(&self, top_left: POINT, sz: SIZE, rop: ROP) -> SysResult<()>

PatBlt function.

Source

pub fn PathToRegion(&self) -> SysResult<DeleteObjectGuard<HRGN>>

PathToRegion function.

Source

pub fn Pie( &self, bounds: RECT, radial_1: POINT, radial_2: POINT, ) -> SysResult<()>

Pie function.

Source

pub fn PixelToHiMetric(&self, x: i32, y: i32) -> (i32, i32)

AtlPixelToHiMetric function.

Converts pixels to HIMETRIC units. The inverse operation is HDC::HiMetricToPixel.

Source

pub fn PolyBezier(&self, pts: &[POINT]) -> SysResult<()>

PolyBezier function.

Source

pub fn PolyBezierTo(&self, pts: &[POINT]) -> SysResult<()>

PolyBezierTo function.

Source

pub fn PolyDraw(&self, pts: &[(POINT, PT)]) -> SysResult<()>

PolyDraw function.

Source

pub fn Polygon(&self, pts: &[POINT]) -> SysResult<()>

Polygon function.

Source

pub fn Polyline(&self, pts: &[POINT]) -> SysResult<()>

Polyline function.

Source

pub fn PolylineTo(&self, pts: &[POINT]) -> SysResult<()>

PolylineTo function.

Source

pub fn PolyPolygon(&self, polygons: &[&[POINT]]) -> SysResult<()>

PolyPolygon function.

Source

pub fn PolyPolyline(&self, polylines: &[&[POINT]]) -> SysResult<()>

PolyPolyline function.

Source

pub fn PtVisible(&self, x: i32, y: i32) -> SysResult<bool>

PtVisible function.

Source

pub fn RealizePalette(&self) -> SysResult<u32>

RealizePalette function.

Source

pub fn Rectangle(&self, bounds: RECT) -> SysResult<()>

Rectangle function.

Source

pub fn RestoreDC(&self, saved_dc: i32) -> SysResult<()>

RestoreDC function.

Source

pub fn RoundRect(&self, bounds: RECT, sz: SIZE) -> SysResult<()>

RoundRect function.

Source

pub fn SaveDC(&self) -> SysResult<i32>

SaveDC function.

Source

pub fn SelectClipPath(&self, mode: RGN) -> SysResult<()>

SelectClipPath function.

Source

pub fn SelectClipRgn(&self, rgn: &HRGN) -> SysResult<REGION>

SelectClipRgn function.

Source

pub fn SelectObject<G>( &self, hgdiobj: &G, ) -> SysResult<SelectObjectGuard<'_, G>>
where G: GdiObject,

SelectObject function.

In the original C implementation, SelectObject returns a handle to the object being replaced. You must perform a cleanup operation, calling SelectObject again, passing the handle to the replaced object.

Here, the cleanup is performed automatically, because SelectObject returns a SelectObjectGuard, which stores the replaced handle and calls SelectObject automatically when the guard goes out of scope. You must, however, keep the guard alive, otherwise the cleanup will be performed right away.

§Examples
use winsafe::{self as w, prelude::*, co};

let hdc: w::HDC; // initialized somewhere

let hpen = w::HPEN::CreatePen(
    co::PS::SOLID,
    1,
    w::COLORREF::from_rgb(0xff, 0x00, 0x88),
)?;

let _pen_guard = hdc.SelectObject(&*hpen); // keep guard alive
Source

pub fn SelectPalette( &self, hpal: &HPALETTE, force_bkgd: bool, ) -> SysResult<HPALETTE>

SelectPalette function.

Source

pub fn SetArcDirection(&self, dir: AD) -> SysResult<AD>

SetArcDirection function.

Source

pub fn SetBkColor(&self, color: COLORREF) -> SysResult<COLORREF>

SetBkColor function.

Source

pub fn SetBkMode(&self, mode: BKMODE) -> SysResult<BKMODE>

SetBkMode function.

Source

pub fn SetBrushOrgEx(&self, new_origin: POINT) -> SysResult<POINT>

SetBrushOrgEx function.

Source

pub fn SetDCBrushColor(&self, color: COLORREF) -> SysResult<COLORREF>

SetDCBrushColor function.

Source

pub fn SetDCPenColor(&self, color: COLORREF) -> SysResult<COLORREF>

SetDCPenColor function.

Source

pub fn SetDIBits( &self, hbm: &HBITMAP, first_scan_line: u32, num_scan_lines: u32, dib_color_data: &[u8], bmi: &BITMAPINFO, color_use: DIB, ) -> SysResult<i32>

SetDIBits function.

Source

pub fn SetGraphicsMode(&self, mode: GM) -> SysResult<GM>

SetGraphicsMode function.

Source

pub fn SetStretchBltMode(&self, mode: STRETCH_MODE) -> SysResult<STRETCH_MODE>

Source

pub fn SetTextAlign(&self, align: TA) -> SysResult<TA>

SetTextAlign function.

Source

pub fn SetTextColor(&self, color: COLORREF) -> SysResult<COLORREF>

SetTextColor function.

Source

pub fn SetTextJustification(&self, extra: i32, count: i32) -> SysResult<()>

Source

pub fn SetViewportExtEx(&self, x: i32, y: i32) -> SysResult<SIZE>

SetViewportExtEx function.

Source

pub fn SetViewportOrgEx(&self, x: i32, y: i32) -> SysResult<POINT>

SetViewportOrgEx function.

Source

pub fn SetWindowExtEx(&self, x: i32, y: i32) -> SysResult<SIZE>

SetWindowExtEx function.

Source

pub fn SetWindowOrgEx(&self, x: i32, y: i32) -> SysResult<POINT>

SetWindowOrgEx function.

Source

pub fn StretchBlt( &self, pos_dest: POINT, sz_dest: SIZE, hdc_src: &HDC, pt_src: POINT, sz_src: SIZE, rop: ROP, ) -> SysResult<()>

StretchBlt function.

Source

pub fn StrokeAndFillPath(&self) -> SysResult<()>

Source

pub fn StrokePath(&self) -> SysResult<()>

StrokePath function.

Source

pub fn TextOut(&self, x: i32, y: i32, text: &str) -> SysResult<()>

TextOut function.

Source

pub fn TransparentBlt( &self, dest_top_left: POINT, dest_sz: SIZE, hdc_src: HDC, src_top_left: POINT, src_sz: SIZE, color_transparent: COLORREF, ) -> SysResult<()>

TransparentBlt function.

Source

pub fn UpdateColors(&self) -> SysResult<()>

UpdateColors function.

Source

pub fn WidenPath(&self) -> SysResult<()>

WidenPath function.

Source

pub unsafe fn raw_copy(&self) -> Self

Available on crate feature user only.

Returns a raw copy of the underlying handle pointer.

§Safety

As the name implies, raw_copy returns a raw copy of the handle, so closing one of the copies won’t close the others. This means a handle can be used after it has been closed, what can lead to errors and undefined behavior. Even worse: sometimes Windows reuses handle values, so you can call a method on a completely different handle type, what can be catastrophic.

However, in some cases the Windows API demands a copy of the handle – raw_copy is an escape hatch to fill this gap.

Source

pub unsafe fn as_mut(&mut self) -> &mut *mut c_void

Available on crate feature user only.

Returns a mutable reference to the underlying raw pointer.

This method can be used as an escape hatch to interoperate with other libraries.

§Safety

This method exposes the raw pointer used by raw Windows calls. It’s an opaque pointer to an internal Windows structure, and no dereferencings should be attempted.

Source

pub fn ptr(&self) -> *mut c_void

Available on crate feature user only.

Returns the underlying raw pointer.

This method exposes the raw pointer used by raw Windows calls. It’s an opaque pointer to an internal Windows structure, and no dereferencings should be attempted.

This method can be used as an escape hatch to interoperate with other libraries.

Source

pub fn DrawFocusRect(&self, rect: RECT) -> SysResult<()>

Available on crate feature user only.

DrawFocusRect function.

Source

pub fn DrawText(&self, text: &str, bounds: RECT, format: DT) -> SysResult<i32>

Available on crate feature user only.

DrawText function.

Source

pub fn DrawTextEx( &self, text: &str, bounds: RECT, format: DT, dtp: Option<&DRAWTEXTPARAMS>, ) -> SysResult<i32>

Available on crate feature user only.

DrawTextExW function.

Source

pub fn EnumDisplayMonitors<F>( &self, rc_clip: Option<RECT>, func: F, ) -> SysResult<()>
where F: FnMut(HMONITOR, HDC, &RECT) -> bool,

Available on crate feature user only.

EnumDisplayMonitors function.

§Examples
use winsafe::{self as w, prelude::*};

let hdc: w::HDC; // initialized somewhere

hdc.EnumDisplayMonitors(
    None,
    |hmon: w::HMONITOR, hdc: w::HDC, rc: &w::RECT| -> bool {
        println!("HMONITOR: {}, ", hmon);
        true
    },
)?;
Source

pub fn ExcludeUpdateRgn(&self, hwnd: &HWND) -> SysResult<REGION>

Available on crate feature user only.

ExcludeUpdateRgn function.

Source

pub fn FrameRect(&self, rc: RECT, hbr: &HBRUSH) -> SysResult<()>

Available on crate feature user only.

FrameRect function.

Source

pub fn InvertRect(&self, rc: RECT) -> SysResult<()>

Available on crate feature user only.

InvertRect function.

Source

pub fn PaintDesktop(&self) -> SysResult<()>

Available on crate feature user only.

PaintDesktop function.

Source

pub fn WindowFromDC(&self) -> Option<HWND>

Available on crate feature user only.

WindowFromDC function.

Trait Implementations§

Source§

impl Deref for DeleteDCGuard

Source§

type Target = HDC

The resulting type after dereferencing.
Source§

fn deref(&self) -> &Self::Target

Dereferences the value.
Source§

impl DerefMut for DeleteDCGuard

Source§

fn deref_mut(&mut self) -> &mut Self::Target

Mutably dereferences the value.
Source§

impl Drop for DeleteDCGuard

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<P, T> Receiver for P
where P: Deref<Target = T> + ?Sized, T: ?Sized,

Source§

type Target = T

🔬This is a nightly-only experimental API. (arbitrary_self_types)
The target type on which the method may be called.
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.