PDA

View Full Version : Having some trouble drawing in a window


BrickInTheWall
03-02-2009, 11:49 PM
Hi guys,

I've been doing some programming, using the windows library, lately and have a problem.

Take a look at my WndProc function:


LRESULT CALLBACK WndProc( HWND hWnd, UINT nMsg, WPARAM wParam, LPARAM lParam ){

// Create an object which has all of the values to calculate X and dX
static XValues xObj( 100, 64, 1000, 0.0002 );

// Create two Point objects necessary to draw a line, which is drawn from 1 to 2
static Point point1( 0, 0 );
static Point point2( 0, 0 );

// Variables to store the difference in position between two values
static int dX = 0, dY = 5;
static int counter = 0;

// Device context and brush user for drawing
HDC hDC;
HBRUSH brush;


// Handle messages
switch( nMsg ){
case WM_CREATE:
// Calculate dX
dX = calcDX( &xObj );

// Set starting position
hDC = GetDC( hWnd );
MoveToEx( hDC, point1.x, point1.y, NULL );
ReleaseDC( hWnd, hDC );

// Create a timer, 1 ms
SetTimer( hWnd, 1, 1 , NULL );
break;

case WM_TIMER:
// Counter used to go through Sine_Table
// Don't walk off the array
if( counter > 63 ){
counter = 0;
}else{
counter++;
}

// Set the coordinates of the newest value
point2.x += dX;
point2.y = Sine_Table[counter];

// Get the dc for drawing
hDC = GetDC( hWnd );

// Draw the line
LineTo( hDC, point2.x, point2.y );

// Set next position
point1.x = point2.x;
point1.y = point2.y;

// Release the dc
ReleaseDC( hWnd, hDC );
break;

case WM_DESTROY:
KillTimer( hWnd, 1 );
PostQuitMessage( 0 );
break;

default:
// Windows handles everything else
return( DefWindowProc( hWnd, nMsg, wParam, lParam ) );
}
return 0;
}


Yes I know I can cut everything up into functions a bit more but I can't be bothered with that right now seeing as this program wont be extended after I complete what I want, which is this:

The program draws a sine wave on screen (right now its still upside down) using values I calculated. calcDX calculates the the distance between two values (depending on the frequency)...all of this is rather shallow so far as this problem I have is annoying me: the Line seems to always be drawn from (0,0). I don't know what is causing this. I thought that the LineTo function always draws from where it has last drawn to.


// Set next position
point1.x = point2.x;
point1.y = point2.y;

is useless right now though.

Any idea what I did wrong? I'm sure its something stupid that I am currently completely oblivious too but its late and I'm angry. :cool:

Cheers,
Chris