CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    Join Date
    Aug 2006
    Location
    Dallas, TX
    Posts
    47

    Minimal Windows Instance

    I'm using a book to learn the windows API using C++. This is an example from the book. But it will not compile. I've checked over the code at least 10 times now and can't seem to find any errors. Any assistance will be appreciated. Thanks in advance

    Code:
    ////////////////////////////////////////////////////////////////
    //Minimal Windows Application
    ////////////////////////////////////////////////////////////////
    #include "windows.h"
    
    LRESULT WINAPI WindowProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam);
    
    int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE HPrevInstance, LPSTR lpCmdLine, int nCmdShow)
    {
    	//struct WNDCLASSEX
    	//{
    	//	UINT cbSize;			//Size of this object in bytes
    	//	UINT style;				//Window Style
    	//	WNDPROC lpfnWndProc;	//Pointer to message processing function
    	//	int cbClsExtra;			//Extra byte after the window class
    	//	int cbWndExtra;			//Extra bytes after the window instance
    	//	HINSTANCE hInstance;	//The application instance handle
    	//	HICON hIcon;			//The application icon
    	//	HCURSOR hCursor;		//The window cursor
    	//	HBRUSH hbrBackground;	//The brush defining the background color
    	//	LPCTSTR lpszMenuName;	//A pointer to the name of the menu resource
    	//	LPCTSTR lpszClassName;	//A Pointer to the class name
    	//	HICON hIconSm;			//A small icon associated with the window
    	//};
    
    
    	WNDCLASSEX WindowClass;		//create a window class object
    
    	static LPCTSTR szAppName = L"OFWin";		//Define Windows Class Name
    	HWND hWnd;									//Window handle
    	MSG msg;									//Window Message Structure
    
    	WindowClass.cbSize = sizeof(WNDCLASSEX);	//Set structure size
    	
    	//Redraw the window if size changes
    	WindowClass.style = CS_HREDRAW | CS_VREDRAW;
    
    	//Define the message handling function
    	WindowClass.lpfnWndProc = WindowProc;
    
    	WindowClass.cbClsExtra = 0;		//No extra bytes after the window class
    	WindowClass.cbWndExtra = 0;		//structure of the window instance
    
    	WindowClass.hInstance = hInstance;		//Application instance handle
    
    	//Set the default application icon
    	WindowClass.hIcon = LoadIcon(0, IDI_APPLICATION);
    
    	//Set the window cursor to be the standard arrow
    	WindowClass.hCursor = LoadCursor(0, IDC_ARROW);
    
    	//Set the gray brush for the background color
    	WindowClass.hbrBackground = static_cast<HBRUSH>(GetStockObject(GRAY_BRUSH));
    
    	WindowClass.lpszMenuName = 0;			//No menu
    	WindowClass.lpszClassName = szAppName;	//Set class name
    	WindowClass.hIconSm = -0;				//Default small icon
    
    	//Now Register our window class
    	RegisterClassEx(&WindowClass);
    
    	//Now we can create the window
    	hWnd = CreateWindow(
    		szAppName,							//The window class name
    		L"A Basic Window the Hard Way",		//The window title
    		WS_OVERLAPPEDWINDOW,				//Window Style as overlapped
    		CW_USEDEFAULT,						//Default screen position of upper left
    		CW_USEDEFAULT,						//corner of our window as x,y...
    		CW_USEDEFAULT,						//Default Window Size
    		CW_USEDEFAULT,						//....
    		0,									//No parent window
    		0,									//No menu
    		hInstance,							//Program instance handle
    		0									//No window creation data
    		);
    
    	ShowWindow(hWnd, nCmdShow);		//Display the window
    	UpdateWindow(hWnd);				//Cause the window clien area to be drawn
    
    	//The MessageLoop
    	while(GetMessage(&msg, 0, 0, 0) == TRUE)	//Get any messages
    	{
    		TranslateMessage(&msg);					//Translate the message
    		DispatchMessage(&msg);					//Dispatch the message
    	}
    
    	return static_cast<int>(msg.wParam);		//End, So return to windows
    }
    
    LRESULT WINAPI WindowProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
    {
    	HDC hDC;
    	PAINTSTRUCT PaintSt;
    	RECT aRect;
    
    	switch(message)
    	{
    		case WM_PAINT:						  // Message is to redraw the window
    			hDC = BeginPaint(hWnd, &PaintSt); //Prepare to draw the window
    			
    			//Get upper left and lower right of the client area
    			GetClientRect(hWnd, &aRect);
    
    			SetBkMode(hDC, TRANSPARENT);	//Set text background mode
    
    			//Now draw the text in the window client area
    			DrawText(
    				hDC,					//Device context handle
    				L"But, soft! What light through yonder window breaks?",
    				-1,						//Indicate null terminated string
    				&aRect,					//Rectangle in which text is to be drawn
    				DT_SINGLELINE|			//Text format - single line
    				DT_CENTER|				//            - centered in the line
    				DT_VCENTER);			//            - line centered in the aRect
    			
    			EndPaint(hWnd, &PaintSt);	//Terminate window redraw operation
    			return 0;
    
    		case WM_DESTROY:				//Window is being destroyed
    			PostQuitMessage(0);
    			return 0;
    
    		default:						//Any other message - we dont
    										//want to know so call
    										//default message processing
    			return DefWindowProc(hWnd, message,wParam, lParam);
    	}
    }
    1>------ Build started: Project: Windows_FirstApp, Configuration: Debug Win32 ------
    1>Compiling...
    1>main.cpp
    1>c:\users\noah\documents\visual studio 2008\projects\windows_firstapp\windows_firstapp\main.cpp(29) : error C2440: 'initializing' : cannot convert from 'const wchar_t [6]' to 'LPCTSTR'
    1> Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
    1>c:\users\noah\documents\visual studio 2008\projects\windows_firstapp\windows_firstapp\main.cpp(75) : error C2664: 'CreateWindowExA' : cannot convert parameter 3 from 'const wchar_t [28]' to 'LPCSTR'
    1> Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
    1>c:\users\noah\documents\visual studio 2008\projects\windows_firstapp\windows_firstapp\main.cpp(114) : error C2664: 'DrawTextA' : cannot convert parameter 2 from 'const wchar_t [52]' to 'LPCSTR'
    1> Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
    1>Build log was saved at "file://c:\Users\Noah\Documents\Visual Studio 2008\Projects\Windows_FirstApp\Windows_FirstApp\Debug\BuildLog.htm"
    1>Windows_FirstApp - 3 error(s), 0 warning(s)
    ========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

  2. #2
    Join Date
    Feb 2005
    Posts
    2,160

    Re: Minimal Windows Instance

    Enable UNICODE in your settings.

  3. #3
    Join Date
    Oct 2006
    Location
    Sweden
    Posts
    3,654

    Re: Minimal Windows Instance

    Or replace all L"some text" with _T("some text") to enable both multibyte and unicode builds.
    Debugging is twice as hard as writing the code in the first place.
    Therefore, if you write the code as cleverly as possible, you are, by
    definition, not smart enough to debug it.
    - Brian W. Kernighan

    To enhance your chance's of getting an answer be sure to read
    http://www.codeguru.com/forum/announ...nouncementid=6
    and http://www.codeguru.com/forum/showthread.php?t=366302 before posting

    Refresh your memory on formatting tags here
    http://www.codeguru.com/forum/misc.php?do=bbcode

    Get your free MS compiler here
    https://visualstudio.microsoft.com/vs

  4. #4
    Join Date
    Aug 2006
    Location
    Dallas, TX
    Posts
    47

    Re: Minimal Windows Instance

    Ok been trying to implement the changes you guys suggested.

    First. I recently purchased VC2008 Pro. So i'm still getting used to the way it's setup. Do you know how to enable unicode on it?

    Second _T and _t are unrecognized when replacing L in the code. Is there an include i need for this?

    I know i'm still really new to this and probably need alot of help. But thanks in advance for any help offered.

  5. #5
    Join Date
    Feb 2005
    Posts
    2,160

    Re: Minimal Windows Instance

    Personally, I'd just stick with the L"" convention and always use UNICODE. It should be the default in VS2008, but I don't know how you created your project.

    Go to menu "Project" -> "Properties" then drill down to "Configuration Properties" -> "General" then change "Character Set" in the right panel to "Use Unicode Character Set".

  6. #6
    Join Date
    Aug 2006
    Location
    Dallas, TX
    Posts
    47

    Re: Minimal Windows Instance

    Thanks. That got it for me. Maybe now i can move forward you guys have been alot of help

  7. #7
    Join Date
    Oct 2006
    Location
    Sweden
    Posts
    3,654

    Re: Minimal Windows Instance

    Selecting between multibyte and unicode build is done in project properties/General/Character Set.

    Ah, sorry _T is declared in tchar.h
    Debugging is twice as hard as writing the code in the first place.
    Therefore, if you write the code as cleverly as possible, you are, by
    definition, not smart enough to debug it.
    - Brian W. Kernighan

    To enhance your chance's of getting an answer be sure to read
    http://www.codeguru.com/forum/announ...nouncementid=6
    and http://www.codeguru.com/forum/showthread.php?t=366302 before posting

    Refresh your memory on formatting tags here
    http://www.codeguru.com/forum/misc.php?do=bbcode

    Get your free MS compiler here
    https://visualstudio.microsoft.com/vs

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured