Available in the LEADTOOLS Imaging toolkit. |
Paint event example for Visual J++
This example uses some Windows API functions to demonstrate the following:
1. Start with the project that you created in Loading and Displaying an Image.
2. Add the following declarations to the private section of your main form.
/** @dll.struct() */
class RECT
{
int left;
int top;
int right;
int bottom;
}
private RECT m_MyRect;
/** @dll.struct() */
class POINT
{
int x;
int y;
}
/** @dll.import("USER32") */
private static native void InvalidateRect ( int hwnd, RECT lpRect, int bErase );
/** @dll.import("GDI32") */
private static native int LineTo ( int hdc, int x, int y );
/** @dll.import("GDI32") */
private static native int MoveToEx ( int hdc, int x, int y, POINT lppt );
3. Return to Form1, add a button to the top of your main form, and in the properties box, change both its name and text to Toggle.
4. Add an edit box to the top of your form, and in the properties box, change its name to Directions., and change its Text property to the following:
Use the Toggle button to turn on or turn off Click-Splat
5. Add the following code to the Toggle button's Click event. This code uses the Enabled property to toggle the LEAD control's ability to respond to click events.
if( !LEAD1.getEnabled() )
{
LEAD1.setEnabled( true );
Directions.setText( "Click on the image for a splat" );
Toggle.setText( "Disable" );
}
else
{
LEAD1.setEnabled( false );
Directions.setText( "Click does nothing" );
Toggle.setText( "Enable" );
}
6. Add the following code to the LEAD1 Click event. When the Enabled property is true, this code uses a Windows API call to generate a paint event.
// Use InvalidateRect to generate a paint message
m_MyRect.left = 0;
m_MyRect.top = 0;
m_MyRect.bottom = (int) LEAD1.getScaleHeight();
m_MyRect.right = (int) LEAD1.getScaleWidth();
int hCtl = LEAD1.getHandle();
InvalidateRect( hCtl, m_MyRect, 1 );
7. Add the following code to the Lead1 Paint event. It uses Windows GDI functions to draw random lines on the control whenever there is a paint event.
// Create a device context for the control that the GDI functions can access
int hLeadClientDC = LEAD1.GetClientDC();
// Draw random lines on the control. This overlay does not affect the bitmap.
int xStart = (int) ( ( Math.random() * LEAD1.getScaleWidth() ) + 1f ) ;
int yStart = (int) ( ( Math.random() * LEAD1.getScaleHeight() ) + 1f ) ;
POINT point = new POINT() ;
MoveToEx( hLeadClientDC, xStart, yStart, point );
for( int i = 0 ; i < 50 ; i++ )
{
int xDelta = (int) ( ( 81 * Math.random() ) - 40f );
int yDelta = (int) ( ( 81 * Math.random() ) - 40f );
int xEnd = xStart + xDelta;
int yEnd = yStart + yDelta;
LineTo( hLeadClientDC, xEnd, yEnd );
MoveToEx( hLeadClientDC, xStart, yStart, point );
}
// Remove the lock from the device control
LEAD1.ReleaseClientDC();
8. Run your program to test it.