GTK 剪貼簿使用例項

2021-04-09 05:55:35 字數 3663 閱讀 3959

gtk+

剪貼簿使用例項

功能強大,但其風格與

win32

上剪貼簿相差極大,對於剛接觸

gtk+

程式設計的程式設計師來說,特別是已經習慣於

win32

剪貼簿用法的程式設計師來說,易用性不是太好。這方面的文件也比較少,最近學習使用

gtk+

基本用法

:

通過剪貼簿操作內建的資料型別非常方便,但內建的型別比較少,只有文字和等等。

1.拷貝。拷貝文字非常簡單,先獲得乙個

clipboard

物件,然後呼叫

gtk_clipboard_set_text。

void

on_button_copy_clicked

(gtkbutton *

button

, gpointer

user_data

) 2.

貼上。拷貝文字稍微麻煩一點,要實現乙個**函式。呼叫

gtk_clipboard_request_text

函式後,

clipboard

**這個函式,並把資料傳遞過來。

static

void

on_paste_text

(gtkclipboard *

clipboard

, const

gchar *

text

, gpointer

data

)

void

on_button_paste_clicked

(gtkbutton *

button

, gpointer

user_data

)

高階用法:

如要使用自定義的資料型別,就有點麻煩了。下面我們用乙個簡單示例,演示一下支援

html

型別的操作。當然,

html

也是文字,你可以以操作文字的方式操作

html

,這裡選擇

html

只是作為演示目的,沒有太大的實用價值。 1.

對外介面。我們選擇與文字操作類似的函式介面。

typedef

void

(* gtkclipboardhtmlreceivedfunc

)(gtkclipboard*

clipboard

,

const

gchar*

html

,

gpointer

data

);

void

gtk_clipboard_set_html

(gtkclipboard *

clipboard

,

const

gchar*

html

,

gint

len);

void

gtk_clipboard_request_html

(gtkclipboard *

clipboard

,

gtkclipboardhtmlreceivedfunc

callback

,

gpointer

user_data);

2.實現拷貝。

剪貼簿中的資料型別是用

atom

表示的,

atom

實際上就是字串的

id,它與字串一一對應,通常在多個程序中都保持這種對應關係(依賴於具體的平台),我們的資料型別為

atom_html

,它是通過

gdk_atom_intern

獲得的。

實現拷貝操作時,要實現兩個**函式

: gtkclipboardgetfunc

和gtkclipboardclearfunc

。前者用來檢查和轉換資料,然後放到剪貼簿中。後者用來清除資料。

static

gdkatom

atom_html

=0;

static

void

html_get_func

(gtkclipboard*

clipboard

,

gtkselectiondata *

selection_data

,

guint

info

,

gpointer

data

)

return

;

}

static

void

html_clear_func

(gtkclipboard *

clipboard

,

gpointer

data

)

void

gtk_clipboard_set_html

(gtkclipboard *

clipboard

,

const

gchar*

html

,

gint

len)

3.

實現貼上。

實現貼上操作時,要實現乙個**函式

gtkclipboardreceivedfunc

,它主要是檢查剪貼簿中的資料型別,如果正確,就呼叫使用者設定的**函式,把資料傳遞過去。

struct

_requesthtmlinfo

;

typedef

struct

_requesthtmlinfo

requesthtmlinfo

;

static

void

request_html_received_func

(gtkclipboard*

clipboard

,

gtkselectiondata *

selection_data

,

gpointer

data

)

return

;

}

void

gtk_clipboard_request_html

(gtkclipboard *

clipboard

,

gtkclipboardhtmlreceivedfunc

callback

,

gpointer

user_data

)

由於我們支援

html

型別操作與文字型別操作的介面類似,其用法與基本用法幾乎一樣,這裡不再多說。

~~end~~

操作剪貼簿

剪貼簿 一 將資料送到剪貼簿 1 記憶體配置 hglobal hglobal hglobal globalalloc ghnd gmem share,lstrlen ptext 1 sizeof tchar 為什麼不用malloc 在windows中執行的應用程式之間必須要共享剪貼簿所儲存的記憶體塊...

剪貼簿相關

public const int wm user 0x400 public const int wm cap edit copy wm user 30 dllimport user32.dll static extern bool sendmessage intptr hwnd,int wmsg,s...

剪貼簿功能

1,複製貼上字串 這個是需要輸入的資訊,即是要複製的資訊 uitextfield textfild uitextfield alloc init self.textfild textfild textfild.placeholder 請輸入資訊 textfild.font uifont system...