Results 1 to 9 of 9

Thread: Why u shouldnt learn for a book...

  1. #1

    Thread Starter
    Lively Member L0phtpDK's Avatar
    Join Date
    Mar 2001
    Location
    ILL
    Posts
    109

    Why u shouldnt learn for a book...

    OK... i recently got Windows 95 Programming For Dummies... (ill wait till u guys stop laughing) and i've have a problem with this code (im just pasting the whole damn thing):
    Code:
    #include <windows.h>
    
    static int nRowSpacing;
    static int nCharSpacing;
    
    int WINAPI WinMain(
    	HINSTANCE  hInstance,
    	HINSTANCE  hPrevInstance,
    	LPSTR	   pszCmdLine,
    	int		   nCmdShow
       );
    
    LRESULT CALLBACK WindowProc(
    	HWND	hWnd,
    	UINT	uMsgId,
    	WPARAM  wParam,
    	LPARAM  lParam
       );
    
    void OutputLine(
    	HDC  hDC,
    	int  nRowNumber,
    	char *pszDescription,
    	int nValue
       );
    
    int WINAPI WinMain(
    	HINSTANCE  hInstance,
    	HINSTANCE  hPrevInstance,
    	LPSTR	   pszCmdLine,
    	int		   nCmdShow
       )
    {
    	static char szAppName[] = "Prog2";
    	HWND hWnd;
    	MSG msg;
    	WNDCLASS wndClass;
    
    	wndClass.style		 = 0;
    	wndClass.lpfnWndProc = WindowProc;
    	wndClass.cbClsExtra	 = 0;
    	wndClass.cbWndExtra  = 0;
    	wndClass.hInstance	 = hInstance;
    	wndClass.hIcon		 = LoadIcon(NULL,IDI_APPLICATION);
    	wndClass.hCursor	 = LoadCursor(NULL, IDC_ARROW);
    	wndClass.hbrBackground = GetStockObject(WHITE_BRUSH); 	//<--Right HERE 
    	wndClass.lpszMenuName  = NULL;
    	wndClass.lpszClassName = szAppName;
    	if (RegisterClass(&wndClass) == 0)
    	{
    		return 0;
    	}
    
    	hWnd = CreateWindow(
    			 szAppName,
    			 szAppName,
    			 WS_OVERLAPPEDWINDOW,
    			 CW_USEDEFAULT,
    			 CW_USEDEFAULT,
    			 CW_USEDEFAULT,
    			 CW_USEDEFAULT,
    			 NULL,
    			 NULL,
    			 hInstance,
    			 NULL);
    	if (hWnd == 0)
    	{
    		return 0;
    	}
    	ShowWindow(hWnd, nCmdShow);
    	UpdateWindow(hWnd);
    
    	while (GetMessage(&msg, NULL, 0, 0))
    	{
    		TranslateMessage(&msg);
    		DispatchMessage(&msg);
    	}
    	return msg.wParam;
    }
    
    LRESULT CALLBACK WindowProc(
    	HWND	hWnd,
    	UINT	uMsgId,
    	WPARAM  wParam,
    	LPARAM  lParam
       )
    {
    	HDC hDC;
    
    	switch (uMsgId)
    	{
    	case WM_CREATE:
    			hDC = GetDC(hWnd);
    
    			TEXTMETRIC textMetrics;
    			GetTextMetrics(hDC, &textMetrics);
    			nRowSpacing = textMetrics.tmHeight + textMetrics.tmExternalLeading;
    			nCharSpacing = textMetrics.tmAveCharWidth;
    
    			ReleaseDC(hWnd, hDC);
    			return 0;
    
    	case WM_PAINT:
    			PAINTSTRUCT paintStruct;
    			hDC = BeginPaint(hWnd, &paintStruct);
    
    			OutputLine(hDC, 1,
    				       "Avg Char Spactin = ",
    					   nCharSpacing);
    			OutputLine(hDC, 2,
    					   "Row spacing = ",
    					   nRowSpacing);
    
    
    			RECT rectWindow;
    			GetWindowRect(hWnd, &rectWindow);
    			OutputLine(hDC, 4,
    					   "Window x location = ",
    					   rectWindow.left);
    			OutputLine(hDC, 5,
    					   "Window y location = ",
    					   rectWindow.top);
    
    			OutputLine(hDC, 7,
    					   "Window x dimension = ",
    					   rectWindow.right - rectWindow.left);
    			OutputLine(hDC, 8,
    					   "Window y dimension = ",
    					   rectWindow.bottom - rectWindow.top);
    
    			EndPaint(hWnd, &paintStruct);
    			return 0;
    
    	case WM_DESTROY:
    			PostQuitMessage(0);
    			return 0;
    
    	default:
    		return DefWindowProc(hWnd,uMsgId,wParam,lParam);
    	}
    }
    
    void OutputLine(
    	HDC  hDC,
    	int  nRowNumber,
    	char *pszDescription,
    	int nValue
       )
    {
    	int nCol1Offset = 20;
    	int nCol2Offset = nCol1Offset + 25 * nCharSpacing;
    	int nRowOffset = nRowNumber * nRowSpacing;
    
    	SetTextAlign(hDC, TA_LEFT | TA_TOP);
    	TextOut(hDC,
    			nCol1Offset,
    		    nRowOffset,
    			pszDescription,
    			lstrlen(pszDescription));
    
    	SetTextAlign(hDC, TA_RIGHT | TA_TOP);
    	char szBuffer[16];
    	wsprintf(szBuffer, "%d", nValue);
    	TextOut(hDC,
    			nCol2Offset,
    		    nRowOffset,
    			szBuffer,
    			lstrlen(szBuffer));
    }
    And the error is this:
    Code:
    error C2440: '=' : cannot convert from 'void *' to 'struct HBRUSH__ *'
            Conversion from 'void*' to pointer to non-'void' requires an explicit cast
    What is the fastest and easiest way to fix this sort of a problem. Also, word of the wise, don't try to learn from book!
    I have no real reason to put anything here....

  2. #2

    Thread Starter
    Lively Member L0phtpDK's Avatar
    Join Date
    Mar 2001
    Location
    ILL
    Posts
    109

    opps

    the debugger points to:
    Code:
    	wndClass.hbrBackground = GetStockObject(WHITE_BRUSH); 	//<--Right HERE
    Sorry that the code is a tad disorganized... i dunno why it messed up while i was copying and pasting
    I have no real reason to put anything here....

  3. #3
    Member
    Join Date
    May 2001
    Location
    UK
    Posts
    40
    Hello L0phtpDK

    Try casting

    Example:

    wndClass.hbrBackground = (WNDCLASS) GetStockObject(WHITE_BRUSH);

    I had the same problem 2 hrs ago..it may work

    HTH

    hmm

  4. #4

    Thread Starter
    Lively Member L0phtpDK's Avatar
    Join Date
    Mar 2001
    Location
    ILL
    Posts
    109
    did work.. i got this error now:
    Code:
    Cpp1.cpp
    C:\Windows\Desktop\programin\prog2\Cpp1.cpp(46) : error C2440: 'type cast' : cannot convert from 'void *' to 'struct tagWNDCLASSA'
            No constructor could take the source type, or constructor overload resolution was ambiguous
    Error executing cl.exe.
    and just to make sure.. my code is now:
    Code:
    	wndClass.style		 = 0;
    	wndClass.lpfnWndProc             = WindowProc;
    	wndClass.cbClsExtra	 = 0;
    	wndClass.cbWndExtra              = 0;
    	wndClass.hInstance	 = hInstance;
    	wndClass.hIcon		 = LoadIcon(NULL,IDI_APPLICATION);
    	wndClass.hCursor	                 = LoadCursor(NULL, IDC_ARROW);
        wndClass.hbrBackground = (WNDCLASS) GetStockObject(WHITE_BRUSH);  	//<--Right HERE 
    	wndClass.lpszMenuName  = NULL;
    	wndClass.lpszClassName = szAppName;
    I have no real reason to put anything here....

  5. #5

    Thread Starter
    Lively Member L0phtpDK's Avatar
    Join Date
    Mar 2001
    Location
    ILL
    Posts
    109
    ok... after reading the last msg that u mentioned before... i did the following:
    Code:
    wndClass.hbrBackground = (HBRUSH) GetStockObject(WHITE_BRUSH);
    it compiled... but it wont let me build it... i get this error when i build it:
    Code:
    Linking...
    LIBCD.lib(crt0.obj) : error LNK2001: unresolved external symbol _main
    Debug/Cpp1.exe : fatal error LNK1120: 1 unresolved externals
    Error executing link.exe.
    I take it i was wrong when i did that....
    I have no real reason to put anything here....

  6. #6
    Member
    Join Date
    May 2001
    Location
    UK
    Posts
    40
    Hi L0phtpDK

    Change the line to read:

    wndClass.hbrBackground = (HBRUSH) GetStockObject(BLACK_BRUSH); //<--Right HERE

    It compiled for me and ran



    HTH

    hmm
    Last edited by hmm; Oct 7th, 2001 at 12:33 PM.

  7. #7

    Thread Starter
    Lively Member L0phtpDK's Avatar
    Join Date
    Mar 2001
    Location
    ILL
    Posts
    109
    it compiles... but i cant Build it.
    it says i still have a linking error.
    I have no real reason to put anything here....

  8. #8
    Member
    Join Date
    May 2001
    Location
    UK
    Posts
    40
    Hi L0phtpDK


    Extract from a FAQ:

    I get following errors when compiling the Win32 demos : 'libcd.lib(crt0.obj) : error LNK2001: unresolved external symbol _main' 'Debug/main.exe : fatal error LNK1120: 1 unresolved externals'.

    This error occurs in VC when you're trying to compile a Win32 project as a console application. The compiler then searches for the function 'main', which causes the error. To fix this, copy your files into a directory, open a Win32 project and add the files to the project. Everything should compile fine. If you're compiling from the command prompt you should add '-mwindows'.


    HTH

    hmm

  9. #9
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    easier way than copiying and pasting:
    in the linker options in project settings, change
    /subsystem:console
    to
    /subsystem:windows

    It should search for WinMain then.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

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