CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 3 123 LastLast
Results 1 to 15 of 39
  1. #1
    Join Date
    Jul 2011
    Posts
    15

    Incorporating Console Code Into a Win32 GUI Application

    I wrote a program as a C++ console application that reads data from .csv files and then modifies the data and creates a new file with updated data. I want to make this code more user friendly, as a GUI application, so the user can choose the file they wish to select with the typical open file window. I am working on learning how to make a Win32 GUI application, and I am starting to get the hang of it, but I don't know how to incorporate my original code into the application. Is there a way to incorporate my code into an application? If so, how would this be done? I am using Dev C++ editor if that makes a difference. I can't show you my original code, and the code for the application is on a computer that doesn't have internet, so I'll have to post the code later when I can transfer the data... I'm looking forward to solving this problem. I hope someone has the answer...

  2. #2
    Join Date
    Feb 2002
    Posts
    4,640

    Re: Incorporating Console Code Into a Win32 GUI Application

    Copy and paste it into your GUI application. Change the name of the original 'main' to something else, and call it from your GUI code.

    Viggy

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

    Re: Incorporating Console Code Into a Win32 GUI Application

    Quote Originally Posted by tehphoenix View Post
    ...I want to make this code more user friendly, as a GUI application, so the user can choose the file they wish to select with the typical open file window...
    Also, if that is ALL you want, you can call ::GetOpenFileName() from your console app like that:
    Code:
    int _tmain(int argc, _TCHAR* argv[])
    {
    	OPENFILENAME ofn = {0};
    	TCHAR szFile[MAX_PATH] = {0};       // buffer for file name
    
    	ofn.lStructSize = sizeof(ofn);
    	ofn.lpstrFile = szFile;
    	ofn.nMaxFile = sizeof(szFile);
    	ofn.lpstrFilter = _T("All\0*.*\0Text\0*.TXT\0");
    	ofn.nFilterIndex = 1;
    	ofn.Flags = OFN_PATHMUSTEXIST | OFN_FILEMUSTEXIST;
    
    	// Display the Open dialog box. 
    	HANDLE hf(0); 
    
    	if (GetOpenFileName(&ofn)==TRUE) 
    		hf = CreateFile(ofn.lpstrFile, 
    			GENERIC_READ,
    			0,
    			(LPSECURITY_ATTRIBUTES) NULL,
    			OPEN_EXISTING,
    			FILE_ATTRIBUTE_NORMAL,
    			(HANDLE) NULL);
    
    	return 0;
    }
    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...

  4. #4
    Join Date
    Jul 2011
    Posts
    15

    Re: Incorporating Console Code Into a Win32 GUI Application

    To VladimirF, I tried incorporating the code you posted into my console application program, but it said that none of the capital letter commands were defined, which they weren't... The place in my code where I need to incorporate the OPENFILE operation is within a seperate function, not in the main function, if that helps. Also, you called your function _tmain, with arguments of int argc, and _TCHAR* argv[]. What does this naming convention do, and what do the arguments do?

    To MrViggy, I'm not sure if putting my original code into the application will be feasable since the entirity of the code is about 1000 lines, and involves 4 files. But, if it is possible to incorporate it, I'd like to incorporate it in my message structure in the WindowProcedure function because that's where the code for using the dropdown menu for File -> Open. So, are you saying that I copy and paste all of my code from the four files somewhere in the middle of my GUI application code and rename the main function and leave all of the other function names and class declarations the same? If so, does it matter where in the code I place it?

    Thanks for your posts guys, it's really helping me look at this problem in a new way. The problem still isn't resolved, but you're definately leading me in the right direction. Looking forward to hearing from you again.

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

    Re: Incorporating Console Code Into a Win32 GUI Application

    Quote Originally Posted by tehphoenix View Post
    To VladimirF, I tried incorporating the code you posted into my console application program, but it said that none of the capital letter commands were defined, which they weren't...
    Please post the exact error message.
    The place in my code where I need to incorporate the OPENFILE operation is within a seperate function, not in the main function, if that helps.
    Nothing stops you from calling the function anywhere you want to call it.
    Also, you called your function _tmain, with arguments of int argc, and _TCHAR* argv[]. What does this naming convention do, and what do the arguments do?
    _tmain() is the entry point for all C/C++ based programs written in Visual C++. No different than main() for regular C/C++ apps. As to the arguments, they mean the same thing as they do here:
    Code:
    int main(int argc, char *argv[])
    argc is the number of command-line arguments, and argv are the actual arguments. If you are using a C++ book, then this should be explained (probably very early in chapter 1).
    To MrViggy, I'm not sure if putting my original code into the application will be feasable since the entirity of the code is about 1000 lines, and involves 4 files. But, if it is possible to incorporate it,
    I don't understand -- all you want to do is open a dialog and select a file. You have a function that does this, so just call the function. I don't see what the issue is.
    Code:
    #include <windows.h>
    
    void OpenUpAFileDialog()
    {
    	OPENFILENAME ofn = {0};
    	TCHAR szFile[MAX_PATH] = {0};       // buffer for file name
    
    	ofn.lStructSize = sizeof(ofn);
    	ofn.lpstrFile = szFile;
    	ofn.nMaxFile = sizeof(szFile);
    	ofn.lpstrFilter = _T("All\0*.*\0Text\0*.TXT\0");
    	ofn.nFilterIndex = 1;
    	ofn.Flags = OFN_PATHMUSTEXIST | OFN_FILEMUSTEXIST;
    
    	// Display the Open dialog box. 
    	HANDLE hf(0); 
    
    	if (GetOpenFileName(&ofn)==TRUE) 
    		hf = CreateFile(ofn.lpstrFile, 
    			GENERIC_READ,
    			0,
    			(LPSECURITY_ATTRIBUTES) NULL,
    			OPEN_EXISTING,
    			FILE_ATTRIBUTE_NORMAL,
    			(HANDLE) NULL);
    
    	return 0;
    }
    
    
    void SomeOtherFunction()
    {
        // do stuff
        OpenUpAFileDialog();
       // do more stuff
    }
    
    int main()
    {
        OpenUpAFileDialog();
        SomeOtherFunction();
    }
    Create a brand new console project. Take the code above. Compile and run it -- see how it works. Then incorporate what you now have learned in your larger program.

    Regards,

    Paul McKenzie
    Last edited by Paul McKenzie; July 6th, 2011 at 05:17 PM.

  6. #6
    Join Date
    Mar 2010
    Location
    Melbourne Australia
    Posts
    454

    Re: Incorporating Console Code Into a Win32 GUI Application

    IF you want to Develop GUI for your CSV file reader , then head over to either WxWidgets or QT ,
    you will be able to develop a smart looking GUI with only few lines of Code. In this day and age mucking around with Low level API ( win32 ) is only required when developing something original which these C++ toolkits do not cover . Otherwise there is not point reinventing the wheel.

  7. #7
    Join Date
    Jul 2011
    Posts
    15

    Re: Incorporating Console Code Into a Win32 GUI Application

    I found one error in my implementation of the code that was posted... I didn't #include <windows.h>. But, I still get an error when compiling the code in a seperate project:
    [Linker error] endefined reference to 'GetOpenFileNameA@4'
    Also, the open file window never comes up. I suspect this is because this is a console application, and using Win32 code doesn't typically work too well in console applications from my experience...
    To explain, Paul McKenzie, the issue is that I don't want a console application, I want a GUI application. But, my working program is written as a console application, and I want that code to be incorporated into my GUI application, hence the title of the thread... Also, the point of using OPENFILENAME is to get the path, or filename of the file that the user wants to use, right? I want to get the path to the file, open the file using an ifstream (the open function of which takes a C string as an argument), and then manipulate the file. Honestly, keeping this program as a console application is a fall back. I really want to have it as GUI application.

    aamir121a, what do you mean by "head over to either WxWidgets or QT"? I am unfamiliar with these terms, are they websites, GUI languages, GUI application creation software??? I still don't know how to incorporate my code into a GUI application... I don't know if I've said "GUI" enough, so I suppose I'll say it one more time... GUI!
    I feel like I'm getting close, thanks for the help, I'm really looking forward to resolving this issue...

  8. #8
    Join Date
    Jul 2011
    Posts
    15

    Re: Incorporating Console Code Into a Win32 GUI Application

    If I compile the code Paul McKenzie posted as a Win32 GUI, it compiles and runs, and brings up an open file window, but I don't know how to use the filename, or whether it is actually getting a filename. Can someone tell me how I can use the path generated by the OPENFILENAME to open a file using ifstream? Also, the code brings up an open file window 4 times, even though I'm pretty sure there are only two calls... Why is this?

  9. #9
    Join Date
    May 2007
    Location
    Scotland
    Posts
    1,164

    Re: Incorporating Console Code Into a Win32 GUI Application

    Are you using the debugger provided to you by Visual Studio? If not, why not? If so, try setting a break point on the line of the OpenUpAFileDialog function that says

    Code:
    return 0;
    which should incidently just be

    Code:
    return;
    and take a look at the contents szFile either directly or via ofn.lpstrFile. What do you see?

    The point is, debuggers are very useful to help show you what is going on.

  10. #10
    Join Date
    Jul 2011
    Posts
    15

    Re: Incorporating Console Code Into a Win32 GUI Application

    I am not using the debugger provided by Visual Studio because I don't have Visual Studio, and I don't like it very much. I am using Dev C++. I already changed the return 0; to just return. How do I take a look at szFile or ofn.lpstrFile? Is this a feature of the debugger?

  11. #11
    Join Date
    May 2007
    Location
    Scotland
    Posts
    1,164

    Re: Incorporating Console Code Into a Win32 GUI Application

    Yes, you should be able to set a break point and then interogate the various objects in the current scope by hovering over the object name and/or looking at a watch window.

    I am a fan of writing portable cross platform code. In the attempt to save a headache or two I went through a phase of trying to use cross-platform IDEs (Eclipse/Code Blocks/Code Lite), but I soon came to realise that under Windows, Visual Studio currently offers the best IDE for C++ development - the debugger is exceptional and the IDE is very well layed out. There are things that I don't like about Visual Studio, the compiler in particular, which allows you to get away with non-standard coding. For example. it allows you to

    1) leave off the typename keyword for typedefs that use template types where the template parameters are as yet undetermined
    2) specialise member functions of template classes, without fully specialising the class template part of the function specialisation.

    and there are many more. It would also be nice if, aswell as project and solution files, the IDE could make use of Makefiles. However, even so Visual Studio is well thought out and very capable. I have come to the point where I develop using Visual Studio and then verify the correctness of code using GCC (this can be done as a post-build step).

    I personally, have yet to come accross a C++ IDE on any platform that can rival the debugging capabilities and the ease of use of Visual Studio. Out of curiosity, why don't you like it?

  12. #12
    Join Date
    Jul 2011
    Posts
    15

    Re: Incorporating Console Code Into a Win32 GUI Application

    I haven't used Visual Studio extensively, and I know I'll have to get used to it eventually because of it's awesome features, but i've been putting this off. I don't like it because it creates tons of extraneous files, and the process for compiling and running is painful compared to Dev C++ where it is very user-friendly and has one button (F9) that compiles and runs... I've been spoiled with Dev and don't want to use Visual... Anyhow, that's my reasoning...

    In other news, I still can't figure out how to incorporate my original code into a GUI application... Unfortunately, I have to use only the software I have right now because the computer I'm using to code has tight restrictions and no internet, and the computer I'm using for the internet has way tighter restrictions (I can pretty much only use the internet with almost no downloading capability... I can't even use the calculator!) and deletes all of my work when I log off...

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

    Re: Incorporating Console Code Into a Win32 GUI Application

    Quote Originally Posted by tehphoenix View Post
    If I compile the code Paul McKenzie posted as a Win32 GUI, it compiles and runs, and brings up an open file window, but I don't know how to use the filename,
    The job of the open file name dialog is for the user to select a file name, and then for you to retrieve the name. Please read the documentation:

    http://msdn.microsoft.com/en-us/libr...(v=vs.85).aspx
    http://msdn.microsoft.com/en-us/libr...(v=vs.85).aspx
    Parameters
    lpofn [in, out]

    LPOPENFILENAME

    A pointer to an OPENFILENAME structure that contains information used to initialize the dialog box. When GetOpenFileName returns, this structure contains information about the user's file selection.
    The open file name dialog doesn't open files -- again, it allows to display a user-friendly, GUI interface so that the user can select a file, and you have to do something with the returned name that the user selected. What that something is, that's up to you. If you want to really open the file, then open the file:
    Code:
    std::ifstream infile(the_name_the_user_selected_from_the_open_dialog);
    Regards,

    Paul McKenzie
    Last edited by Paul McKenzie; July 7th, 2011 at 12:06 PM.

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

    Re: Incorporating Console Code Into a Win32 GUI Application

    Quote Originally Posted by tehphoenix View Post
    I am not using the debugger provided by Visual Studio because I don't have Visual Studio, and I don't like it very much. I am using Dev C++. I already changed the return 0; to just return. How do I take a look at szFile or ofn.lpstrFile? Is this a feature of the debugger?
    You cannot write non-trivial C++ programs without some sort of debugger. It isn't optional, it's mandatory to have a debugger available.

    Secondly, Dev-C++ is an IDE, not a compiler. The compiler that Dev-C++ uses is the gcc compiler. You will see that Dev-C++ has a debugger available. The debugger is really the gdb debugger. So again, no reason to not use a debugger.

    Having said this, Dev-C++ IDE has not been updated in years. You should get CodeBlocks IDE, as that also uses gcc and gdb under the hood.

    Regards,

    Paul McKenzie

  15. #15
    Join Date
    Jul 2011
    Posts
    15

    Re: Incorporating Console Code Into a Win32 GUI Application

    Thank you for the links to the documentation, I'm sure that will be really helpful. Knowing how something works typically allows for better code...
    So, Paul McKenzie... Why is there so much venom in you voice? What did I do to make you so frustrated? It seems to me like you are looking for mistakes in my wording just to criticize me... I don't remember ever saying that Dev C++ was a compiler, I know that it is not... Secondly, since when has having a debugger available been "mandatory"? I'm pretty sure programmers have been doing just fine without them for decades... I'm sure they're a great tool to use, and I'd like to learn how to use the debugger offered by Dev C++ or another IDE... And, as I said earlier, I am limited to working with the software I have available, so getting CodeBlocks IDE isn't really an option... Now, I may have misinterpreted you words as attacking me when they weren't... I'm sure you must enjoy helping people with code since by my calculations you have been posting an average of 5 to 6 times a day for the past 12 years or so... Venomous or not, your posts are pretty helpful, and I'm very thankful.

Page 1 of 3 123 LastLast

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