XNA for C#
DirectX 9 for C#
DirectX 9 for C++
DirectX 9 for VB
Forum
   
My XNA Book
      
       Go to section on this site

Additional info


Latest Forum posts

 Account settings
  Posted by: Anonymous
  When: 07/05/2014 at 09:48:39

 forced subtitle
  Posted by: Applefly
  When: 07/05/2014 at 06:00:48

 convert DVD into PMS
  Posted by: Applefly
  When: 07/05/2014 at 05:55:25

 DVD to Digital Copy easily
  Posted by: VIKIVannessa
  When: 05/05/2014 at 06:52:29

 DVD on Xbox 360/Xbox One Console
  Posted by: VIKIVannessa
  When: 05/05/2014 at 06:51:47

 Extract .Srt Subtitles
  Posted by: Applefly
  When: 04/05/2014 at 03:54:38

 Encode Movie collection
  Posted by: Applefly
  When: 04/05/2014 at 03:52:41

 Convert DVD to WMV
  Posted by: Applefly
  When: 29/04/2014 at 05:53:50

 rip DVDs into digital files
  Posted by: Applefly
  When: 29/04/2014 at 05:51:20

 iTunes movies/music to Kindle Fire
  Posted by: ciciyu80
  When: 29/04/2014 at 05:10:20


Ads

Opening a window using C++

I know, opening a window using C# or other languages is simply a breeze. Using C++, there is a little code involved to get exactly what you want. But when you have accepted this is the way it is done, you are all set to go.

So go on, open Microsoft Visual Studio or whatever IDE you're using and start a new project. Using Microsoft Visual Studio, choose under C++ the Win32 option, and there the 'Win32 Project' item. Make sure you give your project a fancy name. In one of the following wizard screens, select 'Empty project'. If not, you will be introduced to the User Interface Elements, but we want to use DirectX components as interface. If all goes well, you will be presented an empty project.

Add your first file by selecting 'Add new item' in the Project menu and choose C++ file. Call it whatever you want, I called it 'main'.

First add this line to the beginning of your file:


#include<windows.h>
It is simply needed in a windows C++ application. For example, it instructs MS Windows to start your program from the WinMain method, which we will define now. The WinMain method is the starting point of your C++ application. It has to return a number, usually people let it return 0 in case of a successful execution and a non-zero number in case of failure. When called, the method is passed a few arguments by the operating system, such as the handle to the application and additional startup parameters, but we aren't going to use any of them.

For now, let this be your WinMain method:

 int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPreviousInstance,LPSTR lpcmdline,int nCmdShow)
 {
     return 0;
 }

This code should already compile! If you look at your output box at the bottom of the screen, you should see that your application 'has exited with code 0', which is of course your returned integer.

Because this isn't really that exciting yet, let's continue. Next, we're going to open our window. This is simply done by calling the CreateWindowEx method, but this method needs to be passed some parameters when called. Most of these parameters are contained in a WNDCLASSEX structure, which is passed to the CreateWindowEx method.

We will put all the code needed to open a window in a new method we will call NewWindow. We should be able to pass it a few parameters such as the title, the position of the left upper corner, the width and the height. After creating our window it should return the pointer to the window, called the window handle:

 HWND NewWindow(LPCTSTR str_Title,int int_XPos, int int_YPos, int int_Width, int int_Height)
 {
 }

Now let's fill this method. First create the (empty) structure and configure its parameters:

 WNDCLASSEX wnd_Structure;
 
 wnd_Structure.cbSize = sizeof(WNDCLASSEX);
 wnd_Structure.style = CS_HREDRAW | CS_VREDRAW;
 wnd_Structure.lpfnWndProc = OurWindowProcedure;
 wnd_Structure.cbClsExtra = 0;
 wnd_Structure.cbWndExtra = 0;
 wnd_Structure.hInstance = GetModuleHandle(NULL);
 wnd_Structure.hIcon = NULL;
 wnd_Structure.hCursor = NULL;
 wnd_Structure.hbrBackground = GetSysColorBrush(COLOR_BTNFACE);
 wnd_Structure.lpszMenuName = NULL;
 wnd_Structure.lpszClassName = "WindowClassName";
 wnd_Structure.hIconSm = LoadIcon(NULL,IDI_APPLICATION);

Here we indicate, among other less important things, that the window has to redraw if the user moves or resizes the window, and the standard background color of the window. You can also specify a handle to a cursor image or to an application image, if you wish.

One point that should be stressed is that this class needs a reference to a 'Window Procedure', this is a method that will be called every time an event is trigger that is related to the window. This can be triggered when the user clicks anywhere in the window area, but also when the window is for example opened or closed.

We have referenced it to the method called 'OurWindowProcedure', which we still have to create and is defined here below. The method receives some parameters, including a handle to the specific window, the kind if message and 2 additional parameters. In the case of a mouse click, these parameters would be the X and Y coordinates of the click. For now, we aren't going to monitor any event, so we'll just pass the message on to a default Window Procedure, as you can see here:

 LRESULT CALLBACK OurWindowProcedure(HWND han_Wind,UINT uint_Message,WPARAM parameter1,LPARAM parameter2)
 {
     return DefWindowProc(han_Wind,uint_Message,parameter1,parameter2);
 }

Now that we have a valid reference to an existing WindowProcedure, we have to register this window class. If that's done, we can finally call the CreateWindowEx method and pass it our filled WNDCLASSEX structure, along with some more trivial parameters. The CreateWindowEx method will open the window and return a handle to it. It is this handle our NewWindow method has to return.

 RegisterClassEx(&wnd_Structure);
 
 return CreateWindowEx(WS_EX_CONTROLPARENT, "WindowClassName", str_Title, WS_OVERLAPPED | WS_CAPTION | WS_SYSMENU | WS_MINIMIZEBOX | WS_VISIBLE, int_XPos, int_YPos, int_Width, int_Height, NULL, NULL, GetModuleHandle(NULL), NULL);

This concludes our NewWindow method. All we have to do now is call this method from the WinMain method, which will open the window. After this, the program will terminate, so in fact before it terminates we have to empty the memory the window occupates by calling the 'DestroyWindow' method:

 HWND han_Window = NewWindow("DirectX C++ Tutorial",100,100,500,500);
 
 DestroyWindow(han_Window);

This will open a new window with 500x500 pixels and will store a handle to this window in han_Window, which is later used to destroy the window. If you run this code, the window should flash on your screen for a millisecond or something. The simplest thing you can do to have the window remain longer on your screen, is adding this line between the previous ones :

 Sleep(5000);

Which will make your program pause for 5 seconds before continuing, in our case exiting our program.

-- NOTE: If after compiling you get error messages like "cannot convert from 'const char [16]' to 'LPCWSTR'", go to project properties -> configuration properties -> general -> project defaults, and set 'character set' to 'use multi-byte character set'. Thanks to Kin Liang for this remark! --




DirectX Tutorial 1 - Opening a window

If you appreciate the amount of time I spend creating and updating
these pages, feel free to donate -- any amount is welcome !



Click here to go to the forum on this chapter!


Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given in /home1/riemers/public_html/eng/Tutorials/DirectX/C++/Series1/tut1.php(9) : eval()'d code(35) : eval()'d code on line 303



This should be your code so far, I don't think there are many pages on the net that open a window using C++ with so little code:


#include<windows.h>
LRESULT CALLBACK OurWindowProcedure(HWND han_Wind,UINT uint_Message,WPARAM parameter1,LPARAM parameter2)
{
    return DefWindowProc(han_Wind,uint_Message,parameter1,parameter2);
}

HWND NewWindow(LPCTSTR str_Title,int int_XPos, int int_YPos, int int_Width, int int_Height)
{
    WNDCLASSEX wnd_Structure;

    wnd_Structure.cbSize = sizeof(WNDCLASSEX);
    wnd_Structure.style = CS_HREDRAW | CS_VREDRAW;

     wnd_Structure.lpfnWndProc = OurWindowProcedure;
     wnd_Structure.cbClsExtra = 0;
     wnd_Structure.cbWndExtra = 0;
     wnd_Structure.hInstance = GetModuleHandle(NULL);
     wnd_Structure.hIcon = NULL;
     wnd_Structure.hCursor = NULL;
     wnd_Structure.hbrBackground = GetSysColorBrush(COLOR_BTNFACE);
     wnd_Structure.lpszMenuName = NULL;
     wnd_Structure.lpszClassName = "WindowClassName";
     wnd_Structure.hIconSm = LoadIcon(NULL,IDI_APPLICATION);
 
     RegisterClassEx(&wnd_Structure);
 
     return CreateWindowEx(WS_EX_CONTROLPARENT, "WindowClassName", str_Title, WS_OVERLAPPED | WS_CAPTION | WS_SYSMENU | WS_MINIMIZEBOX | WS_VISIBLE, int_XPos, int_YPos, int_Width, int_Height, NULL, NULL, GetModuleHandle(NULL), NULL);
 }
 
 int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPreviousInstance,LPSTR lpcmdline,int nCmdShow)
 {
     HWND han_Window = NewWindow("DirectX C++ Tutorial",100,100,500,500);
     Sleep(5000);
     DestroyWindow(han_Window);
 
     return 0;
 }


Google
 
Webwww.riemers.net


If you appreciate the amount of time I spend creating and updating
these pages, feel free to donate -- any amount is welcome !



- Website design & XNA + DirectX code : Riemer Grootjans -
©2003 - 2011 Riemer Grootjans
Translations

This site in English
This site in Korean
This site in Czech

Microsoft MVP Award



2007 - 2011 MVP Award
DirectX - XNA

Contents

News
Home
Forum
XNA 2.0 Recipes Book (8)
XNA 3.0 Recipes Book (8)
Downloads
Extra Reading (3)
Matrices: geometrical
Matrix Mathematics
Homogenous matrices
Community Projects (1)
Tutorials (160)
XNA 4.0 using C# (89)
DirectX using C# (54)
DirectX using C++ (15)
Series 1: Terrain (15)
Opening a window
Ending the game loop
Linking to the Device
Clearing your window
Drawing a triangle
Culling
Camera
Rotation - Translation
Indices
Terrain creation
Terrain from file
DirectInput
Importing .bmp files
Adding colors
DirectX Light basics
DirectX using VB (2)
-- Expand all --


Thank you!

Support this site --
any amount is welcome !

Stay up-to-date

I don't have the time to keep a News section, so stay informed about the updates by clicking on this RSS file!