2011-02-26 51 views
1

我想繪製背景(像矩形或類似的東西),然後讓它在上面渲染組件。組件將在我繪製的頂部。有沒有辦法做到這一點?一個容器既可以被繪製,也可以在gtk2hs中顯示組件?

這是一個概念的例子。這隻顯示矩形。所以......只需要一些方法來告訴它去渲染組件。

{-# LANGUAGE PackageImports #-} 

import Graphics.UI.Gtk 
import Graphics.UI.Gtk.Gdk.EventM 
import Graphics.UI.Gtk.Gdk.GC 
import "mtl" Control.Monad.Trans(liftIO) 

main = do 
    initGUI 
    window <- windowNew 
    window `onDestroy` mainQuit 
    windowSetDefaultSize window 800 600 
    windowSetPosition window WinPosCenter 

    table <- tableNew 3 3 False 
    button <- buttonNewWithLabel "Test Button" 
    tableAttachDefaults table button 1 2 1 2 
    containerAdd window table 
    table `on` exposeEvent $ update 
    widgetShowAll table 

    widgetShowAll window 
    mainGUI 

update = do 
    win <- eventWindow 
    liftIO $ do 
    gc <- gcNew win 
    drawRectangle win gc False 10 10 90 90 
    return True 

回答

1

這不是特別針對gtk2hs。根據http://developer.gnome.org/gtk/2.24/GtkWidget.html#GtkWidget-expose-event,您需要在更新處理程序中使用return False,以便後續也調用其他處理程序。

當您在示例中更改該按鈕時,該按鈕將覆蓋整個窗口,因此該矩形將被隱藏。但是,例如,使用tableSetHomogeneous table True可以獲得理想的效果。

相關問題