Thread: Window Tutorial

  1. #1
    Registered User
    Join Date
    Nov 2005
    Posts
    2

    Unhappy Window Tutorial

    Hi all,

    I want to learn programming C++ for Windows 32 Programs.
    I can progamm text based programs.
    When i tested the tutorial for Window Programming,
    and wrote this Example Code my 2 Compiler`s get Errors.
    Because i would not have this Header File.
    The first program "Hello World" at the window programming tutorial was no problem for the compilers, but the second one.
    I already read in the FAQ that there is no possiblitie that headers can be downloaded. But can be coded by myself.
    Is this headerfile "stdafx.h" the same not download able category?
    Code:
    /*	Trim fat from windows*/
    #define WIN32_LEAN_AND_MEAN	
    #pragma comment(linker, "/subsystem:windows")
    /*	Pre-processor directives*/
    #include "stdafx.h"
    #include <windows.h>
    /*	Windows Procedure Event Handler*/
    LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM 
    
    wParam, LPARAM lParam)
    {
    	PAINTSTRUCT paintStruct;
    	/*	Device Context*/
    	HDC hDC; 
    	/*	Text for display*/
    	char string[] = "Hello, World!"; 
    	/*	Switch message, condition that is met will 
    
    execute*/
    	switch(message)
    	{
    		/*	Window is being created*/
    		case WM_CREATE: 
    			return 0;
    			break;
    		/*	Window is closing*/
    		case WM_CLOSE: 
    			PostQuitMessage(0);
    			return 0;
    			break;
    		/*	Window needs update*/
    		case WM_PAINT: 
    			hDC = BeginPaint(hwnd,&paintStruct);
    			/*	Set txt color to blue*/
    			SetTextColor(hDC, 
    
    COLORREF(0x00FF0000));
    			/*	Display text in middle of window*/
    			
    
    TextOut(hDC,150,150,string,sizeof(string)-1);
    			EndPaint(hwnd, &paintStruct);
    			return 0;
    			break;
    		default:
    			break;
    	}
    	return (DefWindowProc(hwnd,message,wParam,lParam));
    }
    /*	Main function*/
    int APIENTRY WinMain(HINSTANCE hInstance,
                         HINSTANCE hPrevInstance,
                         LPSTR     lpCmdLine,
                         int       nCmdShow)
    {
    	WNDCLASSEX  windowClass;		//window 
    
    class
    	HWND	      hwnd;				
    
    //window handle
    	MSG		msg;				
    
    //message
    	bool		done;				
    
    //flag saying when app is complete
    	/*	Fill out the window class structure*/
    	windowClass.cbSize = sizeof(WNDCLASSEX);
    	windowClass.style = CS_HREDRAW | CS_VREDRAW;
    	windowClass.lpfnWndProc = WndProc;
    	windowClass.cbClsExtra = 0;
    	windowClass.cbWndExtra = 0;
    	windowClass.hInstance = hInstance;
    	windowClass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
    	windowClass.hCursor = LoadCursor(NULL, IDC_ARROW);
    	windowClass.hbrBackground = 
    
    (HBRUSH)GetStockObject(WHITE_BRUSH);
    	windowClass.lpszMenuName = NULL;
    	windowClass.lpszClassName = "MyClass";
    	windowClass.hIconSm = LoadIcon(NULL, IDI_WINLOGO);
    	/*	Register window class*/
    	if (!RegisterClassEx(&windowClass))
    	{
    		return 0;
    	}
    	/*	Class registerd, so now create window*/
    	hwnd = CreateWindowEx(NULL,		//extended style
    		"MyClass",			                 //class name
    		"A Real Win App",		             //app name
    		WS_OVERLAPPEDWINDOW |		//window style
    		WS_VISIBLE |
    		WS_SYSMENU,
    		100,100,			//x/y coords
    		400,400,			
    
    //width,height
    		NULL,				//handle to parent
    		NULL,				//handle to menu
    		hInstance,			//application instance
    		NULL);				//no extra 
    
    parameter's
    	/*	Check if window creation failed*/
    	if (!hwnd)
    		return 0;
    	done = false; //initialize loop condition variable
    	/*	main message loop*/
    	while(!done)
    	{
    		PeekMessage(&msg,hwnd,NULL,NULL,PM_REMOVE);
    		if (msg.message == WM_QUIT) //check for a 
    
    quit message
    		{
    			done = true; //if found, quit app
    		}
    		else
    		{
    			/*	Translate and dispatch to event queue*/
    			TranslateMessage(&msg); 
    			DispatchMessage(&msg);
    		}
    	}
    	return msg.wParam;
    }


    Please help!
    How could i learn when there is no possiblity to compile the code correctly. I`m using Dev-C++ and/or Digital Mars Compiler.

    Inovthink

  2. #2
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    I want to learn programming C++ for Windows 32 Programs. ... I`m using Dev-C++ ...
    Choose File->New Project (or something similar), and make a new Windows project. It generates a Windows program that compiles perfectly.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  3. #3
    It's full of stars adrianxw's Avatar
    Join Date
    Aug 2001
    Posts
    4,829
    You have some settings somewhere which are wrong. You do not want to include MFC to use the raw Win32 API or use any of the wizards. Just create an empty project.
    Wave upon wave of demented avengers march cheerfully out of obscurity unto the dream.

  4. #4
    Registered User
    Join Date
    Nov 2005
    Posts
    2

    Red face

    Hallo again,

    Thank you for telling me, that Dev-C++ makes a perfect working program.
    I will study it too.
    The Source Code at the top is from the Tutorial from cprogramming.com
    http://www.cprogramming.com/tutorial...ndows_app.html

    So i thought that someone can tell me why my two Compiler won`t find
    this Header file.( 5 ...... grosse~1.cpp stdafx.h: No such file or directory )
    i want to run exactly this Code.

    Now does someone know`s whats up with this header file?
    "stdafx.h"

  5. #5
    Registered User Dante Shamest's Avatar
    Join Date
    Apr 2003
    Posts
    970
    It's a Visual C++ specific header file.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. 6 measly errors
    By beene in forum Game Programming
    Replies: 11
    Last Post: 11-14-2006, 11:06 AM
  2. how i create a window whith all it's elements
    By rasheed in forum Windows Programming
    Replies: 1
    Last Post: 05-31-2006, 06:53 PM
  3. internet
    By yeah in forum C Programming
    Replies: 16
    Last Post: 02-12-2005, 10:37 PM
  4. opengl help
    By heat511 in forum Game Programming
    Replies: 4
    Last Post: 04-05-2004, 01:08 AM
  5. no errors but still errors
    By Megatron in forum Windows Programming
    Replies: 7
    Last Post: 01-12-2003, 11:21 PM