Thread: Variable not updating in WM_PAINT after calling InvalidateRect() in WM_COMMAND

  1. #1
    Registered User
    Join Date
    Apr 2008
    Posts
    610

    Variable not updating in WM_PAINT after calling InvalidateRect() in WM_COMMAND

    I have a global variable 'output' to a structure CALC_DATA...

    Code:
    CALC_DATA output={0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
    this variable is used in two places, 1st within WM_PAINT

    Code:
    	case WM_PAINT:
    		{
    			hdc=BeginPaint(hwnd, &pntS);
    
    			// Show calculatd results
    			DisplayResults(hwnd, hdc, output, rsltFont);
    
    			EndPaint(hwnd, &pntS);
    		}
    and secondly within WM_COMMAND where it gets updated...

    Code:
    		case IDBC_RESULTSBUTTON:
    			 {
    				 if(HIWORD(wParam) == BN_CLICKED)
    				 {
    					 /* Calculate results from input data */
    					 output = ProcessData(hwnd);
    					
    					 /* Repaint */
    					 InvalidateRect (hwnd, NULL, TRUE);
    					 UpdateWindow(hwnd);
    				 }
    			 }
    The program initially sets all the edit boxes with values from a file, by pressing the RESULTS button function ProcessData first grabs data from the edit boxes, calculates results and store them in the variable output. Then the window is repainted to call DisplayResults() with the new values on output.

    PROBLEM: By changing the values on edit boxes to different values (this time not reading from file), and pressing RESULTS button for the second time, after repaint, results and edit boxes are back to the original values... When i run this on debug mode i notice that new values are correctly read from edit boxes and output within WM_COMMAND is correctly initialized with new results but output in WM_PAINT is still what it was before... Q. How can the same variable have different values in WM_PAINT & WM_COMMAND if declared globally?

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    What does ProcessData(hwnd) look like?

    Generally, assigning arrays is not allowed in C, so I'm a bit surprised that it even compiles.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User
    Join Date
    Apr 2008
    Posts
    610
    Quote Originally Posted by matsp View Post
    What does ProcessData(hwnd) look like?

    Generally, assigning arrays is not allowed in C, so I'm a bit surprised that it even compiles.

    --
    Mats
    output is not an array, it' a structure...
    Code:
    typedef struct outdata {
    	double peakPower;
    	double antennaGain;
    	int polarLoss;
    	double radarCrossSect;
    	double wavelenght;
    	double bandwidth;
    	double noiseFigure;
    	double stcTarget;
    	double loss;
    	double cnstnt;
    	double figOfMerit;
    	double radarRange;
    	double PRFRange;
    	double radarHrzn;
    	double maxRadarRange;
    	double maxNMRdrRange;
    } CALC_DATA;
    ProcessData()....

    Code:
    // Calculate required outputs
    CALC_DATA ProcessData(HWND hwnd)
    {
    	CALC_DATA results;
    	
    	/* First retreive data from edit controls */
    	INPUTS inData = readValues(hwnd);	
    
    	/* Calculate all data and produce results to the results structure */
    	results.peakPower = peakPower( inData.rdrTrsRcInfo.pkpwr );
    	results.antennaGain = antennaGain( inData.rdrAntInfo.gain );
    	results.polarLoss = polarLoss( inData.rdrAntInfo.polar );
    	results.radarCrossSect = radarCrossSect( inData.rdrTrgtInfo.rcs );
    	results.wavelenght = waveLenght( inData.rdrTrsRcInfo.freq );
    	results.bandwidth = bandWidth( inData.rdrTrsRcInfo.bndwdth );
    	results.noiseFigure = noiseFigure( inData.rdrTrsRcInfo.noisef );
    	results.stcTarget = staticTarget( inData.genParmInfo.fare, inData.genParmInfo.dtcProb );
    	results.loss = losses( inData.rdrTrsRcInfo.losses );
    	results.cnstnt = constant();
    	results.figOfMerit = figureOfMerit( results.peakPower, results.antennaGain, results.polarLoss, 
    		results.radarCrossSect,results.wavelenght, results.bandwidth, results.noiseFigure, 
    		results.stcTarget, results.loss, results.cnstnt );
    	results.radarRange = radarRange( results.figOfMerit );
    	results.PRFRange = PRFrange( inData.rdrTrsRcInfo.prf );
    	results.radarHrzn = radarHorizon( inData.rdrTrgtInfo.HoT, inData.rdrAntInfo.height );
    	results.maxRadarRange = maxRadarRange(results.radarRange, results.PRFRange, results.radarHrzn, &results.maxNMRdrRange);
    
    	return results;
    }
    all it does is to call functions to perform calculations, then store results to a structure... As i said after stepping through the program using debug tool, this function works 100%... has to do with visibility of 'output' between WM_COMMAND & WM_PAINT and the call of InvalidateRect()...

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    A global variable is visible everywhere. So is the output variable OK at the point where you call InvalidateRect()?
    What if you put a breakpoint on the call to the DisplayResults() function?

    I would still pass in the struct of CALCDATA (as a pointer) into the calculation function - if nothing else, it saves copying the entire data structure. Oh, and use const pointer in DisplayResults. Or if you use C++ you could use reference and const reference respectively.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    Registered User
    Join Date
    Apr 2008
    Posts
    610
    Quote Originally Posted by matsp View Post
    A global variable is visible everywhere. So is the output variable OK at the point where you call InvalidateRect()?
    What if you put a breakpoint on the call to the DisplayResults() function?

    I would still pass in the struct of CALCDATA (as a pointer) into the calculation function - if nothing else, it saves copying the entire data structure. Oh, and use const pointer in DisplayResults. Or if you use C++ you could use reference and const reference respectively.

    --
    Mats
    Quote Originally Posted by matsp View Post
    So is the output variable OK at the point where you call InvalidateRect(), What if you put a breakpoint on the call to the DisplayResults() function?
    Exactly what am trying to figure out... Have tried using breakpoints in vain... I use run-to-cursor (CNTRL+F10) to help debug quicker... Taking it from my Visual Studio C++ 6.0 experience, clicking next to the line (on the grey area) inserts a breakpoint. Then running the code normaly (CNTRL+F5) should stop at the break point, but it doesn't....

    use const pointer in DisplayResults. Or if you use C++ you could use reference and const reference respectively.
    After making these changes now output gets updated correctly, thnx... BUT, repainting resets the edit boxes with old values (ones read from the file)... I have two buttons, one to read from file and one to calculate... When i press calculate for the first time after pressing input file button, results are based on data on edit boxes read from file... then after manually changing the figures and press calculate for the second time... Now correct results are shown (thanx to you), but 'edit boxes are reset back to what was read from the file'

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Pressing F9 on the line you want to stop at should set a breakpoint.

    Is it possible that your are somehow re-reading the values from the file when the dialog box is updated after you've pressed the button?

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  7. #7
    Registered User
    Join Date
    Apr 2008
    Posts
    610
    Quote Originally Posted by matsp View Post
    Pressing F9 on the line you want to stop at should set a breakpoint.
    pressed F9 then CNTRL+F5, program runs but doesn't stop at break point

    Is it possible that your are somehow re-reading the values from the file when the dialog box is updated after you've pressed the button?
    This is a function which sets edit boxes with values and the only place it's called

    Code:
    		case IDBC_FILEBUTTON:
    			 {
    				 if(HIWORD(wParam) == BN_CLICKED)
    				 {
    					 indata=ReadFromFile(hwnd);
    					 SetValues(hwnd, &indata);
    				 }
    			 }
    		}
    
    .............
    
    // Initialize Edit boxes with values from in structure
    void SetValues(HWND hwnd, const INPUTS *in)
    {
    	SetRdrAntn(hwnd, in->rdrAntInfo);		// Radar Antenna edit boxes
    	SetTransReceiv(hwnd, in->rdrTrsRcInfo);	// Transmitter/Receiver edit boxes
    	SetTarget(hwnd, in->rdrTrgtInfo);		// Target edit boxes
    	SetGenParams(hwnd, in->genParmInfo);	// General Parameters edit boxes
    }
    Last edited by csonx_p; 05-28-2008 at 03:48 AM.

  8. #8
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by csonx_p View Post
    pressed F9 then CNTRL+F5, program runs but doesn't stop at break point
    You must run with debugging if you want it to stop at breakpoints. Ctrl+F5 runs it without debugging. To run with debugging, just hit F5.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  9. #9
    Registered User
    Join Date
    Apr 2008
    Posts
    610
    Quote Originally Posted by matsp View Post
    Is it possible that your are somehow re-reading the values from the file when the dialog box is updated after you've pressed the button?

    --
    Mats
    What a shame... Forgot to put break; after each case with WM_COMMAND.... Now sorted, thanx for your efforts

  10. #10
    Registered User
    Join Date
    Apr 2008
    Posts
    610
    Quote Originally Posted by Elysia View Post
    You must run with debugging if you want it to stop at breakpoints. Ctrl+F5 runs it without debugging. To run with debugging, just hit F5.
    doesn't help, just runs... arh! will do without brekpoints canceled!!!

    Works, thanx... but then still can't switch between debug & window.. But it's cool!
    Last edited by csonx_p; 05-28-2008 at 03:59 AM.

  11. #11
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by csonx_p View Post
    Works, thanx... but then still can't switch between debug & window.. But it's cool!
    Unfortunately, that's due to the debugger design.
    There is another thread about it. You will have to retort to threads and tricks to solve it.
    Last edited by Elysia; 05-28-2008 at 04:09 AM.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  12. #12
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by csonx_p View Post
    doesn't help, just runs... arh! will do without brekpoints canceled!!!

    Works, thanx... but then still can't switch between debug & window.. But it's cool!
    Normally, when I debug "paint" problems, I do the debugging (stepping, looking at variables), then remove the breakpoint again to see the actual result on the screen.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  13. #13
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    One trick is to put a MessageBox in your code, apply code changes (Alt+F10), then run the program. IF you run the message handler in another thread, then you will be able to see the window.
    Remember that the message loop MUST be done in a separate thread so that it can continue to loop and process messages, or you will get the same problem as before - frozen window.

    This is usually how I do it.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sorting number
    By Leslie in forum C Programming
    Replies: 8
    Last Post: 05-20-2009, 04:23 AM
  2. Calling system with a string variable
    By 911help in forum C Programming
    Replies: 8
    Last Post: 12-27-2007, 08:32 PM
  3. Replies: 5
    Last Post: 01-13-2006, 12:00 AM
  4. Calling functions via variable
    By SoundFX in forum C++ Programming
    Replies: 3
    Last Post: 08-30-2005, 10:26 PM
  5. Variable Allocation in a simple operating system
    By awkeller in forum C Programming
    Replies: 1
    Last Post: 12-08-2001, 02:26 PM