 |

07-10-2010, 10:00 PM
|
|
Member
BANNED
|
|
Join Date: May 2010
Posts: 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 
|

07-11-2010, 05:53 PM
|
|
Member
BANNED
|
|
Join Date: May 2010
Posts: 85
|
|
Re: Making a button create a file
So does anyone know?
|

07-11-2010, 07:02 PM
|
 |
an cat
|
|
Join Date: Jan 2007
Location: [+]
Posts: 3,286
|
|
Re: 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.
__________________
Do you like bitcoins? Do you want to buy or sell bitcoins? PM me or check out this thread.
Ex-programming mod, Ex-market mod, Ex-Head User Educator, Ex-Global Mod
|

07-11-2010, 10:27 PM
|
|
Guru
|
|
Join Date: Jun 2005
Location: Canada
Posts: 1,844
|
|
Re: 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/...leofstream.htm
Hope it helps.
|

10-27-2010, 07:18 AM
|
|
Newcomer
|
|
Join Date: Dec 2009
Posts: 13
|
|
Re: 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;
}
Last edited by Ashken : 12-10-2011 at 09:57 PM.
Reason: code tags & slight modifications
|

10-27-2010, 03:46 PM
|
 |
Hero
|
|
Join Date: Apr 2005
Posts: 7,068
|
|
Re: 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.
Last edited by SMR : 10-27-2010 at 03:52 PM.
|

10-27-2010, 04:10 PM
|
|
Newcomer
|
|
Join Date: Dec 2009
Posts: 13
|
|
Re: 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.
|

10-27-2010, 04:14 PM
|
 |
Hero
|
|
Join Date: Apr 2005
Posts: 7,068
|
|
Re: Making a button create a file
No, that 'beginner' advice/comment was directed to the author of this thread, not you 
|

10-27-2010, 05:10 PM
|
|
Newcomer
|
|
Join Date: Dec 2009
Posts: 13
|
|
Re: 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.
|
 |
|