Draw a polygon.

Dear sir,
Reference prev mail reghartding drawing a triangle , I did not find any on web.

However I got one for drawing a polygon. I have added the code in Window proc function. But it is conficting with parent window parametrs
and giving two errors. may be i m creating a wrong object.

Plz help me fixing the bug .

Source Code is as under in WIN API 32 :


/* 786 */

#include

//PROTOTYPES OF FUNCTIONS
bool RegisterAppWindow(HINSTANCE hInstance);
LRESULT CALLBACK mainWindowProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam);

//GLOBAL VARIABLES

HPEN hPen ;
/*********************START OF CODE***************************/
//Filling main function
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
LPSTR lpCmdLine, int nCmdShow)
{
HWND hWnd,hPopupWnd;
MSG msg;

if (!RegisterAppWindow(hInstance))
{
return 1;
}

//Creating window
hWnd = CreateWindow("MyFirstWindowClass", "Virtual University",WS_OVERLAPPEDWINDOW | WS_VISIBLE,
100, 100, 400, 400, NULL, NULL, hInstance, NULL);



if(!hWnd)
{
MessageBox(hWnd, "Window Couldn't Created /* You can change this sentence*/", "ERROR", MB_OK);
return 1;
}


//GET DEVICE CONTEXT OF THE WINDOW
HDC hDC;
HGDIOBJ hPen ; // ******* added line
hDC=GetDC(hWnd);



BlackPen =(HPEN) GetStockObject(BLACK_PEN);






SelectObject(hDC, GetStockObject(BLACK_PEN)) ;



Polygon( hDC, *lpPoints , 7 );


//RELEASE THE RESOURCES (DEVICE CONTEXT)
ReleaseDC(hWnd,hDC);

//NORMAL MESSAGE HANDLING
while(GetMessage(&msg, NULL, 0, 0) > 0)
DispatchMessage(&msg);

return msg.wParam;
}

//**********************Register Application Window Class*******************
bool RegisterAppWindow(HINSTANCE hInstance)
{
WNDCLASS wc;
ATOM classAtom;

wc.style = 0; // window style
wc.lpfnWndProc = mainWindowProc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = hInstance;
wc.hIcon = NULL;
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);// background color
wc.lpszMenuName = NULL;
wc.lpszClassName = "MyFirstWindowClass";

classAtom = RegisterClass(&wc);

if(!classAtom)
{
MessageBox(NULL, "Error ! Registering Class", "ERROR", MB_OK);
return 0; // error registering the window class
}
return 1;
}

BOOL Polygon(HDC hdc, CONST POINT *lpPoints, int nCount);

RESULT CALLBACK mainWindowProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
switch (message) // switch case
{
HDC hDC;
PAINTSTRUCT ps;
int nNotificationCode,wControlId;


POINT Pt[7];
Pt[0].x = 20;
Pt[0].y = 50;
Pt[1].x = 180;
Pt[1].y = 50;
Pt[2].x = 180;
Pt[2].y = 20;
Pt[3].x = 230;
Pt[3].y = 70;
Pt[4].x = 180;
Pt[4].y = 120;
Pt[5].x = 180;
Pt[5].y = 90;
Pt[6].x = 20;
Pt[6].y = 90;

case WM_DESTROY:
PostQuitMessage(0);
break;
case WM_PAINT:
hDC=BeginPaint(hWnd,&ps);

// DRAW WHITE RECTANGLE
// Rectangle(hDC,30,30,110,110);

// SELECT BLACK BRUSH IN DC
SelectObject(hDC,BlackBrush);

// DRAW BLACK RECTANGLE
Rectangle(hDC,70,70,150,150);

//RELEASE RESOURCES
EndPaint(hWnd,&ps);
break;

default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
return 0;
}

Plz look in to it and guide.
Rgrds.
ah.
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories