專案地址:
https://github.com/ying32/liblcl
熟悉的庫跨平臺開源無著作權問題
c語言呼叫liblcl示例
// main.c : 此檔案包含 "main" 函式。程式執行將在此處開始并結束。
//
#include "liblcl.h"
#ifdef _WIN32
// UTF8解碼
char *UTF8Decode(char* str) {
int len = MultiByteToWideChar(CP_UTF8, 0, str, -1, 0, 0);
wchar_t* wCharBuffer = (wchar_t*)malloc(len * sizeof(wchar_t) + 1);
MultiByteToWideChar(CP_UTF8, 0, str, -1, wCharBuffer, len);
len = WideCharToMultiByte(CP_ACP, 0, wCharBuffer, -1, 0, 0, 0, NULL);
char* aCharBuffer = (char*)malloc(len * sizeof(char) + 1);
WideCharToMultiByte(CP_ACP, 0, wCharBuffer, -1, aCharBuffer, len, 0, NULL);
free((void*)wCharBuffer);
return aCharBuffer;
}
#endif
// 按鈕單擊事件
void onButton1Click(TObject sender) {
ShowMessage("Hello world!");
}
// 檔案拖放事件
void onOnDropFiles(TObject sender, void* aFileNames, intptr_t len) {
printf("aFileNames: %p, len=%d\n", aFileNames, len);
intptr_t i;
// GetFPStringArrayMember 為一個從Lazarus的string陣列中獲取成員的函式。
for (i = 0; i < len; i++) {
#ifdef _WIN32
// 由于liblcl使用的是UTF-8編碼,所以獲取或者傳入的在Windows下都要經過UTF-8編/解碼
char *filename = UTF8Decode(GetFPStringArrayMember(aFileNames, i));
#else
// Linux與macOS默認都是UTF-8,則無需編/解碼
char *filename = GetFPStringArrayMember(aFileNames, i);
#endif
printf("file[%d]=%s\n", i+1, filename);
#ifdef _WIN32
free((void*)filename);
#endif
}
}
// 視窗鍵盤按下事件
void onFormKeyDown(TObject sender, Char* key, TShiftState shift) {
printf("key=%d, shift=%d\n", *key, shift);
if (*key == vkReturn) {
ShowMessage("press Enter!");
}
TShiftState s = Include(0, ssAlt);
if (InSet(s, ssAlt)) {
printf("ssAlt1\n");
}
s = Exclude(s, ssAlt);
if (!InSet(s, ssAlt)) {
printf("ssAlt2\n");
}
}
// 編輯框內容改變事件
void onEditChange(TObject sender) {
printf("%s\n", Edit_GetText(sender));
}
int main()
{
// 加載庫
#ifdef _WIN32
if (load_liblcl("liblcl.dll")) {
#endif
#ifdef __linux__
if (load_liblcl("liblcl.so")) {
#endif
#ifdef __APPLE__
if (load_liblcl("liblcl.dylib")) {
#endif
// 主視窗顯示在任務欄,僅Windows有效
Application_SetMainFormOnTaskBar(Application, TRUE);
// 應用程式標題,影響到:比如ShowMessage的標題。
Application_SetTitle(Application, "Hello LCL");
// 初始化應用程式
Application_Initialize(Application);
// 創建視窗
TForm form = Application_CreateForm(Application, FALSE);
// 設定視窗標題
Form_SetCaption(form, "LCL Form");
// 設定視窗位置
Form_SetPosition(form, poScreenCenter);
// --- 拖放檔案測驗 ---
// 接受檔案拖放
Form_SetAllowDropFiles(form, TRUE);
// 拖放檔案事件
Form_SetOnDropFiles(form, onOnDropFiles);
// 視窗優先接受按鍵,不受其它影響
Form_SetKeyPreview(form, TRUE);
// 視窗按鍵事件
Form_SetOnKeyDown(form, onFormKeyDown);
// ---------- 從記憶體流或者檔案加載UI布局檔案 ----------
// 從檔案加載視窗設定
// 從流加載
//TMemoryStream mem = NewMemoryStream();
//MemoryStream_Write(mem, data, datalen);
//MemoryStream_SetPosition(mem, 0);
//ResFormLoadFromStream(mem, form);
//MemoryStream_Free(mem);
// 從檔案加載
//ResFormLoadFromFile("./Form1.gfm", form);
// ---------- 動態創建控制元件 ----------
// 創建一個按鈕
TButton btn = Button_Create(form);
// 設定子父視窗
Button_SetParent(btn, form);
// 設定按鈕單擊事件
Button_SetOnClick(btn, onButton1Click);
// 設定按鈕標題
Button_SetCaption(btn, "button1");
// 設定按鈕在Parent的左邊位置
Button_SetLeft(btn, 100);
// 設定按鈕在Parent的頂邊位置
Button_SetTop(btn, 100);
// 創建一個單行檔案框(多行為TMemo)
TEdit edit = Edit_Create(form);
// 設定子父視窗
Edit_SetParent(edit, form);
// 設定左邊
Edit_SetLeft(edit, 10);
// 設定頂邊
Edit_SetTop(edit, 10);
// 設定編輯器內容改變事件
Edit_SetOnChange(edit, onEditChange);
// 運行app
Application_Run(Application);
// 釋放liblcl庫
close_liblcl();
}
return 0;
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/239792.html
標籤:C++ 語言
