- 易迪拓培训,专注于微波、射频、天线设计工程师的培养
LabWindows/CVI虚拟仪器设计技术场景与游戏设计之: 移动控件
(2)程序源代码
//头文件声明
#include <cvirte.h>
#include <userint.h>
#include "utility.h"
#include "移动控件.h"
static int panelHandle;
//主函数
int main (int argc, char *argv[])
{
if (InitCVIRTE (0, argv, 0) == 0)
return –1; /* out of memory */
if ((panelHandle = LoadPanel (0, " 移动控件.uir", PANEL)) < 0)
return –1;
DisplayPanel (panelHandle);
RunUserInterface ();
DiscardPanel (panelHandle);
return 0;
}
//面板回调函数
int CVICALLBACK PanelCB (int panel, int event, void *callbackData,
int eventData1, int eventData2)
{
int LeftButtonDown;
int y;
int x;
int CtrlTop;
int CtrlLeft;
int CursorStyle;
//局部静态变量,旧X、坐标值
static int OldX = 0;
static int OldY = 0;
switch (event)
{
//鼠标移动事件
case EVENT_MOUSE_POINTER_MOVE:
GetCtrlAttribute (panelHandle, PANEL_GRAPH, ATTR_LEFT, &CtrlLeft);
GetCtrlAttribute (panelHandle, PANEL_GRAPH, ATTR_TOP, &CtrlTop);
// 获得鼠标绝对位置、按键等属性
GetGlobalMouseState (&panel, &x, &y, &LeftButtonDown, NULL, NULL);
// 当鼠标左键按下时
if (LeftButtonDown == 1)
{
// 获得鼠标指针形状
GetMouseCursor (&CursorStyle);
if (CursorStyle == VAL_OPEN_HAND_CURSOR)
{
SetCtrlAttribute (panelHandle, PANEL_GRAPH, ATTR_LEFT, CtrlLeft + (x – OldX));
SetCtrlAttribute (panelHandle, PANEL_GRAPH, ATTR_TOP, CtrlTop + (y – OldY));
// 将旧X、Y 坐标值以当前新值代替
OldX = x;
OldY = y;
}
}
break;
//鼠标左击事件
case EVENT_LEFT_CLICK:
// 获得鼠标绝对位置、按键等属性
GetGlobalMouseState (&panel, &x, &y, &LeftButtonDown, NULL, NULL);
// 获得鼠标指针形状
GetMouseCursor (&CursorStyle);
// 将旧X、坐标值以当前新值代替
if (CursorStyle == VAL_OPEN_HAND_CURSOR)
{
OldX = x;
OldY = y;
}
break;
//关闭面板事件
case EVENT_CLOSE:
QuitUserInterface (0);
break;
}
return 0;
}
//定时器
int CVICALLBACK timer (int panel, int control, int event,
void *callbackData, int eventData1, int eventData2)
{
int CtrlWidth;
int CtrlHeight;
int RightButtonDown;
int LeftButtonDown;
int y;
int x;
int CursorStyle;
//定义为局部静态变量,始终保存鼠标指针形状参数
static int MouseStyle = 0;
switch (event)
{
case EVENT_TIMER_TICK:
GetCtrlAttribute (panelHandle, PANEL_GRAPH, ATTR_HEIGHT, &CtrlHeight);
GetCtrlAttribute (panelHandle, PANEL_GRAPH, ATTR_WIDTH, &CtrlWidth);
//获得鼠标相对位置、按键等属性
GetRelativeMouseState (panel, PANEL_GRAPH, &x, &y, &LeftButtonDown, &RightButton
Down, NULL);
//判断鼠标相对控件位置
if (x > CtrlWidth – 5 && x < CtrlWidth + 5 && y > 0 && y < CtrlHeight)
{
//左右箭头
MouseStyle = 1;
}
if (y > CtrlHeight – 5 && y < CtrlHeight + 5 && x > 0 && x < CtrlWidth)
{
//上下箭头
MouseStyle = 2;
}
if (x > CtrlWidth – 5 && x < CtrlWidth + 5 && y > CtrlHeight – 5 && y < CtrlHeight + 5)
{
//"↖↘"箭头
MouseStyle = 3;
}
if (x > 5 && x < CtrlWidth – 5 && y > 5 && y < CtrlHeight – 5)
{
//手型箭头
MouseStyle = 4;
}
if (x < 0 || x > CtrlWidth + 5 || y < 0 || y > CtrlHeight + 5)
{
//默认箭头
MouseStyle = 0;
}
//设置鼠标指针形状
if (LeftButtonDown == 0)
{
switch (MouseStyle) {
case 0:
SetMouseCursor (VAL_DEFAULT_CURSOR);
break;
case 1:
SetMouseCursor (VAL_SIZE_EW_CURSOR); break;
case 2:
SetMouseCursor (VAL_SIZE_NS_CURSOR);
break;
case 3:
SetMouseCursor (VAL_SIZE_NW_SE_CURSOR); break;
case 4:
SetMouseCursor (VAL_OPEN_HAND_CURSOR);
break;
}
} // 设置控件大小
if (LeftButtonDown == 1 && x > 0 && y > 0) {
GetMouseCursor (&CursorStyle); switch (CursorStyle)
{ case VAL_SIZE_EW_CURSOR:
SetCtrlAttribute (panelHandle, PANEL_GRAPH, ATTR_WIDTH, x); break;
case VAL_SIZE_NS_CURSOR:
SetCtrlAttribute (panelHandle, PANEL_GRAPH, ATTR_HEIGHT, y);
break;
case VAL_SIZE_NW_SE_CURSOR:
SetCtrlAttribute (panelHandle, PANEL_GRAPH, ATTR_WIDTH, x); SetCtrlAttribute (panelHandle, PANEL_GRAPH, ATTR_HEIGHT, y);
break;
}
} break;
} return 0;
}
上一篇:LabWindows/CVI虚拟仪器设计技术基本控件使用之:Ring
箭头设置
下一篇:基于NI
VeriStand和JMAG-RT进行高性能电机仿真