上期使用LABwindows/CVI這個(gè)軟件搭建了學(xué)生管理器的UI界面,這期就是將UI界面中各個(gè)控件編程,使其能夠?qū)崿F(xiàn)一定的功能。
關(guān)鍵詞:控件編程;
01控件編程
1.1、生成控件的頭文件內(nèi)容
生成框架代碼之后,我們應(yīng)該可以在uir同名的h文件中看到CVI為我們生成了如下代碼,包含了CVI框架運(yùn)行、CVI用戶界面運(yùn)行所需要的基本函數(shù)的聲明與定義。 show.h是界面文件show.uir的頭文件庫(kù),包含了界面中的控件的定義與聲明:
頭文件內(nèi)柔如下所示:
/**************************************************************************/
/* LabWindows/CVI User Interface Resource (UIR) Include File */
/* */
/* WARNING: Do not add to, delete from, or otherwise modify the contents */
/* of this include file. */
/**************************************************************************/
#include
#ifdef __cplusplus
extern "C" {
#endif
/* Panels and Controls: */
#define PANEL 1 /* callback function: MainCallBack */
#define PANEL_STRING_Name 2 /* control type: string, callback function: (none) */
#define PANEL_BTN_Change 3 /* control type: command, callback function: MainPanelBtnCallback */
#define PANEL_BTN_Show 4 /* control type: command, callback function: MainPanelBtnCallback */
#define PANEL_BTN_insert 5 /* control type: command, callback function: MainPanelBtnCallback */
#define PANEL_BTN_Delete 6 /* control type: command, callback function: MainPanelBtnCallback */
#define PANEL_BTN_Add 7 /* control type: command, callback function: MainPanelBtnCallback */
#define PANEL_NUM_Student_Insert 8 /* control type: numeric, callback function: (none) */
#define PANEL_NUM_Student 9 /* control type: numeric, callback function: (none) */
/* Control Arrays: */
/* (no control arrays in the resource file) */
/* Menu Bars, Menus, and Menu Items: */
/* (no menu bars in the resource file) */
/* Callback Prototypes: */
int CVICALLBACK MainCallBack(int panel, int event, void *callbackData, int eventData1, int eventData2);
int CVICALLBACK MainPanelBtnCallback(int panel, int control, int event, void *callbackData, int eventData1, int eventData2);
#ifdef __cplusplus
}
#endif
1.2、添加的文件
程序如下:
#include
#include //CVI運(yùn)行時(shí)庫(kù)(CVI Run-Time Engine)
#include //CVI用戶界面庫(kù)(CVI User Interface)
#include //包含了C、C++語(yǔ)言的最常用的系統(tǒng)函數(shù)
#include "show.h"
1.3、添加結(jié)構(gòu)體
結(jié)構(gòu)體程序如下:
static int panelHandle;
//為面板的全局句柄,當(dāng)對(duì)面板或者面板上的控件進(jìn)行操作時(shí)經(jīng)常用到
#define LEN sizeof(struct Student) //宏定義節(jié)點(diǎn)長(zhǎng)度得命名
#define TYPE struct Student //宏定義結(jié)構(gòu)體變量命名
struct Student //定義一個(gè)學(xué)生類型結(jié)構(gòu)體,包括學(xué)號(hào),分?jǐn)?shù)
{
char Name[20];
int Number;
TYPE *Next; //next是指針變量,指向結(jié)構(gòu)體變量
};
TYPE *Head = NULL; //定義頭指針地址,全局變量
TYPE *PTmp = NULL; //定義新開(kāi)辟結(jié)點(diǎn)變量,全局變量
以下就是五個(gè)按鍵對(duì)應(yīng)的功能函數(shù)
1.4、動(dòng)態(tài)鏈表函數(shù)
代入如下:
TYPE* Creat(void) //定義函數(shù),此函數(shù)返回一個(gè)指向鏈表頭的指針
{
char Name[20] = {0}; //定義保存姓名的數(shù)組
int Number = 0; //定義保存姓名的整形
GetCtrlAttribute(panelHandle, PANEL_STRING_Name, ATTR_CTRL_VAL, Name);
//得到PANEL_STRING_Name字符框中字符串給Name
GetCtrlAttribute(panelHandle, PANEL_NUM_Student, ATTR_CTRL_VAL, &Number);
//得到PANEL_SPANEL_NUM_Student整形框中字符串給Number
if((strlen(Name) < 1) || (Number < 1))
//根據(jù)輸入的數(shù)據(jù)長(zhǎng)度來(lái)判斷學(xué)生數(shù)據(jù)是否輸入有誤
{
MessagePopup("提示", "請(qǐng)重新輸入..."); //顯示對(duì)話消息對(duì)話框
}
else
{
PTmp = (struct Student *)malloc(sizeof(struct Student));//開(kāi)辟新的結(jié)點(diǎn)空間
memset(PTmp, 0, sizeof(struct Student)); // 將空間清零
PTmp->Number = Number; //結(jié)點(diǎn)中保存學(xué)號(hào),姓名
strcat(PTmp->Name, Name);
struct Student *PMov = Head; //每次都返回頭指針
if(Head == NULL)
{
Head = PTmp; //把第一個(gè)結(jié)點(diǎn)給頭指針
}
else
{
while(PMov->Next) //循環(huán)從頭到尾找指針域
{
PMov = PMov->Next;
}
PMov->Next = PTmp; //找到指針域后更新
}
printf("新添加了1個(gè)學(xué)生:%s 學(xué)號(hào):%d號(hào)\\n", PTmp->Name, PTmp->Number);
//顯示添加信息
}
return (Head); //返回首地址
}
1.5、刪除結(jié)點(diǎn)函數(shù)
代碼如下:
void delet(TYPE* head) //定義結(jié)點(diǎn)刪除函數(shù)
{
TYPE* p = head, * in; //定義兩邊指針
int i = 0; //定義記錄值
char Name[20] = {0}; //定義姓名數(shù)組
GetCtrlAttribute(panelHandle, PANEL_STRING_Name, ATTR_CTRL_VAL, Name);
//得到PANEL_STRING_Name字符框中字符串給Name
while ((strcmp(p->Name, Name) !=0) && p != NULL)
//找到對(duì)應(yīng)姓名并且不為空的結(jié)點(diǎn)
{
in = p; //找到左邊的
p = p->Next; //找到右邊的
i++;
}
if (p != NULL)
{
printf("刪除的學(xué)生為:%s 學(xué)號(hào):%d號(hào)\\n", p->Name, p->Number); //顯示刪除信息
in->Next = p->Next; //將左右鏈接
free(p); //釋放中間結(jié)點(diǎn)
}
else {
MessagePopup("提示", "學(xué)生節(jié)點(diǎn)不存在..."); //顯示對(duì)話消息對(duì)話框
}
}
1.6、鏈表顯示函數(shù)
代碼如下:
void print(TYPE * head) //定義將鏈表輸出函數(shù)
{
TYPE * p; //定義指針
p = head; //使p指向第一個(gè)結(jié)點(diǎn)
int count = 0; //記錄多少哥結(jié)點(diǎn)數(shù)
if(Head == NULL) //判斷是否為空
{
MessagePopup("提示", "表中不包含任何學(xué)生..."); //顯示對(duì)話消息對(duì)話框
}
else
if(head!=NULL) //輸出第一個(gè)結(jié)點(diǎn)后的信息
do {
printf("第%d個(gè)學(xué)生:%s 學(xué)號(hào):%d號(hào)\\n", ++count, p->Name, p->Number);
//顯示結(jié)點(diǎn)信息
p = p->Next; //指向下個(gè)結(jié)點(diǎn)
} while (p != NULL);
}
1.7、鏈表的插入
代碼如下:
void insert(TYPE* head) { //定義鏈表的插入函數(shù)
TYPE* p = head, * in; //定義首尾指針
int i = 0; //記錄值
int n; //定義插入的地方
char Name[20] = {0}; //定義姓名數(shù)組 ,學(xué)號(hào)
int Number = 0;
GetCtrlAttribute(panelHandle, PANEL_NUM_Student_Insert, ATTR_CTRL_VAL, &n);
//得到PANEL_NUM_Student_Insert整形中數(shù)據(jù)給n
while (i < (n-1) && p != NULL) {
p = p->Next;
i++; //找到相應(yīng)結(jié)點(diǎn)
}
if (p != NULL) {
in = (TYPE*)malloc(sizeof(TYPE)); //開(kāi)辟新的空間
memset(in, 0, sizeof(struct Student)); //清零
GetCtrlAttribute(panelHandle, PANEL_STRING_Name, ATTR_CTRL_VAL, Name); //得到PANEL_STRING_Name字符中數(shù)據(jù)給Name
GetCtrlAttribute(panelHandle, PANEL_NUM_Student, ATTR_CTRL_VAL, &Number);//得到PANEL_NUM_Student整形中數(shù)據(jù)給Number
in->Number = Number;
strcat(in->Name, Name); //復(fù)制信息給in->Name
printf("插入學(xué)生:%s 學(xué)號(hào):%d號(hào)\\n", in->Name, in->Number); //顯示插入的信息
in->Next = p->Next;
//填充in節(jié)點(diǎn)的指針域,也就是說(shuō)把in的指針域指向p的下一個(gè)節(jié)點(diǎn)
p->Next = in; //填充p節(jié)點(diǎn)的指針域,把p的指針域重新指向in
}
else {
MessagePopup("提示", "表中不包含任何學(xué)生...");//顯示對(duì)話消息對(duì)話框
}
}
1.8、鏈表的修改
代碼如下:
void change(TYPE* head) //修改指定位置的結(jié)點(diǎn)的信息函數(shù)
{
TYPE* p = head; //傳入首地址
int i = 0; //記錄值
char Name[20] = {0}; //定義姓名數(shù)組 ,學(xué)號(hào)
int Number = 0;
GetCtrlAttribute(panelHandle, PANEL_STRING_Name, ATTR_CTRL_VAL, Name);
//得到PANEL_STRING_Name字符中數(shù)據(jù)給Name
while ((strcmp(p->Name, Name) !=0) && p != NULL) {
//找到對(duì)應(yīng)姓名并且不為空的結(jié)點(diǎn)
p = p->Next;
i++;
} //找到相應(yīng)的位置結(jié)點(diǎn)
if (p != NULL) {
GetCtrlAttribute(panelHandle, PANEL_STRING_Name, ATTR_CTRL_VAL, Name); //得到PANEL_STRING_Name字符中數(shù)據(jù)給Name
GetCtrlAttribute(panelHandle, PANEL_NUM_Student, ATTR_CTRL_VAL, &Number);//得到PANEL_NUM_Student整形中數(shù)據(jù)給Number
p->Number = Number; //保存學(xué)號(hào)
printf("修改的學(xué)生:%s 學(xué)號(hào):%d號(hào)\\n", p->Name, p->Number); //顯示修改的信息
}
else
MessagePopup("提示", "表中不包含該學(xué)生"); //顯示對(duì)話消息對(duì)話框
}
下期就是講解控件怎么回調(diào)??!
-
LabWindows
+關(guān)注
關(guān)注
15文章
62瀏覽量
48273 -
管理器
+關(guān)注
關(guān)注
0文章
254瀏覽量
19069 -
CVI
+關(guān)注
關(guān)注
9文章
41瀏覽量
22494 -
控件編程
+關(guān)注
關(guān)注
0文章
2瀏覽量
5145 -
ui界面
+關(guān)注
關(guān)注
0文章
11瀏覽量
1723
發(fā)布評(píng)論請(qǐng)先 登錄
關(guān)于LabWindows/CVI和Labview
美國(guó)國(guó)家儀器(NI)軟件Labview,Labwindows cvi,Motion,Vision,DAQ開(kāi)發(fā)與培訓(xùn)
采用LabWindows CVI的飛行模擬器應(yīng)用
一種LabWindows/CVI與MATLAB混合編程的實(shí)現(xiàn)
LabWindows/CVI基礎(chǔ)教程
LabWindows/CVI與PLC的MPI通信研究
基于多CPU和LabWindows/CVI 的行車記錄儀系統(tǒng)
cvi 8.0 下載 (NI LabWindows 電子測(cè)量)

NI宣布推出ANSI C開(kāi)發(fā)環(huán)境LabWindows/CVI
LabWindows_CVI多線程技術(shù)在油門(mén)測(cè)試軟件中的應(yīng)用_倪
LabWindows CVI在USB2. 0接口數(shù)據(jù)采集器在開(kāi)發(fā)中的應(yīng)用

Labwindows CVI的簡(jiǎn)介說(shuō)明

使用LABwindows/CVI軟件搭建學(xué)生管理器界面(一)

評(píng)論