歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> LCUI庫:用C語言編寫簡單的照片查看器

LCUI庫:用C語言編寫簡單的照片查看器

日期:2017/3/1 10:38:06   编辑:Linux編程

本程序主要使用了LCUI圖形庫的Picture_Box和Lable窗口部件。

下面是源代碼:

[cpp]

  1. #include "LCUI_Build.h"
  2. #include LCUI_MAIN_H /* 包含LCUI庫的必須頭文件 */
  3. #include LCUI_WIDGETS_H
  4. #include LCUI_FONTS_H
  5. #include LCUI_INPUT_H
  6. #include "all.h"
  7. #define ICON_PATH "/mnt/Data/LC-SOFT/LCPhotoViewer/icon.png" /* 圖標位置 */
  8. static int is_hide = IS_FALSE;/* 一個標志 */
  9. pthread_t lable_view; /* 線程需要用到 */
  10. void *hide(void *in) /* 用於隱藏圖片信息標簽 */
  11. {
  12. unsigned short int alpha = 255;
  13. Lable_Data *lable = (Lable_Data*)in;/* 轉換成存儲lable部件的數據的結構體 */
  14. while(1){
  15. mark:/* goto語句跳轉的位置的標志 */
  16. while(1) {
  17. usleep(100000);
  18. if(is_hide == IS_FALSE) {/* 如果沒有隱藏 */
  19. Lable_Alpha(lable,alpha);
  20. break;
  21. }
  22. }
  23. sleep(2);/* 暫停2秒 */
  24. is_hide = IS_TRUE;/* 標記,正在隱藏 */
  25. for(alpha=255;alpha > 0;--alpha) {
  26. usleep(10000);
  27. if(is_hide == IS_FALSE) {/* 如果不需要隱藏 */
  28. alpha = 255;
  29. Lable_Alpha(lable,alpha);
  30. goto mark; /* 跳轉 */
  31. }
  32. Lable_Alpha(lable,alpha);/* 改變lable部件的透明度 */
  33. }
  34. alpha = 255;/* 不透明 */
  35. is_hide = IS_TRUE;/* 標記,正在隱藏 */
  36. }
  37. pthread_exit(NULL);
  38. }
  39. void Get_Name(char *filepath,char *out_name)
  40. /* 此函數用於獲取文件名 */
  41. {
  42. int i,n = 0;
  43. char *p;
  44. for(i=0;i<strlen(filepath);++i) if(filepath[i]=='/') n = i+1;
  45. p = filepath + n;
  46. strcpy(out_name,p);
  47. }
  48. int main(int argc,char*argv[])
  49. /* 主函數,程序的入口 */
  50. {
  51. LCUI_App LC_Photo_Viewer; /* LCUI程序 */
  52. Lable_Data lable; /* 存儲lable部件的數據的結構體 */
  53. Picture_Box_Data pic_box; /* 存儲Picture_Box部件的數據的結構體 */
  54. Pic_Data icon,pic,mosaics;/* 用於存儲圖形數據的結構體 */
  55. int main_window; /* 用於保存窗口識別代號 */
  56. int width,height,temp;
  57. char text[512],name[256];
  58. int x,y,key,box_width,box_height,Area_width,Area_height;
  59. width = 320; /* 窗口的寬度 */
  60. height = 240; /* 窗口的高度 */
  61. /* 初始化LCUI */
  62. LCUI_Init(&LC_Photo_Viewer);
  63. /* 初始化圖片數據結構體 */
  64. Pic_Data_Init(&pic);
  65. Pic_Data_Init(&mosaics);
  66. Pic_Data_Init(&icon);
  67. /* 創建一個LCUI程序窗口 */
  68. main_window = Create_Window(&LC_Photo_Viewer,width,height);
  69. LCUI_Load_Image(ICON_PATH,&icon); /* 載入圖標 */
  70. Set_Window_Icon(&LC_Photo_Viewer,main_window,ICON_CUSTOM,&icon);
  71. Title_Text_Size(&LC_Photo_Viewer,main_window,12); /* 標題欄中的文本的字體大小為12 */
  72. /* 在標題欄中添加文本 */
  73. Set_Title_Text(&LC_Photo_Viewer,main_window,"LC照片查看器");
  74. /* 獲取窗口內部區域的尺寸 */
  75. box_width = Get_Inside_Box_Width(&LC_Photo_Viewer,main_window);
  76. box_height = Get_Inside_Box_Height(&LC_Photo_Viewer,main_window);
  77. /* 創建一個Picture_Box部件至LC_Photo_Viewer程序的main_window中 */
  78. Create_Picture_Box(&LC_Photo_Viewer,main_window,&pic_box,box_width,box_height);
  79. /* 設定圖像處理模式,CENTER表示的是居中顯示圖像 */
  80. Set_Picture_Box_Size_Mode(&pic_box,CENTER);
  81. /* 在窗口內創建一個lable部件 */
  82. Create_Lable(&LC_Photo_Viewer,main_window,&lable);
  83. Show_Window(&LC_Photo_Viewer,main_window);/* 顯示窗口 */
  84. if(argc == 2){/* 如果總共有2個參數 */
  85. is_hide = IS_FALSE;
  86. /* 與lable部件關聯的文本為"正在讀取圖片..." */
  87. Lable_Text(&lable,"正在讀取圖片...");
  88. temp = LCUI_Load_Image(argv[1],&pic); /* 載入圖片 */
  89. /* 如果LCUI_Load_Image()函數的返回值不為0,那麼讀取圖片就是出問題了 */
  90. if(temp != 0) {
  91. /* 與lable部件關聯的文本為"圖片文件讀取失敗!" */
  92. Lable_Text(&lable,"圖片文件讀取失敗!");
  93. /* 等待按鍵輸入,如果是返回鍵,就退出循環 */
  94. while(1) if(Get_Key() == KEY_BACK) break;
  95. }
  96. else{ /* 否則 */
  97. if(pic.type == TYPE_PNG && pic.flag == HAVE_ALPHA){
  98. /* 如果圖片類型為PNG,並且有alpha通道 */
  99. Graph_Mosaics(&mosaics);/* 載入馬賽克圖形數據,此圖形數據內置於LCUI圖形庫中 */
  100. /* 設定Picture_Box部件的背景圖像為剛剛載入馬賽克圖形,並且平鋪 */
  101. Set_Picture_Box_Background_Image(&pic_box,&mosaics,TILE);
  102. }
  103. Get_Name(argv[1],name);/* 獲取文件名 */
  104. Pic_Data img;
  105. Color_Data back_color,border_color;/* 配色數據的結構體 */
  106. back_color.red = 255;
  107. back_color.green = 255;/* 背景為黃顏色 */
  108. back_color.blue = 0;
  109. border_color.red = 100;/* 邊框為暗黃色 */
  110. border_color.green = 100;
  111. border_color.blue = 0;
  112. /* 生成文本 */
  113. sprintf(text," 文件名: %s \n 尺 寸: %dx%d ",name,pic.width,pic.height);
  114. Lable_Text(&lable,text);
  115. /* 為img分配內存,尺寸和lable部件的尺寸一樣 */
  116. LCUI_Malloc_Graph(&img,lable.width,lable.height);
  117. Fill_Color(&img,back_color);/* 為img填充背景色 */
  118. /* 為img的邊緣繪制矩形邊框,上下左右的邊框尺寸都為1個像素點 */
  119. Draw_Border_To_Graph(&img,border_color,1,1,1,1);
  120. Set_Lable_Image(&lable,&img,STRETCH);
  121. Lable_Site(&lable,5,5); /* 改變lable部件在窗口中的位置 */
  122. /* 將pic中的圖像數據傳給Picture_Box部件,讓它顯示 */
  123. Set_Image_In_Picture_Box(&pic_box,&pic);
  124. /* 獲取當前所顯示的圖片內部的區域位置及尺寸 */
  125. x = Get_Area_Start_X(&pic_box);
  126. y = Get_Area_Start_Y(&pic_box);
  127. /* 獲取圖片內部顯示區域的尺寸 */
  128. pthread_create(&lable_view, NULL, hide, (void*)&lable);
  129. while(1){
  130. Area_width = Get_Area_Width(&pic_box);
  131. Area_height = Get_Area_Height(&pic_box);
  132. key = Get_Key();/* 接受按鍵 */
  133. /* 如果按的是下面的按鍵,計算圖片內部顯示區域的需要移動到的位置坐標 */
  134. if(key == KEY_RIGHT) x += (float)pic.width/10;
  135. if(key == KEY_LEFT) x -= (float)pic.height/10;
  136. if(key == KEY_UP) y -= (float)pic.height/10;
  137. if(key == KEY_DOWN) y += (float)pic.height/10;
  138. if(key == KEY_ENTER){
  139. int style;
  140. style = Get_Window_Style(&LC_Photo_Viewer,main_window);
  141. if(style == Style_LCUI) Set_Window_Style(&LC_Photo_Viewer,main_window,NONE);
  142. else Set_Window_Style(&LC_Photo_Viewer,main_window,Style_LCUI);
  143. /* 重新獲取窗口內部空間的尺寸 */
  144. box_width = Get_Inside_Box_Width(&LC_Photo_Viewer,main_window);
  145. box_height = Get_Inside_Box_Height(&LC_Photo_Viewer,main_window);
  146. Picture_Box_Size(&pic_box,box_width,box_height);
  147. }
  148. if(key == 'i' || key == 'I') {
  149. is_hide = IS_FALSE;/* 標記,不隱藏 */
  150. }
  151. if(key == KEY_BACK) break;
  152. if(x<0) x = 0;
  153. if(x+Area_width>pic.width) x = pic.width - Area_width;
  154. if(y<0) y = 0;
  155. if(y+Area_height>pic.height) y = pic.height - Area_height;
  156. /* 移動圖片浏覽區域 */
  157. Move_View_Area(&pic_box,x,y);
  158. }
  159. pthread_cancel(lable_view);/* 撤銷線程 */
  160. }
  161. }
  162. else {
  163. Get_Name(argv[0],name);
  164. /* 生成文本,保存至字符串變量text中 */
  165. sprintf(text,"按鍵操作說明:\n"
  166. "確認鍵: 隱藏標題欄\n"
  167. "方向鍵: 移動可視區域\n"
  168. "返回鍵: 退出本程序\n"
  169. "I鍵: 顯示圖片信息標簽\n"
  170. "想打開圖片,請在虛擬終端裡輸入:\n"
  171. "%s [圖片文件的位置]",name);
  172. /* 更改lable部件中的文本內容 */
  173. Lable_Site(&lable,5,5);
  174. Lable_Text(&lable,text);
  175. /* 等待按鍵輸入,如果是返回鍵,就退出循環 */
  176. while(1) if(Get_Key() == KEY_BACK) break;
  177. }
  178. Close_Window(&LC_Photo_Viewer,main_window); /* 關閉窗口 */
  179. return 0;
  180. }

運行效果截圖:

Copyright © Linux教程網 All Rights Reserved