using System; using System.Collections.Generic; using System.IO; using System.Runtime.InteropServices; using System.Text; using Vanara.InteropServices; namespace Vanara.PInvoke { public static partial class User32 { /// Messages sent by Clipboard functions to the message window to notify of operations. [PInvokeData("Winuser.h", MSDNShortId = "clipboard-notifications")] public enum ClipboardNotificationMessage { /// /// /// Sent to the clipboard owner by a clipboard viewer window to request the name of a CF_OWNERDISPLAY clipboard format. /// /// A window receives this message through its WindowProc function. /// /// /// If an application processes this message, it should return zero. /// /// /// /// In response to this message, the clipboard owner should copy the name of the owner-display format to the specified buffer, /// not exceeding the buffer size specified by the wParam parameter. /// /// /// A clipboard viewer window sends this message to the clipboard owner to determine the name of the CF_OWNERDISPLAY /// format for example, to initialize a menu listing available formats. /// /// WM_ASKCBFORMATNAME = 0x030C, /// /// Sent to the first window in the clipboard viewer chain when a window is being removed from the chain. /// A window receives this message through its WindowProc function. /// /// /// If an application processes this message, it should return zero. /// /// /// /// Each clipboard viewer window saves the handle to the next window in the clipboard viewer chain. Initially, this handle is the /// return value of the SetClipboardViewer function. /// /// /// When a clipboard viewer window receives the WM_CHANGECBCHAIN message, it should call the SendMessage function /// to pass the message to the next window in the chain, unless the next window is the window being removed. In this case, the /// clipboard viewer should save the handle specified by the lParam parameter as the next window in the chain. /// /// WM_CHANGECBCHAIN = 0x030D, /// /// Sent when the contents of the clipboard have changed. /// /// /// If an application processes this message, it should return zero. /// /// /// To register a window to receive this message, use the AddClipboardFormatListener function. /// WM_CLIPBOARDUPDATE = 0x031D, /// /// Sent to the clipboard owner when a call to the EmptyClipboard function empties the clipboard. /// A window receives this message through its WindowProc function. /// /// /// If an application processes this message, it should return zero. /// WM_DESTROYCLIPBOARD = 0x0307, /// /// /// Sent to the first window in the clipboard viewer chain when the content of the clipboard changes. This enables a clipboard /// viewer window to display the new content of the clipboard. /// /// A window receives this message through its WindowProc function. /// /// /// /// Only clipboard viewer windows receive this message. These are windows that have been added to the clipboard viewer chain by /// using the SetClipboardViewer function. /// /// /// Each window that receives the WM_DRAWCLIPBOARD message must call the SendMessage function to pass the message /// on to the next window in the clipboard viewer chain. The handle to the next window in the chain is returned by /// SetClipboardViewer, and may change in response to a WM_CHANGECBCHAIN message. /// /// WM_DRAWCLIPBOARD = 0x0308, /// /// /// Sent to the clipboard owner by a clipboard viewer window. This occurs when the clipboard contains data in the /// CF_OWNERDISPLAY format and an event occurs in the clipboard viewer's horizontal scroll bar. The owner should scroll /// the clipboard image and update the scroll bar values. /// /// /// /// If an application processes this message, it should return zero. /// /// /// /// The clipboard owner can use the ScrollWindow function to scroll the image in the clipboard viewer window and /// invalidate the appropriate region. /// /// WM_HSCROLLCLIPBOARD = 0x030E, /// /// /// Sent to the clipboard owner by a clipboard viewer window when the clipboard contains data in the CF_OWNERDISPLAY /// format and the clipboard viewer's client area needs repainting. /// /// /// /// If an application processes this message, it should return zero. /// /// /// /// To determine whether the entire client area or just a portion of it needs repainting, the clipboard owner must compare the /// dimensions of the drawing area given in the rcPaint member of PAINTSTRUCT to the dimensions given in the most /// recent WM_SIZECLIPBOARD message. /// /// /// The clipboard owner must use the GlobalLock function to lock the memory that contains the PAINTSTRUCT /// structure. Before returning, the clipboard owner must unlock that memory by using the GlobalUnlock function. /// /// WM_PAINTCLIPBOARD = 0x0309, /// /// /// Sent to the clipboard owner before it is destroyed, if the clipboard owner has delayed rendering one or more clipboard /// formats. For the content of the clipboard to remain available to other applications, the clipboard owner must render data in /// all the formats it is capable of generating, and place the data on the clipboard by calling the SetClipboardData function. /// /// A window receives this message through its WindowProc function. /// /// /// If an application processes this message, it should return zero. /// /// /// /// When responding to a WM_RENDERALLFORMATS message, the clipboard owner must call the OpenClipboard and /// EmptyClipboard functions before calling SetClipboardData. /// /// /// When the application returns, the system removes any unrendered formats from the list of available clipboard formats. For /// information about delayed rendering, see SetClipboardData. /// /// WM_RENDERALLFORMATS = 0x0306, /// /// /// Sent to the clipboard owner if it has delayed rendering a specific clipboard format and if an application has requested data /// in that format. The clipboard owner must render data in the specified format and place it on the clipboard by calling the /// SetClipboardData function. /// /// /// /// If an application processes this message, it should return zero. /// /// /// /// When responding to a WM_RENDERFORMAT message, the clipboard owner must not open the clipboard before calling SetClipboardData. /// /// WM_RENDERFORMAT = 0x0305, /// /// /// Sent to the clipboard owner by a clipboard viewer window when the clipboard contains data in the CF_OWNERDISPLAY /// format and the clipboard viewer's client area has changed size. /// /// /// /// If an application processes this message, it should return zero. /// /// /// /// When the clipboard viewer window is about to be destroyed or resized, a WM_SIZECLIPBOARD message is sent with a null /// rectangle (0, 0, 0, 0) as the new size. This permits the clipboard owner to free its display resources. /// /// /// The clipboard owner must use the GlobalLock function to lock the memory object that contains RECT. Before /// returning, the clipboard owner must unlock the object by using the GlobalUnlock function. /// /// WM_SIZECLIPBOARD = 0x030B, /// /// /// Sent to the clipboard owner by a clipboard viewer window when the clipboard contains data in the CF_OWNERDISPLAY /// format and an event occurs in the clipboard viewer's vertical scroll bar. The owner should scroll the clipboard image and /// update the scroll bar values. /// /// /// /// If an application processes this message, it should return zero. /// /// /// /// The clipboard owner can use the ScrollWindow function to scroll the image in the clipboard viewer window and /// invalidate the appropriate region. /// /// WM_VSCROLLCLIPBOARD = 0x030A, } /// The clipboard formats defined by the system are called standard clipboard formats. // https://docs.microsoft.com/en-us/windows/desktop/dataxchg/standard-clipboard-formats [PInvokeData("Winuser.h", MSDNShortId = "f0af4e61-7ef1-4263-b2c5-e4114515124f")] [Obsolete("Use Vanara.PInvoke.CLIPFORMAT instead. This enumeration will be removed in a future version.")] public enum StandardClipboardFormat : uint { /// A handle to a bitmap (HBITMAP). [CorrespondingType(typeof(HBITMAP))] CF_BITMAP = 2, /// A memory object containing a BITMAPINFO structure followed by the bitmap bits. [CorrespondingType(typeof(Gdi32.SafeBITMAPINFO))] CF_DIB = 8, /// /// A memory object containing a BITMAPV5HEADER structure followed by the bitmap color space information and the bitmap bits. /// //[CorrespondingType(typeof(Gdi32.SafeBITMAPV5HEADER))] CF_DIBV5 = 17, /// Software Arts' Data Interchange Format. CF_DIF = 5, /// /// Bitmap display format associated with a private format. The hMem parameter must be a handle to data that can be displayed in /// bitmap format in lieu of the privately formatted data. /// [CorrespondingType(typeof(HBITMAP))] CF_DSPBITMAP = 0x0082, /// /// Enhanced metafile display format associated with a private format. The hMem parameter must be a handle to data that can be /// displayed in enhanced metafile format in lieu of the privately formatted data. /// [CorrespondingType(typeof(HENHMETAFILE))] CF_DSPENHMETAFILE = 0x008E, /// /// Metafile-picture display format associated with a private format. The hMem parameter must be a handle to data that can be /// displayed in metafile-picture format in lieu of the privately formatted data. /// [CorrespondingType(typeof(SafeMemStruct))] CF_DSPMETAFILEPICT = 0x0083, /// /// Text display format associated with a private format. The hMem parameter must be a handle to data that can be displayed in /// text format in lieu of the privately formatted data. /// [CorrespondingType(typeof(SafeMemString), EncodingType = typeof(UTF8Encoding))] CF_DSPTEXT = 0x0081, /// A handle to an enhanced metafile (HENHMETAFILE). [CorrespondingType(typeof(HENHMETAFILE))] CF_ENHMETAFILE = 14, /// /// Start of a range of integer values for application-defined GDI object clipboard formats. The end of the range is CF_GDIOBJLAST. /// /// Handles associated with clipboard formats in this range are not automatically deleted using the GlobalFree function when the /// clipboard is emptied. Also, when using values in this range, the hMem parameter is not a handle to a GDI object, but is a /// handle allocated by the GlobalAlloc function with the GMEM_MOVEABLE flag. /// /// [CorrespondingType(typeof(Stream))] CF_GDIOBJFIRST = 0x0300, /// See CF_GDIOBJFIRST. CF_GDIOBJLAST = 0x03FF, /// /// A handle to type HDROP that identifies a list of files. An application can retrieve information about the files by passing /// the handle to the DragQueryFile function. /// CF_HDROP = 15, /// /// The data is a handle to the locale identifier associated with text in the clipboard. When you close the clipboard, if it /// contains CF_TEXT data but no CF_LOCALE data, the system automatically sets the CF_LOCALE format to the current input /// language. You can use the CF_LOCALE format to associate a different locale with the clipboard text. /// /// An application that pastes text from the clipboard can retrieve this format to determine which character set was used to /// generate the text. /// /// /// Note that the clipboard does not support plain text in multiple character sets. To achieve this, use a formatted text data /// type such as RTF instead. /// /// /// The system uses the code page associated with CF_LOCALE to implicitly convert from CF_TEXT to CF_UNICODETEXT. Therefore, the /// correct code page table is used for the conversion. /// /// [CorrespondingType(typeof(SafeMemStruct))] CF_LOCALE = 16, /// /// Handle to a metafile picture format as defined by the METAFILEPICT structure. When passing a CF_METAFILEPICT handle by means /// of DDE, the application responsible for deleting hMem should also free the metafile referred to by the CF_METAFILEPICT handle. /// [CorrespondingType(typeof(SafeMemStruct))] CF_METAFILEPICT = 3, /// /// Text format containing characters in the OEM character set. Each line ends with a carriage return/linefeed (CR-LF) /// combination. A null character signals the end of the data. /// [CorrespondingType(typeof(SafeMemString), EncodingType = typeof(UTF8Encoding))] CF_OEMTEXT = 7, /// /// Owner-display format. The clipboard owner must display and update the clipboard viewer window, and receive the /// WM_ASKCBFORMATNAME, WM_HSCROLLCLIPBOARD, WM_PAINTCLIPBOARD, WM_SIZECLIPBOARD, and WM_VSCROLLCLIPBOARD messages. The hMem /// parameter must be NULL. /// CF_OWNERDISPLAY = 0x0080, /// /// Handle to a color palette. Whenever an application places data in the clipboard that depends on or assumes a color palette, /// it should place the palette on the clipboard as well. /// /// If the clipboard contains data in the CF_PALETTE (logical color palette) format, the application should use the SelectPalette /// and RealizePalette functions to realize (compare) any other data in the clipboard against that logical palette. /// /// /// When displaying clipboard data, the clipboard always uses as its current palette any object on the clipboard that is in the /// CF_PALETTE format. /// /// [CorrespondingType(typeof(HPALETTE))] CF_PALETTE = 9, /// Data for the pen extensions to the Microsoft Windows for Pen Computing. CF_PENDATA = 10, /// /// Start of a range of integer values for private clipboard formats. The range ends with CF_PRIVATELAST. Handles associated with /// private clipboard formats are not freed automatically; the clipboard owner must free such handles, typically in response to /// the WM_DESTROYCLIPBOARD message. /// CF_PRIVATEFIRST = 0x0200, /// See CF_PRIVATEFIRST. CF_PRIVATELAST = 0x02FF, /// Represents audio data more complex than can be represented in a CF_WAVE standard wave format. [CorrespondingType(typeof(Stream))] CF_RIFF = 11, /// Microsoft Symbolic Link (SYLK) format. CF_SYLK = 4, /// /// Text format. Each line ends with a carriage return/linefeed (CR-LF) combination. A null character signals the end of the /// data. Use this format for ANSI text. /// [CorrespondingType(typeof(SafeMemString), EncodingType = typeof(UTF8Encoding))] CF_TEXT = 1, /// Tagged-image file format. [CorrespondingType(typeof(Stream))] CF_TIFF = 6, /// /// Unicode text format. Each line ends with a carriage return/linefeed (CR-LF) combination. A null character signals the end of /// the data. /// [CorrespondingType(typeof(SafeMemString), EncodingType = typeof(UnicodeEncoding))] CF_UNICODETEXT = 13, /// Represents audio data in one of the standard wave formats, such as 11 kHz or 22 kHz PCM. [CorrespondingType(typeof(Stream))] CF_WAVE = 12, } /// /// Places the given window in the system-maintained clipboard format listener list. /// /// /// Type: HWND /// A handle to the window to be placed in the clipboard format listener list. /// /// /// Type: BOOL /// Returns TRUE if successful, FALSE otherwise. Call GetLastError for additional details. /// /// /// /// When a window has been added to the clipboard format listener list, it is posted a WM_CLIPBOARDUPDATE message whenever the /// contents of the clipboard have changed. /// /// // https://docs.microsoft.com/en-us/windows/desktop/api/winuser/nf-winuser-addclipboardformatlistener BOOL // AddClipboardFormatListener( HWND hwnd ); [DllImport(Lib.User32, SetLastError = true, ExactSpelling = true)] [PInvokeData("winuser.h", MSDNShortId = "addclipboardformatlistener")] [return: MarshalAs(UnmanagedType.Bool)] public static extern bool AddClipboardFormatListener(HWND hwnd); /// /// Removes a specified window from the chain of clipboard viewers. /// /// /// Type: HWND /// A handle to the window to be removed from the chain. The handle must have been passed to the SetClipboardViewer function. /// /// /// Type: HWND /// /// A handle to the window that follows the hWndRemove window in the clipboard viewer chain. (This is the handle returned by /// SetClipboardViewer, unless the sequence was changed in response to a WM_CHANGECBCHAIN message.) /// /// /// /// Type: BOOL /// /// The return value indicates the result of passing the WM_CHANGECBCHAIN message to the windows in the clipboard viewer chain. /// Because a window in the chain typically returns FALSE when it processes WM_CHANGECBCHAIN, the return value from /// ChangeClipboardChain is typically FALSE. If there is only one window in the chain, the return value is typically TRUE. /// /// /// /// /// The window identified by hWndNewNext replaces the hWndRemove window in the chain. The SetClipboardViewer function sends a /// WM_CHANGECBCHAIN message to the first window in the clipboard viewer chain. /// /// For an example, see Removing a Window from the Clipboard Viewer Chain. /// // https://docs.microsoft.com/en-us/windows/desktop/api/winuser/nf-winuser-changeclipboardchain BOOL ChangeClipboardChain( HWND // hWndRemove, HWND hWndNewNext ); [DllImport(Lib.User32, SetLastError = false, ExactSpelling = true)] [PInvokeData("winuser.h", MSDNShortId = "changeclipboardchain")] [return: MarshalAs(UnmanagedType.Bool)] public static extern bool ChangeClipboardChain(HWND hWndRemove, HWND hWndNewNext); /// /// Closes the clipboard. /// /// /// Type: BOOL /// If the function succeeds, the return value is nonzero. /// If the function fails, the return value is zero. To get extended error information, call GetLastError. /// /// /// /// When the window has finished examining or changing the clipboard, close the clipboard by calling CloseClipboard. This /// enables other windows to access the clipboard. /// /// Do not place an object on the clipboard after calling CloseClipboard. /// Examples /// For an example, see Example of a Clipboard Viewer. /// // https://docs.microsoft.com/en-us/windows/desktop/api/winuser/nf-winuser-closeclipboard BOOL CloseClipboard( ); [DllImport(Lib.User32, SetLastError = true, ExactSpelling = true)] [PInvokeData("winuser.h", MSDNShortId = "closeclipboard")] [return: MarshalAs(UnmanagedType.Bool)] public static extern bool CloseClipboard(); /// /// Retrieves the number of different data formats currently on the clipboard. /// /// /// Type: int /// If the function succeeds, the return value is the number of different data formats currently on the clipboard. /// If the function fails, the return value is zero. To get extended error information, call GetLastError. /// // https://docs.microsoft.com/en-us/windows/desktop/api/winuser/nf-winuser-countclipboardformats int CountClipboardFormats( ); [DllImport(Lib.User32, SetLastError = true, ExactSpelling = true)] [PInvokeData("winuser.h", MSDNShortId = "countclipboardformats")] public static extern int CountClipboardFormats(); /// /// /// Empties the clipboard and frees handles to data in the clipboard. The function then assigns ownership of the clipboard to the /// window that currently has the clipboard open. /// /// /// /// Type: BOOL /// If the function succeeds, the return value is nonzero. /// If the function fails, the return value is zero. To get extended error information, call GetLastError. /// /// /// /// Before calling EmptyClipboard, an application must open the clipboard by using the OpenClipboard function. If the /// application specifies a NULL window handle when opening the clipboard, EmptyClipboard succeeds but sets the /// clipboard owner to NULL. Note that this causes SetClipboardData to fail. /// /// Examples /// For an example, see Copying Information to the Clipboard. /// // https://docs.microsoft.com/en-us/windows/desktop/api/winuser/nf-winuser-emptyclipboard BOOL EmptyClipboard( ); [DllImport(Lib.User32, SetLastError = true, ExactSpelling = true)] [PInvokeData("winuser.h", MSDNShortId = "emptyclipboard")] [return: MarshalAs(UnmanagedType.Bool)] public static extern bool EmptyClipboard(); /// /// Enumerates the data formats currently available on the clipboard. /// /// Clipboard data formats are stored in an ordered list. To perform an enumeration of clipboard data formats, you make a series of /// calls to the EnumClipboardFormats function. For each call, the format parameter specifies an available clipboard format, /// and the function returns the next available clipboard format. /// /// /// /// Type: UINT /// A clipboard format that is known to be available. /// /// To start an enumeration of clipboard formats, set format to zero. When format is zero, the function retrieves the first available /// clipboard format. For subsequent calls during an enumeration, set format to the result of the previous /// EnumClipboardFormats call. /// /// /// /// Type: UINT /// /// If the function succeeds, the return value is the clipboard format that follows the specified format, namely the next available /// clipboard format. /// /// /// If the function fails, the return value is zero. To get extended error information, call GetLastError. If the clipboard is not /// open, the function fails. /// /// /// If there are no more clipboard formats to enumerate, the return value is zero. In this case, the GetLastError function returns /// the value ERROR_SUCCESS. This lets you distinguish between function failure and the end of enumeration. /// /// /// /// /// You must open the clipboard before enumerating its formats. Use the OpenClipboard function to open the clipboard. The /// EnumClipboardFormats function fails if the clipboard is not open. /// /// /// The EnumClipboardFormats function enumerates formats in the order that they were placed on the clipboard. If you are /// copying information to the clipboard, add clipboard objects in order from the most descriptive clipboard format to the least /// descriptive clipboard format. If you are pasting information from the clipboard, retrieve the first clipboard format that you can /// handle. That will be the most descriptive clipboard format that you can handle. /// /// /// The system provides automatic type conversions for certain clipboard formats. In the case of such a format, this function /// enumerates the specified format, then enumerates the formats to which it can be converted. For more information, see Standard /// Clipboard Formats and Synthesized Clipboard Formats. /// /// Examples /// For an example, see Example of a Clipboard Viewer. /// // https://docs.microsoft.com/en-us/windows/desktop/api/winuser/nf-winuser-enumclipboardformats UINT EnumClipboardFormats( UINT // format ); [DllImport(Lib.User32, SetLastError = true, ExactSpelling = true)] [PInvokeData("winuser.h", MSDNShortId = "enumclipboardformats")] public static extern uint EnumClipboardFormats(uint format); /// /// Enumerates the data formats currently available on the clipboard. /// Clipboard data formats are stored in an ordered list. /// /// An enumeration of the data formats currently available on the clipboard. /// /// /// You must open the clipboard before enumerating its formats. Use the OpenClipboard function to open the clipboard. The /// EnumClipboardFormats function fails if the clipboard is not open. /// /// /// The EnumClipboardFormats function enumerates formats in the order that they were placed on the clipboard. If you are /// copying information to the clipboard, add clipboard objects in order from the most descriptive clipboard format to the least /// descriptive clipboard format. If you are pasting information from the clipboard, retrieve the first clipboard format that you can /// handle. That will be the most descriptive clipboard format that you can handle. /// /// /// The system provides automatic type conversions for certain clipboard formats. In the case of such a format, this function /// enumerates the specified format, then enumerates the formats to which it can be converted. For more information, see Standard /// Clipboard Formats and Synthesized Clipboard Formats. /// /// public static IEnumerable EnumClipboardFormats() { var fmt = 0U; while (true) { fmt = EnumClipboardFormats(fmt); if (fmt > 0) yield return fmt; else { Win32Error.GetLastError().ThrowIfFailed(); break; } } } /// /// Retrieves data from the clipboard in a specified format. The clipboard must have been opened previously. /// /// /// Type: UINT /// A clipboard format. For a description of the standard clipboard formats, see Standard Clipboard Formats. /// /// /// Type: HANDLE /// If the function succeeds, the return value is the handle to a clipboard object in the specified format. /// If the function fails, the return value is NULL. To get extended error information, call GetLastError. /// /// /// Caution Clipboard data is not trusted. Parse the data carefully before using it in your application. /// An application can enumerate the available formats in advance by using the EnumClipboardFormats function. /// /// The clipboard controls the handle that the GetClipboardData function returns, not the application. The application should /// copy the data immediately. The application must not free the handle nor leave it locked. The application must not use the handle /// after the EmptyClipboard or CloseClipboard function is called, or after the SetClipboardData function is called with the same /// clipboard format. /// /// /// The system performs implicit data format conversions between certain clipboard formats when an application calls the /// GetClipboardData function. For example, if the CF_OEMTEXT format is on the clipboard, a window can retrieve data in the /// CF_TEXT format. The format on the clipboard is converted to the requested format on demand. For more information, see Synthesized /// Clipboard Formats. /// /// Examples /// For an example, see Copying Information to the Clipboard. /// // https://docs.microsoft.com/en-us/windows/desktop/api/winuser/nf-winuser-getclipboarddata HANDLE GetClipboardData( UINT uFormat ); [DllImport(Lib.User32, SetLastError = true, ExactSpelling = true)] [PInvokeData("winuser.h", MSDNShortId = "getclipboarddata")] public static extern IntPtr GetClipboardData(uint uFormat); /// /// /// Retrieves from the clipboard the name of the specified registered format. The function copies the name to the specified buffer. /// /// /// /// Type: UINT /// The type of format to be retrieved. This parameter must not specify any of the predefined clipboard formats. /// /// /// Type: LPTSTR /// The buffer that is to receive the format name. /// /// /// Type: int /// The maximum length, in characters, of the string to be copied to the buffer. If the name exceeds this limit, it is truncated. /// /// /// Type: int /// If the function succeeds, the return value is the length, in characters, of the string copied to the buffer. /// /// If the function fails, the return value is zero, indicating that the requested format does not exist or is predefined. To get /// extended error information, call GetLastError. /// /// /// /// Security Considerations /// /// Using this function incorrectly might compromise the security of your program. For example, miscalculating the proper size of the /// lpszFormatName buffer, especially when the application is used in both ANSI and Unicode versions, can cause a buffer overflow. /// Also, note that the string is truncated if it is longer than the cchMaxCount parameter, which can lead to loss of information. /// /// Examples /// For an example, see Example of a Clipboard Viewer. /// // https://docs.microsoft.com/en-us/windows/desktop/api/winuser/nf-winuser-getclipboardformatnamea int GetClipboardFormatNameA( UINT // format, LPSTR lpszFormatName, int cchMaxCount ); [DllImport(Lib.User32, SetLastError = true, CharSet = CharSet.Auto)] [PInvokeData("winuser.h", MSDNShortId = "getclipboardformatname")] public static extern int GetClipboardFormatName(uint format, StringBuilder lpszFormatName, int cchMaxCount); /// /// Retrieves the window handle of the current owner of the clipboard. /// /// /// Type: HWND /// If the function succeeds, the return value is the handle to the window that owns the clipboard. /// If the clipboard is not owned, the return value is NULL. To get extended error information, call GetLastError. /// /// /// The clipboard can still contain data even if the clipboard is not currently owned. /// /// In general, the clipboard owner is the window that last placed data in clipboard. The EmptyClipboard function assigns clipboard ownership. /// /// Examples /// For an example, see Example of a Clipboard Viewer. /// // https://docs.microsoft.com/en-us/windows/desktop/api/winuser/nf-winuser-getclipboardowner HWND GetClipboardOwner( ); [DllImport(Lib.User32, SetLastError = true, ExactSpelling = true)] [PInvokeData("winuser.h", MSDNShortId = "getclipboardowner")] public static extern HWND GetClipboardOwner(); /// /// Retrieves the clipboard sequence number for the current window station. /// /// /// Type: DWORD /// /// The return value is the clipboard sequence number. If you do not have WINSTA_ACCESSCLIPBOARD access to the window station, /// the function returns zero. /// /// /// /// /// The system keeps a serial number for the clipboard for each window station. This number is incremented whenever the contents of /// the clipboard change or the clipboard is emptied. You can track this value to determine whether the clipboard contents have /// changed and optimize creating DataObjects. If clipboard rendering is delayed, the sequence number is not incremented until the /// changes are rendered. /// /// // https://docs.microsoft.com/en-us/windows/desktop/api/winuser/nf-winuser-getclipboardsequencenumber DWORD // GetClipboardSequenceNumber( ); [DllImport(Lib.User32, SetLastError = false, ExactSpelling = true)] [PInvokeData("winuser.h", MSDNShortId = "getclipboardsequencenumber")] public static extern uint GetClipboardSequenceNumber(); /// /// Retrieves the handle to the first window in the clipboard viewer chain. /// /// /// Type: HWND /// If the function succeeds, the return value is the handle to the first window in the clipboard viewer chain. /// If there is no clipboard viewer, the return value is NULL. To get extended error information, call GetLastError. /// // https://docs.microsoft.com/en-us/windows/desktop/api/winuser/nf-winuser-getclipboardviewer HWND GetClipboardViewer( ); [DllImport(Lib.User32, SetLastError = true, ExactSpelling = true)] [PInvokeData("winuser.h", MSDNShortId = "getclipboardviewer")] public static extern HWND GetClipboardViewer(); /// /// Retrieves the handle to the window that currently has the clipboard open. /// /// /// Type: HWND /// /// If the function succeeds, the return value is the handle to the window that has the clipboard open. If no window has the /// clipboard open, the return value is NULL. To get extended error information, call GetLastError. /// /// /// /// /// If an application or DLL specifies a NULL window handle when calling the OpenClipboard function, the clipboard is opened /// but is not associated with a window. In such a case, GetOpenClipboardWindow returns NULL. /// /// // https://docs.microsoft.com/en-us/windows/desktop/api/winuser/nf-winuser-getopenclipboardwindow HWND GetOpenClipboardWindow( ); [DllImport(Lib.User32, SetLastError = true, ExactSpelling = true)] [PInvokeData("winuser.h", MSDNShortId = "getopenclipboardwindow")] public static extern HWND GetOpenClipboardWindow(); /// /// Retrieves the first available clipboard format in the specified list. /// /// /// Type: UINT* /// /// The clipboard formats, in priority order. For a description of the standard clipboard formats, see Standard Clipboard Formats . /// /// /// /// Type: int /// /// The number of entries in the paFormatPriorityList array. This value must not be greater than the number of entries in the list. /// /// /// /// Type: int /// /// If the function succeeds, the return value is the first clipboard format in the list for which data is available. If the /// clipboard is empty, the return value is NULL. If the clipboard contains data, but not in any of the specified formats, the return /// value is –1. To get extended error information, call GetLastError. /// /// // https://docs.microsoft.com/en-us/windows/desktop/api/winuser/nf-winuser-getpriorityclipboardformat int GetPriorityClipboardFormat( // UINT *paFormatPriorityList, int cFormats ); [DllImport(Lib.User32, SetLastError = true, ExactSpelling = true)] [PInvokeData("winuser.h", MSDNShortId = "getpriorityclipboardformat")] public static extern int GetPriorityClipboardFormat([MarshalAs(UnmanagedType.LPArray, SizeParamIndex = 1)] uint[] paFormatPriorityList, int cFormats); /// /// Retrieves the currently supported clipboard formats. /// /// /// Type: PUINT /// An array of clipboard formats. For a description of the standard clipboard formats, see Standard Clipboard Formats. /// /// /// Type: UINT /// The number of entries in the array pointed to by lpuiFormats. /// /// /// Type: PUINT /// The actual number of clipboard formats in the array pointed to by lpuiFormats. /// /// /// Type: BOOL /// The function returns TRUE if successful; otherwise, FALSE. Call GetLastError for additional details. /// // https://docs.microsoft.com/en-us/windows/desktop/api/winuser/nf-winuser-getupdatedclipboardformats BOOL // GetUpdatedClipboardFormats( PUINT lpuiFormats, UINT cFormats, PUINT pcFormatsOut ); [DllImport(Lib.User32, SetLastError = true, ExactSpelling = true)] [PInvokeData("winuser.h", MSDNShortId = "getupdatedclipboardformats")] [return: MarshalAs(UnmanagedType.Bool)] public static extern bool GetUpdatedClipboardFormats([MarshalAs(UnmanagedType.LPArray, SizeParamIndex = 1)] uint[] lpuiFormats, uint cFormats, out uint pcFormatsOut); /// /// Determines whether the clipboard contains data in the specified format. /// /// /// Type: UINT /// /// A standard or registered clipboard format. For a description of the standard clipboard formats, see Standard Clipboard Formats . /// /// /// /// Type: BOOL /// If the clipboard format is available, the return value is nonzero. /// If the clipboard format is not available, the return value is zero. To get extended error information, call GetLastError. /// /// /// /// Typically, an application that recognizes only one clipboard format would call this function when processing the WM_INITMENU or /// WM_INITMENUPOPUP message. The application would then enable or disable the Paste menu item, depending on the return value. /// Applications that recognize more than one clipboard format should use the GetPriorityClipboardFormat function for this purpose. /// /// Examples /// For an example, see Pasting Information from the Clipboard. /// // https://docs.microsoft.com/en-us/windows/desktop/api/winuser/nf-winuser-isclipboardformatavailable BOOL // IsClipboardFormatAvailable( UINT format ); [DllImport(Lib.User32, SetLastError = true, ExactSpelling = true)] [PInvokeData("winuser.h", MSDNShortId = "isclipboardformatavailable")] [return: MarshalAs(UnmanagedType.Bool)] public static extern bool IsClipboardFormatAvailable(uint format); /// /// Opens the clipboard for examination and prevents other applications from modifying the clipboard content. /// /// /// Type: HWND /// /// A handle to the window to be associated with the open clipboard. If this parameter is NULL, the open clipboard is /// associated with the current task. /// /// /// /// Type: BOOL /// If the function succeeds, the return value is nonzero. /// If the function fails, the return value is zero. To get extended error information, call GetLastError. /// /// /// OpenClipboard fails if another window has the clipboard open. /// An application should call the CloseClipboard function after every successful call to OpenClipboard. /// /// The window identified by the hWndNewOwner parameter does not become the clipboard owner unless the EmptyClipboard function is called. /// /// /// If an application calls OpenClipboard with hwnd set to NULL, EmptyClipboard sets the clipboard owner to /// NULL; this causes SetClipboardData to fail. /// /// Examples /// For an example, see Copying Information to the Clipboard. /// // https://docs.microsoft.com/en-us/windows/desktop/api/winuser/nf-winuser-openclipboard BOOL OpenClipboard( HWND hWndNewOwner ); [DllImport(Lib.User32, SetLastError = true, ExactSpelling = true)] [PInvokeData("winuser.h", MSDNShortId = "openclipboard")] [return: MarshalAs(UnmanagedType.Bool)] public static extern bool OpenClipboard(HWND hWndNewOwner); /// /// Registers a new clipboard format. This format can then be used as a valid clipboard format. /// /// /// Type: LPCTSTR /// The name of the new format. /// /// /// Type: UINT /// If the function succeeds, the return value identifies the registered clipboard format. /// If the function fails, the return value is zero. To get extended error information, call GetLastError. /// /// /// /// If a registered format with the specified name already exists, a new format is not registered and the return value identifies the /// existing format. This enables more than one application to copy and paste data using the same registered clipboard format. Note /// that the format name comparison is case-insensitive. /// /// Registered clipboard formats are identified by values in the range 0xC000 through 0xFFFF. /// /// When registered clipboard formats are placed on or retrieved from the clipboard, they must be in the form of an HGLOBAL value. /// /// Examples /// For an example, see Registering a Clipboard Format. /// // https://docs.microsoft.com/en-us/windows/desktop/api/winuser/nf-winuser-registerclipboardformata UINT RegisterClipboardFormatA( // LPCSTR lpszFormat ); [DllImport(Lib.User32, SetLastError = true, CharSet = CharSet.Auto)] [PInvokeData("winuser.h", MSDNShortId = "registerclipboardformat")] public static extern uint RegisterClipboardFormat(string lpszFormat); /// /// Removes the given window from the system-maintained clipboard format listener list. /// /// /// Type: HWND /// A handle to the window to remove from the clipboard format listener list. /// /// /// Type: BOOL /// Returns TRUE if successful, FALSE otherwise. Call GetLastError for additional details. /// /// /// When a window has been removed from the clipboard format listener list, it will no longer receive WM_CLIPBOARDUPDATE messages. /// // https://docs.microsoft.com/en-us/windows/desktop/api/winuser/nf-winuser-removeclipboardformatlistener BOOL // RemoveClipboardFormatListener( HWND hwnd ); [DllImport(Lib.User32, SetLastError = true, ExactSpelling = true)] [PInvokeData("winuser.h", MSDNShortId = "removeclipboardformatlistener")] [return: MarshalAs(UnmanagedType.Bool)] public static extern bool RemoveClipboardFormatListener(HWND hwnd); /// /// /// Places data on the clipboard in a specified clipboard format. The window must be the current clipboard owner, and the application /// must have called the OpenClipboard function. (When responding to the WM_RENDERFORMAT and WM_RENDERALLFORMATS messages, the /// clipboard owner must not call OpenClipboard before calling SetClipboardData.) /// /// /// /// Type: UINT /// /// The clipboard format. This parameter can be a registered format or any of the standard clipboard formats. For more information, /// see Standard Clipboard Formats and Registered Clipboard Formats. /// /// /// /// Type: HANDLE /// /// A handle to the data in the specified format. This parameter can be NULL, indicating that the window provides data in the /// specified clipboard format (renders the format) upon request. If a window delays rendering, it must process the WM_RENDERFORMAT /// and WM_RENDERALLFORMATS messages. /// /// /// If SetClipboardData succeeds, the system owns the object identified by the hMem parameter. The application may not write /// to or free the data once ownership has been transferred to the system, but it can lock and read from the data until the /// CloseClipboard function is called. (The memory must be unlocked before the Clipboard is closed.) If the hMem parameter identifies /// a memory object, the object must have been allocated using the function with the GMEM_MOVEABLE flag. /// /// /// /// Type: HANDLE /// If the function succeeds, the return value is the handle to the data. /// If the function fails, the return value is NULL. To get extended error information, call GetLastError. /// /// /// /// Windows 8: Bitmaps to be shared with Windows Store app apps must be in the CF_BITMAP format (device-dependent bitmap). /// /// /// If an application calls SetClipboardData in response to WM_RENDERFORMAT or WM_RENDERALLFORMATS, the application should not /// use the handle after SetClipboardData has been called. /// /// /// If an application calls OpenClipboard with hwnd set to NULL, EmptyClipboard sets the clipboard owner to NULL; this /// causes SetClipboardData to fail. /// /// /// The system performs implicit data format conversions between certain clipboard formats when an application calls the /// GetClipboardData function. For example, if the CF_OEMTEXT format is on the clipboard, a window can retrieve data in the /// CF_TEXT format. The format on the clipboard is converted to the requested format on demand. For more information, see /// Synthesized Clipboard Formats. /// /// Examples /// For an example, see Copying Information to the Clipboard. /// // https://docs.microsoft.com/en-us/windows/desktop/api/winuser/nf-winuser-setclipboarddata HANDLE SetClipboardData( UINT uFormat, // HANDLE hMem ); [DllImport(Lib.User32, SetLastError = true, ExactSpelling = true)] [PInvokeData("winuser.h", MSDNShortId = "setclipboarddata")] public static extern IntPtr SetClipboardData(uint uFormat, IntPtr hMem); /// /// /// Adds the specified window to the chain of clipboard viewers. Clipboard viewer windows receive a WM_DRAWCLIPBOARD message whenever /// the content of the clipboard changes. This function is used for backward compatibility with earlier versions of Windows. /// /// /// /// Type: HWND /// A handle to the window to be added to the clipboard chain. /// /// /// Type: HWND /// /// If the function succeeds, the return value identifies the next window in the clipboard viewer chain. If an error occurs or there /// are no other windows in the clipboard viewer chain, the return value is NULL. To get extended error information, call GetLastError. /// /// /// /// /// The windows that are part of the clipboard viewer chain, called clipboard viewer windows, must process the clipboard messages /// WM_CHANGECBCHAIN and WM_DRAWCLIPBOARD. Each clipboard viewer window calls the SendMessage function to pass these messages to the /// next window in the clipboard viewer chain. /// /// /// A clipboard viewer window must eventually remove itself from the clipboard viewer chain by calling the ChangeClipboardChain /// function — for example, in response to the WM_DESTROY message. /// /// /// The SetClipboardViewer function exists to provide backward compatibility with earlier versions of Windows. The clipboard /// viewer chain can be broken by an application that fails to handle the clipboard chain messages properly. New applications should /// use more robust techniques such as the clipboard sequence number or the registration of a clipboard format listener. For further /// details on these alternatives techniques, see Monitoring Clipboard Contents. /// /// Examples /// For an example, see Adding a Window to the Clipboard Viewer Chain. /// // https://docs.microsoft.com/en-us/windows/desktop/api/winuser/nf-winuser-setclipboardviewer HWND SetClipboardViewer( HWND // hWndNewViewer ); [DllImport(Lib.User32, SetLastError = true, ExactSpelling = true)] [PInvokeData("winuser.h", MSDNShortId = "setclipboardviewer")] public static extern HWND SetClipboardViewer(HWND hWndNewViewer); /// /// Defines the metafile picture format used for exchanging metafile data through the clipboard. /// // https://docs.microsoft.com/en-us/windows/desktop/api/wingdi/ns-wingdi-tagmetafilepict typedef struct tagMETAFILEPICT { LONG mm; // LONG xExt; LONG yExt; HMETAFILE hMF; } METAFILEPICT, *LPMETAFILEPICT; [PInvokeData("wingdi.h", MSDNShortId = "metafilepict")] [StructLayout(LayoutKind.Sequential)] public struct METAFILEPICT { /// /// Type: LONG /// The mapping mode in which the picture is drawn. /// public int mm; /// /// Type: LONG /// /// The size of the metafile picture for all modes except the MM_ISOTROPIC and MM_ANISOTROPIC modes. (For more /// information about these modes, see the yExt member.) The x-extent specifies the width of the rectangle within which /// the picture is drawn. The coordinates are in units that correspond to the mapping mode. /// /// public int xExt; /// /// Type: LONG /// /// The size of the metafile picture for all modes except the MM_ISOTROPIC and MM_ANISOTROPIC modes. The y-extent /// specifies the height of the rectangle within which the picture is drawn. The coordinates are in units that correspond to the /// mapping mode. For MM_ISOTROPIC and MM_ANISOTROPIC modes, which can be scaled, the xExt and yExt /// members contain an optional suggested size in MM_HIMETRIC units. For MM_ANISOTROPIC pictures, xExt and /// yExt can be zero when no suggested size is supplied. For MM_ISOTROPIC pictures, an aspect ratio must be /// supplied even when no suggested size is given. (If a suggested size is given, the aspect ratio is implied by the size.) To /// give an aspect ratio without implying a suggested size, set xExt and yExt to negative values whose ratio is the /// appropriate aspect ratio. The magnitude of the negative xExt and yExt values is ignored; only the ratio is used. /// /// public int yExt; /// /// Type: HMETAFILE /// A handle to a memory metafile. /// public HMETAFILE hMF; } } }