CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 12 of 12

Thread: My own "editor"

  1. #1
    Join Date
    Jul 2012
    Posts
    24

    My own "editor"

    Hi guys,

    We got a piece of HW from National Instruments, which allows us to communicate through PC with an external device connected to the NI HW. We want to make some sort of program - editor, which will allow us to make our own GUI - buttons, timers, edit boxes etc. to control the device or make some testing HW. The source code will be untouchable by the user, the user will be only able to open the app and into the client area add some predefined elements like buttons etc. and program these buttons. So he will program his own source code for his project somewhere in the background (not changing the source code of the app).

    Example:
    We got a basic app, with a white client area. When I click the RMB, some sort of menu will appear and from that menu I'll be able to choose, which element to add - button, edit box, other predefined element etc. and then I'll be able to program this button somewhere in my own *.c files and header files and then compile them and add them to the project, so the button does, what the person wants, independently of the application, which can be understood as only a developing SW. So the person doesn't need to understand the win32 and all around it, he will just program the function of the elements.

    What part of win32 should I study for this? Any suggestions, how to make such a piece of SW? Don't bother that this is an app for a professional company to make. We just want to make some simple app. and we expect, that there will be alot of limitations for the users from the beggining.

  2. #2
    Join Date
    Jul 2012
    Posts
    24

    Re: My own "editor"

    Ok, here is my approach, see code:

    Code:
    #include <windows.h>
    #include "MyFce.h"
    
    //#define DEBUG
    
    /*  Declare Windows procedure  */
    LRESULT CALLBACK WindowProcedure (HWND, UINT, WPARAM, LPARAM);
    
    /*  Make the class name into a global variable  */
    char szClassName[ ] = "WindowsApp";
    
    int WINAPI WinMain (HINSTANCE hThisInstance,
                        HINSTANCE hPrevInstance,
                        LPSTR lpszArgument,
                        int nFunsterStil)
    
    {
        HWND hwnd;               /* This is the handle for our window */
        MSG messages;            /* Here messages to the application are saved */
        WNDCLASSEX wincl;        /* Data structure for the windowclass */
    
        /* The Window structure */
        wincl.hInstance = hThisInstance;
        wincl.lpszClassName = szClassName;
        wincl.lpfnWndProc = WindowProcedure;      /* This function is called by windows */
        wincl.style = CS_DBLCLKS;                 /* Catch double-clicks */
        wincl.cbSize = sizeof (WNDCLASSEX);
    
        /* Use default icon and mouse-pointer */
        wincl.hIcon = LoadIcon (NULL, IDI_APPLICATION);
        wincl.hIconSm = LoadIcon (NULL, IDI_APPLICATION);
        wincl.hCursor = LoadCursor (NULL, IDC_ARROW);
        wincl.lpszMenuName = NULL;                 /* No menu */
        wincl.cbClsExtra = 0;                      /* No extra bytes after the window class */
        wincl.cbWndExtra = 0;                      /* structure or the window instance */
        /* Use Windows's default color as the background of the window */
        wincl.hbrBackground = (HBRUSH) COLOR_BACKGROUND;
    
        /* Register the window class, and if it fails quit the program */
        if (!RegisterClassEx (&wincl))
            return 0;
    
        /* The class is registered, let's create the program*/
        hwnd = CreateWindowEx (
               0,                   /* Extended possibilites for variation */
               szClassName,         /* Classname */
               "Windows App",       /* Title Text */
               WS_OVERLAPPEDWINDOW, /* default window */
               CW_USEDEFAULT,       /* Windows decides the position */
               CW_USEDEFAULT,       /* where the window ends up on the screen */
               544,                 /* The programs width */
               375,                 /* and height in pixels */
               HWND_DESKTOP,        /* The window is a child-window to desktop */
               NULL,                /* No menu */
               hThisInstance,       /* Program Instance handler */
               NULL                 /* No Window Creation data */
               );
    
        /* Make the window visible on the screen */
        ShowWindow (hwnd, nFunsterStil);
    
        /* Run the message loop. It will run until GetMessage() returns 0 */
        while (GetMessage (&messages, NULL, 0, 0))
        {
            /* Translate virtual-key messages into character messages */
            TranslateMessage(&messages);
            /* Send message to WindowProcedure */
            DispatchMessage(&messages);
        }
    
        /* The program return-value is 0 - The value that PostQuitMessage() gave */
        return messages.wParam;
    }
    
    
    /*  This function is called by the Windows function DispatchMessage()  */
    
    LRESULT CALLBACK WindowProcedure (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
    {
        static HWND         hwndButtons[10];
        static int          cxChar, cyChar, cBtn = 0;
        int                 i;
        
        switch (message)                  /* handle the messages */
        {
    /*---------------------------- WM_COMMAND ------------------------------------*/
            case WM_COMMAND:
                switch(LOWORD(wParam)){
                    case 0:
                    case 1:
                    case 2:
                    case 3:
                    case 4:
                    case 5:
                    case 6:
                    case 7:
                    case 8:
                    case 9:
                        if(LOWORD(wParam) < 4)
                            UserButtonFce();
    //                        MessageBox(NULL, "XXXXX", NULL, 0);
                        if(LOWORD(wParam) > 3)
    //                        MessageBox(NULL, "AAAAA", NULL, 0);
                        return 0;
                    
                }
                return 0;
    /*----------------------------------------------------------------------------*/
    
            case WM_CREATE:
                cxChar = LOWORD(GetDialogBaseUnits());
                cyChar = HIWORD(GetDialogBaseUnits());
                
                for(i = 0; i < 10; i++){
                    hwndButtons[i] = CreateWindowEx(
                        0,
                        TEXT("button"),
                        TEXT("DEFAULT NAME"),
                        WS_CHILD | BS_DEFPUSHBUTTON,
                        0, 0,
                        20*cxChar, 7*cyChar/4,
                        hwnd, (HMENU) i,
                        ((LPCREATESTRUCT) lParam) -> hInstance,
                        NULL
                    );
                }
                return 0;
                
            case WM_LBUTTONDOWN:
                #ifdef DEBUG
                MessageBox(NULL, "MB clicked", NULL, 0);
                #endif
                if(cBtn < 10){
                    ShowWindow(hwndButtons[cBtn], SW_SHOW);
                    MoveWindow(hwndButtons[cBtn], LOWORD(lParam), HIWORD(lParam), 20*cxChar, 7*cyChar/4, TRUE);
                    cBtn++;
                }
                return 0;
            
            case WM_DESTROY:
                PostQuitMessage (0);       /* send a WM_QUIT to the message queue */
                break;
            default:                      /* for messages that we don't deal with */
                return DefWindowProc (hwnd, message, wParam, lParam);
        }
    
        return 0;
    }
    
    MyFce.h file:
    
    /* User defined functions used in main.c*/
    
    
    BOOL UserButtonFce(void);
    
    BOOL UserButtonFce(void){
        MessageBox(NULL, "Externi fce", NULL, 0);
        return TRUE;
    }
    I can create the button with a mouse click into the client area. And then I can program this button in a different file and then include it in my code. I know, that there are a lof of mistakes, but thats the first try, if possible and how to. Any suggestions, what should I care, or how should it be done etc? Btw. the program needs to be compiled offline, before the changes take place. Is it possible to do this dynamically? Are *.dll libraries the right way or how should this be done?
    Last edited by HellMaster22; August 17th, 2012 at 06:01 AM.

  3. #3
    Join Date
    Apr 1999
    Posts
    27,449

    Re: My own "editor"

    Quote Originally Posted by HellMaster22 View Post
    Ok, here is my approach, see code:
    If you used code tags, then possibly others will actually read the code. As it is now, it's almost impossible to read as it is unformatted.

    Secondly, what you describe is similar to a dialog or resource editor. Only if you're well-versed in the Windows API should you undertake such a task, as this is not trivial.
    We just want to make some simple app. and we expect, that there will be alot of limitations for the users from the beggining
    The issue with this type of approach is that once you need to make enhancements, you've coded yourself into a corner and need to throw away most of the code. You should start out with something that is well-designed so that it is easier to add to it instead of something that is "quick and dirty".

    Regards,

    Paul McKenzie
    Last edited by Paul McKenzie; August 17th, 2012 at 05:51 AM.

  4. #4
    Join Date
    Jul 2012
    Posts
    24

    Re: My own "editor"

    Quote Originally Posted by Paul McKenzie View Post
    If you used code tags, then possibly others will actually read the code. As it is now, it's almost impossible to read as it is unformatted.
    Paul McKenzie
    Sorry for that, it should be fixed. :-)

    The issue with this type of approach is that once you need to make enhancements, you've coded yourself into a corner and need to throw away most of the code. You should start out with something that is well-designed so that it is easier to add to it instead of something that is "quick and dirty".
    Ok, I get it and as I mentioned, this was just a try. But can you help, how should this be done? Is there some "tutorial" or inspiration, how to make such a resource/dialog editor?
    Last edited by HellMaster22; August 17th, 2012 at 06:12 AM.

  5. #5
    Join Date
    Jul 2005
    Location
    Netherlands
    Posts
    2,042

    Re: My own "editor"

    Quote Originally Posted by HellMaster22 View Post
    Ok, I get it and as I mentioned, this was just a try. But can you help, how should this be done? Is there some "tutorial" or inspiration, how to make such a resource/dialog editor?
    The editor is only a front end. You should start by thinking about how the back-end works. How do you define an arbitrary collection of user interface controls? How do you link actions performed on these controls to arbitrary pieces of code? I think using C to code the user interface actions is a bad choice. Using a scripting language like Lua, Python or Ruby is probably much easier.
    Cheers, D Drmmr

    Please put [code][/code] tags around your code to preserve indentation and make it more readable.

    As long as man ascribes to himself what is merely a posibility, he will not work for the attainment of it. - P. D. Ouspensky

  6. #6
    Join Date
    Jul 2012
    Posts
    24

    Re: My own "editor"

    Quote Originally Posted by D_Drmmr View Post
    How do you define an arbitrary collection of user interface controls? How do you link actions performed on these controls to arbitrary pieces of code? I think using C to code the user interface actions is a bad choice. Using a scripting language like Lua, Python or Ruby is probably much easier.
    This is something, what bothers me. How to link the controls in a running program.
    So u suggest not to use the win32 for GUI and then try to link the controls with an arbitrary code? I should use the languages you mentioned?

  7. #7
    Join Date
    Jul 2005
    Location
    Netherlands
    Posts
    2,042

    Re: My own "editor"

    Quote Originally Posted by HellMaster22 View Post
    So u suggest not to use the win32 for GUI and then try to link the controls with an arbitrary code? I should use the languages you mentioned?
    If you want to have a program with a GUI, then you need to use some GUI library, be it Win32, MFC, Qt, wxWidgets or whatever. You need to be able to draw buttons, respond to mouse clicks, etc. You can do all that with the GUI library you choose.

    The next step will be to create and place controls based on some specification that is not known until runtime. This should be pretty straightforward.
    Finally, you need to execute some script or function (also not known until runtime) when a button is clicked, etc. For this, you will first have to catch the button being clicked with the GUI framework you've chosen and then implement a mechanism with which you can execute some script or function defined at runtime. It's possible to do this by calling some functions in a delay-loaded DLL, but that can be pretty hard to get right, depending on what the code has to do exactly. It's also possible to use some scripting language in which you define some functions that can be called by your C framework when a button is clicked, etc. This code could then call some functions that are exposed by your C framework to do something useful.
    Cheers, D Drmmr

    Please put [code][/code] tags around your code to preserve indentation and make it more readable.

    As long as man ascribes to himself what is merely a posibility, he will not work for the attainment of it. - P. D. Ouspensky

  8. #8
    Join Date
    Aug 2000
    Location
    New York, NY, USA
    Posts
    5,656

    Re: My own "editor"

    Quote Originally Posted by HellMaster22 View Post
    Example:
    We got a basic app, with a white client area. When I click the RMB, some sort of menu will appear and from that menu I'll be able to choose, which element to add - button, edit box, other predefined element etc. and then I'll be able to program this button somewhere in my own *.c files and header files and then compile them and add them to the project, so the button does, what the person wants, independently of the application, which can be understood as only a developing SW. So the person doesn't need to understand the win32 and all around it, he will just program the function of the elements.
    So, the user will write the code to perform some HW interaction, and you will provide the button that calls his function?
    Not a lot of value, in my opinion.
    Vlad - MS MVP [2007 - 2012] - www.FeinSoftware.com
    Convenience and productivity tools for Microsoft Visual Studio:
    FeinWindows - replacement windows manager for Visual Studio, and more...

  9. #9
    Join Date
    Jul 2012
    Posts
    24

    Re: My own "editor"

    Quote Originally Posted by VladimirF View Post
    So, the user will write the code to perform some HW interaction, and you will provide the button that calls his function?
    Not a lot of value, in my opinion.
    This was just an idea. We are still in progress, what we need and how should it look. The main task is to simplify the programming to the "end" user, which is interested only in setting some timers to send a message on the bus and run the program. But the person is not interested in solving things like how the timer should be programmed, that he needs to initialize the bus, make some variables around it etc. He only wants to call a function timer(time, message), set a time and the message and thats all or something like that. Set as many timers he wants and set other functions he needs to, which are needed. Right now we are solving, how could it be done. Even if it is possible.

    Lets say he will open an editor, add there two boxes, one representing his device and one representing some source of messages and actions done by the program. These two devices will be connected with a bus (the user chooses lets say serial communication). When he opens the controlling box, some sort of editor will open and allow the person to set call the predefined functions he needs. This will be then compiled and then he just pushes a start button to run, what he made and the device will be controlled through this. But the end user won't have to program the bus, the communication between these two devices, he just chooses, what to perform. Hopefully is it more clear. I'm sorry for maybe a wrong describing.
    Last edited by HellMaster22; August 18th, 2012 at 06:20 AM.

  10. #10
    Join Date
    Jul 2012
    Posts
    24

    Re: My own "editor"

    Quote Originally Posted by D_Drmmr View Post
    If you want to have a program with a GUI, then you need to use some GUI library, be it Win32, MFC, Qt, wxWidgets or whatever. You need to be able to draw buttons, respond to mouse clicks, etc. You can do all that with the GUI library you choose.

    The next step will be to create and place controls based on some specification that is not known until runtime. This should be pretty straightforward.
    Finally, you need to execute some script or function (also not known until runtime) when a button is clicked, etc. For this, you will first have to catch the button being clicked with the GUI framework you've chosen and then implement a mechanism with which you can execute some script or function defined at runtime. It's possible to do this by calling some functions in a delay-loaded DLL, but that can be pretty hard to get right, depending on what the code has to do exactly. It's also possible to use some scripting language in which you define some functions that can be called by your C framework when a button is clicked, etc. This code could then call some functions that are exposed by your C framework to do something useful.
    This is what I needed to know. Thanks for that. The scripting language will be for the "end" user, who will just call the functions he needs and set some parameters. Can you describe more in detail, how this can be done? The linking between the GUI in win32 and the scripting language? Where can I get more informations about it or what should I look for?

  11. #11
    Join Date
    Jul 2005
    Location
    Netherlands
    Posts
    2,042

    Re: My own "editor"

    Quote Originally Posted by HellMaster22 View Post
    This is what I needed to know. Thanks for that. The scripting language will be for the "end" user, who will just call the functions he needs and set some parameters. Can you describe more in detail, how this can be done? The linking between the GUI in win32 and the scripting language? Where can I get more informations about it or what should I look for?
    I'd suggest to have a look at Lua first. It should be relatively easy to use with C. Just go through the online tutorial and you'll probably get a good idea of what's possible. Then try it out yourself to know how it works for sure, before you start to really design what you want to achieve. I'd save the implementation of a GUI front-end for later, though you can consider it already in your design of course.
    Cheers, D Drmmr

    Please put [code][/code] tags around your code to preserve indentation and make it more readable.

    As long as man ascribes to himself what is merely a posibility, he will not work for the attainment of it. - P. D. Ouspensky

  12. #12
    Join Date
    Jul 2012
    Posts
    24

    Re: My own "editor"

    Quote Originally Posted by D_Drmmr View Post
    I'd suggest to have a look at Lua first. It should be relatively easy to use with C. Just go through the online tutorial and you'll probably get a good idea of what's possible. Then try it out yourself to know how it works for sure, before you start to really design what you want to achieve. I'd save the implementation of a GUI front-end for later, though you can consider it already in your design of course.
    Ok, thanks for showing me the direction. I'll dig deeper and then come back later with another questions. :-)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured