7 Replies - 5722 Views - Last Post: 22 February 2010 - 12:52 AM Rate Topic: -----

#1 Joesavage1  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 90
  • Joined: 20-July 09

Win32 hdc Problem

Posted 21 February 2010 - 05:12 AM

Hi,

Recently I've been having some C++ Win32 problems with HDC, I think its a really stupid mistake because I kinda remember doing it before and thinking "God how stupid are you?".

This is the simple code that I cant get to work:


#include "stdafx.h"
#include <windows.h> 
#define WINDOW_CLASS_NAME "WINCLASS1"




HWND main_window_handle = NULL; //Save the window's handle

//Start of function WindowProc
LRESULT CALLBACK WindowProc(HWND hwnd, 
UINT msg, 
WPARAM wparam, 
LPARAM lparam)
{
// this is the main message handler of the system
PAINTSTRUCT    ps;    // used in WM_PAINT
HDC    hdc;   // handle to a device context

// what is the message? 
switch(msg)
{   
case WM_CREATE: 
{
//TODO: initialization stuff here
return(0);
} break;

case WM_PAINT: 
{
// simply validate the window
hdc = BeginPaint(hwnd,&ps);   
EndPaint(hwnd,&ps);
return(0);
} break;

case WM_DESTROY: 
{
// kill the application   
PostQuitMessage(0);
return(0);
} break;

default:break;

} // end switch statement

// process any messages that we didn't take care of 
return (DefWindowProc(hwnd, msg, wparam, lparam));

}
//End of function WindowProc

int WINAPI WinMain(   HINSTANCE hinstance, //Begin WinMain
HINSTANCE hprevinstance,
LPSTR lpcmdline,
int ncmdshow)
{

WNDCLASS winclass;   //This will hold the windows class we create
HWND    hwnd;    //Declaring a handle
MSG msg;

//Filling in the window class stucture..
winclass.style    = CS_DBLCLKS | CS_OWNDC | 
CS_HREDRAW | CS_VREDRAW;
winclass.lpfnWndProc   = WindowProc;
winclass.cbClsExtra    = 0;
winclass.cbWndExtra    = 0;
winclass.hInstance    = hinstance;
winclass.hIcon    = LoadIcon(NULL, IDI_APPLICATION);
winclass.hCursor    = LoadCursor(NULL, IDC_ARROW);
winclass.hbrBackground   = (HBRUSH)GetStockObject(BLACK_BRUSH);
winclass.lpszMenuName   = NULL;
winclass.lpszClassName   = WINDOW_CLASS_NAME;

// register the window class
RegisterClass(&winclass);


// create the window
hwnd = CreateWindow(WINDOW_CLASS_NAME, // class
"Title",    // title
WS_OVERLAPPEDWINDOW | WS_VISIBLE,
0,0,    // x,y
320,200, // width, height
NULL,    // handle to parent 
NULL,    // handle to menu
hinstance,// instance
NULL);   // creation stuff

// save the window handle in a global
main_window_handle = hwnd;

while(1) //Start main loop
{
if (PeekMessage(&msg,NULL,0,0,PM_REMOVE))
{ 
// test if this wants to quit
if (msg.message == WM_QUIT)
break; //If it wants to quit break out of the loop

// translate any accelerator keys
TranslateMessage(&msg);

// send the message to WindowProc
DispatchMessage(&msg);
} // end if

//Start Main Programming
//Invalidate the whole client area
InvalidateRect(hwnd, NULL, TRUE);
//Start Painting
hdc = BeginPaint(hwnd,&ps);
TextOut(hdc,0,0,"Hello",5);
//Stop Painting
EndPaint(hwnd, &ps);

} // end while loop
return(msg.wParam); //Way of returning to windows
}


And I get these errors:

1>c:\users\user\documents\visual studio 2008\projects\testabc\testabc\testabc.cpp(113) : error C2065: 'hdc' : undeclared identifier
1>c:\users\user\documents\visual studio 2008\projects\testabc\testabc\testabc.cpp(113) : error C2065: 'ps' : undeclared identifier
1>c:\users\user\documents\visual studio 2008\projects\testabc\testabc\testabc.cpp(114) : error C2065: 'hdc' : undeclared identifier
1>c:\users\user\documents\visual studio 2008\projects\testabc\testabc\testabc.cpp(116) : error C2065: 'ps' : undeclared identifier



Please Help,

Thanks In Advance,


Joe

This post has been edited by Joesavage1: 21 February 2010 - 05:24 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Win32 hdc Problem

#2 sarmanu  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 967
  • View blog
  • Posts: 2,362
  • Joined: 04-December 09

Re: Win32 hdc Problem

Posted 21 February 2010 - 05:39 AM

You declare:
PAINTSTRUCT    ps;    // used in WM_PAINT
HDC    hdc;


In WindowProc function, but they are recognized ONLY in that function. Adding:
PAINTSTRUCT    ps;    // used in WM_PAINT
HDC    hdc;


to WinMain function should solve your issue.
Was This Post Helpful? 1
  • +
  • -

#3 Ferencn  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 71
  • View blog
  • Posts: 322
  • Joined: 01-February 10

Re: Win32 hdc Problem

Posted 21 February 2010 - 05:42 AM

both hdc and ps are variables unknown to your WinMain function.
So you either have to add them to your WinMain function, or move the drawing to your WindowProc function, where those two variables are actually present!
Was This Post Helpful? 1
  • +
  • -

#4 Joesavage1  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 90
  • Joined: 20-July 09

Re: Win32 hdc Problem

Posted 21 February 2010 - 06:13 AM

Thanks, Works Now!! However the text flickers TONNES, Do you know if there is a way to resolve this?
Was This Post Helpful? 0
  • +
  • -

#5 Aphex19  Icon User is offline

  • Born again Pastafarian.
  • member icon

Reputation: 617
  • View blog
  • Posts: 1,873
  • Joined: 02-August 09

Re: Win32 hdc Problem

Posted 21 February 2010 - 11:41 AM

View PostJoesavage1, on 21 February 2010 - 05:13 AM, said:

Thanks, Works Now!! However the text flickers TONNES, Do you know if there is a way to resolve this?


try adding the WS_CLIPCHILDREN windows style to you're CreatWindow function. eg

		// Create the window 
	hwnd = CreateWindowEx(0,
                          ClsName,
                          WndName,
               		 WS_OVERLAPPEDWINDOW | WS_CLIPCHILDREN,	//style
                          CW_USEDEFAULT,	//int x position
               			  CW_USEDEFAULT,		//int y position
                          600,							//int width
               			  400,							//int Height
                          NULL,							//Parent
                          NULL,
                          hInstance,
                          NULL);


ps, excuse the messy code, it wouldnt paste correctly.

This post has been edited by Aphex19: 21 February 2010 - 11:43 AM

Was This Post Helpful? 0
  • +
  • -

#6 Joesavage1  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 90
  • Joined: 20-July 09

Re: Win32 hdc Problem

Posted 21 February 2010 - 12:29 PM

The exact code you said I should replace it with:

hwnd = CreateWindowEx(0,
WINDOW_CLASS_NAME,
"Title",
WS_OVERLAPPEDWINDOW | WS_CLIPCHILDREN, //style
CW_USEDEFAULT,        //int x position
CW_USEDEFAULT,                //int y position
600,                                                  //int width
400,                                                  //int Height
NULL,                                                 //Parent
NULL,
hinstance,
NULL);


Doesn't actually make the window visible, so adding the WS_VISIBLE style flag seemed needed:

WS_OVERLAPPEDWINDOW | WS_CLIPCHILDREN | WS_VISIBLE,


However the flicker problem still remains, and I can only really read the text properly when I drag the application around (because when I drag it the contents freezes (as it does in all Win32 apps Ive seen so far))..


Please Help,

Thanks In Advance,


Joe
Was This Post Helpful? 0
  • +
  • -

#7 Martyn.Rae  Icon User is offline

  • The programming dinosaur
  • member icon

Reputation: 545
  • View blog
  • Posts: 1,420
  • Joined: 22-August 09

Re: Win32 hdc Problem

Posted 21 February 2010 - 02:35 PM

ok, the code you have produced is, I am sorry to say completely and utterly wrong. I am not surprised that you say :

Quote

However the flicker problem still remains, and I can only really read the text properly when I drag the application around (because when I drag it the contents freezes (as it does in all Win32 apps Ive seen so far))..


I do not know if you have formulated the model you have produced, or have copied it from somebody else but ... it's so terribly wrong!

I have commented and corrected this code for you so it should run nicely.

#include "stdafx.h" 
#include <windows.h>  
#define WINDOW_CLASS_NAME "WINCLASS1" 

HWND main_window_handle = NULL; //Save the window's handle 
 
//Start of function WindowProc 
LRESULT CALLBACK WindowProc(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam) { 
    // this is the main message handler of the system 
    PAINTSTRUCT    ps;    // used in WM_PAINT 
    HDC    hdc;   // handle to a device context 
 
    // what is the message?  
    switch(msg) {    
        case WM_CREATE:  
        { 
            //TODO: initialization stuff here 
            return(0); 
        } break; 
 
        case WM_PAINT:  
        { 
            // simply validate the window // <<<--- no, no ,no
            /* >>> The WM_PAINT message is sent to a window when some   <<<
               >>> part of the window requires painting! Here, and only <<<
               >>> here should you use BeginPaint ... and EndPaint      <<<
               >>> they should not appear anywhere else!!!!!            <<< */
            hdc = BeginPaint(hwnd,&ps);    
            EndPaint(hwnd,&ps); 
            return(0); 
        } break; 
 
        case WM_DESTROY:  
        { 
            // kill the application    
            PostQuitMessage(0); 
            return(0); 
        } break; 
 
        default:break; 
 
    } // end switch statement 
 
    // process any messages that we didn't take care of  
    return (DefWindowProc(hwnd, msg, wparam, lparam));
} 
//End of function WindowProc 
 
 //Begin WinMain 
int WINAPI WinMain(HINSTANCE hinstance, HINSTANCE hprevinstance,
                   LPSTR pcmdline, int ncmdshow) { 

    WNDCLASS winclass;   //This will hold the windows class we create 
    HWND    hwnd;    //Declaring a handle 
    MSG msg; 
 
    //Filling in the window class stucture.. 
    winclass.style    = CS_DBLCLKS | CS_OWNDC | CS_HREDRAW | CS_VREDRAW; 
    winclass.lpfnWndProc   = WindowProc; 
    winclass.cbClsExtra    = 0; 
    winclass.cbWndExtra    = 0; 
    winclass.hInstance    = hinstance; 
    winclass.hIcon    = LoadIcon(NULL, IDI_APPLICATION); 
    winclass.hCursor    = LoadCursor(NULL, IDC_ARROW); 
    winclass.hbrBackground   = (HBRUSH)GetStockObject(BLACK_BRUSH); 
    winclass.lpszMenuName   = NULL; 
    winclass.lpszClassName   = WINDOW_CLASS_NAME; 
 
    // register the window class 
    RegisterClass(&winclass); 
 
 
    // create the window 
    hwnd = CreateWindow(WINDOW_CLASS_NAME, // class 
                        "Title",    // title 
                        WS_OVERLAPPEDWINDOW | WS_VISIBLE, 
                        0,0,    // x,y 
                        320,200, // width, height 
                        NULL,    // handle to parent  
                        NULL,    // handle to menu 
                        hinstance,// instance 
                        NULL);   // creation stuff 
 
    // save the window handle in a global 
    main_window_handle = hwnd; 
 
    /* >>> I have absolutely no idea where you got this code from <<<
       >>> but it is wrong. This loop says for every message      <<<
       >>> processed by the message loop, invalidate the entire   <<<
       >>> client window area, then output the string "Hello"!    <<<
       >>> This means when you move the window, every single      <<<
       >>> WM_MOVE message will cause the window to receive a     <<<
       >>> paint message. Every resize of the window which causes <<<
       >>> a WM_SIZE message to be sent to the message queue,     <<<
       >>> will cause a paint message to be processed.            <<<
       >>> I am surprised your computer hasn't dropped to it's    <<<
       >>> knees and begged you to stop!                          <<< */

    while(1) { //Start main loop 
        if (PeekMessage(&msg,NULL,0,0,PM_REMOVE)) {  
            // test if this wants to quit 
            if (msg.message == WM_QUIT) 
                break; //If it wants to quit break out of the loop 
 
            // translate any accelerator keys 
            TranslateMessage(&msg); 
 
            // send the message to WindowProc 
            DispatchMessage(&msg); 
        } // end if 
 
        //Start Main Programming 
        //Invalidate the whole client area 
        InvalidateRect(hwnd, NULL, TRUE); 
        //Start Painting 
        hdc = BeginPaint(hwnd,&ps); 
        TextOut(hdc,0,0,"Hello",5); 
        //Stop Painting 
        EndPaint(hwnd, &ps); 
    } // end while loop 
    return(msg.wParam); //Way of returning to windows 
}



I have written the correct way to code this.

#include "stdafx.h" 
#include <windows.h>  
#define WINDOW_CLASS_NAME "WINCLASS1" 

HWND main_window_handle = NULL; //Save the window's handle 
 
//Start of function WindowProc 
LRESULT CALLBACK WindowProc(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam) { 
    // this is the main message handler of the system 
    PAINTSTRUCT    ps;    // used in WM_PAINT 
    HDC    hdc;   // handle to a device context 
 
    // what is the message?  
    switch(msg) {    
        case WM_CREATE:  
        { 
            //TODO: initialization stuff here 
            return(0); 
        }
 
        case WM_PAINT:  
        { 
            hdc = BeginPaint(hwnd,&ps);    
            TextOut(hdc,0,0,"Hello",5); 
            EndPaint(hwnd,&ps); 
            return(0); 
        } 
 
        case WM_DESTROY:  
        { 
            // kill the application    
            PostQuitMessage(0); 
            return(0); 
        }
 
        default:
            break; 
 
    } // end switch statement 
 
    // process any messages that we didn't take care of  
    return (DefWindowProc(hwnd, msg, wparam, lparam));
} 
//End of function WindowProc 
 
 //Begin WinMain 
int WINAPI WinMain(HINSTANCE hinstance, HINSTANCE hprevinstance,
                   LPSTR pcmdline, int ncmdshow) { 

    WNDCLASS winclass;   //This will hold the windows class we create 
    HWND    hwnd;    //Declaring a handle 
    MSG msg; 
 
    //Filling in the window class stucture.. 
    winclass.style    = CS_DBLCLKS | CS_OWNDC | CS_HREDRAW | CS_VREDRAW; 
    winclass.lpfnWndProc   = WindowProc; 
    winclass.cbClsExtra    = 0; 
    winclass.cbWndExtra    = 0; 
    winclass.hInstance    = hinstance; 
    winclass.hIcon    = LoadIcon(NULL, IDI_APPLICATION); 
    winclass.hCursor    = LoadCursor(NULL, IDC_ARROW); 
    winclass.hbrBackground   = (HBRUSH)GetStockObject(BLACK_BRUSH); 
    winclass.lpszMenuName   = NULL; 
    winclass.lpszClassName   = WINDOW_CLASS_NAME; 
 
    // register the window class 
    RegisterClass(&winclass); 
 
 
    // create the window 
    hwnd = CreateWindow(WINDOW_CLASS_NAME, // class 
                        "Title",    // title 
                        WS_OVERLAPPEDWINDOW | WS_VISIBLE, 
                        0,0,    // x,y 
                        320,200, // width, height 
                        NULL,    // handle to parent  
                        NULL,    // handle to menu 
                        hinstance,// instance 
                        NULL);   // creation stuff 
 
    // save the window handle in a global 
    main_window_handle = hwnd; 
 
    while ( GetMessage(&msg,NULL,0,0) ) { //Start main loop 
        // translate any accelerator keys 
        TranslateMessage(&msg); 
 
        // send the message to WindowProc 
        DispatchMessage(&msg); 
    } // end if 
 
    return (0); //Way of returning to windows 
}



That will remove the flicker altogether.
Was This Post Helpful? 0
  • +
  • -

#8 Guest_Joe*


Reputation:

Re: Win32 hdc Problem

Posted 22 February 2010 - 12:52 AM

View PostMartyn.Rae, on 21 February 2010 - 01:35 PM, said:

ok, the code you have produced is, I am sorry to say completely and utterly wrong. I am not surprised that you say :

Quote

However the flicker problem still remains, and I can only really read the text properly when I drag the application around (because when I drag it the contents freezes (as it does in all Win32 apps Ive seen so far))..


I do not know if you have formulated the model you have produced, or have copied it from somebody else but ... it's so terribly wrong!

I have commented and corrected this code for you so it should run nicely.

That will remove the flicker altogether.



Ah thanks, I just needed to remove the line that invalidated the whole window.

Ive also decided to change to GetDC when Im using drawing code outside of WM_PAINT as its a lot easier.

This the code I ended up using if anyone wants to know:


#include "stdafx.h"
#include <windows.h> 
#define WINDOW_CLASS_NAME "WINCLASS1"




HWND main_window_handle = NULL; //Save the window's handle

//Start of function WindowProc
LRESULT CALLBACK WindowProc(HWND hwnd, 
UINT msg, 
WPARAM wparam, 
LPARAM lparam)
{
// this is the main message handler of the system
PAINTSTRUCT    ps;    // used in WM_PAINT
HDC    hdc;   // handle to a device context

// what is the message? 
switch(msg)
{   
case WM_CREATE: 
{
//TODO: initialization stuff here
return(0);
} break;

case WM_PAINT: 
{
//Start Painting
hdc = BeginPaint(hwnd,&ps);
TextOut(hdc,0,0,"Hello",5);
//Stop Painting
EndPaint(hwnd, &ps);
return(0);
} break;

case WM_DESTROY: 
{
// kill the application   
PostQuitMessage(0);
return(0);
} break;

default:break;

} // end switch statement

// process any messages that we didn't take care of 
return (DefWindowProc(hwnd, msg, wparam, lparam));

}
//End of function WindowProc

int WINAPI WinMain(   HINSTANCE hinstance, //Begin WinMain
HINSTANCE hprevinstance,
LPSTR lpcmdline,
int ncmdshow)
{

WNDCLASS winclass;   //This will hold the windows class we create
HWND    hwnd;    //Declaring a handle
MSG msg;

//Filling in the window class stucture..
winclass.style    = CS_DBLCLKS | CS_OWNDC | 
CS_HREDRAW | CS_VREDRAW;
winclass.lpfnWndProc   = WindowProc;
winclass.cbClsExtra    = 0;
winclass.cbWndExtra    = 0;
winclass.hInstance    = hinstance;
winclass.hIcon    = LoadIcon(NULL, IDI_APPLICATION);
winclass.hCursor    = LoadCursor(NULL, IDC_ARROW);
winclass.hbrBackground   = (HBRUSH)GetStockObject(BLACK_BRUSH);
winclass.lpszMenuName   = NULL;
winclass.lpszClassName   = WINDOW_CLASS_NAME;

// register the window class
RegisterClass(&winclass);


hwnd = CreateWindowEx(0,
WINDOW_CLASS_NAME,
"Title",
WS_OVERLAPPEDWINDOW | WS_CLIPCHILDREN | WS_VISIBLE, //style
CW_USEDEFAULT,        //int x position
CW_USEDEFAULT,                //int y position
600,                                                  //int width
400,                                                  //int Height
NULL,                                                 //Parent
NULL,
hinstance,
NULL);




// save the window handle in a global
main_window_handle = hwnd;

while(1) //Start main loop
{
if (PeekMessage(&msg,NULL,0,0,PM_REMOVE))
{ 
// test if this wants to quit
if (msg.message == WM_QUIT)
break; //If it wants to quit break out of the loop

// translate any accelerator keys
TranslateMessage(&msg);

// send the message to WindowProc
DispatchMessage(&msg);
} // end if

//Start Main Programming
PAINTSTRUCT    ps; 
HDC    hdc;


} // end while loop
return(msg.wParam); //Way of returning to windows
}


Was This Post Helpful? 0

Page 1 of 1