请教各位大大这样的问题,用RCP开发,派生SWT的Combo,代码是在网上找到的,但是发现一个奇怪的问题,就是所有被重载的方法都不会被调用,跟踪堆栈的时候发现调用的永远都是父类的方法,而掉不到重写的方法来。我的开发环境是:win7 x86_32 eclipse3.6 java 1.6.0_21源代码如下:package org.eclipse.swt.widgets;import org.eclipse.swt.SWT;
import org.eclipse.swt.events.PaintEvent;
import org.eclipse.swt.events.PaintListener;
import org.eclipse.swt.graphics.Drawable;
import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.graphics.GCData;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.internal.win32.DRAWITEMSTRUCT;
import org.eclipse.swt.internal.win32.LRESULT;
import org.eclipse.swt.internal.win32.OS;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;@SuppressWarnings("restriction")
public class CustomCombo extends Combo { public CustomCombo(Composite parent, int style) {
super(parent, style);
//我做了个实验,如果加入下面的语句,就会抛出java.lang.IllegalAccessError异常,说是没有权限
//widgetStyle();
final int CB_SETITEMHEIGHT = 0x0153;
OS.SendMessage( handle, CB_SETITEMHEIGHT, 0, 24 );
OS.SendMessage( handle, CB_SETITEMHEIGHT, -1, 24 );
}
@Override
protected void checkSubclass()
{
} @Override
//这个方法永远调用不到,总是调用到父类的方法去了
int widgetStyle() {
final int CBS_OWNERDRAWFIXED = 0x0010;
final int CBS_HASSTRINGS = 0x0200;
// final int CBS_OWNERDRAWVARIABLE = 0x0020;
return super.widgetStyle() | CBS_OWNERDRAWFIXED | CBS_HASSTRINGS;
} @Override
public void dispose()
{
super.dispose();
} @Override
//这个方法永远调用不到,总是调用到父类的方法去了
LRESULT wmDrawChild( int wParam, int lParam )
{
super.wmDrawChild(wParam, lParam);
DRAWITEMSTRUCT dis = new DRAWITEMSTRUCT();
OS.MoveMemory( dis, lParam, DRAWITEMSTRUCT.sizeof ); GC gc = new GC( new DCWrapper( dis.hDC ) );
Rectangle rc = new Rectangle( dis.left, dis.top, dis.right - dis.left,
dis.bottom - dis.top );
Display display = getDisplay();
if ( (dis.itemState & OS.ODS_SELECTED) != 0 )
{
gc
.setBackground( display
.getSystemColor( SWT.COLOR_LIST_SELECTION ) );
gc.setForeground( display
.getSystemColor( SWT.COLOR_LIST_SELECTION_TEXT ) );
gc.fillRectangle( rc );
}
else
{
gc.setBackground( display
.getSystemColor( SWT.COLOR_LIST_BACKGROUND ) );
gc.setForeground( display
.getSystemColor( SWT.COLOR_LIST_FOREGROUND ) );
gc.fillRectangle( rc );
}
String text = getItem( dis.itemID );
gc.drawText( text, dis.left + 20, dis.top );
gc.dispose();
return null;
}
private static class DCWrapper implements Drawable
{
private int hdc; DCWrapper( int hdc )
{
this.hdc = hdc;
} public int internal_new_GC( GCData data )
{
return hdc;
} public void internal_dispose_GC( int handle, GCData data )
{
}
}
import org.eclipse.swt.events.PaintEvent;
import org.eclipse.swt.events.PaintListener;
import org.eclipse.swt.graphics.Drawable;
import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.graphics.GCData;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.internal.win32.DRAWITEMSTRUCT;
import org.eclipse.swt.internal.win32.LRESULT;
import org.eclipse.swt.internal.win32.OS;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;@SuppressWarnings("restriction")
public class CustomCombo extends Combo { public CustomCombo(Composite parent, int style) {
super(parent, style);
//我做了个实验,如果加入下面的语句,就会抛出java.lang.IllegalAccessError异常,说是没有权限
//widgetStyle();
final int CB_SETITEMHEIGHT = 0x0153;
OS.SendMessage( handle, CB_SETITEMHEIGHT, 0, 24 );
OS.SendMessage( handle, CB_SETITEMHEIGHT, -1, 24 );
}
@Override
protected void checkSubclass()
{
} @Override
//这个方法永远调用不到,总是调用到父类的方法去了
int widgetStyle() {
final int CBS_OWNERDRAWFIXED = 0x0010;
final int CBS_HASSTRINGS = 0x0200;
// final int CBS_OWNERDRAWVARIABLE = 0x0020;
return super.widgetStyle() | CBS_OWNERDRAWFIXED | CBS_HASSTRINGS;
} @Override
public void dispose()
{
super.dispose();
} @Override
//这个方法永远调用不到,总是调用到父类的方法去了
LRESULT wmDrawChild( int wParam, int lParam )
{
super.wmDrawChild(wParam, lParam);
DRAWITEMSTRUCT dis = new DRAWITEMSTRUCT();
OS.MoveMemory( dis, lParam, DRAWITEMSTRUCT.sizeof ); GC gc = new GC( new DCWrapper( dis.hDC ) );
Rectangle rc = new Rectangle( dis.left, dis.top, dis.right - dis.left,
dis.bottom - dis.top );
Display display = getDisplay();
if ( (dis.itemState & OS.ODS_SELECTED) != 0 )
{
gc
.setBackground( display
.getSystemColor( SWT.COLOR_LIST_SELECTION ) );
gc.setForeground( display
.getSystemColor( SWT.COLOR_LIST_SELECTION_TEXT ) );
gc.fillRectangle( rc );
}
else
{
gc.setBackground( display
.getSystemColor( SWT.COLOR_LIST_BACKGROUND ) );
gc.setForeground( display
.getSystemColor( SWT.COLOR_LIST_FOREGROUND ) );
gc.fillRectangle( rc );
}
String text = getItem( dis.itemID );
gc.drawText( text, dis.left + 20, dis.top );
gc.dispose();
return null;
}
private static class DCWrapper implements Drawable
{
private int hdc; DCWrapper( int hdc )
{
this.hdc = hdc;
} public int internal_new_GC( GCData data )
{
return hdc;
} public void internal_dispose_GC( int handle, GCData data )
{
}
}
//这个方法永远调用不到,总是调用到父类的方法去了
int widgetStyle()
这个方法 父类是调不到的
public 加上