怎么windows.polygon不灵啦?(50分)

  • 主题发起人 主题发起人 jqw
  • 开始时间 开始时间
J

jqw

Unregistered / Unconfirmed
GUEST, unregistred user!
各位兄弟:<br><br>&nbsp; &nbsp;我在Delphi4+NT下调用Windows API函数Polygon,但就是<br>画不出多边形来,这是为什么?请各位大吓帮忙。<br><br>&nbsp; 下面是部分程序:<br><br>procedure TForm1.Button1Click(Sender: TObject);<br>var<br>&nbsp; //<br>&nbsp; DC:HDC;<br>&nbsp; brush:HBRUSH;<br>&nbsp; PEN:HPEn;<br>&nbsp; oldmode:integer;<br>&nbsp; //<br>&nbsp; PointsArr:Array of TPoint;<br>begin<br>&nbsp; DC:=GetDC(Handle);//换成 DC:=Canvas.Handle 也不灵<br>&nbsp; brush:=0;<br>&nbsp; pen:=0;<br>&nbsp; //<br><br>&nbsp; try<br>&nbsp; &nbsp; brush:=CreateSolidBrush(Clred);<br>&nbsp; &nbsp; oldMode:=SetBkMode(DC,Transparent);//<br>&nbsp; &nbsp; SelectObject(DC,brush);<br><br>&nbsp; &nbsp; Pen:=CreatePen(PS_SOLID,1,clred);<br>&nbsp; &nbsp; SelectObject(DC,Pen);<br><br>&nbsp; &nbsp; SetLength(PointsArr,4);//<br><br>&nbsp; &nbsp; PointsArr[0]:=Point(20,2);<br>&nbsp; &nbsp; PointsArr[1]:=Point(120,6);<br>&nbsp; &nbsp; PointsArr[2]:=Point(100,200);<br>&nbsp; &nbsp; PointsArr[3]:=Point(20,200);<br><br>&nbsp; &nbsp; Windows.Polygon(DC,PointsArr, 4); //??<br>&nbsp; finally<br>&nbsp; &nbsp; DeleteObject(brush);<br>&nbsp; &nbsp; DeleteObject(PEN);<br>&nbsp; end;<br><br>&nbsp; SetBkMode(DC,oldMode);//<br>end;<br><br>&nbsp; 多谢!!<br>
 
兄弟们,帮忙呀?!<br>分不够,可以再加!
 
兄弟们,帮忙呀?!<br>分不够,可以再加!<br>
 
按照你的要求改了一下 :-)<br><br>procedure TForm1.Button1Click(Sender: TObject);<br>var<br>&nbsp; DC:HDC;<br>&nbsp; brush:HBRUSH;<br>&nbsp; PEN:HPEn;<br>&nbsp; oldmode:integer;<br>&nbsp; Acanvas:TCanvas;<br>&nbsp; PointsArr:Array of TPoint;<br>begin<br>&nbsp; DC:=GetDC(0);<br>&nbsp; brush:=0;<br>&nbsp; pen:=0;<br>&nbsp; Acanvas:=TCanvas.Create;<br>&nbsp; Acanvas.Handle:=DC;<br><br>&nbsp; try<br>&nbsp; &nbsp; brush:=CreateSolidBrush(Clred);<br>&nbsp; &nbsp; oldMode:=SetBkMode(DC,Transparent);<br>&nbsp; &nbsp; SelectObject(DC,brush);<br><br>&nbsp; &nbsp; Pen:=CreatePen(PS_SOLID,1,clred);<br>&nbsp; &nbsp; SelectObject(DC,Pen);<br><br>&nbsp; &nbsp; SetLength(PointsArr,4);<br><br>&nbsp; &nbsp; PointsArr[0]:=Point(20,2);<br>&nbsp; &nbsp; PointsArr[1]:=Point(120,6);<br>&nbsp; &nbsp; PointsArr[2]:=Point(100,200);<br>&nbsp; &nbsp; PointsArr[3]:=Point(20,200);<br><br>&nbsp; &nbsp; Acanvas.Polygon(PointsArr);<br>&nbsp; finally<br>&nbsp; &nbsp; DeleteObject(brush);<br>&nbsp; &nbsp; DeleteObject(pen);<br>&nbsp; &nbsp; Acanvas.Free;<br>&nbsp; end;<br><br>&nbsp; SetBkMode(DC,oldMode);<br>&nbsp; ReleaseDC (0,dc);<br><br>end;<br>
 
这样做…<br>type<br>&nbsp; TXYPoints = array[0..1023] of TPoint;<br>&nbsp; PXYPoints = ^TXYPoints;<br>&nbsp; ....<br><br>procedure TForm1.Button1Click(Sender: TObject);<br>var<br>&nbsp; //<br>&nbsp; DC:HDC;<br>&nbsp; brush:HBRUSH;<br>&nbsp; PEN:HPEn;<br>&nbsp; oldmode:integer;<br>&nbsp; //<br>&nbsp; PointsArr: &nbsp;PXYPoints; &nbsp;//指针,动态的<br>begin<br>&nbsp; DC:=GetDC(Handle);//换成 DC:=Canvas.Handle 也不灵<br>&nbsp; brush:=0;<br>&nbsp; pen:=0;<br>&nbsp; //<br><br>&nbsp; try<br>&nbsp; &nbsp; brush:=CreateSolidBrush(Clred);<br>&nbsp; &nbsp; oldMode:=SetBkMode(DC,Transparent);//<br>&nbsp; &nbsp; SelectObject(DC,brush);<br><br>&nbsp; &nbsp; Pen:=CreatePen(PS_SOLID,1,clred);<br>&nbsp; &nbsp; SelectObject(DC,Pen);<br><br>&nbsp; &nbsp; GetMem(PointsArr,4 * SizeOf(TPoint));<br><br>&nbsp; &nbsp; PointsArr[0] := Point(20,2);<br>&nbsp; &nbsp; PointsArr[1] := Point(120,6);<br>&nbsp; &nbsp; PointsArr[2] := Point(100,200);<br>&nbsp; &nbsp; PointsArr[3] := Point(20,200);<br><br>&nbsp; &nbsp; Windows.Polygon(DC,PointsArr^, 4); <br>&nbsp; &nbsp; FreeMem(PointsArr,4 * SizeOf(TPoint));<br>&nbsp; finally<br>&nbsp; &nbsp; DeleteObject(brush);<br>&nbsp; &nbsp; DeleteObject(PEN);<br>&nbsp; end;<br><br>&nbsp; SetBkMode(DC,oldMode);//<br>end;<br>
 
wjiachun<br>呵,你画到屏幕上去了!<br>jqw<br>问题的关键是setlength不对,不信你把你的程序改成这样就可以了。<br><br>procedure TForm1.Button1Click(Sender: TObject);<br>var<br>&nbsp; //<br>&nbsp; DC:HDC;<br>&nbsp; brush:HBRUSH;<br>&nbsp; PEN:HPEn;<br>&nbsp; oldmode:integer;<br>&nbsp; //<br>&nbsp; PointsArr:Array[0..3] of TPoint;<br>begin<br>&nbsp; DC:=GetDC(Handle); &nbsp;brush:=0;<br>&nbsp; pen:=0;<br>&nbsp; //<br><br>&nbsp; try<br>&nbsp; &nbsp; brush:=CreateSolidBrush(Clred);<br>&nbsp; &nbsp; oldMode:=SetBkMode(DC,Transparent);//<br>&nbsp; &nbsp; SelectObject(DC,brush);<br><br>&nbsp; &nbsp; Pen:=CreatePen(PS_SOLID,1,clred);<br>&nbsp; &nbsp; SelectObject(DC,Pen);<br><br>&nbsp; &nbsp; //SetLength(PointsArr,4);<br>&nbsp; &nbsp; ShowMessage(IntToStr(SizeOf(PointsArr))); //长度为32个字节,如果用原来你的setlength,长度仍为4个字节<br>&nbsp; &nbsp; PointsArr[0]:=Point(20,2);<br>&nbsp; &nbsp; PointsArr[1]:=Point(120,6);<br>&nbsp; &nbsp; PointsArr[2]:=Point(100,200);<br>&nbsp; &nbsp; PointsArr[3]:=Point(20,200);<br><br>&nbsp; &nbsp; Windows.Polygon(DC,PointsArr, 4); //??<br>&nbsp; finally<br>&nbsp; &nbsp; DeleteObject(brush);<br>&nbsp; &nbsp; DeleteObject(PEN);<br>&nbsp; end;<br><br>&nbsp; SetBkMode(DC,oldMode);//<br>end;<br><br>
 
吕雪松:我以为他就是想屏幕作画的。
 
谢谢两位大虾,我先试试!
 
to wjiachun:我心里一直在谢谢你对我和大家的帮助<br>to 吕雪松:大虾一针见血<br>
 
&nbsp;这个问题其实不是 SetLength 的原因。因为动态数组的实质其实是一个指针,不论 SetLength<br>多少,其 Length 总是 4 。但是数组的长度的确是正确地设置了,否则后来会出现非法访问。<br><br>&nbsp; jqw 的源程序只要改一个地方就可以了,这是因为动态数组和静态数组的内存处理不同引起的:<br><br>&nbsp; &nbsp; &nbsp; Windows.Polygon(DC,PointsArr, 4); <br>改成:Windows.Polygon(DC,PointsArr&lt;font color = #ff0000&gt;&lt;strong&gt;[0]&lt;/font&gt;&lt;/strong&gt;, 4); <br><br>ps To jqw : 写 DLL 和用不用 API 或者 TCanvas 没有关系。
 
Arkain的方法非常的好
 
后退
顶部