Newbie question, multiple forms.
Hi everybody i am brand new to these forums and to C++ so i am no doubt going to be asking some really stupid questions so i apologise in advance for that. :)
I have a firm background in VB.Net but the market out there is paying out far more much money to C programmers for me to sit by and not try to grab a pieve of the action. I've heard how difficult C++ is to learn so i'm ready for a rough ride.
First off i was wondering if anyone could point me in the direct of some good tutorials. All the ones i've found are for VC++ using the .Net framework. Or are for describing the types and variables etc. With my background i'm quite keen to get stuck in, so a tutorial on creating a simple app with 2 or 3 forms using a button to open each form, or something like that so I can get a look at the code and try to get my head around it.
I appreciate any responses
Thanks and regards.
[930 byte] By [
zeek840] at [2007-11-11 10:22:53]

# 1 Re: Newbie question, multiple forms.
you really, really need to start with commandline text based programs to get a feel for the language. Jumping into an extended form of the language is going to be overwhelming even with your background.
The web is covered in tutorials, some better than others. Just try a search and see what you can find. Any program with the line "using namespace std;" in it is a good start, without this line there is a severe risk that the code is of an older format and possibly a bad example.
jonnin at 2007-11-11 20:59:04 >

# 2 Re: Newbie question, multiple forms.
Thanks for the respose, but after working with VB.net for 5 years and having to read and interpret C# code for some of that time i feel that I cam handle the task of getting straight in. Thanks for the info on std; i'll check it out. For now though if I could ask you one moew question. I have made my first C++ appilcation and for now all it is, is a form with three buttons on it each opening a different form. I understand that each window has it's own Window CallBack routine. But it is the creation of the windows that is confusing me, or rather where they should be created. For example...
In VB.Net you would declare an instance of a form, from anywhere you like and pop it onto the screen. This doesn't seem to be the same for C++ or am i missing something?
Here is an InitApp routine that registers and creates 2 windows then shows one of them..
void InitApp(HINSTANCE hInst)
{
WNDCLASS wc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hbrBackground = (HBRUSH) GetStockObject(LTGRAY_BRUSH);
wc.hInstance = hInst;
wc.hCursor = LoadCursor(NULL,IDC_ARROW);
wc.hIcon = LoadIcon(NULL,IDI_APPLICATION);
wc.lpfnWndProc = (WNDPROC) MainProc;
wc.lpszClassName = "Main";
wc.lpszMenuName = NULL;
wc.style = CS_HREDRAW | CS_VREDRAW;
RegisterClass(&wc);
wc.lpszClassName = "Other";
wc.lpfnWndProc = (WNDPROC) OtherProc;
RegisterClass(&wc);
hwnd = CreateWindow("Main","Main Window",WS_OVERLAPPEDWINDOW,0,0,170,55,0,0,hInst,0);
other = CreateWindow("Other","Other",WS_OVERLAPPEDWINDOW,200,200,200,200,0,0,hInst,0);
ShowWindow(hwnd,SW_SHOW);
UpdateWindow(hwnd);
}
It seems that creating windows here is quite simple but what, would i simply create all the windows I need for my application in this one routine. Even asking that question i find myself saying, 'No, ofcourse not' But i'd just like some clarification.
# 3 Re: Newbie question, multiple forms.
Hi,
Normally you would start out creating your Dialog resource in the dialog editor.
Then, when you create the handlers for your windows, the IDE will ask you whether you want to create a class for your dialog. Answer "yes" and fill in the blanks (name etc). In your class view window (if you have it open) you will see your newly created class. Now you use this class in the same way you would use other classes, i.e. you create objects of this class. You have all the freedom you'd expect to create these object, particularily you can create them as members of other classes as well as as local objects. By calling the DoModal() member of the objects you can display the window.
Example:
// myDialog.h
// this file is created for you by the Wizard
// but amended by you along with the assotiated myDialog.cpp implementation
class CMyDialog : public CDialog
{
// ...
};
// someOther.h
class CSomeOther : public CDialog
{
CMyDialog m_myDialog;
};
// someOther.cpp
// ...
void CSomeOther::someMethod()
{
// here display the Dialog and handle Dialogs events...
m_myDialog.DoModal();
// here the Dialog is dismissed (no longer visible)
// member m_myDialog keeps its state
}
//...
void CSomeOther::asLocalObject()
{
// create a local CMyDialog - object
CMyDialog myLocalDialog;
myLocalDialog.DoModal();
// when asLocalObject() method is exited, the local object is deleted
}
Is that what you were asking?
Cheers,
D
# 4 Re: Newbie question, multiple forms.
Yes your describing OOP it's just a bit different from VB.Net so it's going to take a while to get used to this. It's because classes in vb.net are very simple. For example...
Public Class MyClass
Public Sub New
Dim MyForm as new Form
MyForm.Show
End sub
Forgive me posting VB on this forum :) So as you can see you can create a windows for very easily in VB.net in the above example.
My last query is that the HINSTANCE part of the WNDCLASSEX structure is described on MSDN as ..
Handle to the instance that contains the window procedure for the class.
Where it says handle to the instance does mean the instance of the current application?
HWND hwnd;
HWND other;
HWND bopen;
HINSTANCE g_hInst;
// winmain entry point
int APIENTRY WinMain(HINSTANCE hInst,HINSTANCE hPrev,LPSTR line,int CmdShow)
{
MSG msg;
g_hInst = hInst;
InitApp(hInst);
while(GetMessage(&msg,0,0,0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
Where g_hInst is set here, is that the handle that has to be set in the WNDCLASSEX structure for every window created in the particular application?
Please don't get annoyed with me if i've got that totally wrong. I'm just a bit confused, or am I?
# 5 Re: Newbie question, multiple forms.
Hi,
your example seems to be using the "pedestrian" version of the WinAPI, so the authors of your tutorial probably show this for didactical reasons. In real life you much rather would use the wizards provided to create your dialogs (unless you have a masochistic streak). If you use the wizards, creating and displaying a window is not much more difficult than in VB, although it might take some time to understand the inner workings. Some people will be shouting "Oh no you should rather do the pedestrian way", but if you create nicely layered apps with a MVC/MVP architecture and you only use Dialogs to enter/view data, the wizards will do just fine.
So if I were you, I'd look what VS offers on the side of different project types
(Dialog-based/console apps/DLLs) and create projects, fill in the gaps and concentrate on what is really important in C++:
- allocation/deallocation of memory (constructors/destructors/...)
- inheritance
- templates
- standard template library (STL)
There's a lot to take in when you come from VB ;-)
Cheers,
D
# 6 Re: Newbie question, multiple forms.
Oh, the 'pedestrian' method. I must admit i quite like this method. I've always like using WIN32 API's even in VB.Net so i'll probably carry on playing around. But it's nice to know that C++ has some RAD aspects. I'll check into them.
Cheers mate you've been a great help.
Oh, and yes i know there's a lot to take in. But there comes a time when people should start using a proper language!! :) ;)
Never thought i'd here myself type something like that!!!
# 7 Re: Newbie question, multiple forms.
The H in front of the API types means its a pointer, by the way. (H = Handle = pointer in their lingo). On the CPP side you often have to take a handle to your current window and pass it around, or your current control etc.
jonnin at 2007-11-11 21:05:06 >

# 8 Re: Newbie question, multiple forms.
Oh, the 'pedestrian' method. I must admit i quite like this method. I've always like using WIN32 API's even in VB.Net so i'll probably carry on playing around. But it's nice to know that C++ has some RAD aspects. I'll check into them.
Cheers mate you've been a great help.
Oh, and yes i know there's a lot to take in. But there comes a time when people should start using a proper language!! :) ;)
Never thought i'd here myself type something like that!!!
If you really want a c++ RAD tool then try using C++ Builder. Developing applications with BCB is faster than with VC++. Plus many people say that their VCL librarary is better than MFC. If you learn the VCL you can use CLX which are very similar but provide cross platform support.
Ivan** at 2007-11-11 21:06:05 >
