⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 msgdemo.c

📁 labwindow 编程的toolbox例程。
💻 C
字号:
/*---------------------------------------------------------------------------*/
/*                                                                           */
/* FILE:    msgdemo.c                                                        */
/*                                                                           */
/* PURPOSE: This example illustrates how to use functions in the Programmer's*/
/*          Toolbox Instrument Driver (toolslib\toolbox\toolbox.fp to enable */
/*          your panel to respond to any Windows message sent to it.  The    */
/*          application installs callback functions for two different Windows*/
/*          messages, arbitrarily chosen for this example.  Command buttons  */
/*          on the UI allow you to post the appropriate messages from one    */
/*          panel to the other, and the callbacks will respond by displaying */
/*          the received wParam and lParam.  This functionality is useful in */
/*          the case of a third-party API that requires you to respond to    */
/*          windows messages directly -- you can install a callback for any  */
/*          message number, unlike the UI Library's RegisterWinMsgCallback.  */
/*                                                                           */
/*          The driver also lets you respond to simple shell file Drag and   */
/*          Drop occurrences.                                                */
/*                                                                           */
/*---------------------------------------------------------------------------*/

/*---------------------------------------------------------------------------*/
/* Include files                                                             */
/*---------------------------------------------------------------------------*/
#include "windows.h"
#include <formatio.h>
#include <utility.h>
#include <ansi_c.h>
#include <userint.h>
#include "toolbox.h"
#include "msgdemo.h"

/*---------------------------------------------------------------------------*/
/* Module-globals                                                            */
/*---------------------------------------------------------------------------*/
static int g_mainPanelHandle = 0;
static int g_secondPanelHandle = 0;
static int g_mainHWND = 0;
static int g_secondHWND = 0;

/*---------------------------------------------------------------------------*/
/* Internal function prototypes                                              */
/*---------------------------------------------------------------------------*/
int CVICALLBACK MainPnlMsgCallback   (int panelHandle, int message,
                                      unsigned int* wParam,
                                      unsigned int* lParam,
                                      void* callbackData);
int CVICALLBACK SecondPnlMsgCallback (int panelHandle, int message,
                                      unsigned int* wParam,
                                      unsigned int* lParam,
                                      void* callbackData);

/*---------------------------------------------------------------------------*/
/* This is the application's entry-point.                                    */
/*---------------------------------------------------------------------------*/
int __stdcall WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
                       LPSTR lpszCmdLine, int nCmdShow)
{
    int stat = 0;
    int dragAndDropEnabled = 0;
    
    if (InitCVIRTE (hInstance, 0, 0) == 0)  
        return -1; 
    if ((g_mainPanelHandle = LoadPanel (0, "msgdemo.uir", MAINPNL)) < 0)
        return -1; 
    if ((g_secondPanelHandle = LoadPanel (0, "msgdemo.uir", SCNDPNL)) < 0)
        {
        DiscardPanel (g_mainPanelHandle);
        return -1;
        }
    SetPanelAttribute (g_secondPanelHandle, ATTR_SYSTEM_MENU_VISIBLE, 0);
    
    /* Enable Drag-And-Drop notification for our main panel */
    if (stat = EnableDragAndDrop (g_mainPanelHandle))
        {
        MessagePopup ("Error", GetGeneralErrorString (stat));
        dragAndDropEnabled = 0;
        }
    else    
        dragAndDropEnabled = 1;
        
    /* Attach callback functions to two arbitrary message numbers, one for   */
    /* each panel.                                                           */ 
    if (stat = InstallWinMsgCallback (g_mainPanelHandle, 9678,
                                      MainPnlMsgCallback,
                                      VAL_MODE_IN_QUEUE,
                                      NULL, &g_mainHWND))
        {
        MessagePopup ("Error", GetGeneralErrorString (stat));
        g_mainHWND = 0;
        }
    if (stat = InstallWinMsgCallback (g_secondPanelHandle, 9679,
                                      SecondPnlMsgCallback,
                                      VAL_MODE_INTERCEPT, NULL,
                                      &g_secondHWND))
        {
        MessagePopup ("Error", GetGeneralErrorString (stat));
        g_secondHWND = 0;
        }
    
    /* Display the panels and run the GUI -- the app must process events in  */
    /* order to receive Windows messages.                                    */
    DisplayPanel (g_mainPanelHandle);
    DisplayPanel (g_secondPanelHandle);
    RunUserInterface ();
    
    /* Detach the two message callbacks */
    if (g_mainHWND)
        RemoveWinMsgCallback (g_mainPanelHandle, 9678);
    if (g_secondHWND)
        RemoveWinMsgCallback (g_secondPanelHandle, 9679);

    /* Disable DragAndDrop notification for the main panel*/
    if (dragAndDropEnabled)
        DisableDragAndDrop (g_mainPanelHandle);

    /* Clean up and return */
    DiscardPanel (g_mainPanelHandle);
    DiscardPanel (g_secondPanelHandle);
    CloseCVIRTE ();
    return 0;
}

/*---------------------------------------------------------------------------*/
/* This function receives notification when the main panel receives the      */
/* Windows message configured in WT_InstallWinMsgCallback ().                */
/*---------------------------------------------------------------------------*/
int CVICALLBACK MainPnlMsgCallback (int panelHandle, int message,
                                    unsigned int* wParam, unsigned int* lParam,
                                    void* callbackData)
{   
    /* Pay attention if our Windows handle was changed... */
    if (message == EVENT_NEWHANDLE)
        {
        g_mainHWND = *wParam;    
        MessagePopup ("","New handle!!!");
        }
    else
        {
        SetCtrlVal (g_mainPanelHandle, MAINPNL_MESSAGE, message);
        SetCtrlVal (g_mainPanelHandle, MAINPNL_WPARAMRCV, *wParam);
        SetCtrlVal (g_mainPanelHandle, MAINPNL_LPARAMRCV, *lParam);
        }
    return 0;
}

/*---------------------------------------------------------------------------*/
/* This function receives notification when the secondary panel receives the */
/* Windows message configured in WT_InstallWinMsgCallback ().                */
/*---------------------------------------------------------------------------*/
int CVICALLBACK SecondPnlMsgCallback (int panelHandle, int message,
                                      unsigned int* wParam,
                                      unsigned int* lParam, void* callbackData)
{
    /* Pay attention if our Windows handle was changed... */
    if (message == EVENT_NEWHANDLE)
        {
        g_secondHWND = *wParam;  
        MessagePopup ("","New handle!!!");
        }
    else
        {
        SetCtrlVal (g_secondPanelHandle, SCNDPNL_MESSAGE, message);
        SetCtrlVal (g_secondPanelHandle, SCNDPNL_WPARAMRCV, *wParam);
        SetCtrlVal (g_secondPanelHandle, SCNDPNL_LPARAMRCV, *lParam);
        }
    
    /* This callback is executing in intercept mode.  Return non-zero if we  */
    /* want to swallow it before CVI gets it.                                */
    return 0;
}

/*---------------------------------------------------------------------------*/
/* This is a standard panel callback.  After calling WT_EnableDragAndDrop (),*/
/* however, we will receive WT_EVENT_FILESDROPPED whenever the user drops    */
/* files onto the panel.  eventData1 will in this case be a pointer to a     */
/* memory block telling us how many files were dropped as well as their      */
/* full path/filenames.                                                      */
/*---------------------------------------------------------------------------*/
int CVICALLBACK MainPanelCB (int panel, int event, void *callbackData,
                             int eventData1, int eventData2)
{
    int   numFiles = 0;
    int   bitmapID;
    char* filename;
    char** pfilenames;
    
    switch (event)
        {
        case EVENT_CLOSE:
            QuitUserInterface (0);
            break;
        case EVENT_FILESDROPPED:  
            ResetTextBox (g_mainPanelHandle, MAINPNL_FILENAMES, "");
            
            /* Cycle through each file dropped into the panel and display it */
            /* if its a bitmap.                                              */
            if (!(pfilenames = (char**)eventData1))
                /* No files */
                return 0;
            while (filename = pfilenames[numFiles])
                {   
                numFiles++;
                InsertTextBoxLine (g_mainPanelHandle, MAINPNL_FILENAMES,
                                   -1, filename);
          
                /* Is this a bitmap? */
                if (FindPattern (filename, 0, strlen(filename), ".bmp", 0, 0)
                    != -1)
                    {
                    
                    /* Yes, display it */
                    GetBitmapFromFile (filename, &bitmapID);
                    CanvasDrawBitmap (g_mainPanelHandle, MAINPNL_BITMAP, 
                                      bitmapID, VAL_ENTIRE_OBJECT,
                                      VAL_ENTIRE_OBJECT);
                    DiscardBitmap (bitmapID);
                    }
                
                /* Free the memory for this string */
                free (filename);    
                }
            
            /* Free the memory for the list of strings */
            free (pfilenames);    
            SetCtrlVal (g_mainPanelHandle,MAINPNL_NUMDROPPED, numFiles);
            break;
        default:
            break;
        }
    return 0;
}

/*---------------------------------------------------------------------------*/
/* Send a windows message to the second panel, which is waiting for it.      */
/*---------------------------------------------------------------------------*/
int CVICALLBACK SendMessageToSecondCB (int panel, int control, int event,
                                       void *callbackData, int eventData1,
                                       int eventData2)
{
    unsigned int wParam;
    unsigned int lParam;
    
    switch (event)
        {
        case EVENT_COMMIT:
            GetCtrlVal (g_mainPanelHandle, MAINPNL_WPARAMSEND, &wParam);
            GetCtrlVal (g_mainPanelHandle, MAINPNL_LPARAMSEND, &lParam);
            
            /* Send a message using a Windows SDK function */
            SendMessage ((HWND)g_secondHWND, 9679, (WPARAM)wParam,
                         (LPARAM)lParam); 
            break;
        }
    return 0;
}

/*---------------------------------------------------------------------------*/
/* Post a windows message to the main panel, which is waiting for it.        */
/*---------------------------------------------------------------------------*/
int CVICALLBACK PostMessageToMainCB (int panel, int control, int event,
                                     void *callbackData, int eventData1,
                                     int eventData2)
{
    unsigned int wParam;
    unsigned int lParam;
        
    switch (event)
        {
        case EVENT_COMMIT:
            GetCtrlVal (g_secondPanelHandle, SCNDPNL_WPARAMSEND, &wParam);
            GetCtrlVal (g_secondPanelHandle, SCNDPNL_LPARAMSEND, &lParam);
        
            /* Post a message using a Windows SDK function */
            PostMessage ((HWND)g_mainHWND, 9678, wParam, lParam); 
            break;
        }
    return 0;
}

int CVICALLBACK QuitCallback (int panel, int control, int event,
        void *callbackData, int eventData1, int eventData2)
{
    switch (event)
        {
        case EVENT_COMMIT:
            QuitUserInterface (0);
            break;
        }
    return 0;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -