//*********************************************************
// Labwindows/cvi8.5 密码操作
// szlihongtao
// 2010-06-17
// 参照 王建新的<Labwindows/cvi测试技术及工程应用> page90
// 稍有改写
//*********************************************************
#include "toolbox.h"
#include "combobox.h"
#include "pwctrl.h"
#include "hotringctrl.h"
#include <cvirte.h>
#include <userint.h>
#include "userandpassword.h"
//*********************************************************
static int panelpassword;
static int panelHandle;
//*********************************************************
static void init(void)
{
Combo_NewComboBox (panelHandle, PANEL_COMBO); //将String控件转化为Combo Box控件
PasswordCtrl_ConvertFromString (panelHandle, PANEL_PASSWORD); //将String控件转化为Password控件
//向Combo Box控件中插入项目内容
Combo_InsertComboItem (panelHandle, PANEL_COMBO, -1, "杨过");
Combo_InsertComboItem (panelHandle, PANEL_COMBO, -1, "小龙女");
Combo_InsertComboItem (panelHandle, PANEL_COMBO, -1, "杨康");
Combo_InsertComboItem (panelHandle, PANEL_COMBO, -1, "郭靖");
//用于控制是否显示提示内容,默认为显示
//这条语句可以取消
SetCtrlToolTipAttribute (panelHandle, PANEL_CMD_OK, CTRL_TOOLTIP_ATTR_ENABLE, 1);
//当鼠标悬停设置每个控件应显示提示的内容
SetCtrlToolTipAttribute (panelHandle, PANEL_CMD_OK, CTRL_TOOLTIP_ATTR_TEXT, "这是确定按钮");
SetCtrlToolTipAttribute (panelHandle, PANEL_CMD_CLEAR, CTRL_TOOLTIP_ATTR_TEXT, "这是取消按钮");
SetCtrlToolTipAttribute (panelHandle, PANEL_CMD_VIEW, CTRL_TOOLTIP_ATTR_TEXT, "这是查看密码按钮");
SetCtrlToolTipAttribute (panelHandle, PANEL_COMBO, CTRL_TOOLTIP_ATTR_TEXT, "请输入用户名");
SetCtrlToolTipAttribute (panelHandle, PANEL_PASSWORD, CTRL_TOOLTIP_ATTR_TEXT, "请输入密码");
}
//*********************************************************
int main (int argc, char *argv[])
{
if (InitCVIRTE (0, argv, 0) == 0)
return -1; /* out of memory */
if ((panelHandle = LoadPanel (0, "userandpassword.uir", PANEL)) < 0)
return -1;
init();
DisplayPanel (panelHandle);
RunUserInterface ();
DiscardPanel (panelHandle);
return 0;
}
//*********************************************************
//确认是否输入正确
//*********************************************************
int CVICALLBACK ok (int panel, int control, int event,
void *callbackData, int eventData1, int eventData2)
{
int len;
int plen;
char *username;
char *passwordstring;
switch (event)
{
case EVENT_COMMIT:
{
//得到Combo Box中字符串的长度
GetCtrlAttribute (panelHandle, PANEL_COMBO, ATTR_STRING_TEXT_LENGTH, &len);
//动态分配字符串长度加1的空间,最后一个空间存储结束符'\0'值
username = (char *) malloc (len * sizeof (char) + 1);
username[0] = '\0'; //清除用户名中的内容
//strcpy(username,"");
//将用户名存储到username
GetCtrlVal (panelHandle, PANEL_COMBO, username);
//得到Password中内容的长度
PasswordCtrl_GetAttribute (panelHandle, PANEL_PASSWORD, ATTR_PASSWORD_MAX_LENGTH, &plen);
//动态分配Password长度加1的空间,最后一个空间存储结束符'\0'值
passwordstring = (char *) malloc (plen * sizeof (char) + 1);
passwordstring[0] = '\0'; //清除Password中的内容
//取得Password中存储的内容
PasswordCtrl_GetAttribute (panelHandle, PANEL_PASSWORD, ATTR_PASSWORD_VAL, passwordstring);
//判断密码输入是否正确
if ((strcmp (username, "杨过") ==0)
&& (strcmp (passwordstring, "yangguo") == 0))
MessagePopup ("密码", "密码正确!");
else
MessagePopup ("密码", "密码不正确!");
free (username); //释放动态分配的内存
free (passwordstring);
break;
}
}
return 0;
}
//*********************************************************
int CVICALLBACK clear (int panel, int control, int event,
void *callbackData, int eventData1, int eventData2)
{
switch (event)
{
case EVENT_COMMIT:
{
//清除密码
SetCtrlVal(panelHandle, PANEL_PASSWORD,"");
//PasswordCtrl_SetAttribute (panelHandle, PANEL_PASSWORD, ATTR_PASSWORD_VAL, "");
break;
}
}
return 0;
}
//*********************************************************
int CVICALLBACK timer (int panel, int control, int event,
void *callbackData, int eventData1, int eventData2)
{
int keymodifiers;
int rightdwon = 0;
int leftdwon = 0;
int y;
int x;
int len;
char *passwordstring;
char message[100];
switch (event)
{
case EVENT_TIMER_TICK:
{
//得到鼠标的状态,包括x,y坐标及左键、右键是否按下
GetGlobalMouseState (&panelHandle, &x, &y, &leftdwon, &rightdwon, &keymodifiers);
//如果按下左键、右键及Ctrl键时,弹出对话框,显示密码
if ((leftdwon==1)
&& (rightdwon==1)
&& (keymodifiers == VAL_MENUKEY_MODIFIER))
{
PasswordCtrl_GetAttribute (panelHandle, PANEL_PASSWORD, ATTR_PASSWORD_MAX_LENGTH, &len);
passwordstring = (char *) malloc (len * sizeof (char));
PasswordCtrl_GetAttribute (panelHandle, PANEL_PASSWORD, ATTR_PASSWORD_VAL, passwordstring);
strcat (message, "输入的密码是:\n");
strcat (message, passwordstring);
MessagePopup ("密码", message);
free (passwordstring);
}
break;
}
}
return 0;
}
//*********************************************************
int CVICALLBACK passwordview (int panel, int control, int event,
void *callbackData, int eventData1, int eventData2)
{
switch (event)
{
case EVENT_COMMIT:
{
panelpassword = LoadPanel (0, "userandpassword.uir", PANEL_2);
//采用模式对话框
InstallPopup (panelpassword);
break;
}
}
return 0;
}
//*********************************************************
int CVICALLBACK hidepanel (int panel, int control, int event,
void *callbackData, int eventData1, int eventData2)
{
switch (event)
{
case EVENT_COMMIT:
DiscardPanel (panelpassword);
break;
}
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;
}
//*********************************************************
//*********************************************************
//*********************************************************
用户1724215 2014-1-9 16:01
用户574984 2012-9-19 13:51