3 Replies - 525 Views - Last Post: 19 July 2009 - 04:15 PM Rate Topic: -----

#1 imakaia  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 25-March 09

Windows Programming Questions: Where the Hell is the the code piece re

Post icon  Posted 19 July 2009 - 10:40 AM

So once again I have the Hello Windows 98 programm as an example while I proceed in reading the book.

I've red that insidte the windows procedure there is the piece of code responsible for updating (re-drawing) the window (client area included) each time that it is moved, uncovered or modified

If I havent red wrong the pe
iece of code is the response to the WM_PAINT message located inside the "switch"

Quote

switch (message)
{
case WM_PAINT:
hdc = BeginPaint (hwnd, &ps) ;

GetClientRect (hwnd, &rect) ;

DrawText (hdc, TEXT ("Hello, Windows 98!"), -1, &rect,
DT_SINGLELINE | DT_CENTER | DT_VCENTER) ;
EndPaint (hwnd, &ps) ;

return 0 ;

case WM_DESTROY:
PostQuitMessage (0) ;
return 0 ;
}


The strange is that even if i delete this piece of code the window is updated the same with no Problem.

The only problem occurs if i delete the WM_DESTROY reaction. then if i close the window the programm does not terminate and i have to do it through the Task Manager.

You see somtime I feel that many piece of codes are typical meaning that even if they are not included there is no problem.

Does windows has an misscode-safe system behind with automated corrections?

#include <windows.h>

LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM);

int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
					PSTR szCmdLine, int iCmdShow)
{
	 static TCHAR szAppName[] = TEXT ("HelloWin");
	 HWND		 hwnd;
	 MSG		  msg;
	 WNDCLASS	 wndclass;

	 wndclass.style		 = CS_HREDRAW | CS_VREDRAW;
	 wndclass.lpfnWndProc   = WndProc;
	 wndclass.cbClsExtra	= 0;
	 wndclass.cbWndExtra	= 0;
	 wndclass.hInstance	 = hInstance;
	 wndclass.hIcon		 = LoadIcon (NULL, IDI_APPLICATION);
	 wndclass.hCursor	   = LoadCursor (NULL, IDC_ARROW);
	 wndclass.hbrBackground = (HBRUSH) GetStockObject (WHITE_BRUSH);
	 wndclass.lpszMenuName  = NULL;
	 wndclass.lpszClassName = szAppName;

	 if (!RegisterClass (&wndclass))
	 {
		  MessageBox (NULL, TEXT ("This program requires Windows NT!"), 
					  szAppName, MB_IConerror);
		  return 0;
	 }
	 hwnd = CreateWindow (szAppName,				  // window class name
						  TEXT ("The Hello Program"), // window caption
						  WS_OVERLAPPEDWINDOW,		// window style
						  CW_USEDEFAULT,			  // initial x position
						  CW_USEDEFAULT,			  // initial y position
						  CW_USEDEFAULT,			  // initial x size
						  CW_USEDEFAULT,			  // initial y size
						  NULL,					   // parent window handle
						  NULL,					   // window menu handle
						  hInstance,				  // program instance handle
						  NULL);					 // creation parameters
	 
	 ShowWindow (hwnd, iCmdShow);
	 UpdateWindow (hwnd);
	 
	 while (GetMessage (&msg, NULL, 0, 0))
	 {
		  TranslateMessage (&msg);
		  DispatchMessage (&msg);
	 }
	 return msg.wParam;
}

LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
	 HDC		 hdc;
	 PAINTSTRUCT ps;
	 RECT		rect;
	 
	 switch (message)
	 {

	 case WM_PAINT:
		  hdc = BeginPaint (hwnd, &ps);
		  
		  GetClientRect (hwnd, &rect);
		  
		  DrawText (hdc, TEXT ("Hello, Windows 98!"), -1, &rect,
					DT_SINGLELINE | DT_CENTER | DT_VCENTER);
		  EndPaint (hwnd, &ps);
		  return 0;
		  
	 case WM_DESTROY:
		  PostQuitMessage (0);
		  return 0;
	 }
	 return DefWindowProc (hwnd, message, wParam, lParam);
}



Is This A Good Question/Topic? 0
  • +

Replies To: Windows Programming Questions: Where the Hell is the the code piece re

#2 NickDMax  Icon User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2250
  • View blog
  • Posts: 9,245
  • Joined: 18-February 07

Re: Windows Programming Questions: Where the Hell is the the code piece re

Posted 19 July 2009 - 12:18 PM

well I am a little confused, if you remove the WM_PAINT section of the code, you will get a window but it will not say "Hello, Windows 98!".

Windows will mannage the drawing of the window itself, but you need to determine what goes into it. You can actually control what kind of window gets draw with the WNDCLASS and CreateWindow properties.

so can you post the version of the code that is confusing you?
Was This Post Helpful? 0
  • +
  • -

#3 imakaia  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 25-March 09

Re: Windows Programming Questions: Where the Hell is the the code piece re

Posted 19 July 2009 - 02:22 PM

oh yes im sorry. The window indeed doesnt contain any text if the paint is removed.

But the act tha windows manages to draw it itsel, thats exactly my question. The basic window drawing is an automated process (like many others) that isnt depended from some piece of code in the curent programm.

So its actually done with the ShowWindow (hwnd, iCmdShow) ; ?
Was This Post Helpful? 0
  • +
  • -

#4 Noxica  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 23
  • Joined: 10-April 09

Re: Windows Programming Questions: Where the Hell is the the code piece re

Posted 19 July 2009 - 04:15 PM

View Postimakaia, on 19 Jul, 2009 - 01:22 PM, said:

oh yes im sorry. The window indeed doesnt contain any text if the paint is removed.

But the act tha windows manages to draw it itsel, thats exactly my question. The basic window drawing is an automated process (like many others) that isnt depended from some piece of code in the curent programm.

So its actually done with the ShowWindow (hwnd, iCmdShow) ; ?



All windows programming is done through something called an API. Which lets you tap into a specific library or into certain hardware.

Most of the work of creating a window is done through windows.h. Mostly the code you write is just allocating memory for the window and setting up the properties of the window. The operating system as a whole is in charge of making sure your window plays nice with everyone else at least with the code you have so far.

Does that answer your question?

Your ShowWindow function does create the window. There is a lot more going on behind the scenes though which is built into the very way windows works as a OS.

You can look up more stuff on msdn or google if your really curious.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1