jasie24
January 4th, 2003, 04:30 PM
Why this simple program gives me a protection fault error?(it does what i want,but in the same time it appears that error..)
it is written in borlandc 3.1 for windows but ,it's strange that another almost same program with this ,but it has something in adition,so it's larger ,and that works and this not.why?
here is the code....
#include <windows.h>
long FAR PASCAL WndProc(HWND hWnd,WORD message,WORD wParam,LONG lParam);
char name[]="Text";
int PASCAL WinMain(HANDLE hInstance,HANDLE hPrevInstance,LPSTR lpszCmdLine,int nCmdShow)
{
HWND hwnd;
MSG msg;
if(!hPrevInstance)
{
WNDCLASS wndclass;
wndclass.style=CS_HREDRAW|CS_VREDRAW;
wndclass.lpfnWndProc=(WNDPROC)WndProc;
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);
wndclass.lpszMenuName=NULL;
wndclass.lpszClassName=name;
if(!RegisterClass(&wndclass)) return FALSE;
}
hwnd=CreateWindow(name,"Afisare cu TextOut",
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT,CW_USEDEFAULT,
CW_USEDEFAULT,CW_USEDEFAULT,
NULL,NULL,hInstance,NULL);
if(!hwnd) return FALSE;
ShowWindow(hwnd,nCmdShow);
UpdateWindow(hwnd);
while(GetMessage(&msg,NULL,0,0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return msg.wParam;
}
long FAR PASCAL WndProc(HWND hwnd,WORD message,WORD wParam,LONG lParam)
{
HDC hdc;
PAINTSTRUCT ps;
static TEXTMETRIC tm;
static short cxChar,cyChar;
char szBuffer[10];
switch(message)
{
case WM_CREATE:
hdc=GetDC(hwnd);
GetTextMetrics(hdc,&tm);
cxChar=tm.tmAveCharWidth;
cyChar=tm.tmHeight+tm.tmExternalLeading;
ReleaseDC(hwnd,hdc);
return 0;
case WM_PAINT:
hdc=BeginPaint(hwnd,&ps);
TextOut(hdc,cxChar,cyChar,szBuffer,
wsprintf(szBuffer,"tm.tmHeight= %d",tm.tmHeight));
TextOut(hdc,cxChar,2*cyChar,szBuffer,
wsprintf(szBuffer,"tm.tmAscent= %d",tm.tmAscent));
TextOut(hdc,cxChar,3*cyChar,szBuffer,
wsprintf(szBuffer,"tm.tmDescent= %d",tm.tmDescent));
TextOut(hdc,cxChar,4*cyChar,szBuffer,
wsprintf(szBuffer,"tm.tmInternalLeading= %d",tm.tmInternalLeading));
TextOut(hdc,cxChar,5*cyChar,szBuffer,
wsprintf(szBuffer,"tm.tmExternalLeading= %d",tm.tmExternalLeading));
EndPaint(hwnd,&ps);
return 0;
case WM_DESTROY:
PostQuitMessage(0);
return 0;
}
return DefWindowProc(hwnd,message,wParam,lParam);
}
it is written in borlandc 3.1 for windows but ,it's strange that another almost same program with this ,but it has something in adition,so it's larger ,and that works and this not.why?
here is the code....
#include <windows.h>
long FAR PASCAL WndProc(HWND hWnd,WORD message,WORD wParam,LONG lParam);
char name[]="Text";
int PASCAL WinMain(HANDLE hInstance,HANDLE hPrevInstance,LPSTR lpszCmdLine,int nCmdShow)
{
HWND hwnd;
MSG msg;
if(!hPrevInstance)
{
WNDCLASS wndclass;
wndclass.style=CS_HREDRAW|CS_VREDRAW;
wndclass.lpfnWndProc=(WNDPROC)WndProc;
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);
wndclass.lpszMenuName=NULL;
wndclass.lpszClassName=name;
if(!RegisterClass(&wndclass)) return FALSE;
}
hwnd=CreateWindow(name,"Afisare cu TextOut",
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT,CW_USEDEFAULT,
CW_USEDEFAULT,CW_USEDEFAULT,
NULL,NULL,hInstance,NULL);
if(!hwnd) return FALSE;
ShowWindow(hwnd,nCmdShow);
UpdateWindow(hwnd);
while(GetMessage(&msg,NULL,0,0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return msg.wParam;
}
long FAR PASCAL WndProc(HWND hwnd,WORD message,WORD wParam,LONG lParam)
{
HDC hdc;
PAINTSTRUCT ps;
static TEXTMETRIC tm;
static short cxChar,cyChar;
char szBuffer[10];
switch(message)
{
case WM_CREATE:
hdc=GetDC(hwnd);
GetTextMetrics(hdc,&tm);
cxChar=tm.tmAveCharWidth;
cyChar=tm.tmHeight+tm.tmExternalLeading;
ReleaseDC(hwnd,hdc);
return 0;
case WM_PAINT:
hdc=BeginPaint(hwnd,&ps);
TextOut(hdc,cxChar,cyChar,szBuffer,
wsprintf(szBuffer,"tm.tmHeight= %d",tm.tmHeight));
TextOut(hdc,cxChar,2*cyChar,szBuffer,
wsprintf(szBuffer,"tm.tmAscent= %d",tm.tmAscent));
TextOut(hdc,cxChar,3*cyChar,szBuffer,
wsprintf(szBuffer,"tm.tmDescent= %d",tm.tmDescent));
TextOut(hdc,cxChar,4*cyChar,szBuffer,
wsprintf(szBuffer,"tm.tmInternalLeading= %d",tm.tmInternalLeading));
TextOut(hdc,cxChar,5*cyChar,szBuffer,
wsprintf(szBuffer,"tm.tmExternalLeading= %d",tm.tmExternalLeading));
EndPaint(hwnd,&ps);
return 0;
case WM_DESTROY:
PostQuitMessage(0);
return 0;
}
return DefWindowProc(hwnd,message,wParam,lParam);
}