Making a button create a file

Discussion in 'Programming General' started by Gfxguy, Jul 10, 2010.

Making a button create a file
  1. Unread #1 - Jul 10, 2010 at 5:00 PM
  2. Gfxguy
    Joined:
    May 18, 2010
    Posts:
    85
    Referrals:
    0
    Sythe Gold:
    0

    Gfxguy Member
    Banned

    Making a button create a file

    I want to make a button on a windows form in visual C++ express edition make a batchfile on the users desktop also i want it to ask if they would like this option to happen if you know how to do this i would very much appreciate the response!
    If this isn't possible i would love for you to show me if there is different types of files such as .txt and what not files i can create:p
     
  3. Unread #2 - Jul 11, 2010 at 12:53 PM
  4. Gfxguy
    Joined:
    May 18, 2010
    Posts:
    85
    Referrals:
    0
    Sythe Gold:
    0

    Gfxguy Member
    Banned

    Making a button create a file

    So does anyone know?
     
  5. Unread #3 - Jul 11, 2010 at 2:02 PM
  6. cp
    Joined:
    Jan 30, 2007
    Posts:
    3,278
    Referrals:
    6
    Sythe Gold:
    0

    cp an cat
    Banned

    Making a button create a file

    Why don't you go actually learn basic C++? File writing is something you should know if you actually bothered to learn the language.
     
  7. Unread #4 - Jul 11, 2010 at 5:27 PM
  8. blindkilla
    Joined:
    Jun 22, 2005
    Posts:
    1,896
    Referrals:
    0
    Sythe Gold:
    6
    Discord Unique ID:
    282000633404456960
    Discord Username:
    sogord

    blindkilla Guru
    $25 USD Donor New

    Making a button create a file

    Is this a .NET application? Because if it is I would advise to go with C# instead.

    Here is an example of writting to a file with c++. Where it opens the file "test", you can specify your own filename and extension, so if you're creating a batch file you would use "test.bat".

    http://www.java2s.com/Code/Cpp/File/Writetofileofstream.htm

    Hope it helps.
     
  9. Unread #5 - Oct 27, 2010 at 2:18 AM
  10. Ashken
    Joined:
    Dec 18, 2009
    Posts:
    12
    Referrals:
    0
    Sythe Gold:
    0

    Ashken Newcomer

    Making a button create a file

    To be compiled with visual C++ 2008 and above

    Code:
    
    #include <windows.h>
    #include <shlobj.h>
    #include <fstream>
    #include <cstdlib>
    
    
    using namespace std;
    
    #define BTN 0
    
    char name[]="app";
    char path[MAX_PATH];
    
    
    LRESULT CALLBACK WndProc(HWND,UINT,WPARAM,LPARAM);
    
    HWND hwnd;
    HWND button;
    HINSTANCE hInst;
    ofstream out;
    int file(HWND);
    
    int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR lpstr,int cmd)
    {
    
        WNDCLASSEX wc;
        MSG msg;
    
        wc.hInstance = hInstance;
        wc.lpfnWndProc = WndProc;
        wc.lpszClassName = name;
        wc.style = 0;
        wc.cbSize = sizeof(WNDCLASSEX);
        wc.hIcon = LoadIcon(NULL,IDI_APPLICATION);
        wc.hIconSm = LoadIcon(NULL,IDI_APPLICATION);
        wc.hCursor = LoadCursor(NULL,IDC_ARROW);
        wc.lpszMenuName =0;
        wc.cbClsExtra = 0;
        wc.cbWndExtra = 0;
        wc.hbrBackground =(HBRUSH)(COLOR_BTNFACE+1);
    
        if(!RegisterClassEx(&wc))
        {
        MessageBox(hwnd,"Window Class Registration Failed!\nThis Application Requires Windows NT and above","Error",MB_OK);
        return 1;
        }
    
        hwnd = CreateWindowEx(
                             0,
                             name,
                             "Ashken",
                             WS_OVERLAPPEDWINDOW&~(WS_MAXIMIZEBOX|WS_THICKFRAME),
                             CW_USEDEFAULT,
                             CW_USEDEFAULT,
                             150,
                             100,
                             0,
                             NULL,
                             hInstance,
                             NULL
                             );
        ShowWindow(hwnd,cmd);
    
    
    
        while(GetMessage(&msg,NULL,0,0)>0)
        {
            TranslateMessage(&msg);
            DispatchMessage(&msg);
        }
    
        return msg.wParam;
    }
    LRESULT CALLBACK WndProc(HWND hwnd,UINT message,WPARAM wparam,LPARAM lparam)
    {
        switch(message)
        {
            case WM_CREATE:
            button = CreateWindow("button","OK",BS_PUSHBUTTON|WS_TABSTOP|WS_CHILD|WS_VISIBLE,30,20,80,30,hwnd,(HMENU)BTN,hInst,0);
    
    
            break;
            case WM_COMMAND:
            switch(wparam)
            {
                case BTN:
                file(hwnd);
                break;
            }
    
            break;
            case WM_DESTROY:
            PostQuitMessage(0);
            break;
            default:
            return DefWindowProc(hwnd,message,wparam,lparam);
        }
        return 0;
    }
    
    int file(HWND hwnd)
    {
        SHGetSpecialFolderPath(hwnd,path,CSIDL_DESKTOPDIRECTORY,FALSE);
             
    
        //Name & extension of the file
        char fname[MAX_PATH]="\\file.bat";
    
        strcat(path,fname);
        out.open(path);
    	if(!out.is_open())
    	{
    		MessageBox(hwnd,path,"Could not create File",MB_OK|MB_ICONERROR);
    		return 1;
    	}
    
     //The contents of the File
        out<<"echo This is the file created!!!\n\npause";
        out.close();
    
        MessageBox(hwnd, path, "File Created at", MB_OK | MB_ICONINFORMATION);
    
        ShellExecute(NULL,"open",path,0,0,SW_NORMAL);
    
    	return 0;
    }
    
    
     
  11. Unread #6 - Oct 27, 2010 at 10:46 AM
  12. Govind
    Joined:
    Apr 22, 2005
    Posts:
    7,825
    Referrals:
    13
    Sythe Gold:
    23
    Prove it! Trole Tier 1 Prizebox Tortoise Penis Le Monkey UWotM8? Wait, do you not have an Archer rank? Potamus

    Govind The One Musketeer
    Mudkips Highly Respected Retired Administrator

    Making a button create a file

    Button presses are trapped by comparing (HWND)lParam to the button handle; I've never known wParam to work for it.

    Code:
    case WM_COMMAND:
    if((HWND)lParam==hButton)
    {
    //writefile
    }
    Though honestly if you're a newcomer, stay away from Win32 until you learn the language itself. It's a very steep learning curve.
     
  13. Unread #7 - Oct 27, 2010 at 11:10 AM
  14. Ashken
    Joined:
    Dec 18, 2009
    Posts:
    12
    Referrals:
    0
    Sythe Gold:
    0

    Ashken Newcomer

    Making a button create a file

    Yep wparam does work too.no matter how much we try it is almost impossible to know everything about a particular language or framework.You are right there is still a lot i don't know about C++, especially algorithms but what i know is enough to get me playing with the big boys' toys.
     
  15. Unread #8 - Oct 27, 2010 at 11:14 AM
  16. Govind
    Joined:
    Apr 22, 2005
    Posts:
    7,825
    Referrals:
    13
    Sythe Gold:
    23
    Prove it! Trole Tier 1 Prizebox Tortoise Penis Le Monkey UWotM8? Wait, do you not have an Archer rank? Potamus

    Govind The One Musketeer
    Mudkips Highly Respected Retired Administrator

    Making a button create a file

    No, that 'beginner' advice/comment was directed to the author of this thread, not you :)
     
  17. Unread #9 - Oct 27, 2010 at 12:10 PM
  18. Ashken
    Joined:
    Dec 18, 2009
    Posts:
    12
    Referrals:
    0
    Sythe Gold:
    0

    Ashken Newcomer

    Making a button create a file

    either way it applies to me too!! good advice,i should wind up on C++ lest i shoot myself in the foot.
     
< Steamstealer V 1.0 | Want to learn Java? >

Users viewing this thread
1 guest


 
 
Adblock breaks this site