• 易迪拓培训,专注于微波、射频、天线设计工程师的培养
首页 > 测试测量 > 技术文章 > LabWindows/CVI虚拟仪器设计技术场景与游戏设计之:时钟制作

LabWindows/CVI虚拟仪器设计技术场景与游戏设计之:时钟制作

录入:edatop.com    点击:

(2)程序源代码

//头文件声明
#include <ansi_c.h>
#include <utility.h>
#include <cvirte.h>
#include <userint.h>
#include "时钟制作.h"
static int panelHandle;
//主函数
int main (int argc, char *argv[])

int i;
if (InitCVIRTE (0, argv, 0) == 0)
return –1;  /* out of memory */
if ((panelHandle = LoadPanel (0, " 时钟制作.uir", PANEL)) < 0)
return –1;
//设置表盘数字底色透明
for (i = 2; i < 14; i ++)
{
SetCtrlAttribute (panelHandle, i, ATTR_TEXT_BGCOLOR, VAL_TRANSPARENT);
}
//调用定时器回调函数
timer (panelHandle, PANEL_TIMER, EVENT_TIMER_TICK, NULL, NULL, NULL);
DisplayPanel (panelHandle);
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 timer (int panel, int control, int event,
void *callbackData, int eventData1, int eventData2)
{
char *timestr;
char CurTime[50];
int Seconds;
int Minutes;
int Hours;
int sdeg;
int mdeg;
int hdeg;
double srad;
double mrad;
double hrad;
//记录表盘指针状态
static int sx2,sy2,soldx2,soldy2;
static int mx2,my2,moldx2,moldy2;
static int hx2,hy2,holdx2,holdy2;
switch (event)
{
case EVENT_TIMER_TICK:
// 获得系统时间
GetSystemTime (&Hours, &Minutes, &Seconds);
// 转换为角度
sdeg = Seconds * 6;
mdeg = Minutes * 6;
hdeg = Hours * 30 + (0.5 * Minutes);
// 设置秒针参数
// 设置Canvas 控件绘图笔宽度
SetCtrlAttribute (panelHandle, PANEL_CANVAS, ATTR_PEN_WIDTH, 2);
soldx2 = sx2;
soldy2 = sy2;
// 转换为弧度
srad = (3.14 / 180) * sdeg;
// 擦除上一次绘制图形
SetCtrlAttribute (panelHandle, PANEL_CANVAS, ATTR_PEN_COLOR, VAL_BLACK);
CanvasDrawLine (panelHandle, PANEL_CANVAS, MakePoint (150,150), MakePoint (soldx2,
soldy2));

// 计算指针末端位置
sx2 = 150 + 100 * sin(srad);
sy2 = 150 – 100 * cos(srad);
// 绘制新图形
SetCtrlAttribute (panelHandle, PANEL_CANVAS, ATTR_PEN_COLOR, VAL_RED);
CanvasDrawLine (panelHandle, PANEL_CANVAS, MakePoint (150,150), MakePoint (sx2,sy2));
// 设置分针参数
SetCtrlAttribute (panelHandle, PANEL_CANVAS, ATTR_PEN_WIDTH, 4);
moldx2 = mx2;
moldy2 = my2;
mrad = (3.14 / 180) * mdeg;
SetCtrlAttribute (panelHandle, PANEL_CANVAS, ATTR_PEN_COLOR, VAL_BLACK);
CanvasDrawLine (panelHandle, PANEL_CANVAS, MakePoint (150,150), MakePoint (moldx2,
moldy2)); 
mx2 = 150 + 80 * sin(mrad);
my2 = 150 – 80 * cos(mrad);
SetCtrlAttribute (panelHandle, PANEL_CANVAS, ATTR_PEN_COLOR, VAL_BLUE);
CanvasDrawLine (panelHandle, PANEL_CANVAS, MakePoint (150,150), MakePoint (mx2,my2));
// 设置时针参数
SetCtrlAttribute (panelHandle, PANEL_CANVAS, ATTR_PEN_WIDTH, 6);
holdx2 = hx2;
holdy2 = hy2;
hrad = (3.14 / 180) * hdeg;
SetCtrlAttribute (panelHandle, PANEL_CANVAS, ATTR_PEN_COLOR, VAL_BLACK);
CanvasDrawLine (panelHandle, PANEL_CANVAS, MakePoint (150,150), MakePoint (holdx2,
holdy2)); 
hx2 = 150 + 65 * sin(hrad);
hy2 = 150 – 65 * cos(hrad);
SetCtrlAttribute (panelHandle, PANEL_CANVAS, ATTR_PEN_COLOR, VAL_WHITE);
CanvasDrawLine (panelHandle, PANEL_CANVAS, MakePoint (150,150), MakePoint (hx2,hy2));
// 获得当前时间字符串
timestr = TimeStr ();
CurTime[0] = '';
strcat (CurTime, " 当前时间:");
strcat (CurTime, timestr);
// 标题栏显示当前时间
SetPanelAttribute (panelHandle, ATTR_TITLE, CurTime);
break;

return 0; 


 

点击浏览:矢量网络分析仪、频谱仪、示波器,使用操作培训教程

上一篇:LabWindows/CVI虚拟仪器设计技术场景与游戏设计之: 下雪场景
下一篇:LabWindows/CVI虚拟仪器设计技术基本控件使用之: Tab 控件

微波射频测量操作培训课程详情>>
射频和天线工程师培训课程详情>>

  网站地图