Thread: DC is not pointing to my window!

  1. #1

    DC is not pointing to my window!

    I got a DC setup like this:
    Code:
    HDC hdcMainWin = GetDC(MainWindow);
    and instead of pointing to the client area of MainWindow, it goes to the top-left corner of the screen.

    I've included the .exe file to demonstrate.

  2. #2
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,793
    More likely its an error with your code.

    But as you posted the exe, there's no way to suggest anything

  3. #3
    Here is how I am creating the window, the loop, and the creation of the DC.

    Code:
    HWND MainWindow;
    ...
    int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE, LPSTR, int nCmdShow)
    {
    	{
    		WNDCLASS wc;
    		wc.cbClsExtra = 0;
    		wc.cbWndExtra = 0;
    		wc.hbrBackground = (HBRUSH)COLOR_WINDOW;
    		wc.hCursor = LoadCursor(NULL, IDC_ARROW);
    		wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
    		wc.hInstance = hInstance;
    		wc.lpfnWndProc = MainWndProc;
    		wc.lpszClassName = "TED MainWindow";
    		wc.lpszMenuName = NULL;
    		wc.style = 0;
    
    		if (!RegisterClass(&wc))
    		{
    			MessageBox(NULL, "Error registering window class!", "Error", MB_OK);
    			return 1;
    		}
    	}
    
    	if (!CreateWindowEx(0, "TED MainWindow", 
    		"GDI Tile Engine Demo - Main Window", 
    		WS_OVERLAPPED | WS_VISIBLE | WS_MINIMIZEBOX | WS_SYSMENU,
    		CW_USEDEFAULT,	CW_USEDEFAULT,	640, 480, NULL, NULL, hInstance,
    		NULL))
    	{
    		MessageBox(NULL, "Error creating main window!", "Error", MB_OK);
    		return 1;
    	}
    
    	MSG msg;
            HDC hdcMainWin = GetDC(MainWindow);
    	HBITMAP hbmBall = LoadBitmap(hInstance, MAKEINTRESOURCE(IDB_BITMAP1));
    
    
       while (TRUE)
    	{
    		if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
    		{
    			if (msg.message != WM_QUIT)
    			{
    				TranslateMessage(&msg);
    				DispatchMessage(&msg);
    			}
    			else
    				break;
    		}
    			tempX ++;
    			tempY ++;
    			graphics.BeginFrame(hdcMainWin);
    			graphics.TransBlit(hbmBall, tempX, tempY);
    			graphics.EndFrame();
    	}
            ReleaseDC(MainWindow, hdcMainWin);
    	return msg.wParam;
    }

  4. #4
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    Your problem is that MainWindow is null. GetDC(NULL) gives you a HDC to the screen, not the window. Try this instead:
    Code:
    MainWindow = CreateWindowEx(0, "TED MainWindow", 
    		"GDI Tile Engine Demo - Main Window", 
    		WS_OVERLAPPED | WS_VISIBLE | WS_MINIMIZEBOX | WS_SYSMENU,
    		CW_USEDEFAULT,	CW_USEDEFAULT,	640, 480, NULL, NULL, hInstance,
    		NULL);
    if(!MainWindow)
    {
       //stuff
    }
    That way you actually get the handle to the window
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C or C++
    By AcerN30 in forum Game Programming
    Replies: 41
    Last Post: 05-30-2008, 06:57 PM
  2. 6 measly errors
    By beene in forum Game Programming
    Replies: 11
    Last Post: 11-14-2006, 11:06 AM
  3. Pong is completed!!!
    By Shamino in forum Game Programming
    Replies: 11
    Last Post: 05-26-2005, 10:50 AM
  4. OpenGL Window
    By Morgul in forum Game Programming
    Replies: 1
    Last Post: 05-15-2005, 12:34 PM
  5. OpenGL and Windows
    By sean345 in forum Game Programming
    Replies: 5
    Last Post: 06-24-2002, 10:14 PM