通过一个抢答器的项目设计了解CVI常用控件的几个基本使用方法。
设定和读取控件值方法:
控件值一般是控件的最主要的一个属性值,如按钮的ON/OFF状态;number的数值等。
GetCtrlVal (面板名, 控件名, 返回值地址):获取控件值;
SetCtrlVal (面板名, 控件名, 设定值):设定控制值;
设定和读取控件属性方法:
具体某个控件的属性名关键字,可以通过帮助或函数输入助手查询到。
GetCtrlAttribute (面板名, 控件名, 属性名,返回值地址 );
SetCtrlAttribute (面板名, 控件名, 属性名,设定值);
界面设计:
打开CVI开发软件,通过模板创建一个项目,在面板上拖放相关控件,效果如下图。
拖放一个COMMAND button控件作为退出按钮;拖放一个SWITCH控件作为主持人开关;拖放6个LED控件指示抢答结果;拖放6个toggle button控件为抢答按钮;顶部一个string控件显示开始抢答的状态指示;
当主持人开关在ON位置时,开始抢答,任意选手按下按钮,点亮相应LED灯,其它选手不能再抢答。
全部代码如下:
#include <cvirte.h>
#include <userint.h>
#include "CVIResponder.h"
static int panelHandle;
//全局变量,主持人开关状态
int startflag=0 ;
//复位各控件状态
void ResetCtrlStatus()
{
//把LED灯控件全部灭掉
SetCtrlVal (panelHandle, PANEL_LED1, 0);
SetCtrlVal (panelHandle, PANEL_LED2, 0);
SetCtrlVal (panelHandle, PANEL_LED3, 0);
SetCtrlVal (panelHandle, PANEL_LED4, 0);
SetCtrlVal (panelHandle, PANEL_LED5, 0);
SetCtrlVal (panelHandle, PANEL_LED6, 0);
//把抢答按钮置于未选择状态
SetCtrlVal(panelHandle,PANEL_TOGGLEBUTTON1,0);
SetCtrlVal(panelHandle,PANEL_TOGGLEBUTTON2,0);
SetCtrlVal(panelHandle,PANEL_TOGGLEBUTTON3,0);
SetCtrlVal(panelHandle,PANEL_TOGGLEBUTTON4,0);
SetCtrlVal(panelHandle,PANEL_TOGGLEBUTTON5,0);
SetCtrlVal(panelHandle,PANEL_TOGGLEBUTTON6,0);
//抢答按钮初始状态禁止操作
SetCtrlDimmed();
}
//设置抢答按钮禁止使用
void SetCtrlDimmed()
{
SetCtrlAttribute (panelHandle, PANEL_TOGGLEBUTTON1, ATTR_DIMMED, 1);
SetCtrlAttribute (panelHandle, PANEL_TOGGLEBUTTON2, ATTR_DIMMED, 1);
SetCtrlAttribute (panelHandle, PANEL_TOGGLEBUTTON3, ATTR_DIMMED, 1);
SetCtrlAttribute (panelHandle, PANEL_TOGGLEBUTTON4, ATTR_DIMMED, 1);
SetCtrlAttribute (panelHandle, PANEL_TOGGLEBUTTON5, ATTR_DIMMED, 1);
SetCtrlAttribute (panelHandle, PANEL_TOGGLEBUTTON6, ATTR_DIMMED, 1);
}
//设置抢答按钮允许使用
void ResetCtrlDimmed()
{
SetCtrlAttribute (panelHandle, PANEL_TOGGLEBUTTON1, ATTR_DIMMED, 0);
SetCtrlAttribute (panelHandle, PANEL_TOGGLEBUTTON2, ATTR_DIMMED, 0);
SetCtrlAttribute (panelHandle, PANEL_TOGGLEBUTTON3, ATTR_DIMMED, 0);
SetCtrlAttribute (panelHandle, PANEL_TOGGLEBUTTON4, ATTR_DIMMED, 0);
SetCtrlAttribute (panelHandle, PANEL_TOGGLEBUTTON5, ATTR_DIMMED, 0);
SetCtrlAttribute (panelHandle, PANEL_TOGGLEBUTTON6, ATTR_DIMMED, 0);
}
//抢答器程序,num表示几号抢答按钮被按下
void Responder(int num)
{
if(startflag){
switch (num)
{
case 1:
SetCtrlVal(panelHandle,PANEL_LED1,1);
break;
case 2:
SetCtrlVal(panelHandle,PANEL_LED2,1);
break;
case 3:
SetCtrlVal(panelHandle,PANEL_LED3,1);
break;
case 4:
SetCtrlVal(panelHandle,PANEL_LED4,1);
break;
case 5:
SetCtrlVal(panelHandle,PANEL_LED5,1);
break;
case 6:
SetCtrlVal(panelHandle,PANEL_LED6,1);
break;
default:
break;
}
SetCtrlDimmed();
}
}
int main (int argc, char *argv[])
{
if (InitCVIRTE (0, argv, 0) == 0)
return -1; /* out of memory */
if ((panelHandle = LoadPanel (0, "CVIResponder.uir", PANEL)) < 0)
return -1;
DisplayPanel (panelHandle);
ResetCtrlStatus();
RunUserInterface ();
DiscardPanel (panelHandle);
return 0;
}
int CVICALLBACK panelCB (int panel, int event, void *callbackData,int eventData1, int eventData2)
{
switch (event)
{
case EVENT_GOT_FOCUS:
break;
case EVENT_LOST_FOCUS:
break;
case EVENT_CLOSE:
break;
}
return 0;
}
int CVICALLBACK btnExit (int panel, int control, int event,void *callbackData, int eventData1, int eventData2)
{
switch (event)
{
case EVENT_COMMIT:
QuitUserInterface (0);
break;
}
return 0;
}
int CVICALLBACK btnPerson1 (int panel, int control, int event,void *callbackData, int eventData1, int eventData2)
{
switch (event)
{
case EVENT_COMMIT:
Responder(1);
break;
}
return 0;
}
int CVICALLBACK btnPerson2 (int panel, int control, int event,void *callbackData, int eventData1, int eventData2)
{
switch (event)
{
case EVENT_COMMIT:
Responder(2);
break;
}
return 0;
}
int CVICALLBACK btnPerson3 (int panel, int control, int event,void *callbackData, int eventData1, int eventData2)
{
switch (event)
{
case EVENT_COMMIT:
Responder(3);
break;
}
return 0;
}
int CVICALLBACK btnPerson4 (int panel, int control, int event,void *callbackData, int eventData1, int eventData2)
{
switch (event)
{
case EVENT_COMMIT:
Responder(4);
break;
}
return 0;
}
int CVICALLBACK btnPerson5 (int panel, int control, int event,void *callbackData, int eventData1, int eventData2)
{
switch (event)
{
case EVENT_COMMIT:
Responder(5);
break;
}
return 0;
}
int CVICALLBACK btnPerson6 (int panel, int control, int event,void *callbackData, int eventData1, int eventData2)
{
switch (event)
{
case EVENT_COMMIT:
Responder(6);
break;
}
return 0;
}
int CVICALLBACK btnSwitch (int panel, int control, int event,void *callbackData, int eventData1, int eventData2)
{
switch (event)
{
case EVENT_COMMIT:
GetCtrlVal (panelHandle, PANEL_BINARYSWITCH, &startflag);
if(startflag)
{
SetCtrlAttribute (panelHandle, PANEL_STRING, ATTR_TEXT_COLOR, VAL_GREEN);
ResetCtrlDimmed();
}
else
{
SetCtrlAttribute (panelHandle, PANEL_STRING, ATTR_TEXT_COLOR, VAL_GRAY);
ResetCtrlStatus();
}
break;
}
return 0;
}
int CVICALLBACK strStartLED (int panel, int control, int event,void *callbackData, int eventData1, int eventData2)
{
switch (event)
{
case EVENT_COMMIT:
break;
}
return 0;
}