C # detailed description of how to operate Clipboard to read data instances in the Clipboard

Source: Internet
Author: User

C # detailed description of how to operate Clipboard to read data instances in the Clipboard

This example describes how to read data from the Clipboard using C # Clipboard. Share it with you for your reference. The specific analysis is as follows:

1. Customize a class and ensure its serializability: implement the ISerializable interface; or mark it with [Serializable] (if there is a parent class, the parent class also needs to be marked; you can [NonSerialized ()] Mark Fields that do not want to be serialized in the class)

2. register the Custom Data Format: Call the static method DataFormats. GetFormat ()

3. Save data to clipboard: Use the IdataObject interface to create a data object and set the data. Call the Clipboard. SetDataObject () method.

4. obtain data from clipboard: Call GetDataPresent () of the DataObject instance to ensure the data format is compatible with the application. Call the GetData () method of IDataObject to obtain data.

Example program:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

Using System;

Using System. Collections. Generic;

Using System. ComponentModel;

Using System. Data;

Using System. Drawing;

Using System. Linq;

Using System. Text;

Using System. Windows. Forms;

Using System. Runtime. Serialization. Formatters. Binary;

Namespace _ ClipboardTest _

{

Public partial class Form1: Form

{

[System. Runtime. InteropServices. DllImport ("user32")]

Private static extern IntPtr SetClipboardViewer (IntPtr hwnd );

[System. Runtime. InteropServices. DllImport ("user32")]

Private static extern IntPtr changeconboardchain (IntPtr hwnd, IntPtr hWndNext );

[System. Runtime. InteropServices. DllImport ("user32")]

Private static extern int SendMessage (IntPtr hwnd, int wMsg, IntPtr wParam, IntPtr lParam );

Const int WM_DRAWCLIPBOARD = 0x308;

Const int WM_CHANGECBCHAIN = 0x30D;

Public Form1 ()

{

InitializeComponent ();

}

Private void Form1_Load (object sender, EventArgs e)

{

// Obtain the next window handle in the observation chain

NextClipHwnd = SetClipboardViewer (this. Handle );

}

Protected override void WndProc (ref System. Windows. Forms. Message m)

{

Switch (m. Msg)

{

Case WM_DRAWCLIPBOARD:

// Pass the WM_DRAWCLIPBOARD message to the window in the next observation chain

SendMessage (NextClipHwnd, m. Msg, m. WParam, m. LParam );

IDataObject iData = Clipboard. GetDataObject ();

// Detect text

If (iData. GetDataPresent (DataFormats. Text) | iData. GetDataPresent (DataFormats. OemText ))

{

This. richTextBox1.Text = (String) iData. GetData (DataFormats. Text );

}

// Detect Images

If (iData. GetDataPresent (DataFormats. Bitmap ))

{

PictureBox1.Image = Clipboard. GetImage ();

MyItem item = new MyItem ();

Item. CopyToClipboard ();

}

// Detect custom types

If (iData. GetDataPresent (typeof (MyItem). FullName ))

{

// MyItem item = (MyItem) iData. GetData (typeof (MyItem). FullName );

MyItem item = GetFromClipboard ();

If (item! = Null)

{

This. richTextBox1.Text = item. ItemName;

}

}

Break;

Default:

Base. WndProc (ref m );

Break;

}

}

Private void formpolicclosed (object sender, System. EventArgs e)

{

// Delete the observation window from the observation chain (first parameter: handle of the window to be deleted;

// Second parameter: Observe the handle of the next window in the chain)

Changeconboardchain (this. Handle, NextClipHwnd );

// Send the changed message WM_CHANGECBCHAIN to the window in the next observation chain

SendMessage (NextClipHwnd, WM_CHANGECBCHAIN, this. Handle, NextClipHwnd );

}

IntPtr NextClipHwnd;

Protected static MyItem GetFromClipboard ()

{

MyItem item = null;

IDataObject dataObj = Clipboard. GetDataObject ();

String format = typeof (MyItem). FullName;

If (dataObj. GetDataPresent (format ))

{

Item = dataObj. GetData (format) as MyItem;

}

Return item;

}

}

[Serializable]

Public class MyItem

{

Public MyItem ()

{

ItemName = "This is a Custom Item ";

}

Public string ItemName

{

Get {return itemName ;}

}

Private string itemName;

 

Public void CopyToClipboard ()

{

DataFormats. Format format = DataFormats. GetFormat (typeof (MyItem). FullName );

IDataObject dataObj = new DataObject ();

DataObj. SetData (format. Name, false, this );

Clipboard. SetDataObject (dataObj, false );

}

}

}

I hope this article will help you with C # programming.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.