答:
#include <utility.h>
#include <userint.h>
int panelHandle;
int button1Handle, button2Handle;
int CVICALLBACK MyCallback (int panel, int control, int event,
void *callbackData, int eventData1, int eventData2)
{
if (event == EVENT_COMMIT) {
if (control == button1Handle)
MessagePopup("Button 1", "Button 1 was clicked");
else if (control == button2Handle)
MessagePopup("Button 2", "Button 2 was clicked");
}
return 0;
}
int main (int argc, char *argv[])
{
if (InitCVIRTE (0, argv, 0) == 0)
return -1; /* out of memory */
panelHandle = LoadPanel (0, "MyPanel.uir", PANEL);
button1Handle = GetCtrlHandle (panelHandle, PANEL_BUTTON1);
button2Handle = GetCtrlHandle (panelHandle, PANEL_BUTTON2);
SetCtrlAttribute (panelHandle, PANEL_BUTTON1, ATTR_CALLBACK_DATA, (void *) 1);
SetCtrlAttribute (panelHandle, PANEL_BUTTON2, ATTR_CALLBACK_DATA, (void *) 2);
SetCtrlAttribute (panelHandle, PANEL_BUTTON1, ATTR_CALLBACK_FUNCTION, MyCallback);
SetCtrlAttribute (panelHandle, PANEL_BUTTON2, ATTR_CALLBACK_FUNCTION, MyCallback);
DisplayPanel (panelHandle);
RunUserInterface ();
DiscardPanel (panelHandle);
return 0;
}
在该代码中,MyCallback函数为两个按钮实现了事件响应,当每个按钮被单击时,它将弹出一个消息框来显示按钮的标识。在main函数中,我们使用GetCtrlHandle获取每个按钮的句柄,然后使用SetCtrlAttribute设置每个按钮的回调函数。