6 Replies - 1064 Views - Last Post: 29 June 2011 - 03:14 AM Rate Topic: -----

#1 nader2121  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 28-June 11

gui drawing in c++

Posted 28 June 2011 - 12:04 PM

what is the problem in the following code every time i initialize it the interface starts but when i press the button of drawing curve an error appears??!!! plz help me...


#include "wingui.h"
#include "math.h"
#define PI 3.14
float m, k;
static float f =0 ,t =0 ;
int h, r=0,z=10,a=-25;
char text[128];
int width = GetSystemMetrics(SM_CXSCREEN);
	int height = GetSystemMetrics(SM_CYSCREEN);

void drawing(int from, int to)
{
        HDC hDC=GetDC(hWnd) ;
		 MoveToEx(hDC,from,335,NULL);
			float c = a*PI/10;
			if (z<18)
			 {do
			  {
				float f = sin(c);
				
				LineTo(hDC,from,235-(f*100)*z/10);
				from +=z;
				if(c >= (1000*PI)) break;
				c = c+PI/10;
		     } while(from<to);
			}
			else 
			{do
			  {
				float f = sin(c);
				LineTo(hDC,from,235-(f*100)*17/10);
				from += 17;
				if(c >= (1000*PI)) break;
				c += (PI/10);
		     } while(from<to);
			}
ReleaseDC(hWnd,hDC);
}

HWND hSecondEdit,hThirdEdit;
LRESULT WINAPI WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{    HDC hDC = GetDC(hWnd);
	RECT R;
    GetClientRect(hWnd, &R);
    
	switch(message)
	{
	case WM_CREATE:
		{
			SetWindowText(hWnd, "Equation Analyzer");
			MoveWindow(hWnd,10,10,1330,720,FALSE);
			HWND hButton1,hButton2,hButton3,hButton4, hFirstEdit,hSecondEdit,hThirdEdit,hFourthEdit,hFifthEdit;
	hButton1= CreateWindow("Button", "" , WS_CHILD|WS_VISIBLE, width/20, 10*height/12 , 200, 50, hWnd ,(HMENU)(UINT_PTR)1,NULL,NULL);
	  SetWindowText(hButton1, "Draw Curve");
    hButton2 = CreateWindow("Button", "" , WS_CHILD|WS_VISIBLE, 5*width/20 , 10*height/12 , 200, 50, hWnd ,(HMENU)(UINT_PTR)2,NULL,NULL);
	  SetWindowText(hButton2, "Draw Equation");
    hButton3 = CreateWindow("Button", "" , WS_CHILD|WS_VISIBLE, 16*width/20, 3*height/12, 200, 50, hWnd ,(HMENU)(UINT_PTR)3,NULL,NULL);
	  SetWindowText(hButton3, "Zoom In");
	 hButton4 = CreateWindow("Button", "" , WS_CHILD|WS_VISIBLE, 16*width/20, 5*height/12, 200, 50, hWnd ,(HMENU)(UINT_PTR)4,NULL,NULL);
	  SetWindowText(hButton4, "Zoom Out");
    hFirstEdit =CreateWindow("EDIT","",WS_CHILD|WS_VISIBLE|WS_BORDER|ES_MULTILINE|ES_WANTRETURN,90,550 ,620,35,hWnd,(HMENU)(UINT_PTR)4,NULL,NULL);
      SetWindowText(hFirstEdit,"");   //f(x)
    hSecondEdit =CreateWindow("EDIT","",WS_CHILD|WS_VISIBLE|WS_BORDER|ES_MULTILINE|ES_WANTRETURN,95,595 ,60,25,hWnd,(HMENU)(UINT_PTR)5,NULL,NULL);
      SetWindowText(hSecondEdit,"");   //from
    hThirdEdit =CreateWindow("EDIT","",WS_CHILD|WS_VISIBLE|WS_BORDER|ES_MULTILINE|ES_WANTRETURN,190,595 ,80,25,hWnd,(HMENU)(UINT_PTR)6,NULL,NULL);
      SetWindowText(hThirdEdit,"");   //to
    hFourthEdit =CreateWindow("EDIT","",WS_CHILD|WS_VISIBLE|WS_BORDER|ES_MULTILINE|ES_WANTRETURN,375,595 ,80,25,hWnd,(HMENU)(UINT_PTR)7,NULL,NULL);
      SetWindowText(hFourthEdit,"");   //resolution
    hFifthEdit =CreateWindow("EDIT","",WS_CHILD|WS_VISIBLE|WS_BORDER|ES_MULTILINE|ES_WANTRETURN,490,595 ,80,25,hWnd,(HMENU)(UINT_PTR)8,NULL,NULL);
      SetWindowText(hFifthEdit,"");   //x

	 
		}
		break;
		case WM_COMMAND:
		{   HDC hDC=GetDC(hWnd);
			WORD controlEvent = HIWORD(wParam);
			WORD controlID = LOWORD(wParam);
			HWND hControl = (HWND)lParam;
			if(controlEvent==BN_CLICKED) ;
			{ 
			    GetWindowText(hSecondEdit,text,128);
				f=atof(text);
				GetWindowText(hThirdEdit,text,128);
				t=atof(text);

				if(wParam==1) drawing(f,t) ;
				if(wParam==3) z++ ;
                if(wParam==4) z--;
				
                PatBlt(hDC,0,0,3000,3000,WHITENESS);
                InvalidateRect(hWnd,NULL,FALSE);
			ReleaseDC(hWnd,hDC);
			}
			
			break;
  }
		
          case WM_PAINT :
		   {
		     HDC hDC=GetDC(hWnd);
            HBRUSH holdbrush, hbrush1,hbrush2,hbrush3,hbrush4,hbrush5,hbrush6;
			HPEN holdPen, hPen1,hPen2,hPen3,hPen4;
			HFONT holdfont, hfont1,hfont2,hfont3,hfont4;

            hbrush1 = WGCreateBrush (RGB(0,0,0),HS_SOLID);
			hbrush2 = WGCreateBrush (RGB(255,255,255),HS_SOLID);
			hbrush3 = WGCreateBrush (RGB(245,194,194),HS_SOLID);
			hbrush4 = WGCreateBrush (RGB(255,255,255),HS_NULL);
			hbrush5 = WGCreateBrush (RGB(150,150,200),HS_SOLID);
			hbrush6 = WGCreateBrush (RGB(0,204,153),HS_SOLID);
			hPen1   = WGCreatePen   (RGB(0,255,0),5,PS_SOLID);
			hPen2   = WGCreatePen   (RGB(150,150,150),1,PS_SOLID);
			hPen3   = WGCreatePen   (RGB(150,150,150),3,PS_SOLID);
			hPen4   = WGCreatePen   (RGB(200,200,200),3,PS_SOLID);
			hfont1  =WGCreateFont ("Calibri",15,FS_BOLD,-600);
			hfont2  =WGCreateFont ("Calibri",15,FS_BOLD,0);
            hfont3  =WGCreateFont ("New Times Roman",16,FS_ITALIC|FS_UNDERLINE,0);
			hfont4  =WGCreateFont ("New Times Roman",20,FS_ITALIC|FS_BOLD,0);
			

		    holdbrush = (HBRUSH)SelectObject(hDC,hbrush3);
			Rectangle(hDC,0,0,10000,4500);

			SelectObject(hDC,hbrush1);
			Rectangle(hDC,30,30,1000,450);
			
			SelectObject(hDC,hbrush5);
            Rectangle(hDC,90,470,710,540);

			SelectObject(hDC,hbrush6);
            Ellipse(hDC,980,450,1330,700);

			holdfont=(HFONT)SelectObject(hDC,hfont3);
			SetBkMode(hDC, TRANSPARENT);
			WGPrintf(hDC,1125,470,"Made By :");
			WGPrintf(hDC,1125,660,"EE 2014");

			holdfont=(HFONT)SelectObject(hDC,hfont4);
			SetBkMode(hDC, TRANSPARENT);
			WGPrintf(hDC,1050,500,"* Beshoy Nabil       Sec.3");
			WGPrintf(hDC,1050,530,"* Mina George      Sec.8");
			WGPrintf(hDC,1050,560,"* Mina Nabil          Sec.8");
			WGPrintf(hDC,1050,590,"* Moris Bahgat     Sec.7");
			WGPrintf(hDC,1050,620,"* Nader Hany       Sec.8");

			SelectObject(hDC,hbrush1);
			SetBkMode(hDC, TRANSPARENT);
			WGPrintf(hDC,10,480,"Equation");
			WGPrintf(hDC,40,550,"F(X)");
            WGPrintf(hDC,40,595,"From");
            WGPrintf(hDC,160,595,"To");
            WGPrintf(hDC,275,595,"Resolution");
            WGPrintf(hDC,460,595,"x=");

           holdPen = (HPEN)SelectObject(hDC,hPen2);
           for(int i =70;i<width-400;i=i+25)
            {
              for( int h =60;h<400;h=h+25)
               {
                 Rectangle(hDC,i,h,i+25,h+25);
               }
            }
		    SelectObject(hDC,hPen4); 
		    MoveToEx(hDC,520,60,NULL);
			LineTo(hDC,520,410);
			MoveToEx(hDC,70,235,NULL);
			LineTo(hDC,970,235);

		    SelectObject(hDC,hPen3);  
			MoveToEx(hDC,70,60,NULL);
			LineTo(hDC,970,60);
			MoveToEx(hDC,70,410,NULL);
			LineTo(hDC,970,410);
		    MoveToEx(hDC,70,60,NULL);
			LineTo(hDC,70,410);
			MoveToEx(hDC,970,60,NULL);
			LineTo(hDC,970,410);
			
			
		
		       k=0-18;
                for(int i=80;i<=1000;i=i+25)
              {
               SetBkMode(hDC, TRANSPARENT);
               SetTextColor(hDC,RGB(225,200,0));
               SelectObject(hDC,hfont1);
               WGPrintf(hDC,i,408,"%.1f",10*k/z);
               k++;
		      }                              
		
			 
			m=0+(450/120);
            for(int i=50;i<=400;i=i+25)
            {
              SetTextColor(hDC,RGB(225,200,0));
              SelectObject(hDC,hfont2);
              WGPrintf(hDC,45,i,"%.1f",10*m/z);
              m=m-0.5;
            }
	      


			
			
			DeleteObject(hbrush1);
			DeleteObject(hbrush2);
			DeleteObject(hbrush3);
			DeleteObject(hbrush4);
			DeleteObject(hbrush5);
			DeleteObject(hbrush6);
            DeleteObject(hPen1);
		    DeleteObject(hPen2);
		    DeleteObject(hPen3);
		    DeleteObject(hPen4);
		    DeleteObject(hfont1);
		    DeleteObject(hfont2);
		    DeleteObject(hfont3);
			break;
		   }
	}
	ReleaseDC(hWnd,hDC);
	return WGDefaultWindowProc(hWnd, message, wParam, lParam);
}


int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE, LPSTR, int nCmdShow)
{
	return WGCreateMainWindow(hInstance, nCmdShow, WndProc);
}


Is This A Good Question/Topic? 0
  • +

Replies To: gui drawing in c++

#2 vividexstance  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 656
  • View blog
  • Posts: 2,246
  • Joined: 31-December 10

Re: gui drawing in c++

Posted 28 June 2011 - 12:49 PM

Could you post the error message, and what compiler/IDE you're using?
Was This Post Helpful? 0
  • +
  • -

#3 nader2121  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 28-June 11

Re: gui drawing in c++

Posted 28 June 2011 - 01:33 PM

as soon as i press the button draw curve the following error appear as in this pic
http://www.mediafire...3vz2cmo&thumb=4
i am using visual c++, win 32,win 32 project
Was This Post Helpful? 0
  • +
  • -

#4 jimblumberg  Icon User is offline

  • member icon


Reputation: 4017
  • View blog
  • Posts: 12,400
  • Joined: 25-December 09

Re: gui drawing in c++

Posted 28 June 2011 - 02:36 PM

It looks like GetDC() is failing for some reason. What is hWnd? You should probably test that hDc is not null before you continue your program.

Jim
Was This Post Helpful? 0
  • +
  • -

#5 Curtis Rutland  Icon User is online

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 4463
  • View blog
  • Posts: 7,777
  • Joined: 08-June 10

Re: gui drawing in c++

Posted 28 June 2011 - 04:58 PM

Jeez, mediafire sucks as an image server. Rehosted that error on imgur for anyone who actually wants to be able to read what it says.

Posted Image
Was This Post Helpful? 0
  • +
  • -

#6 Elliotd123  Icon User is offline

  • New D.I.C Head

Reputation: 12
  • View blog
  • Posts: 37
  • Joined: 24-January 11

Re: gui drawing in c++

Posted 28 June 2011 - 05:43 PM

View Postnader2121, on 28 June 2011 - 12:04 PM, said:

void drawing(int from, int to)
{
        HDC hDC=GetDC(hWnd) ;
		 
LRESULT WINAPI WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)



Here you try to use the hWnd variable that you declare in WndProc()...(later in your code)
Maybe you need to pass a reference to hWnd as an argument for your drawing() function

void drawing(int from, int to, HWND &hWnd)
{...}


Then of course you would have to add hWnd to your drawing function call.

line 87 becomes

if(wParam==1) drawing(f,t, hWnd) ;


This post has been edited by Elliotd123: 28 June 2011 - 05:55 PM

Was This Post Helpful? 2
  • +
  • -

#7 snoopy11  Icon User is offline

  • Engineering ● Software
  • member icon

Reputation: 768
  • View blog
  • Posts: 2,238
  • Joined: 20-March 10

Re: gui drawing in c++

Posted 29 June 2011 - 03:14 AM

Yes there is no maybe about it

you do have to pass hWnd to the function void drawing.
right now GetDC will be returning NULL and thats
probably your biggest problem you can pass it by reference or value

passing it by value example.

void drawing(int from, int to, HWND hWnd)
{rest of function}



line 42 you declare

HDC hDC = GetDC(hWnd);


which is fine.
Then on line 72 you re-declare the same thing
HDC hDC = GetDC(hWnd);


why you have already declared it on line 42 you dont need to do it again ?

Then again you re-declare it a second time on line 101 ??

why you have already declared it on line 42 you dont need to do it again ?

In your WM_PAINT:
ReleaseDC(hWnd,hDC);


is outside it when it should be inside the WM_PAINT:
so your WM_PAINT looks like this
case WM_PAINT :
	           {
	.......rest of function....
	            DeleteObject(hfont1);
	            DeleteObject(hfont2);
	            DeleteObject(hfont3);
	            
                    ReleaseDC(hWnd,hDC);
                    break;
	           }




Finally in your WM_PAINT:
you should be using BeginPaint and EndPaint functions.
see MSDN links below on how to implement this.

BeginPaint MSDN

EndPaint MSDN

Best Wishes Snoopy.

This post has been edited by snoopy11: 29 June 2011 - 03:15 AM

Was This Post Helpful? 1
  • +
  • -

Page 1 of 1