using System; using System.Collections.Generic; using System.Drawing; using System.Linq; using System.Runtime.InteropServices; using Vanara.InteropServices; namespace Vanara.PInvoke { public static partial class Gdi32 { /// /// The EnumObjectsProc function is an application-defined callback function used with the EnumObjects function. It is used to /// process the object data. The GOBJENUMPROC type defines a pointer to this callback function. EnumObjectsProc is a /// placeholder for the application-defined function name. /// /// /// /// /// To continue enumeration, the callback function must return a nonzero value. This value is user-defined. /// To stop enumeration, the callback function must return zero. /// /// An application must register this function by passing its address to the EnumObjects function. // https://docs.microsoft.com/en-us/windows/win32/api/wingdi/nc-wingdi-gobjenumproc GOBJENUMPROC Gobjenumproc; int Gobjenumproc( // LPVOID Arg1, LPARAM Arg2 ) {...} [PInvokeData("wingdi.h", MSDNShortId = "05a0f329-add9-4e92-9a9a-e2cf0ba5a1c3")] [UnmanagedFunctionPointer(CallingConvention.Winapi)] public delegate int EnumObjectsProc(IntPtr Arg1, IntPtr Arg2); /// Values used by the function. public enum DeviceCap { /// Device driver version DRIVERVERSION = 0, /// Device classification TECHNOLOGY = 2, /// Horizontal size in millimeters HORZSIZE = 4, /// Vertical size in millimeters VERTSIZE = 6, /// Horizontal width in pixels HORZRES = 8, /// Vertical height in pixels VERTRES = 10, /// Number of bits per pixel BITSPIXEL = 12, /// Number of planes PLANES = 14, /// Number of brushes the device has NUMBRUSHES = 16, /// Number of pens the device has NUMPENS = 18, /// Number of markers the device has NUMMARKERS = 20, /// Number of fonts the device has NUMFONTS = 22, /// Number of colors the device supports NUMCOLORS = 24, /// Size required for device descriptor PDEVICESIZE = 26, /// Curve capabilities CURVECAPS = 28, /// Line capabilities LINECAPS = 30, /// Polygonal capabilities POLYGONALCAPS = 32, /// Text capabilities TEXTCAPS = 34, /// Clipping capabilities CLIPCAPS = 36, /// Bitblt capabilities RASTERCAPS = 38, /// Length of the X leg ASPECTX = 40, /// Length of the Y leg ASPECTY = 42, /// Length of the hypotenuse ASPECTXY = 44, /// Shading and Blending caps SHADEBLENDCAPS = 45, /// Logical pixels inch in X LOGPIXELSX = 88, /// Logical pixels inch in Y LOGPIXELSY = 90, /// Number of entries in physical palette SIZEPALETTE = 104, /// Number of reserved entries in palette NUMRESERVED = 106, /// Actual color resolution COLORRES = 108, // Printing related DeviceCaps. These replace the appropriate Escapes /// Physical Width in device units PHYSICALWIDTH = 110, /// Physical Height in device units PHYSICALHEIGHT = 111, /// Physical Printable Area x margin PHYSICALOFFSETX = 112, /// Physical Printable Area y margin PHYSICALOFFSETY = 113, /// Scaling factor x SCALINGFACTORX = 114, /// Scaling factor y SCALINGFACTORY = 115, /// Current vertical refresh rate of the display device (for displays only) in Hz VREFRESH = 116, /// Vertical height of entire desktop in pixels DESKTOPVERTRES = 117, /// Horizontal width of entire desktop in pixels DESKTOPHORZRES = 118, /// Preferred blt alignment BLTALIGNMENT = 119 } /// Object Definitions for EnumObjects [PInvokeData("wingdi.h", MSDNShortId = "2a7b60b2-9a68-4c56-9376-c1b780488535")] public enum ObjType { /// [CorrespondingType(typeof(HPEN))] OBJ_PEN = 1, /// [CorrespondingType(typeof(HBRUSH))] OBJ_BRUSH = 2, /// [CorrespondingType(typeof(HDC))] OBJ_DC = 3, /// OBJ_METADC = 4, /// [CorrespondingType(typeof(HPALETTE))] OBJ_PAL = 5, /// [CorrespondingType(typeof(HFONT))] OBJ_FONT = 6, /// [CorrespondingType(typeof(HBITMAP))] OBJ_BITMAP = 7, /// [CorrespondingType(typeof(HRGN))] OBJ_REGION = 8, /// [CorrespondingType(typeof(HMETAFILE))] OBJ_METAFILE = 9, /// OBJ_MEMDC = 10, /// OBJ_EXTPEN = 11, /// OBJ_ENHMETADC = 12, /// [CorrespondingType(typeof(HENHMETAFILE))] OBJ_ENHMETAFILE = 13, /// [CorrespondingType(typeof(HCOLORSPACE))] OBJ_COLORSPACE = 14, } /// Stock object type. [PInvokeData("wingdi.h", MSDNShortId = "b14ddc05-7e7b-4fc6-b7e3-efe892df7e21")] public enum StockObjectType { /// Windows fixed-pitch (monospace) system font. [CorrespondingType(typeof(HFONT))] ANSI_FIXED_FONT = 11, /// Windows variable-pitch (proportional space) system font. [CorrespondingType(typeof(HFONT))] ANSI_VAR_FONT = 12, /// Black brush. [CorrespondingType(typeof(HBRUSH))] BLACK_BRUSH = 4, /// Black pen. [CorrespondingType(typeof(HPEN))] BLACK_PEN = 7, /// /// Solid color brush. The default color is white. The color can be changed by using the SetDCBrushColor function. For more /// information, see the Remarks section. /// [CorrespondingType(typeof(HBRUSH))] DC_BRUSH = 18, /// /// Solid pen color. The default color is white. The color can be changed by using the SetDCPenColor function. For more /// information, see the Remarks section. /// [CorrespondingType(typeof(HPEN))] DC_PEN = 19, /// /// Default font for user interface objects such as menus and dialog boxes. It is not recommended that you use DEFAULT_GUI_FONT /// or SYSTEM_FONT to obtain the font used by dialogs and windows; for more information, see the remarks section. The default /// font is Tahoma. /// [CorrespondingType(typeof(HFONT))] DEFAULT_GUI_FONT = 17, /// Default palette. This palette consists of the static colors in the system palette. [CorrespondingType(typeof(HPALETTE))] DEFAULT_PALETTE = 15, /// Device-dependent font. [CorrespondingType(typeof(HFONT))] DEVICE_DEFAULT_FONT = 14, /// Dark gray brush. [CorrespondingType(typeof(HBRUSH))] DKGRAY_BRUSH = 3, /// Gray brush. [CorrespondingType(typeof(HBRUSH))] GRAY_BRUSH = 2, /// Hollow brush (equivalent to NULL_BRUSH). [CorrespondingType(typeof(HBRUSH))] HOLLOW_BRUSH = NULL_BRUSH, /// Light gray brush. [CorrespondingType(typeof(HBRUSH))] LTGRAY_BRUSH = 1, /// Null brush (equivalent to HOLLOW_BRUSH). [CorrespondingType(typeof(HBRUSH))] NULL_BRUSH = 5, /// Null pen. The null pen draws nothing. [CorrespondingType(typeof(HPEN))] NULL_PEN = 8, /// Original equipment manufacturer (OEM) dependent fixed-pitch (monospace) font. [CorrespondingType(typeof(HFONT))] OEM_FIXED_FONT = 10, /// /// Fixed-pitch (monospace) system font. This stock object is provided only for compatibility with 16-bit Windows versions /// earlier than 3.0. /// [CorrespondingType(typeof(HFONT))] SYSTEM_FIXED_FONT = 16, /// /// System font. By default, the system uses the system font to draw menus, dialog box controls, and text. It is not recommended /// that you use DEFAULT_GUI_FONT or SYSTEM_FONT to obtain the font used by dialogs and windows; for more information, see the /// remarks section. The default system font is Tahoma. /// [CorrespondingType(typeof(HFONT))] SYSTEM_FONT = 13, /// White brush. [CorrespondingType(typeof(HBRUSH))] WHITE_BRUSH = 0, /// White pen. [CorrespondingType(typeof(HPEN))] WHITE_PEN = 6, } /// The CancelDC function cancels any pending operation on the specified device context (DC). /// A handle to the DC. /// /// If the function succeeds, the return value is nonzero. /// If the function fails, the return value is zero. /// /// /// /// The CancelDC function is used by multithreaded applications to cancel lengthy drawing operations. If thread A initiates a /// lengthy drawing operation, thread B may cancel that operation by calling this function. /// /// /// If an operation is canceled, the affected thread returns an error and the result of its drawing operation is undefined. The /// results are also undefined if no drawing operation was in progress when the function was called. /// /// // https://docs.microsoft.com/en-us/windows/win32/api/wingdi/nf-wingdi-canceldc BOOL CancelDC( HDC hdc ); [DllImport(Lib.Gdi32, SetLastError = false, ExactSpelling = true)] [PInvokeData("wingdi.h", MSDNShortId = "1dcb3dfe-0ab0-4bf5-ac2f-7a9c11712eef")] [return: MarshalAs(UnmanagedType.Bool)] public static extern bool CancelDC(HDC hdc); /// The CreateCompatibleDC function creates a memory device context (DC) compatible with the specified device. /// /// A handle to an existing DC. If this handle is NULL, the function creates a memory DC compatible with the application's current screen. /// /// /// If the function succeeds, the return value is the handle to a memory DC. /// If the function fails, the return value is NULL. /// /// /// A memory DC exists only in memory. When the memory DC is created, its display surface is exactly one monochrome pixel wide and /// one monochrome pixel high. Before an application can use a memory DC for drawing operations, it must select a bitmap of the /// correct width and height into the DC. To select a bitmap into a DC, use the CreateCompatibleBitmap function, specifying the /// height, width, and color organization required. /// /// When a memory DC is created, all attributes are set to normal default values. The memory DC can be used as a normal DC. You can /// set the attributes; obtain the current settings of its attributes; and select pens, brushes, and regions. /// /// /// The CreateCompatibleDC function can only be used with devices that support raster operations. An application can determine /// whether a device supports these operations by calling the GetDeviceCaps function. /// /// /// When you no longer need the memory DC, call the DeleteDC function. We recommend that you call DeleteDC to delete the DC. However, /// you can also call DeleteObject with the HDC to delete the DC. /// /// /// If hdc is NULL, the thread that calls CreateCompatibleDC owns the HDC that is created. When this thread is destroyed, the HDC is /// no longer valid. Thus, if you create the HDC and pass it to another thread, then exit the first thread, the second thread will /// not be able to use the HDC. /// /// /// ICM: If the DC that is passed to this function is enabled for Image Color Management (ICM), the DC created by the function is /// ICM-enabled. The source and destination color spaces are specified in the DC. /// /// [DllImport(Lib.Gdi32, ExactSpelling = true, SetLastError = true)] [PInvokeData("Wingdi.h", MSDNShortId = "dd183489")] public static extern SafeHDC CreateCompatibleDC([Optional] HDC hDC); /// The CreateDC function creates a device context (DC) for a device using the specified name. /// /// A pointer to a null-terminated character string that specifies either DISPLAY or the name of a specific display device. For /// printing, we recommend that you pass NULL to lpszDriver because GDI ignores lpszDriver for printer devices. /// /// /// /// A pointer to a null-terminated character string that specifies the name of the specific output device being used, as shown by the /// Print Manager (for example, Epson FX-80). It is not the printer model name. The lpszDevice parameter must be used. /// /// To obtain valid names for displays, call EnumDisplayDevices. /// /// If lpszDriver is DISPLAY or the device name of a specific display device, then lpszDevice must be NULL or that same device /// name. If lpszDevice is NULL, then a DC is created for the primary display device. /// /// If there are multiple monitors on the system, calling will create a DC covering all the monitors. /// /// /// This parameter is ignored and should be set to NULL. It is provided only for compatibility with 16-bit Windows. /// /// /// /// A pointer to a DEVMODE structure containing device-specific initialization data for the device driver. The DocumentProperties /// function retrieves this structure filled in for a specified device. The pdm parameter must be NULL if the device driver is /// to use the default initialization (if any) specified by the user. /// /// If lpszDriver is DISPLAY, pdm must be NULL; GDI then uses the display device's current DEVMODE. /// /// /// If the function succeeds, the return value is the handle to a DC for the specified device. /// If the function fails, the return value is NULL. /// /// /// Note that the handle to the DC can only be used by a single thread at any one time. /// For parameters lpszDriver and lpszDevice, call EnumDisplayDevices to obtain valid names for displays. /// When you no longer need the DC, call the DeleteDC function. /// /// If lpszDriver or lpszDevice is DISPLAY, the thread that calls CreateDC owns the HDC that is created. When this /// thread is destroyed, the HDC is no longer valid. Thus, if you create the HDC and pass it to another thread, then /// exit the first thread, the second thread will not be able to use the HDC. /// /// /// When you call CreateDC to create the HDC for a display device, you must pass to pdm either NULL or a pointer /// to DEVMODE that matches the current DEVMODE of the display device that lpszDevice specifies. We recommend to pass /// NULL and not to try to exactly match the DEVMODE for the current display device. /// /// /// When you call CreateDC to create the HDC for a printer device, the printer driver validates the DEVMODE. If the /// printer driver determines that the DEVMODE is invalid (that is, printer driver can’t convert or consume the DEVMODE), the /// printer driver provides a default DEVMODE to create the HDC for the printer device. /// /// /// ICM: To enable ICM, set the dmICMMethod member of the DEVMODE structure (pointed to by the pInitData parameter) to /// the appropriate value. /// /// Examples /// For an example, see Capturing an Image. /// // https://docs.microsoft.com/en-us/windows/win32/api/wingdi/nf-wingdi-createdca HDC CreateDCA( LPCSTR pwszDriver, LPCSTR pwszDevice, // LPCSTR pszPort, const DEVMODEA *pdm ); [DllImport(Lib.Gdi32, SetLastError = false, CharSet = CharSet.Auto)] [PInvokeData("wingdi.h", MSDNShortId = "6fc443c8-da97-4196-a9ed-179a4e583849")] public static extern SafeHDC CreateDC([Optional] string pwszDriver, [Optional] string pwszDevice, [Optional] string pszPort, ref DEVMODE pdm); /// The CreateDC function creates a device context (DC) for a device using the specified name. /// /// A pointer to a null-terminated character string that specifies either DISPLAY or the name of a specific display device. For /// printing, we recommend that you pass NULL to lpszDriver because GDI ignores lpszDriver for printer devices. /// /// /// /// A pointer to a null-terminated character string that specifies the name of the specific output device being used, as shown by the /// Print Manager (for example, Epson FX-80). It is not the printer model name. The lpszDevice parameter must be used. /// /// To obtain valid names for displays, call EnumDisplayDevices. /// /// If lpszDriver is DISPLAY or the device name of a specific display device, then lpszDevice must be NULL or that same device /// name. If lpszDevice is NULL, then a DC is created for the primary display device. /// /// If there are multiple monitors on the system, calling will create a DC covering all the monitors. /// /// /// This parameter is ignored and should be set to NULL. It is provided only for compatibility with 16-bit Windows. /// /// /// /// A pointer to a DEVMODE structure containing device-specific initialization data for the device driver. The DocumentProperties /// function retrieves this structure filled in for a specified device. The pdm parameter must be NULL if the device driver is /// to use the default initialization (if any) specified by the user. /// /// If lpszDriver is DISPLAY, pdm must be NULL; GDI then uses the display device's current DEVMODE. /// /// /// If the function succeeds, the return value is the handle to a DC for the specified device. /// If the function fails, the return value is NULL. /// /// /// Note that the handle to the DC can only be used by a single thread at any one time. /// For parameters lpszDriver and lpszDevice, call EnumDisplayDevices to obtain valid names for displays. /// When you no longer need the DC, call the DeleteDC function. /// /// If lpszDriver or lpszDevice is DISPLAY, the thread that calls CreateDC owns the HDC that is created. When this /// thread is destroyed, the HDC is no longer valid. Thus, if you create the HDC and pass it to another thread, then /// exit the first thread, the second thread will not be able to use the HDC. /// /// /// When you call CreateDC to create the HDC for a display device, you must pass to pdm either NULL or a pointer /// to DEVMODE that matches the current DEVMODE of the display device that lpszDevice specifies. We recommend to pass /// NULL and not to try to exactly match the DEVMODE for the current display device. /// /// /// When you call CreateDC to create the HDC for a printer device, the printer driver validates the DEVMODE. If the /// printer driver determines that the DEVMODE is invalid (that is, printer driver can’t convert or consume the DEVMODE), the /// printer driver provides a default DEVMODE to create the HDC for the printer device. /// /// /// ICM: To enable ICM, set the dmICMMethod member of the DEVMODE structure (pointed to by the pInitData parameter) to /// the appropriate value. /// /// Examples /// For an example, see Capturing an Image. /// // https://docs.microsoft.com/en-us/windows/win32/api/wingdi/nf-wingdi-createdca HDC CreateDCA( LPCSTR pwszDriver, LPCSTR pwszDevice, // LPCSTR pszPort, const DEVMODEA *pdm ); [DllImport(Lib.Gdi32, SetLastError = false, CharSet = CharSet.Auto)] [PInvokeData("wingdi.h", MSDNShortId = "6fc443c8-da97-4196-a9ed-179a4e583849")] public static extern SafeHDC CreateDC([Optional] string pwszDriver, [Optional] string pwszDevice, [Optional] string pszPort, [Optional] IntPtr pdm); /// /// The CreateIC function creates an information context for the specified device. The information context provides a fast way /// to get information about the device without creating a device context (DC). However, GDI drawing functions cannot accept a handle /// to an information context. /// /// /// A pointer to a null-terminated character string that specifies the name of the device driver (for example, Epson). /// /// /// A pointer to a null-terminated character string that specifies the name of the specific output device being used, as shown by the /// Print Manager (for example, Epson FX-80). It is not the printer model name. The lpszDevice parameter must be used. /// /// /// This parameter is ignored and should be set to NULL. It is provided only for compatibility with 16-bit Windows. /// /// /// A pointer to a DEVMODE structure containing device-specific initialization data for the device driver. The DocumentProperties /// function retrieves this structure filled in for a specified device. The lpdvmInit parameter must be NULL if the device /// driver is to use the default initialization (if any) specified by the user. /// /// /// If the function succeeds, the return value is the handle to an information context. /// If the function fails, the return value is NULL. /// /// When you no longer need the information DC, call the DeleteDC function. // https://docs.microsoft.com/en-us/windows/win32/api/wingdi/nf-wingdi-createica HDC CreateICA( LPCSTR pszDriver, LPCSTR pszDevice, // LPCSTR pszPort, const DEVMODEA *pdm ); [DllImport(Lib.Gdi32, SetLastError = false, CharSet = CharSet.Auto)] [PInvokeData("wingdi.h", MSDNShortId = "dcb08ce7-9ded-497c-936c-48d3026a0004")] public static extern SafeHDC CreateIC([Optional] string pszDriver, [Optional] string pszDevice, [Optional] string pszPort, ref DEVMODE pdm); /// /// The CreateIC function creates an information context for the specified device. The information context provides a fast way /// to get information about the device without creating a device context (DC). However, GDI drawing functions cannot accept a handle /// to an information context. /// /// /// A pointer to a null-terminated character string that specifies the name of the device driver (for example, Epson). /// /// /// A pointer to a null-terminated character string that specifies the name of the specific output device being used, as shown by the /// Print Manager (for example, Epson FX-80). It is not the printer model name. The lpszDevice parameter must be used. /// /// /// This parameter is ignored and should be set to NULL. It is provided only for compatibility with 16-bit Windows. /// /// /// A pointer to a DEVMODE structure containing device-specific initialization data for the device driver. The DocumentProperties /// function retrieves this structure filled in for a specified device. The lpdvmInit parameter must be NULL if the device /// driver is to use the default initialization (if any) specified by the user. /// /// /// If the function succeeds, the return value is the handle to an information context. /// If the function fails, the return value is NULL. /// /// When you no longer need the information DC, call the DeleteDC function. // https://docs.microsoft.com/en-us/windows/win32/api/wingdi/nf-wingdi-createica HDC CreateICA( LPCSTR pszDriver, LPCSTR pszDevice, // LPCSTR pszPort, const DEVMODEA *pdm ); [DllImport(Lib.Gdi32, SetLastError = false, CharSet = CharSet.Auto)] [PInvokeData("wingdi.h", MSDNShortId = "dcb08ce7-9ded-497c-936c-48d3026a0004")] public static extern SafeHDC CreateIC([Optional] string pszDriver, [Optional] string pszDevice, [Optional] string pszPort, [Optional] IntPtr pdm); /// The DeleteDC function deletes the specified device context (DC). /// A handle to the device context. /// If the function succeeds, the return value is nonzero. If the function fails, the return value is zero. /// /// An application must not delete a DC whose handle was obtained by calling the GetDC function. Instead, it must call the ReleaseDC /// function to free the DC. /// [DllImport(Lib.Gdi32, ExactSpelling = true, SetLastError = true)] [return: MarshalAs(UnmanagedType.Bool)] [System.Security.SecurityCritical] [PInvokeData("Wingdi.h", MSDNShortId = "dd183533")] public static extern bool DeleteDC(HDC hdc); /// /// The DeleteObject function deletes a logical pen, brush, font, bitmap, region, or palette, freeing all system resources associated /// with the object. After the object is deleted, the specified handle is no longer valid. /// /// A handle to a logical pen, brush, font, bitmap, region, or palette. /// /// If the function succeeds, the return value is nonzero. If the specified handle is not valid or is currently selected into a DC, /// the return value is zero. /// /// /// Do not delete a drawing object (pen or brush) while it is still selected into a DC. /// When a pattern brush is deleted, the bitmap associated with the brush is not deleted. The bitmap must be deleted independently. /// [DllImport(Lib.Gdi32, ExactSpelling = true, SetLastError = true)] [return: MarshalAs(UnmanagedType.Bool)] [PInvokeData("Wingdi.h", MSDNShortId = "dd183539")] public static extern bool DeleteObject(HGDIOBJ hObject); // TODO: DeviceCapabilities from winspool.drv ?? /// /// The DrawEscape function provides drawing capabilities of the specified video display that are not directly available /// through the graphics device interface (GDI). /// /// A handle to the DC for the specified video display. /// The escape function to be performed. /// The number of bytes of data pointed to by the lpszInData parameter. /// A pointer to the input structure required for the specified escape. /// /// /// If the function is successful, the return value is greater than zero except for the QUERYESCSUPPORT draw escape, which checks for /// implementation only. /// /// If the escape is not implemented, the return value is zero. /// If an error occurred, the return value is less than zero. /// /// /// When an application calls the DrawEscape function, the data identified by cbInput and lpszInData is passed directly to the /// specified display driver. /// // https://docs.microsoft.com/en-us/windows/win32/api/wingdi/nf-wingdi-drawescape int DrawEscape( HDC hdc, int iEscape, int cjIn, // LPCSTR lpIn ); [DllImport(Lib.Gdi32, SetLastError = false, ExactSpelling = true)] [PInvokeData("wingdi.h", MSDNShortId = "306eec06-6d29-43bc-aff0-a267efa52ccd")] public static extern int DrawEscape(HDC hdc, int iEscape, int cjIn, [MarshalAs(UnmanagedType.LPStr)] string lpIn); /// /// The EnumObjects function enumerates the pens or brushes available for the specified device context (DC). This function /// calls the application-defined callback function once for each available object, supplying data describing that object. /// EnumObjects continues calling the callback function until the callback function returns zero or until all of the objects /// have been enumerated. /// /// A handle to the DC. /// The object type. This parameter can be OBJ_BRUSH or OBJ_PEN. /// /// A pointer to the application-defined callback function. For more information about the callback function, see the EnumObjectsProc function. /// /// /// A pointer to the application-defined data. The data is passed to the callback function along with the object information. /// /// /// If the function succeeds, the return value is the last value returned by the callback function. Its meaning is user-defined. /// /// If the objects cannot be enumerated (for example, there are too many objects), the function returns zero without calling the /// callback function. /// /// // https://docs.microsoft.com/en-us/windows/win32/api/wingdi/nf-wingdi-enumobjects int EnumObjects( HDC hdc, int nType, GOBJENUMPROC // lpFunc, LPARAM lParam ); [DllImport(Lib.Gdi32, SetLastError = false, ExactSpelling = true)] [PInvokeData("wingdi.h", MSDNShortId = "2a7b60b2-9a68-4c56-9376-c1b780488535")] public static extern int EnumObjects(HDC hdc, ObjType nType, EnumObjectsProc lpFunc, [Optional] IntPtr lParam); /// /// The EnumObjects function enumerates the pens or brushes available for the specified device context (DC). This function /// calls the application-defined callback function once for each available object, supplying data describing that object. /// EnumObjects continues calling the callback function until the callback function returns zero or until all of the objects /// have been enumerated. /// /// The type of the handle to enumerate. Currently, only HPEN and HBRUSH are supported. /// A handle to the DC. /// An enumeration of the object handles for the . /// The supplied type cannot be enumerated by this function. [PInvokeData("wingdi.h", MSDNShortId = "2a7b60b2-9a68-4c56-9376-c1b780488535")] public static IEnumerable EnumObjects(HDC hdc) where T : IHandle { ObjType ev = 0; if ((int)Enum.GetValues(typeof(ObjType)).Cast().FirstOrDefault(v => CorrespondingTypeAttribute.CanGet(ev = v, typeof(T))) == 0) throw new ArgumentException($"The supplied type cannot be enumerated by this function."); var l = new List(); EnumObjects(hdc, ev, EnumProc); return l.Cast(); int EnumProc(IntPtr Arg1, IntPtr Arg2) { l.Add(Arg1); return 1; } } /// /// The GetCurrentObject function retrieves a handle to an object of the specified type that has been selected into the /// specified device context (DC). /// /// A handle to the DC. /// /// The object type to be queried. This parameter can be one of the following values. /// /// /// Value /// Meaning /// /// /// OBJ_BITMAP /// Returns the current selected bitmap. /// /// /// OBJ_BRUSH /// Returns the current selected brush. /// /// /// OBJ_COLORSPACE /// Returns the current color space. /// /// /// OBJ_FONT /// Returns the current selected font. /// /// /// OBJ_PAL /// Returns the current selected palette. /// /// /// OBJ_PEN /// Returns the current selected pen. /// /// /// /// /// If the function succeeds, the return value is a handle to the specified object. /// If the function fails, the return value is NULL. /// /// /// /// An application can use the GetCurrentObject and GetObject functions to retrieve descriptions of the graphic objects /// currently selected into the specified DC. /// /// Examples /// For an example, see Retrieving Graphic-Object Attributes and Selecting New Graphic Objects. /// // https://docs.microsoft.com/en-us/windows/win32/api/wingdi/nf-wingdi-getcurrentobject HGDIOBJ GetCurrentObject( HDC hdc, UINT type ); [DllImport(Lib.Gdi32, SetLastError = false, ExactSpelling = true)] [PInvokeData("wingdi.h", MSDNShortId = "d7e2310c-6a9e-4195-824c-1a83382a5c5b")] public static extern HGDIOBJ GetCurrentObject(HDC hdc, ObjType type); /// The GetDCBrushColor function retrieves the current brush color for the specified device context (DC). /// A handle to the DC whose brush color is to be returned. /// /// If the function succeeds, the return value is the COLORREF value for the current DC brush color. /// If the function fails, the return value is CLR_INVALID. /// /// /// For information on setting the brush color, see SetDCBrushColor. /// ICM: Color management is performed if ICM is enabled. /// // https://docs.microsoft.com/en-us/windows/win32/api/wingdi/nf-wingdi-getdcbrushcolor COLORREF GetDCBrushColor( HDC hdc ); [DllImport(Lib.Gdi32, SetLastError = false, ExactSpelling = true)] [PInvokeData("wingdi.h", MSDNShortId = "98844fb1-7ad8-4fbd-be59-9a19065253da")] public static extern COLORREF GetDCBrushColor(HDC hdc); /// /// The GetDCOrgEx function retrieves the final translation origin for a specified device context (DC). The final translation /// origin specifies an offset that the system uses to translate device coordinates into client coordinates (for coordinates in an /// application's window). /// /// A handle to the DC whose final translation origin is to be retrieved. /// A pointer to a POINT structure that receives the final translation origin, in device coordinates. /// /// If the function succeeds, the return value is nonzero. /// If the function fails, the return value is zero. /// /// The final translation origin is relative to the physical origin of the screen. // https://docs.microsoft.com/en-us/windows/win32/api/wingdi/nf-wingdi-getdcorgex BOOL GetDCOrgEx( HDC hdc, LPPOINT lppt ); [DllImport(Lib.Gdi32, SetLastError = false, ExactSpelling = true)] [PInvokeData("wingdi.h", MSDNShortId = "795c6a69-7146-4d1a-abf9-ce1d740ca946")] [return: MarshalAs(UnmanagedType.Bool)] public static extern bool GetDCOrgEx(HDC hdc, out Point lppt); /// The GetDCPenColor function retrieves the current pen color for the specified device context (DC). /// A handle to the DC whose brush color is to be returned. /// /// If the function succeeds, the return value is a COLORREF value for the current DC pen color. /// If the function fails, the return value is CLR_INVALID. /// /// /// For information on setting the pen color, see SetDCPenColor. /// ICM: Color management is performed if ICM is enabled. /// // https://docs.microsoft.com/en-us/windows/win32/api/wingdi/nf-wingdi-getdcpencolor COLORREF GetDCPenColor( HDC hdc ); [DllImport(Lib.Gdi32, SetLastError = false, ExactSpelling = true)] [PInvokeData("wingdi.h", MSDNShortId = "3a1d579f-fbc6-4021-a37e-0184b2cc7d5d")] public static extern COLORREF GetDCPenColor(HDC hdc); /// The GetDeviceCaps function retrieves device-specific information for the specified device. /// A handle to the DC. /// The item to be returned. /// /// The return value specifies the value of the desired item. When nIndex is BITSPIXEL and the device has 15bpp or 16bpp, the return /// value is 16. /// // https://msdn.microsoft.com/en-us/library/windows/desktop/dd144877(v=vs.85).aspx [DllImport(Lib.Gdi32, SetLastError = false, ExactSpelling = true)] [PInvokeData("Wingdi.h", MSDNShortId = "dd144877")] public static extern int GetDeviceCaps(HDC hdc, DeviceCap index); /// The GetLayout function returns the layout of a device context (DC). /// A handle to the device context. /// /// If the function succeeds, it returns the layout flags for the current device context. /// If the function fails, it returns GDI_ERROR. For extended error information, call GetLastError. /// /// /// The layout specifies the order in which text and graphics are revealed in a window or device context. The default is left to /// right. The GetLayout function tells you if the default has been changed through a call to SetLayout. For more information, /// see "Window Layout and Mirroring" in Window Features. /// // https://docs.microsoft.com/en-us/windows/win32/api/wingdi/nf-wingdi-getlayout DWORD GetLayout( HDC hdc ); [DllImport(Lib.Gdi32, SetLastError = true, ExactSpelling = true)] [PInvokeData("wingdi.h", MSDNShortId = "2bbc0bef-55e5-4f11-a195-d379e95e44bf")] public static extern DCLayout GetLayout(HDC hdc); /// The GetObject function retrieves information for the specified graphics object. /// /// A handle to the graphics object of interest. This can be a handle to one of the following: a logical bitmap, a brush, a font, a /// palette, a pen, or a device independent bitmap created by calling the CreateDIBSection function. /// /// The number of bytes of information to be written to the buffer. /// /// A pointer to a buffer that receives the information about the specified graphics object. If the /// parameter is NULL, the function return value is the number of bytes required to store the information it writes to the buffer for /// the specified graphics object. /// /// /// If the function succeeds, and is a valid pointer, the return value is the number of bytes stored /// into the buffer. /// /// If the function succeeds, and is NULL, the return value is the number of bytes required to hold the /// information the function would store into the buffer. /// /// If the function fails, the return value is zero. /// /// /// The buffer pointed to by the parameter must be sufficiently large to receive the information about /// the graphics object. Depending on the graphics object, the function uses a BITMAP, DIBSECTION, EXTLOGPEN, LOGBRUSH, LOGFONT, or /// LOGPEN structure, or a count of table entries (for a logical palette). /// /// If is a handle to a bitmap created by calling CreateDIBSection, and the specified buffer is large /// enough, the GetObject function returns a DIBSECTION structure. In addition, the bmBits member of the BITMAP structure contained /// within the DIBSECTION will contain a pointer to the bitmap's bit values. /// /// /// If is a handle to a bitmap created by any other means, GetObject returns only the width, height, and /// color format information of the bitmap. You can obtain the bitmap's bit values by calling the GetDIBits or GetBitmapBits function. /// /// /// If is a handle to a logical palette, GetObject retrieves a 2-byte integer that specifies the number of /// entries in the palette. The function does not retrieve the LOGPALETTE structure defining the palette. To retrieve information /// about palette entries, an application can call the GetPaletteEntries function. /// /// /// If is a handle to a font, the LOGFONT that is returned is the LOGFONT used to create the font. If /// Windows had to make some interpolation of the font because the precise LOGFONT could not be represented, the interpolation will /// not be reflected in the LOGFONT. For example, if you ask for a vertical version of a font that doesn't support vertical painting, /// the LOGFONT indicates the font is vertical, but Windows will paint it horizontally. /// /// // https://docs.microsoft.com/en-us/windows/win32/api/wingdi/nf-wingdi-getobject // int GetObject( HANDLE h, int c, LPVOID pv ); [DllImport(Lib.Gdi32, SetLastError = false, CharSet = CharSet.Auto)] [PInvokeData("wingdi.h", MSDNShortId = "555ab876-d990-426d-915c-f98df82a10aa")] public static extern int GetObject(HGDIOBJ hgdiobj, int cbBuffer, IntPtr lpvObject); /// The GetObject function retrieves information for the specified graphics object. /// The output structure type. /// /// A handle to the graphics object of interest. This can be a handle to one of the following: a logical bitmap, a brush, a font, a /// palette, a pen, or a device independent bitmap created by calling the CreateDIBSection function. /// /// The output structure holding the information for the graphics object. [PInvokeData("Wingdi.h", MSDNShortId = "dd144904")] public static T GetObject(IGraphicsObjectHandle hgdiobj) where T : struct { using var ptr = GetObject(hgdiobj, Marshal.SizeOf(typeof(T))); var ot = GetObjectType(hgdiobj.DangerousGetHandle()); switch (ot) { case ObjType.OBJ_PEN: if (typeof(T) == typeof(EXTLOGPEN) || typeof(T) == typeof(LOGPEN)) return ptr.ToStructure(); break; case ObjType.OBJ_BRUSH: if (typeof(T) == typeof(LOGBRUSH)) return ptr.ToStructure(); break; case ObjType.OBJ_PAL: if (typeof(T) == typeof(ushort)) return ptr.ToStructure(); break; case ObjType.OBJ_FONT: if (typeof(T) == typeof(LOGFONT)) return ptr.ToStructure(); break; case ObjType.OBJ_BITMAP: if (typeof(T) == typeof(BITMAP) && ptr.Size >= Marshal.SizeOf(typeof(BITMAP))) return ptr.ToStructure(); if (typeof(T) == typeof(DIBSECTION) && ptr.Size == Marshal.SizeOf(typeof(DIBSECTION))) return ptr.ToStructure(); break; } throw new ArgumentException($"The specified type ({typeof(T).Name}) cannot be retrieved from an object of type {ot}."); } /// The GetObject function retrieves information for the specified graphics object. /// /// A handle to the graphics object of interest. This can be a handle to one of the following: a logical bitmap, a brush, a font, a /// palette, a pen, or a device independent bitmap created by calling the CreateDIBSection function. /// /// Size of the buffer. If this value is 0, then the size is requested. /// Allocated memory holding the information for the graphics object. [PInvokeData("Wingdi.h", MSDNShortId = "dd144904")] public static ISafeMemoryHandle GetObject(IGraphicsObjectHandle hgdiobj, int bufferSize = 0) { if (bufferSize == 0) { bufferSize = GetObject(hgdiobj.DangerousGetHandle(), 0, IntPtr.Zero); if (bufferSize == 0) Win32Error.ThrowLastError(); } var ptr = new SafeHGlobalHandle(bufferSize); var sz = GetObject(hgdiobj.DangerousGetHandle(), ptr.Size, ptr); if (sz == 0) Win32Error.ThrowLastError(); ptr.Size = sz; return ptr; } /// The GetObjectType retrieves the type of the specified object. /// A handle to the graphics object. /// /// If the function succeeds, the return value identifies the object. This value can be one of the following. /// /// /// Value /// Meaning /// /// /// OBJ_BITMAP /// Bitmap /// /// /// OBJ_BRUSH /// Brush /// /// /// OBJ_COLORSPACE /// Color space /// /// /// OBJ_DC /// Device context /// /// /// OBJ_ENHMETADC /// Enhanced metafile DC /// /// /// OBJ_ENHMETAFILE /// Enhanced metafile /// /// /// OBJ_EXTPEN /// Extended pen /// /// /// OBJ_FONT /// Font /// /// /// OBJ_MEMDC /// Memory DC /// /// /// OBJ_METAFILE /// Metafile /// /// /// OBJ_METADC /// Metafile DC /// /// /// OBJ_PAL /// Palette /// /// /// OBJ_PEN /// Pen /// /// /// OBJ_REGION /// Region /// /// /// If the function fails, the return value is zero. /// // https://docs.microsoft.com/en-us/windows/win32/api/wingdi/nf-wingdi-getobjecttype DWORD GetObjectType( HGDIOBJ h ); [DllImport(Lib.Gdi32, SetLastError = true, ExactSpelling = true)] [PInvokeData("wingdi.h", MSDNShortId = "334a2c95-3bf4-44dc-abce-df3a3a2d37a8")] public static extern ObjType GetObjectType(HGDIOBJ h); /// The GetStockObject function retrieves a handle to one of the stock pens, brushes, fonts, or palettes. /// /// The type of stock object. This parameter can be one of the following values. /// /// /// Value /// Meaning /// /// /// BLACK_BRUSH /// Black brush. /// /// /// DKGRAY_BRUSH /// Dark gray brush. /// /// /// DC_BRUSH /// /// Solid color brush. The default color is white. The color can be changed by using the SetDCBrushColor function. For more /// information, see the Remarks section. /// /// /// /// GRAY_BRUSH /// Gray brush. /// /// /// HOLLOW_BRUSH /// Hollow brush (equivalent to NULL_BRUSH). /// /// /// LTGRAY_BRUSH /// Light gray brush. /// /// /// NULL_BRUSH /// Null brush (equivalent to HOLLOW_BRUSH). /// /// /// WHITE_BRUSH /// White brush. /// /// /// BLACK_PEN /// Black pen. /// /// /// DC_PEN /// /// Solid pen color. The default color is white. The color can be changed by using the SetDCPenColor function. For more information, /// see the Remarks section. /// /// /// /// NULL_PEN /// Null pen. The null pen draws nothing. /// /// /// WHITE_PEN /// White pen. /// /// /// ANSI_FIXED_FONT /// Windows fixed-pitch (monospace) system font. /// /// /// ANSI_VAR_FONT /// Windows variable-pitch (proportional space) system font. /// /// /// DEVICE_DEFAULT_FONT /// Device-dependent font. /// /// /// DEFAULT_GUI_FONT /// /// Default font for user interface objects such as menus and dialog boxes. It is not recommended that you use DEFAULT_GUI_FONT or /// SYSTEM_FONT to obtain the font used by dialogs and windows; for more information, see the remarks section. The default font is Tahoma. /// /// /// /// OEM_FIXED_FONT /// Original equipment manufacturer (OEM) dependent fixed-pitch (monospace) font. /// /// /// SYSTEM_FONT /// /// System font. By default, the system uses the system font to draw menus, dialog box controls, and text. It is not recommended that /// you use DEFAULT_GUI_FONT or SYSTEM_FONT to obtain the font used by dialogs and windows; for more information, see the remarks /// section. The default system font is Tahoma. /// /// /// /// SYSTEM_FIXED_FONT /// /// Fixed-pitch (monospace) system font. This stock object is provided only for compatibility with 16-bit Windows versions earlier /// than 3.0. /// /// /// /// DEFAULT_PALETTE /// Default palette. This palette consists of the static colors in the system palette. /// /// /// /// /// If the function succeeds, the return value is a handle to the requested logical object. /// If the function fails, the return value is NULL. /// /// /// /// It is not recommended that you employ this method to obtain the current font used by dialogs and windows. Instead, use the /// SystemParametersInfo function with the SPI_GETNONCLIENTMETRICS parameter to retrieve the current font. SystemParametersInfo will /// take into account the current theme and provides font information for captions, menus, and message dialogs. /// /// /// Use the DKGRAY_BRUSH, GRAY_BRUSH, and LTGRAY_BRUSH stock objects only in windows with the CS_HREDRAW and CS_VREDRAW styles. Using /// a gray stock brush in any other style of window can lead to misalignment of brush patterns after a window is moved or sized. The /// origins of stock brushes cannot be adjusted. /// /// The HOLLOW_BRUSH and NULL_BRUSH stock objects are equivalent. /// It is not necessary (but it is not harmful) to delete stock objects by calling DeleteObject. /// /// Both DC_BRUSH and DC_PEN can be used interchangeably with other stock objects like BLACK_BRUSH and BLACK_PEN. For information on /// retrieving the current pen or brush color, see GetDCBrushColor and GetDCPenColor. See Setting the Pen or Brush Color for an /// example of setting colors. The GetStockObject function with an argument of DC_BRUSH or DC_PEN can be used interchangeably /// with the SetDCPenColor and SetDCBrushColor functions. /// /// Examples /// For an example, see Setting the Pen or Brush Color. /// // https://docs.microsoft.com/en-us/windows/win32/api/wingdi/nf-wingdi-getstockobject HGDIOBJ GetStockObject( int i ); [DllImport(Lib.Gdi32, SetLastError = false, ExactSpelling = true)] [PInvokeData("wingdi.h", MSDNShortId = "b14ddc05-7e7b-4fc6-b7e3-efe892df7e21")] public static extern HGDIOBJ GetStockObject(StockObjectType i); /// The ResetDC function updates the specified printer or plotter device context (DC) using the specified information. /// A handle to the DC to update. /// A pointer to a DEVMODE structure containing information about the new DC. /// /// If the function succeeds, the return value is a handle to the original DC. /// If the function fails, the return value is NULL. /// /// /// /// An application will typically use the ResetDC function when a window receives a WM_DEVMODECHANGE message. ResetDC /// can also be used to change the paper orientation or paper bins while printing a document. /// /// /// The ResetDC function cannot be used to change the driver name, device name, or the output port. When the user changes the /// port connection or device name, the application must delete the original DC and create a new DC with the new information. /// /// /// An application can pass an information DC to the ResetDC function. In that situation, ResetDC will always return a /// printer DC. /// /// /// ICM: The color profile of the DC specified by the hdc parameter will be reset based on the information contained in the /// lpInitData member of the DEVMODE structure. /// /// // https://docs.microsoft.com/en-us/windows/win32/api/wingdi/nf-wingdi-resetdca HDC ResetDCA( HDC hdc, const DEVMODEA *lpdm ); [DllImport(Lib.Gdi32, SetLastError = false, CharSet = CharSet.Auto)] [PInvokeData("wingdi.h", MSDNShortId = "3f77db51-90d1-4a87-812b-1e129ae8fde9")] public static extern HDC ResetDC(HDC hdc, ref DEVMODE lpdm); /// /// The RestoreDC function restores a device context (DC) to the specified state. The DC is restored by popping state /// information off a stack created by earlier calls to the SaveDC function. /// /// A handle to the DC. /// /// The saved state to be restored. If this parameter is positive, nSavedDC represents a specific instance of the state to be /// restored. If this parameter is negative, nSavedDC represents an instance relative to the current state. For example, -1 restores /// the most recently saved state. /// /// /// If the function succeeds, the return value is nonzero. /// If the function fails, the return value is zero. /// /// /// Each DC maintains a stack of saved states. The SaveDC function pushes the current state of the DC onto its stack of saved states. /// That state can be restored only to the same DC from which it was created. After a state is restored, the saved state is destroyed /// and cannot be reused. Furthermore, any states saved after the restored state was created are also destroyed and cannot be used. /// In other words, the RestoreDC function pops the restored state (and any subsequent states) from the state information stack. /// // https://docs.microsoft.com/en-us/windows/win32/api/wingdi/nf-wingdi-restoredc BOOL RestoreDC( HDC hdc, int nSavedDC ); [DllImport(Lib.Gdi32, SetLastError = false, ExactSpelling = true)] [PInvokeData("wingdi.h", MSDNShortId = "7043edbb-b3ea-4946-a2ba-cae356b04d1d")] [return: MarshalAs(UnmanagedType.Bool)] public static extern bool RestoreDC(HDC hdc, int nSavedDC); /// /// The SaveDC function saves the current state of the specified device context (DC) by copying data describing selected /// objects and graphic modes (such as the bitmap, brush, palette, font, pen, region, drawing mode, and mapping mode) to a context stack. /// /// A handle to the DC whose state is to be saved. /// /// If the function succeeds, the return value identifies the saved state. /// If the function fails, the return value is zero. /// /// /// The SaveDC function can be used any number of times to save any number of instances of the DC state. /// A saved state can be restored by using the RestoreDC function. /// // https://docs.microsoft.com/en-us/windows/win32/api/wingdi/nf-wingdi-savedc int SaveDC( HDC hdc ); [DllImport(Lib.Gdi32, SetLastError = false, ExactSpelling = true)] [PInvokeData("wingdi.h", MSDNShortId = "f438cd7f-436f-436c-b32e-67f5558740cb")] public static extern int SaveDC(HDC hdc); /// /// The SelectObject function selects an object into the specified device context (DC). The new object replaces the previous object /// of the same type. /// /// A handle to the DC. /// /// A handle to the object to be selected. The specified object must have been created by using one of the following functions. /// /// /// If the selected object is not a region and the function succeeds, the return value is a handle to the object being replaced. If /// the selected object is a region and the function succeeds, the return value is one of the following values. /// [DllImport(Lib.Gdi32, ExactSpelling = true, SetLastError = true)] [PInvokeData("Wingdi.h", MSDNShortId = "dd162957")] public static extern HGDIOBJ SelectObject(HDC hDC, HGDIOBJ hObject); /// /// SetDCPenColor function sets the current device context (DC) pen color to the specified color value. If the device cannot /// represent the specified color value, the color is set to the nearest physical color. /// /// A handle to the DC. /// The new pen color. /// /// If the function succeeds, the return value specifies the previous DC pen color as a COLORREF value. If the function fails, the /// return value is CLR_INVALID. /// /// /// /// The function returns the previous DC_PEN color, even if the stock pen DC_PEN is not selected in the DC; however, this will not be /// used in drawing operations until the stock DC_PEN is selected in the DC. /// /// /// The GetStockObject function with an argument of DC_BRUSH or DC_PEN can be used interchangeably with the SetDCPenColor and /// SetDCBrushColor functions. /// /// ICM: Color management is performed if ICM is enabled. /// Examples /// For an example of setting colors, see Setting the Pen or Brush Color. /// // https://docs.microsoft.com/en-us/windows/win32/api/wingdi/nf-wingdi-setdcpencolor COLORREF SetDCPenColor( HDC hdc, COLORREF color ); [DllImport(Lib.Gdi32, SetLastError = false, ExactSpelling = true)] [PInvokeData("wingdi.h", MSDNShortId = "057608eb-7209-4714-bf02-660a13d59016")] public static extern COLORREF SetDCPenColor(HDC hdc, COLORREF color); /// The SetLayout function changes the layout of a device context (DC). /// A handle to the DC. /// The DC layout. /// If the function succeeds, it returns the previous layout of the DC. If the function fails, it returns GDI_ERROR. /// /// The layout specifies the order in which text and graphics are revealed in a window or a device context. The default is left to /// right. The SetLayout function changes this to be right to left, which is the standard in Arabic and Hebrew cultures. /// [DllImport(Lib.Gdi32, ExactSpelling = true, SetLastError = true)] [PInvokeData("Wingdi.h", MSDNShortId = "dd162979")] public static extern DCLayout SetLayout(HDC hdc, DCLayout dwLayout); } }