您现在的位置是:首页 >

wxpython自定义控件 开发Eclipse下的自定义控件[2]

火烧 2022-04-25 23:57:06 1035
开发Ecli e下的自定义控件[2] addPai tLi te er ew Pai tLi te er { u lic void ai tCo trol Pai tEve t e {GC gc =

开发Eclipse下的自定义控件[2]  

addPaintListener(new PaintListener() {public void paintControl(PaintEvent e) {GC gc = e gc;Point size = getSize();int beginx = e x;int beginy = (e y / lineHeight) * lineHeight;int beginLine = (e y cy) / lineHeight;int endLine = beginLine + e height / lineHeight + ;if (endLine > getItemCount())endLine = getItemCount();for (int i = beginLine; i < endLine; i++) {boolean selected = false;if (i == rowSel)selected = true;onPaint(gc i cx beginy + (i beginLine) * lineHeight selected);}}});

  这里要注意的是从PaintEvent中获取的x y height width是需要重绘的区域 x y是以控件的左上角为原点的坐标 在我们的程序中 为了性能起见 我们先根据需要重绘的区域计算出需要重绘的行数 只重绘相应的行 而不是将整个控件重绘 我们程序中用到的onPaint用于绘制一行

  接下来 我们要让我们的控件响应键盘上下键对列表项进行选择 我们已对向上键的处理为例 首先当用户按了向上键时 我们需要改变选择 并且重绘旧的和新的选择项 如果选择项已经到了列表的顶部 我们还需要同时滚动滚动条

addListener(SWT KeyDown new Listener() {public void handleEvent(Event event) {switch (event keyCode) {case SWT ARROW_UP: // 处理向上键if (rowSel != ) {oldRowSel = rowSel;rowSel ;if (oldRowSel != rowSel) { //发送消息让控件重绘((Canvas) event widget) redraw(cx (rowSel + cy/ lineHeight)* lineHeight maxX lineHeight* false);}if (rowSel < cy / lineHeight) { //如果需要 滚动滚动条ScrollBar bar = ((Canvas) event widget) getVerticalBar();bar setSelection(bar getSelection() lineHeight);scrollVertical(bar);}selectionChanged(); // 发送selectionChanged事件}break;case SWT ARROW_DOWN: // down arror key…break;}}});

wxpython自定义控件 开发Eclipse下的自定义控件[2]

  接下来 我们要让我们的控件响应鼠标对列表项进行选择 首先我们要计算出鼠标选中的行号 注意MouseEvent中的y值只是相对于控件左上角的坐标 我们需要加上滚动出了控件的部分

lishixinzhi/Article/program/Java/ky/201311/28985  
永远跟党走
  • 如果你觉得本站很棒,可以通过扫码支付打赏哦!

    • 微信收款码
    • 支付宝收款码