Windows Programming

Started by neha kapila, January 20, 2015, 07:18:02 AM

Previous topic - Next topic

neha kapila

hey, i am a beginner in windows programming. I have written a code for drawing shapes  as follows:-

#include <windows.h>

void OnPaint(HWND);
void OnDestroy(HWND);

int stdcall WinMain(HINSTANCE HInstance,HINSTANCE hPrevInstance,LPSTR lpszCmdline, int nCmdShow)
{
   MSG m;

   //PERFORM APPLICATION INITIALIZATION
InitInstance(hinstance, nCmdShow, "Text");
   //main message loop
while(GetMessage(&m,NULL,0,0))
      DispatchMessage(&m);
   return 0;
}
LRESULT CALLBACK WndProc(HWND hWnd , UINT message, WPARAM wParam,LPARAM lParam)
/*WndProc=windows procedure.This function is a message processing function
for our window. It will process any messages Windows sends to our program.
A callback function is a function in your program that is called by the operating system.
You see, in our program we will never actually call the function WndProc().
Windows will do that when ever it sends a message to our program. more on that later.*/
{
   switch(message)
   {
      case WM_DESTROY:
         OnDestroy(hWnd);
         break;
      case WM_PAINT:
         OnPaint(hWnd);
         break;
      default:
         return DefWindowProc(hWnd,message,wParam,lParam);
   }
   return 0;

}
void OnDestroy(HWND hWnd)
{
   PostQuitMessage(0);
}

void OnPaint (HWND hWnd)
{
   HDC hdc;
   PAINTSTRUCT ps;
   HBRUSH hbr;
   HGDIOBJ holdbr;
   POINT pt[5]={250,150,250,300,300,350,400,300,320,190};
   
   hdc=BeginPaint(hWnd,&ps);

   hbr=CreateSolidBrush(RGB(25,0,0));
   holdbr=SelectObject(hdc,hbr);

   MoveToEx(hdc,10,10,NULL);
   LineTo(hdc,200,10);

   Rectangle(hdc,10,20,200,100);

   RoundRect(hdc,10,120,200,220,20,20);

   Ellipse(hdc, 10,240,200,340);

   Pie(hdc,250,10,350,110,350,110,350,10);

   Polygon(hdc,pt,5);

   SelectObject(hdc,holdbr);
   DeleteObject(hbr);


   EndPAint(hWnd, &ps);
}

and i am getting following errors:
C:\Documents and Settings\acer\My Documents\Pelles C Projects\new_try\nn.c(9): error #2001: Syntax error: expected ';' but found 'WinMain'.

C:\Documents and Settings\acer\My Documents\Pelles C Projects\new_try\nn.c(9): warning #2099: Missing type specifier; assuming 'int'.

C:\Documents and Settings\acer\My Documents\Pelles C Projects\new_try\nn.c(10): error #2120: Redeclaration of 'WinMain', previously declared at C:\Program Files\PellesC\Include\Win\winbase.h(1438); expected 'int __stdcall function(HINSTANCE, HINSTANCE, char *, int)' but found 'int __cdecl function(HINSTANCE, HINSTANCE, char *, int)'.

C:\Documents and Settings\acer\My Documents\Pelles C Projects\new_try\nn.c(14): warning #2018: Undeclared function 'InitInstance'; assuming 'extern' returning 'int'.

C:\Documents and Settings\acer\My Documents\Pelles C Projects\new_try\nn.c(14): error #2048: Undeclared identifier 'hinstance'.

C:\Documents and Settings\acer\My Documents\Pelles C Projects\new_try\nn.c(76): warning #2018: Undeclared function 'EndPAint'; assuming 'extern' returning 'int'.
*** Error code: 1 ***
Done.

Please help me out :(

jj2007

int WINAPI WinMain(...
InitInstance(HInstance

czerny

The compiler sees int stdcall as definition of an int variable named stdcall and wants a closing ';'

neha kapila

Thanxs for the help but now i m getting other errors as mentioned below:

C:\Documents and Settings\acer\My Documents\Pelles C Projects\new_try\nn.c(81): warning #2018: Undeclared function 'EndPAint'; assuming 'extern' returning 'int'.
Building new_try.exe.
POLINK: error: Unresolved external symbol '_InitInstance'.
POLINK: error: Unresolved external symbol '__imp__DispatchMessageA@4'.
POLINK: error: Unresolved external symbol '__imp__GetMessageA@16'.
POLINK: error: Unresolved external symbol '__imp__DefWindowProcA@16'.
POLINK: error: Unresolved external symbol '__imp__PostQuitMessage@4'.
POLINK: error: Unresolved external symbol '__imp__BeginPaint@8'.
POLINK: error: Unresolved external symbol '__imp__CreateSolidBrush@4'.
POLINK: error: Unresolved external symbol '__imp__SelectObject@8'.
POLINK: error: Unresolved external symbol '__imp__MoveToEx@16'.
POLINK: error: Unresolved external symbol '__imp__LineTo@12'.
POLINK: error: Unresolved external symbol '__imp__Rectangle@20'.
POLINK: error: Unresolved external symbol '__imp__RoundRect@28'.
POLINK: error: Unresolved external symbol '__imp__Ellipse@20'.
POLINK: error: Unresolved external symbol '__imp__Pie@36'.
POLINK: error: Unresolved external symbol '__imp__Polygon@12'.
POLINK: error: Unresolved external symbol '__imp__DeleteObject@4'.
POLINK: error: Unresolved external symbol '_EndPAint'.
POLINK: error: Unresolved external symbol '_main'.
POLINK: fatal error: 18 unresolved external(s).
*** Error code: 1 ***
Done.

Here is the formatted code:
#include <stdio.h>
#include<conio.h>


#include <windows.h>
//#include <helper.h>

void OnPaint(HWND);
void OnDestroy(HWND);

int WINAPI WinMain(HINSTANCE HInstance,HINSTANCE hPrevInstance,LPSTR lpszCmdline, int nCmdShow)
{
   MSG m;

   //PERFORM APPLICATION INITIALIZATION       


InitInstance(HInstance, nCmdShow, "Text");
   //main message loop
while(GetMessage(&m,NULL,0,0))
      
   DispatchMessage(&m);
   return 0;
}
LRESULT CALLBACK WndProc(HWND hWnd , UINT message, WPARAM wParam,LPARAM lParam)
/*WndProc=windows procedure.This function is a message processing function
for our window. It will process any messages Windows sends to our program.
A callback function is a function in your program that is called by the operating system.
You see, in our program we will never actually call the function WndProc().
Windows will do that when ever it sends a message to our program. more on that later.*/
{
   switch(message)
   {
      case WM_DESTROY:
         OnDestroy(hWnd);
         break;
      case WM_PAINT:
         OnPaint(hWnd);
         break;
      default:
         return DefWindowProc(hWnd,message,wParam,lParam);
   }
   return 0;

}
void OnDestroy(HWND hWnd)
{
   PostQuitMessage(0);
}

void OnPaint (HWND hWnd)
{
   HDC hdc;
   PAINTSTRUCT ps;
   HBRUSH hbr;
   HGDIOBJ holdbr;
   POINT pt[5]={250,150,250,300,300,350,400,300,320,190};
   
   hdc=BeginPaint(hWnd,&ps);

   hbr=CreateSolidBrush(RGB(25,0,0));
   holdbr=SelectObject(hdc,hbr);

   MoveToEx(hdc,10,10,NULL);
   LineTo(hdc,200,10);

   Rectangle(hdc,10,20,200,100);

   RoundRect(hdc,10,120,200,220,20,20);

   Ellipse(hdc, 10,240,200,340);

   Pie(hdc,250,10,350,110,350,110,350,10);

   Polygon(hdc,pt,5);

   SelectObject(hdc,holdbr);
   DeleteObject(hbr);


   EndPAint(hWnd, &ps);
}





czerny

It looks as if you haven't made a windows but a console project.
If this doesn't help, make a zip from your project and post it as attachment.

frankie

The llinker missing symbol errors are telling you that you have not included system libraries in the link.
"It is better to be hated for what you are than to be loved for what you are not." - Andre Gide

jj2007

Quote from: neha kapila on January 28, 2015, 08:06:21 AMwarning #2018: Undeclared function 'EndPAint'; ...

   EndPAint(hWnd, &ps);

This is the second time that your compiler tells you to be more careful with your spelling. Are you sure you have a future in programming?

neha kapila

i have attached the zip file. Please help as i am a novice and no idea about how to make a windows project from the console one.

czerny

This is a console project, not a windows project.

neha kapila

then how can i make it a windows project?

czerny

Quote from: neha kapila on January 29, 2015, 10:11:41 AM
then how can i make it a windows project?
File->New->Project->win32

neha kapila

win32 program (exe)
win32 dynamic library (DLL)
win32 static lib
which 1 out of these?

czerny

What do you want to write, a program or a library?

neha kapila


czerny

Quote from: neha kapila on February 03, 2015, 07:54:07 AM
A program to draw shapes.
If you want to write a program, you should select win32 program (exe).