2014-05-19 212 views
1

将此组件拖放到窗体上时,TImage未显示地图图像(六边形)直到我将组件拖动到窗体上,然后才会显示它,直到我停止拖动窗体。 (这一切都处于设计模式)。我如何让它始终显示?不要只是拖动它。TImage Not Showing up

type 
     THexMap = Class(TScrollingWinControl) 

构造

Constructor THexMap.Create(AOwner: Tcomponent); 
begin 
    inherited Create(AOwner); 

    Width := DEFAULT_MAP_WIDTH; 
    Height := DEFAULT_MAP_HEIGHT; 

    FCanvas := timage.Create(self); 
    tempMap := timage.Create(self); 

    fcanvas.Parent := self; 
    tempmap.Parent := self; 

    fCanvas.Width := DEFAULT_MAP_WIDTH; 
    fCAnvas.Height := DEFAULT_MAP_WIDTH; 


    { Set intial property values for component } 

    //create map 
    MakeSolidMap; 

end; 

MakeSolidMap

Procedure THexMap.MakeSolidMap; 
var 
p0 : TPoint; 
looprow,Loopcol : integer; 
begin 
     TempMap.width := ((HexColumns-1) * round((1.5 * HexRadius))) + (2 * hexRadius); 
     TempMap.height := ((HexRows) * (2 * rise)) + rise; 

     With TempMap.Canvas do 
     begin 
     {set Background color} 
     brush.Color := BackColor; 
     fillrect(rect(0,0,TempMap.Width,TempMap.Height)); 

     {draw Hex's left to right/top to bottom} 
     for looprow := 1 to HexRows do 
      begin 
      for loopcol := 1 to HexColumns do 
       begin 
       {compute center coords} 
       p0 := ConvertCoords(Point(LoopCol,LoopRow),ptROWCOL); 

       {draw the hex} 
       DrawSolidHex(Tempmap,bsSolid,hexColor,psSolid,LineColor,P0.X,p0.Y,hexRadius,hex3d); 

      end; 
      end; 
     end; 
end; 

DrawSoildHex

procedure THexMap.DrawSolidHex(Target: timage; 
           FillStyle: TBrushStyle; 
           FillColor: TColor; 
           LineStyle: TPenStyle; 
           LineColor: TColor; 
           x: Integer; 
           y: Integer; 
           Radius: Integer; 
           button: Boolean); 
var 
    p0,p1,p2,p3,p4,p5,p6:TPoint; 
begin 
    p0 := Point(x,y); 

    {compute each point based on hex center} 
    p1.X := p0.X - round(Radius /2); 
    p1.Y := p0.Y - rise; 
    p2.X := p0.X + round(Radius/2); 
    p2.Y := p1.Y; 
    p3.X := p0.X + Radius; 
    p3.Y := p0.Y; 
    p4.X := p2.X; 
    p4.Y := p0.Y + rise; 
    p5.X := p1.X; 
    p5.Y := p4.Y; 
    p6.X := p0.X - Radius; 
    p6.Y := p0.Y; 

    {set color/style of lines} 
    target.canvas.Pen.Color := LineColor; 
    target.canvas.Pen.Style := LineStyle; 

    {set color/style of hex} 
    target.canvas.Brush.Color := FillColor; 
    Target.canvas.Brush.Style := FillStyle; 

    {draw the hex} 
    target.canvas.Polygon([p1,p2,p3,p4,p5,p6]); 

    {if desired, draw the boarder for the hex} 
    if button = true then 
    begin 
    with target.canvas do 
    begin 
     pen.Mode :=pmCopy; 
     pen.Color :=clWhite; 
     moveto(p5.X+1,p5.Y-1); 
     lineto(p6.X+1,p6.Y); 
     lineto(p1.X+1,p1.Y+1); 
     lineto(p2.X-1,p2.Y+1); 
     pen.Color :=clBlack; 
     lineto(p3.X-1,p3.Y); 
     lineto(p4.X-1,p4.Y-1); 
     lineto(p5.X+1,p5.Y-1); 
    end; 
    end; 
    invalidate; 
end; 

的WndProc

procedure THexMap.WndProc(var Message: TMessage); 
const 
    DISCARD_CURRENT_ORIGIN = nil; 
var 
    R : TRect; 
    PS : PAINTSTRUCT; 
begin 
    if Message.Msg = WM_PAINT then 
    begin 
    if GetUpdateRect(Handle, nil, false) then 
    begin 
     BeginPaint(Handle, PS); 
     try 
     R := PS.rcPaint; 
     bitblt(fCanvas.Canvas.Handle, R.Left, R.Top, R.Width, R.Height, TempMap.Canvas.Handle, R.Left+FOffset.X, R.Top+FOffset.Y, SRCCOPY); 
     finally 
     EndPaint(Handle, PS); 
     end; 
    end 
    else 
     inherited; 
    end 
    else 
    inherited; 
end; 
+0

GetUpdateRect总是返回true。如果没有什么要更新,为什么要合成一个油漆信息? –

+0

你是对的。我已经添加了它,因为我将它设置为假时,我做了一个不同的过程,只改变其中一个六边形颜色/图像因此,我可以只改变一个六边形而不是整个图像。但是这似乎对我产生了反感......谢谢! –

回答

3

什么都没有显示,因为您已经接管了WM_PAINT的控制权。在处理WM_PAINT时,您不会在由BeginPaint返回的设备上下文中绘制任何内容。您不会调用继承的处理程序,该程序会调用Paint,然后绘制子对象。因此没有任何东西出现在你的控制下

在我看来,你需要决定使用视觉控件,让VCL画它们,或者自己绘制你的控件。你目前正试图做到两者兼得,但都做不到!

我不能建议修复,因为我真的不知道你在做什么。我不明白你为什么有视觉控制并重写paint消息处理程序。要继续前进,您需要选择一种或另一种方法。

+0

正是!去除油漆并让组件控制它的工作,当然最终我将不得不改变这一点,但我为什么会发生这个问题的大问题已经解决了!谢谢。 –