var
  LCanvas: TCanvas;
  a: HDC;
begin
  a := CreateCompatibleDC(Canvas.Handle);
  LCanvas := TCanvas.Create;
  LCanvas.Handle := a;
  LCanvas.TextOut(0,0,'asddddddddddddddddd');
  BitBlt(Canvas.Handle, 0, 0, Width, Height, LCanvas.Handle, 0, 0, SRCCOPY);
  LCanvas.Free;
  DeleteDC(a);
end;
这样是不是就建立了内存DC?这样对吗?我运行后发现并没有把LCanvas上的内容拷贝到窗体上,跟踪后发现LCanvas的宽度长度都是1,所以才显示不了。我尝试改变LCanvas的ClipRect的大小发现他是只读的。我查遍了MSDN也没有找到改变HDC大小的API,请问如何解决?

解决方案 »

  1.   

    这是DELPHI帮助上的话ResBefore an application can use a memory device context for drawing operations, it must select a bitmap of the correct width and height into the device context. Once a bitmap has been selected, the device context can be used to prepare images that will be copied to the screen or printed. procedure TForm1.Button1Click(Sender: TObject);
    var
      LCanvas: TCanvas;
      a: HDC;
      b: HBITMAP;//add
    begin
      a := CreateCompatibleDC(Canvas.Handle);
      LCanvas := TCanvas.Create;
      LCanvas.Handle := a;
      b:=CreateCompatibleBitmap(a,100,100);//add
      SelectObject(a,b);//add
      LCanvas.TextOut(0,0,'asddddddddddddddddd');
      BitBlt(Canvas.Handle, 0, 0, Width, Height, LCanvas.Handle, 0, 0, SRCCOPY);
      LCanvas.Free;
      DeleteDC(a);
      DeleteObject(b);//add
    end;
      

  2.   

    谢谢了,但是为什么bitblt后显示的是黑色背景呢?
      

  3.   

    CreateCompatibleBitmap创建一个新的BMP后,它的背景缺省是黑色,
    你bitblt是使用SRCCOPY方式把一整块拷贝过后,当然也会把黑色拷贝过去.