Post by smc44 on Nov 20, 2008 18:46:26 GMT -5
This tutorial will show you how to make a simple message box using C++(GCC Compiler) , also show you how to use strings or chars, and the last thing it will do is show you how to write a file.
(Before you start make a folder on your desktop called WINAPI, this folder will be for the program)
First do file>new(most IDE's will be the same) then make an empty project then add a new C++ file(be sure to save this all to your WINAPI folder on your dektop)
Copy and paste the code into the empty C++ file!
//WINAPI, Chars, and FileWriting
#include <windows.h>
#include <iostream.h>
#include <fstream.h>
using namespace std;
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
LPSTR lpCmdLine, int nCmdShow)
{
const char *x = "Press ok to write a text file!!";
MessageBox(NULL, x, "Window Caption!", MB_OK | MB_ICONERROR);
if (MB_OK | MB_ICONERROR)
{
ofstream f("FileName.txt");
f << "This is some text written to the text file!" << endl;
f.close();
}
return 0;}
Ok now all of the icnludes are headers(.h = header) that are used to make this code work, the first header is windows.h, this header is used for WINAPI or anything for windows, the second header is iostream.h which can be used for many things but in this case for an output, and the last header file is the one used for wrting files which is fstream.h. Now that you know what the headers are i can tell you how the code actually works.
WINAPI WinMain and everything in the parentheses is part of the code that will allow a message box using windows. On the next line that say const char *x = "Press ok to write a text file!!" this is for a const (which means will stay the same) for text (char is for the text char = character). On the next line that says MessageBox.....etc... this is the code for the message box that will appear when runing this code. NULL in that part means nothing, the x is already declared as a const char so refer to that line of code to see the text that will be there, and the text after that is the caption of the window, and the MB_OK and MB_ICONERROR are used in the message box(there are many more besides MB_OK and MB_ICONERROR, there are ones like MB_YESNOCANCEL or MB_ICONEXCLAMATION). The code that is after the meessage box is used to tell the porgam to write a file when you press the "ok" on the message box all of the code below if in the {} is excuted if you press ok on the messagebox, so ofstream f("text.txt") will be excuted which in this case will be excuted to write a text file, then return (0); will return everthing in the program which means it will tell everthing to return its intitial value.
(I forgot the line of code that says f << "text" << endl;, this code is what will be written in the text file, the reason why the << go to the left is because that means out and you will be writing text to a file out of the porgram, if you wanted to do in you would flip the symbols like this >>, but besides that the endl; justs tells the pogram to end that line and if anymore text is written to that text file it will be wrirten on the next line. Last but not least is the line of coed that says f.close(); this is tells the text file to close when its done being writtten to(stops meomory links thanks nicky
Thats everthing you can mess with that change some text add some text in the text file....have fun
(Before you start make a folder on your desktop called WINAPI, this folder will be for the program)
First do file>new(most IDE's will be the same) then make an empty project then add a new C++ file(be sure to save this all to your WINAPI folder on your dektop)
Copy and paste the code into the empty C++ file!
//WINAPI, Chars, and FileWriting
#include <windows.h>
#include <iostream.h>
#include <fstream.h>
using namespace std;
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
LPSTR lpCmdLine, int nCmdShow)
{
const char *x = "Press ok to write a text file!!";
MessageBox(NULL, x, "Window Caption!", MB_OK | MB_ICONERROR);
if (MB_OK | MB_ICONERROR)
{
ofstream f("FileName.txt");
f << "This is some text written to the text file!" << endl;
f.close();
}
return 0;}
Ok now all of the icnludes are headers(.h = header) that are used to make this code work, the first header is windows.h, this header is used for WINAPI or anything for windows, the second header is iostream.h which can be used for many things but in this case for an output, and the last header file is the one used for wrting files which is fstream.h. Now that you know what the headers are i can tell you how the code actually works.
WINAPI WinMain and everything in the parentheses is part of the code that will allow a message box using windows. On the next line that say const char *x = "Press ok to write a text file!!" this is for a const (which means will stay the same) for text (char is for the text char = character). On the next line that says MessageBox.....etc... this is the code for the message box that will appear when runing this code. NULL in that part means nothing, the x is already declared as a const char so refer to that line of code to see the text that will be there, and the text after that is the caption of the window, and the MB_OK and MB_ICONERROR are used in the message box(there are many more besides MB_OK and MB_ICONERROR, there are ones like MB_YESNOCANCEL or MB_ICONEXCLAMATION). The code that is after the meessage box is used to tell the porgam to write a file when you press the "ok" on the message box all of the code below if in the {} is excuted if you press ok on the messagebox, so ofstream f("text.txt") will be excuted which in this case will be excuted to write a text file, then return (0); will return everthing in the program which means it will tell everthing to return its intitial value.
(I forgot the line of code that says f << "text" << endl;, this code is what will be written in the text file, the reason why the << go to the left is because that means out and you will be writing text to a file out of the porgram, if you wanted to do in you would flip the symbols like this >>, but besides that the endl; justs tells the pogram to end that line and if anymore text is written to that text file it will be wrirten on the next line. Last but not least is the line of coed that says f.close(); this is tells the text file to close when its done being writtten to(stops meomory links thanks nicky
Thats everthing you can mess with that change some text add some text in the text file....have fun