Click here to Skip to main content
15,390,470 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i have 2 questions if i may?..
1)i tried to use GetWindowText with the variables from &to but nothing happened they still 0 as i initialize them..i made a test to know if tey change or not by using WGPrintf(hDC,600,670,"%f",f); but it printed 0.00000 so it means it didn't change
2)i need the draw curve button to draw the curve win i click it so i think i must use the draw fn at case wm_command...but when i put it like that
case 1:
					{   
				       if(controlEvent==BN_CLICKED)
					   drawing(hWnd,from,to);
					   InvalidateRect(hWnd,NULL,FALSE);
					   break;
					}  

nothing appears so what shal i do???
thank you in advance...

#include "wingui.h"
#include "math.h"
#define PI 3.14
float m,l,f,t,c;
static float from =0 ,to =0 ;
int h,k, r=0,z=10,a=-25;

int width = GetSystemMetrics(SM_CXSCREEN);
int height = GetSystemMetrics(SM_CYSCREEN);
void drawing(HWND hWnd,float f,float t)
 {
   HDC hDC=GetDC(hWnd);
   HPEN holdPen, hPen1;
   hPen1   = WGCreatePen   (RGB(0,255,0),5,PS_SOLID);
   holdPen = (HPEN)SelectObject(hDC,hPen1);
   MoveToEx(hDC,74,235,NULL); 
   if (z<18)
     { 
       do
         { 
           float y = sin(((f-74)*PI/100)*10/z);
           LineTo(hDC,f,235-(y*100)*z/10);
           f+=1;
         } while(f<t);>
     }
   else 
     {
       do
         {
		   float y = sin(((f-74)*PI/100)*10/17);
           LineTo(hDC,f,235-(y*100)*17/10);
           f+=1;
         } while(f<t);>
     }
  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);
    int width = GetSystemMetrics(SM_CXSCREEN);
	int height = GetSystemMetrics(SM_CYSCREEN);
	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, 16*width/20, 1*height/12 , 200, 50, hWnd ,(HMENU)(UINT_PTR)1,NULL,NULL);
	  SetWindowText(hButton1, "Draw Curve");
    hButton2 = CreateWindow("Button", "" , WS_CHILD|WS_VISIBLE, 16*width/20 , 2*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, 4*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,625 ,620,35,hWnd,(HMENU)(UINT_PTR)5,NULL,NULL);
      SetWindowText(hFirstEdit,"");   //f(x)
    hSecondEdit =CreateWindow("EDIT","",WS_CHILD|WS_VISIBLE|WS_BORDER|ES_MULTILINE|ES_WANTRETURN,95,670 ,60,25,hWnd,(HMENU)(UINT_PTR)6,NULL,NULL);
      SetWindowText(hSecondEdit,"");   //from
    hThirdEdit =CreateWindow("EDIT","",WS_CHILD|WS_VISIBLE|WS_BORDER|ES_MULTILINE|ES_WANTRETURN,190,670 ,80,25,hWnd,(HMENU)(UINT_PTR)7,NULL,NULL);
      SetWindowText(hThirdEdit,"");   //to
    hFourthEdit =CreateWindow("EDIT","",WS_CHILD|WS_VISIBLE|WS_BORDER|ES_MULTILINE|ES_WANTRETURN,375,670 ,80,25,hWnd,(HMENU)(UINT_PTR)8,NULL,NULL);
      SetWindowText(hFourthEdit,"");   //resolution
    hFifthEdit =CreateWindow("EDIT","",WS_CHILD|WS_VISIBLE|WS_BORDER|ES_MULTILINE|ES_WANTRETURN,490,670 ,80,25,hWnd,(HMENU)(UINT_PTR)9,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;
			 switch(controlID)
			   {
				
                 case 1:
					 { 
					  if(controlEvent==BN_CLICKED)  
				     
					  InvalidateRect(hWnd,NULL,FALSE);
					   break;
					 }   
				 case 3:              
					 {
				       if(controlEvent==BN_CLICKED)
					   z++;
					   InvalidateRect(hWnd,NULL,FALSE);
					   
				       break;
				     }
                case 4:              
				     {
				      if(controlEvent==BN_CLICKED) 
					  z--;
					  InvalidateRect(hWnd,NULL,FALSE);
			           break;
					 }
					
					  case 6:              
				       {
                        if(controlEvent==EN_CHANGE)
					      {
				            char text1[128];
						    GetWindowText(hSecondEdit,text1,128);
				            from = atof(text1);
					        f=from;
						WGPrintf(hDC,600,670,"%f",f);
							break;
				          }
					   }
					     
			     case 7:              
				     {
				       if(controlEvent==EN_CHANGE)
					  {
						  char text2[128];
				          GetWindowText(hSecondEdit,text2,128);
				          to = atof(text2);
						  t=to;
						  break;
					  }
					 }
				     }
				
			ReleaseDC(hWnd,hDC);
			return 0;
	}
		
          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,1010,455);
			
			SelectObject(hDC,hbrush5);
            Rectangle(hDC,90,470,710,615);
	
			SelectObject(hDC,hbrush1);
			SetBkMode(hDC, TRANSPARENT);
			WGPrintf(hDC,10,525,"Equation");
			WGPrintf(hDC,40,625,"F(X)");
            WGPrintf(hDC,40,670,"From");
            WGPrintf(hDC,160,670,"To");
            WGPrintf(hDC,275,670,"Resolution");
            WGPrintf(hDC,460,670,"x=");
			
           holdPen = (HPEN)SelectObject(hDC,hPen2);
           for(int i =70;i<width-400;i>            {
              for( int h =60;h<400;h=h+30)
               {
                 Rectangle(hDC,i,h,i+35,h+30);
               }
            }
		    SelectObject(hDC,hPen3);  
			MoveToEx(hDC,70,60,NULL);
			LineTo(hDC,980,60);
			MoveToEx(hDC,70,420,NULL);
			LineTo(hDC,980,420);
		    MoveToEx(hDC,70,60,NULL);
			LineTo(hDC,70,420);
			MoveToEx(hDC,980,60,NULL);
			LineTo(hDC,980,420);
			
			
		
		       k=0;
                for(int i=80;i<=1015;i=i+35)
              {
               SetBkMode(hDC, TRANSPARENT);
               SetTextColor(hDC,RGB(225,200,0));
               SelectObject(hDC,hfont1);
               WGPrintf(hDC,i,420,"%d",10*k/z);
               k+=5;
		      }                              
		
			 
			m=0.24;
            for(int i=50;i<=410;i=i+30)
            {
              SetTextColor(hDC,RGB(225,200,0));
              SelectObject(hDC,hfont2);
              WGPrintf(hDC,40,i,"%.2f",10*m/z);
              m-=0.02;
            }
			 drawing(hWnd,f,t);
			
			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);
			ReleaseDC(hWnd,hDC);
			break;
		   }
	}
	return WGDefaultWindowProc(hWnd, message, wParam, lParam);
}

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

I haven't done any C++ in WinForms in many years so I may be a little off base but I do a lot of custom drawing with C#.
Why are you calling the drawing() function then doing a InvalidateRect?
You are drawing then calling InvalidateRect which forces the WM_PAINT message to be sent.
All drawing needs to be centralized and done when the WM_PAINT message is sent.

Hope this helps and good luck.
   
Comments
   
Those points are all correct. I can confirm it as I clearly remember drawing in raw Windows API. My 5.
--SA
Albert Holguin 3-Jul-11 13:19pm
   
Point about InvalidateRect() is correct...
1. You try calling GetWindowText(hSecondEdit,text1,128); but you've set the text to blank before on WM_CREATE with SetWindowText(hSecondEdit,"");, that's the reason you get blanks back.

2. You seem to be attempting to draw to the main window, but then you overwrite that after you call InvalidateRect(). You should probably define where you're going to be drawing to better (as in a separate window or a control on the main window).
   
Comments
Mike Hankey 3-Jul-11 13:42pm
   
I've found that drawing directly to the/a window is much more efficient and cuts down on flickering. Some controls are more efficient then others, i.e. the Panel control is very inefficient never use it as a canvas.
Albert Holguin 3-Jul-11 15:44pm
   
There's other ways to get around the flickering... draw off screen then bit blit...
Mike Hankey 3-Jul-11 16:15pm
   
True enough, guess was thinking more along the lines of C#. Like I mentioned above it's been a long time since I've done C++ in WinForms.
Albert Holguin 3-Jul-11 18:01pm
   
WinForms is also a .Net thing, not really C++/WinAPI/MFC... lol

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900