[attach]131043[/attach
//*********************************************************
// Labwindows/cvi8.5 文件浏览操作
// szlihongtao
// 2010-06-17
// 参照 王建新的<Labwindows/cvi测试技术及工程应用> page96
// 稍有改写
//*********************************************************
#include "windows.h"
#include "toolbox.h"
#include <utility.h>
#include <formatio.h>
#include "FileBrowser.h"
#include "pathctrl.h"
#include <cvirte.h>
#include <userint.h>
#include "fileio.h"
//*********************************************************
static int j = 0; // 系统驱动器的的个数
static int panelHandle;
//自定义函数,获得驱动器的盘符及数目
void drivevolume (void);
//*********************************************************
int main (int argc, char *argv[])
{
if (InitCVIRTE (0, argv, 0) == 0)
return -1; /* out of memory */
if ((panelHandle = LoadPanel (0, "fileio.uir", PANEL)) < 0)
return -1;
//创建一个新的FileBrowser控件
//ROOT_LEVEL_STRING,以我的电脑作为根目录
FileBrowser_Create (panelHandle, PANEL_FILEBROWSER, ROOT_LEVEL_STRING);
//创建一个新的pathcontrol控件
//可以看见的列表项为8个
//在输入路径的时候,自动会提供8个相关的地址供选择
NewPathCtrl (panelHandle, PANEL_PATH, 8, 1);
DisplayPanel (panelHandle);
drivevolume(); //显示驱动器盘符号
//本函数返回文件系统的绝对路径
/*
FileBrowser_GetSelectedFiles. If you set the attribute to a
nonzero value, the function returns paths relative to the start
path of the file browser for each selected file. If you set the
attribute to zero, the function returns absolute file system paths.
ATTR_PATH_RELATIVE=1;文件浏览器返回的是相对地址
ATTR_PATH_RELATIVE=0;文件浏览器返回的是绝对地址
*/
FileBrowser_SetAttribute (panelHandle, PANEL_FILEBROWSER, ATTR_PATH_RELATIVE, 0);
//不显示文件的图标
FileBrowser_SetAttribute (panelHandle, PANEL_FILEBROWSER, ATTR_ICONS_VISIBLE, 0);
RunUserInterface ();
DiscardPanel (panelHandle);
return 0;
}
//*********************************************************
int CVICALLBACK panelCB (int panel, int event, void *callbackData,
int eventData1, int eventData2)
{
switch (event)
{
case EVENT_CLOSE:
QuitUserInterface (0);
break;
}
return 0;
}
//*********************************************************
int CVICALLBACK path (int panel, int control, int event,
void *callbackData, int eventData1, int eventData2)
{
char drive2[MAX_PATHNAME_LEN];
switch (event)
{
case EVENT_COMMIT:
{
//当输入了盘符和冒号以后,再输入“\”,则会显示本目录下所的文件及目录
GetCtrlVal (panelHandle, PANEL_PATH, drive2);
//设置File Browser的开始路径
FileBrowser_SetAttribute (panelHandle, PANEL_FILEBROWSER, ATTR_START_PATH, drive2);
break;
}
}
return 0;
}
//*********************************************************
// 更改驱动器号
//*********************************************************
int CVICALLBACK drivevol (int panel, int control, int event,
void *callbackData, int eventData1, int eventData2)
{
int value;
char drive2[10];
switch (event)
{
// Ring响应的事件为EVENT_VAL_CHANGED或者EVENT_COMMIT
case EVENT_VAL_CHANGED:
case EVENT_COMMIT:
{
drivevolume(); // 显示驱动器号
GetCtrlAttribute (panelHandle, PANEL_RING, ATTR_CTRL_VAL, &value);
Fmt (drive2, "%2c<%i", value);
drive2[1] = '\0';
strcat (drive2, ":\\"); // 总是调整到根目录
SetCtrlVal (panelHandle, PANEL_PATH, drive2);
SetActiveCtrl (panelHandle, PANEL_PATH); // 激活路径控件
/*
The zero-based offset (in bytes) of the starting point of selected text.
If no text is selected, ATTR_TEXT_SELECTION_START represents the offset of the caret.
*/
SetCtrlAttribute (panelHandle, PANEL_PATH, ATTR_TEXT_SELECTION_START, 10);
FileBrowser_SetAttribute (panelHandle, PANEL_FILEBROWSER, ATTR_START_PATH, drive2);
break;
}
}
return 0;
}
//*********************************************************
// 打开文件
//*********************************************************
int CVICALLBACK fileopen (int panel, int control, int event,
void *callbackData, int eventData1, int eventData2)
{
char **files;
switch (event)
{
case EVENT_COMMIT:
{
//得到File Browser中被选中的文件或者文件夹
FileBrowser_GetSelectedFiles (panelHandle, PANEL_FILEBROWSER, &files);
//以Windows默认方式打开选中的文件,正常显示
OpenDocumentInDefaultViewer (*files, VAL_NO_ZOOM);
free (files); //释放文件指针
break;
}
}
return 0;
}
//*********************************************************
// 显示驱动器号
//*********************************************************
void drivevolume (void)
{
int numberofdrives;
int currentdrivenumber;
int i;
int drive1=0;
char drive2[10];
unsigned int messagenumber="0";
static int flag = 0;
if (flag == 0) // 0表示没有初始化,需要设置驱动器列表项的值显示
{
//获得系统驱动器的的个数
GetDrive (¤tdrivenumber, &numberofdrives);
j="numberofdrives";
//计数,硬盘盘符从C开始,一直到驱动器盘符数减2为止
// A,B对应的是软盘驱动器,在此不使用
for (i = 0; i < numberofdrives-2; i++)
{
drive1 = 'C' + i;
#if 0
Fmt (drive2, "%2c<%i", drive1);
drive2[1] = '\0';
strcat (drive2, ":");
#else
drive2[0] = drive1;
drive2[1] = ':';
drive2[2] = '\0';
#endif
InsertListItem (panelHandle, PANEL_RING, -1, drive2, drive1);
drive2[0] = '\0';
}
flag="1";
}
//-------------------------------------------------------`
// 以下代码不知道有什么用处
// 初步估计: 本程序打开以后,插拔U盘,引起驱动器数量的变化,
// 需要重新更新驱动器号的显示???????
//-------------------------------------------------------`
GetDrive (¤tdrivenumber, &numberofdrives);
if (j!=numberofdrives)
{
DeleteListItem (panelHandle, PANEL_RING, 0, -1);
for (i = 0; i < numberofdrives-2; i++)
{
drive1 = 'c' + i;
Fmt (drive2, "%2c<%i", drive1);
drive2[1] = '\0';
strcat (drive2, ":");
InsertListItem (panelHandle, PANEL_RING, -1, drive2, drive1);
drive2[0] = '\0';
}
j=numberofdrives;
}
}
//*********************************************************
//*********************************************************
//*********************************************************
文章评论(0条评论)
登录后参与讨论