LCUI库:用C语言编写简单的照片查看器


本程序主要使用了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.   
  40. void Get_Name(char *filepath,char *out_name)  
  41. /* 此函数用于获取文件名 */  
  42. {  
  43.     int i,n = 0;  
  44.     char *p;  
  45.     for(i=0;i<strlen(filepath);++i) if(filepath[i]=='/') n = i+1;  
  46.     p = filepath + n;  
  47.     strcpy(out_name,p);  
  48. }  
  49.   
  50.   
  51. int main(int argc,char*argv[])  
  52. /* 主函数,程序的入口 */  
  53. {  
  54.     LCUI_App         LC_Photo_Viewer; /* LCUI程序 */  
  55.     Lable_Data       lable;           /* 存储lable部件的数据的结构体 */  
  56.     Picture_Box_Data pic_box;         /* 存储Picture_Box部件的数据的结构体 */  
  57.     Pic_Data         icon,pic,mosaics;/* 用于存储图形数据的结构体 */  
  58.     int              main_window;     /* 用于保存窗口识别代号 */  
  59.     int             width,height,temp;   
  60.     char            text[512],name[256];  
  61.     int x,y,key,box_width,box_height,Area_width,Area_height;  
  62.       
  63.     width  = 320; /* 窗口的宽度 */  
  64.     height = 240; /* 窗口的高度 */  
  65.     /* 初始化LCUI */  
  66.     LCUI_Init(&LC_Photo_Viewer);    
  67.     /* 初始化图片数据结构体 */  
  68.     Pic_Data_Init(&pic);  
  69.     Pic_Data_Init(&mosaics);  
  70.     Pic_Data_Init(&icon);  
  71.     /* 创建一个LCUI程序窗口 */  
  72.     main_window = Create_Window(&LC_Photo_Viewer,width,height);    
  73.     LCUI_Load_Image(ICON_PATH,&icon); /* 载入图标 */  
  74.     Set_Window_Icon(&LC_Photo_Viewer,main_window,ICON_CUSTOM,&icon);  
  75.     Title_Text_Size(&LC_Photo_Viewer,main_window,12); /* 标题栏中的文本的字体大小为12 */  
  76.       
  77.     /* 在标题栏中添加文本 */  
  78.     Set_Title_Text(&LC_Photo_Viewer,main_window,"LC照片查看器");   
  79.       
  80.     /* 获取窗口内部区域的尺寸 */  
  81.     box_width  = Get_Inside_Box_Width(&LC_Photo_Viewer,main_window);  
  82.     box_height = Get_Inside_Box_Height(&LC_Photo_Viewer,main_window);  
  83.     /* 创建一个Picture_Box部件至LC_Photo_Viewer程序的main_window中 */  
  84.     Create_Picture_Box(&LC_Photo_Viewer,main_window,&pic_box,box_width,box_height);  
  85.       
  86.     /* 设定图像处理模式,CENTER表示的是居中显示图像 */  
  87.     Set_Picture_Box_Size_Mode(&pic_box,CENTER);  
  88.       
  89.     /* 在窗口内创建一个lable部件 */  
  90.     Create_Lable(&LC_Photo_Viewer,main_window,&lable);  
  91.     Show_Window(&LC_Photo_Viewer,main_window);/* 显示窗口 */  
  92.     if(argc == 2){/* 如果总共有2个参数 */  
  93.         is_hide = IS_FALSE;  
  94.         /* 与lable部件关联的文本为"正在读取图片..." */  
  95.         Lable_Text(&lable,"正在读取图片...");  
  96.         temp = LCUI_Load_Image(argv[1],&pic); /* 载入图片 */  
  97.         /* 如果LCUI_Load_Image()函数的返回值不为0,那么读取图片就是出问题了 */  
  98.         if(temp != 0) {   
  99.             /* 与lable部件关联的文本为"图片文件读取失败!" */  
  100.             Lable_Text(&lable,"图片文件读取失败!");  
  101.             /* 等待按键输入,如果是返回键,就退出循环 */  
  102.             while(1) if(Get_Key() == KEY_BACK) break;  
  103.         }  
  104.         else/* 否则 */  
  105.             if(pic.type == TYPE_PNG && pic.flag == HAVE_ALPHA){  
  106.                 /* 如果图片类型为PNG,并且有alpha通道 */  
  107.                 Graph_Mosaics(&mosaics);/* 载入马赛克图形数据,此图形数据内置于LCUI图形库中 */  
  108.                   
  109.                 /* 设定Picture_Box部件的背景图像为刚刚载入马赛克图形,并且平铺 */  
  110.                 Set_Picture_Box_Background_Image(&pic_box,&mosaics,TILE);  
  111.             }  
  112.             Get_Name(argv[1],name);/* 获取文件名 */  
  113.             Pic_Data   img;  
  114.             Color_Data back_color,border_color;/* 配色数据的结构体 */  
  115.             back_color.red   = 255;  
  116.             back_color.green = 255;/* 背景为黄颜色 */  
  117.             back_color.blue  = 0;  
  118.             border_color.red   = 100;/* 边框为暗黄色 */  
  119.             border_color.green = 100;  
  120.             border_color.blue  = 0;  
  121.             /* 生成文本 */  
  122.             sprintf(text," 文件名: %s \n 尺  寸: %dx%d ",name,pic.width,pic.height);  
  123.             Lable_Text(&lable,text);  
  124.             /* 为img分配内存,尺寸和lable部件的尺寸一样 */  
  125.             LCUI_Malloc_Graph(&img,lable.width,lable.height);  
  126.             Fill_Color(&img,back_color);/* 为img填充背景色 */  
  127.             /* 为img的边缘绘制矩形边框,上下左右的边框尺寸都为1个像素点 */  
  128.             Draw_Border_To_Graph(&img,border_color,1,1,1,1);  
  129.             Set_Lable_Image(&lable,&img,STRETCH);  
  130.             Lable_Site(&lable,5,5); /* 改变lable部件在窗口中的位置 */  
  131.             /* 将pic中的图像数据传给Picture_Box部件,让它显示 */  
  132.             Set_Image_In_Picture_Box(&pic_box,&pic);  
  133.             /* 获取当前所显示的图片内部的区域位置及尺寸 */  
  134.             x = Get_Area_Start_X(&pic_box);  
  135.             y = Get_Area_Start_Y(&pic_box);  
  136.             /* 获取图片内部显示区域的尺寸 */  
  137.             pthread_create(&lable_view, NULL, hide, (void*)&lable);  
  138.             while(1){  
  139.                 Area_width  = Get_Area_Width(&pic_box);  
  140.                 Area_height = Get_Area_Height(&pic_box);  
  141.                 key = Get_Key();/* 接受按键 */  
  142.                 /* 如果按的是下面的按键,计算图片内部显示区域的需要移动到的位置坐标 */  
  143.                 if(key == KEY_RIGHT)    x += (float)pic.width/10;  
  144.                 if(key == KEY_LEFT)     x -= (float)pic.height/10;  
  145.                 if(key == KEY_UP)       y -= (float)pic.height/10;  
  146.                 if(key == KEY_DOWN)     y += (float)pic.height/10;  
  147.                 if(key == KEY_ENTER){  
  148.                     int style;  
  149.                     style = Get_Window_Style(&LC_Photo_Viewer,main_window);  
  150.                     if(style == Style_LCUI) Set_Window_Style(&LC_Photo_Viewer,main_window,NONE);  
  151.                     else Set_Window_Style(&LC_Photo_Viewer,main_window,Style_LCUI);   
  152.                     /* 重新获取窗口内部空间的尺寸 */                   
  153.                     box_width  = Get_Inside_Box_Width(&LC_Photo_Viewer,main_window);  
  154.                     box_height = Get_Inside_Box_Height(&LC_Photo_Viewer,main_window);  
  155.                     Picture_Box_Size(&pic_box,box_width,box_height);  
  156.                 }  
  157.                 if(key == 'i' || key == 'I') {  
  158.                     is_hide = IS_FALSE;/* 标记,不隐藏 */  
  159.                 }  
  160.                 if(key == KEY_BACK)     break;  
  161.                 if(x<0)                 x = 0;  
  162.                 if(x+Area_width>pic.width)   x = pic.width - Area_width;  
  163.                 if(y<0)                 y = 0;  
  164.                 if(y+Area_height>pic.height) y = pic.height - Area_height;  
  165.                 /* 移动图片浏览区域 */  
  166.                 Move_View_Area(&pic_box,x,y);  
  167.             }  
  168.             pthread_cancel(lable_view);/* 撤销线程 */  
  169.         }  
  170.     }  
  171.     else {  
  172.         Get_Name(argv[0],name);  
  173.         /* 生成文本,保存至字符串变量text中 */  
  174.         sprintf(text,"按键操作说明:\n"  
  175.         "确认键: 隐藏标题栏\n"  
  176.         "方向键: 移动可视区域\n"  
  177.         "返回键: 退出本程序\n"  
  178.         "I键: 显示图片信息标签\n"  
  179.         "想打开图片,请在虚拟终端里输入:\n"  
  180.         "%s [图片文件的位置]",name);  
  181.         /* 更改lable部件中的文本内容 */  
  182.         Lable_Site(&lable,5,5);  
  183.         Lable_Text(&lable,text);  
  184.         /* 等待按键输入,如果是返回键,就退出循环 */  
  185.         while(1) if(Get_Key() == KEY_BACK) break;  
  186.     }  
  187.     Close_Window(&LC_Photo_Viewer,main_window); /* 关闭窗口 */  
  188.     return 0;  
  189. }  

运行效果截图:

相关内容