为什么我不能实现在Pandel上加载图片?(有源码帮看下)

wenyongjie 2005-11-15 03:06:26
TPanel *panel;
panel=new TPanel(this);
panel->Parent=main; //main是主程序窗口
panel->Left=1;
panel->Top=50;
panel->Widht=Width/12;
panel->Height=Height-50;

//以下是加载图片
HDC hdc;
Graphics::TBitmap *bitmap=new Graphics::TBitmap();
bitmap->LoadFromFile("c:\\1.bmp");
hdc=GetDC(panel->Handle);
::StretchBlt(hdc,0,0,panel->Width,panel->Height,Bitmap->Canvas->Handle,0,0,Bitmap->Width,Bitmap->Height,SRCCOPY);
...全文
115 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
pp616 2005-11-16
  • 打赏
  • 举报
回复
建议你从TCustomPanel或TPanel继承一下。重载一下WndProc自己画图再加上个String型的图片文件名的属性。
这样使用起来会比较方便:)
Maconel 2005-11-15
  • 打赏
  • 举报
回复
从你上边的回复,我猜,是因为panel刚刚创建出来,还没有完成最后一次刷新,就将bmp画到上边了。所以还是建议使用WM_PAINT的方法或者image的方法。
wenyongjie 2005-11-15
  • 打赏
  • 举报
回复
To pp616(傻小子) 的是可以的,但不合我的要求,
我要这样:点某一功能按钮,如Button;就出现这个PANEL,它上面要加载图片,如果在Create里建立PANEL的话,要Panel->Visible=false;点Button时,Panel->Visible=true;但要点击两次才能图片才能加载进PANEL中,
Maconel 2005-11-15
  • 打赏
  • 举报
回复
1、你的panel是动态创建的,一定要等它创建好了再画图。可以把创建panel和画图分别放在2个按钮事件中试试。
2、看载入图片是否成功,是不是文件路径或文件名有错误。
3、后边的getdc和stretchblt都可以通过返回值判断是否成功。如果有错,可以通过getlasterror看看是什么错。
pp616 2005-11-15
  • 打赏
  • 举报
回复
//---------------------------------------------------------------------------

#include <vcl.h>
#pragma hdrstop

#include "Unit1.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
TPanel *panel;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
}
//---------------------------------------------------------------------------

void __fastcall TForm1::FormCreate(TObject *Sender)
{
panel=new TPanel(this);
panel->Parent=this; //main是主程序窗口
panel->Left=1;
panel->Top=50;
panel->Width=Width/12;
panel->Height=this->Height-100;
panel->Show();
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
HDC hdc;
Graphics::TBitmap *bitmap=new Graphics::TBitmap();
bitmap->LoadFromFile("d:\\lp3.bmp");
hdc=GetDC(panel->Handle);
::StretchBlt(hdc,0,0,panel->Width,panel->Height,
bitmap->Canvas->Handle,0,0,bitmap->Width,bitmap->Height,SRCCOPY);
::ReleaseDC(panel->Handle,hdc);
bitmap->FreeImage();
delete bitmap;
}
//---------------------------------------------------------------------------

肯定能显示出来。
wenyongjie 2005-11-15
  • 打赏
  • 举报
回复
楼上的几位,我的代码是先写在纸上在输入这里的所以大小写可能会不正确, 我在CB里运行通过,
但就是不出图像,还有就是我是在ButtonClick 事件里运行的,但只显示了Panel ,里面没图片,

要想刷新后不被擦掉,是要重载Paint
但我现在连显示都显示不了真是怪了
Maconel 2005-11-15
  • 打赏
  • 举报
回复
象楼上所说的,应该在WM_PAINT消息中绘制,否则刷新后就被擦掉了。
一般象这种情况,不用这么复杂,只要在panel上放一个TImage,然后用image来显示图象即可。
Waiting4you 2005-11-15
  • 打赏
  • 举报
回复
截获panel的WM_PAINT和WM_ERASEBKGND消息(用WindowProc方便些),在消息处理里写加载图片的代码.
constantine 2005-11-15
  • 打赏
  • 举报
回复
hdc没有释放!!
shadowstar 2005-11-15
  • 打赏
  • 举报
回复
代码基本是正确的,除了大小写。

但是如果这段代码在OnCreate, OnShow 等事件里执行的话恐怕起不到作用,楼主试试把这段代码放到ButtonClick事件里试试。

我想是可以的,但如果想使图像持久保持在Panel上,可能要重载它的OnPaint之类的事件。

13,822

社区成员

发帖
与我相关
我的任务
社区描述
C++ Builder相关内容讨论区
社区管理员
  • 基础类社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧