Archived

This topic is now archived and is closed to further replies.

Alim Doener

Problem with C++Builder 6!

Recommended Posts

I've created a form and a button with C++Builder 6. When you press this button a new window should be opened, but everytime I execute it there's an error. ///////////////////////////////////////////////////////////////////////////// /////////////////////Here's the code of the Header-File////////////////////// /////////////////////////////////////////////////////////////////////////////
//---------------------------------------------------------------------------


#ifndef Unit1H
#define Unit1H
//---------------------------------------------------------------------------

#include <Classes.hpp>
#include <Controls.hpp>
#include <StdCtrls.hpp>
#include <Forms.hpp>


HWND            hWnd=NULL;
HINSTANCE hInstance = NULL;
//---------------------------------------------------------------------------

class TForm1 : public TForm
{
__published:	// Von der IDE verwaltete Komponenten

        TButton *Button1;
        void __fastcall Button1Click(TObject *Sender);
private:	// Anwender-Deklarationen

public:		// Anwender-Deklarationen

        __fastcall TForm1(TComponent* Owner);
};
//---------------------------------------------------------------------------

extern PACKAGE TForm1 *Form1;
//---------------------------------------------------------------------------

#endif
[ /source]

/////////////////////////////////////////////////////////////////////////////

////////////////and here's the code of the *.cpp-File////////////////////////

/////////////////////////////////////////////////////////////////////////////


[ source]
void __fastcall TForm1::Button1Click(TObject *Sender)
{
 WNDCLASS	wc;		        // Windows class structure

 DWORD		dwExStyle;              // Window extended style

 DWORD		dwStyle;                // Window style

 RECT		WindowRect;             // Grabs rctangle upper left / lower right values

 WindowRect.left = (long)0;              // Set left value to 0

 WindowRect.right = (long)400;		// Set right value to requested width

 WindowRect.top = (long)0;               // Set top value to 0

 WindowRect.bottom = (long)400;       // Set bottom value to requested height


 	hInstance               = GetModuleHandle(NULL);		// Grab an instance for our window

	wc.style                = CS_HREDRAW | CS_VREDRAW | CS_OWNDC;   // Redraw on size, and own DC for window

  //    wc.lpfnWndProc          = (WNDPROC) WndProc;			// WndProc handles messages

	wc.cbClsExtra           = 0;					// No extra window data

	wc.cbWndExtra           = 0;					// No extra window data

	wc.hInstance            = hInstance;				// Set the Instance

	wc.hIcon                = LoadIcon(NULL, IDI_WINLOGO);		// Load the default icon

	wc.hCursor              = LoadCursor(NULL, IDC_ARROW);		// Load the arrow pointer

	wc.hbrBackground        = NULL;					// No background required for GL

	wc.lpszMenuName		= NULL;					// We don't want a menu

	wc.lpszClassName	= "OpenGL";				// Set the class name


	if (!RegisterClass(&wc))					// Attempt to register the window class

	{
		MessageBox(NULL,"Failed to register the window class.","Error",MB_OK|MB_ICONEXCLAMATION);

	}
             if(!(hWnd = CreateWindowEx(WS_EX_APPWINDOW | WS_EX_WINDOWEDGE,          // Extended Style For The Window

                "OpenGL",				// Class name

		"Open GL Fenster",			// Window title

		WS_OVERLAPPEDWINDOW |		      	// Defined window style

	      	WS_CLIPSIBLINGS |			// Required window style

	    	WS_CLIPCHILDREN,			// Required window style

		0, 0,					// Window position

		(long) 640,                             	// Calculate window width

		(long) 480,	                                // Calculate window height

		NULL,					// No parent window

		NULL,					// No menu

		hInstance,				// Instance

		NULL)))					// Dont pass anything to WM_CREATE


        {

		MessageBox(NULL,"Konnte Fenster nicht initialisieren","ERROR",MB_OK|MB_ICONEXCLAMATION);
	}

}
//---------------------------------------------------------------------------

[ /source]

Share this post


Link to post
Share on other sites
I don''t want to ask, but I will anyway: why not just create another form and use that ? Why are you using API calls instead of doing things the obvious way?

Also, put code in [ source]blahblah[ /source] (without the extra spaces).

Share this post


Link to post
Share on other sites
Later I want to initialize a Window for OpenGL-use. Works it with a form instead of a Window too?
Could U give me a little example for this?
But how can I get the handle of this form?

Should I create the form when the programm will be executed or should i hide it until the button is pressed?


Share this post


Link to post
Share on other sites
I can't give complete answers here since I don't have access to BC++ Builder. However... yes, a form is basically a wrapper around your standard window. You can access the handle of the form via the Handle property , which is available for most objects too (to be precise, in any class descending from TWinControl, at least in Delphi again).

I'd be very surprised if you couldn't use OpenGL with a standard form (or something like a TPanel on said form, which I've definitely seen!), but I don't know for sure. I'll let someone else answer this but my answer would be "yeah, it's very likely to be possible."

As for creating forms at the start or not, it's a decision you have to make. Forms are automatically created by default (Project->Options in Delphi, probably the same in Builder). Generally, you want to create forms only when they're used since otherwise they'll sit around using resources. This is especially true of seldom-seen dialogues like about boxes. However, you can have forms auto-created if you want them to be displayed quickly. It doesn't matter too much most of the time - worry about this at the end of the project once you've got your game doing something! An advantage of creating-and-destroying-on-demand is that you don't have to worry about resetting the form to an initial state each time, since it'll happen automagically.

[edited by - Alimonster on July 4, 2003 6:09:55 AM]

Share this post


Link to post
Share on other sites
It works. Now I''ve a red form which becomes active when I press a button *lol*

My mistake was that I put some stuff of the OpenGL-Initialization in the class of form1, which is only a control panel.

@Alimonster:
Has your nick a special meaning?

Yes, C++Builder is mostly like Delphi. That''s why i use it instead of Visual Studio. Before I came to C++ I coded Delphi, so I''m experienced in using Borland products.

Share this post


Link to post
Share on other sites
quote:
Original post by Alim Doener
@Alimonster:
Has your nick a special meaning?

Nope, nothing special . My name is Ali (Alistair) and when I grew up I used Alibug. It made sense to me that as I got older, the bug grew up into a monster, and having "bug" in a nickname isn''t helpful if you''re a coder .

quote:
Yes, C++Builder is mostly like Delphi. That''s why i use it instead of Visual Studio. Before I came to C++ I coded Delphi, so I''m experienced in using Borland products.

Aye. I use Delphi all the time because I love it. I try to avoid C++ though because I hate all the syntactic abbreviations and the slow compilation. I''m glad that you prefer Builder over Visual Studio -- it''s a sign that you like productivity, heh. I cringe when I see people making programming harder than it needs to be (hence my initial reaction, which is also the same when I see people using MFC instead of the VCL ).

Share this post


Link to post
Share on other sites