• Log in or sign up for an account here: https://www.sythe.org/login

Making a button create a file

Gfxguy

Member
Banned
Joined
May 18, 2010
Sythe Gold
0
Messages
85
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
 
Making a button create a file
So does anyone know?
 
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.
 
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.
 
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;
}
 
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.
 
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.
 
Making a button create a file
No, that 'beginner' advice/comment was directed to the author of this thread, not you :)
 
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.
 
Runescape Gamblingrunescape gambling
Back
Top