如何在对话框中显示一个位图
解决方案 »
- 一个程序从VC6转移到VC2008 窗口风格怎么不是 VC2008的?
- vc richtext 建立文档失败是为什么?
- 有没有研究羊毛与羊绒纤维鉴别的?计算机图像处理的方法
- 继承CDialog的类重载DoModal中 多次调用父类CDialog的DoModal有问题(VS05)
- 请问怎样在VC++的程序中运行一个java程序,3x
- 初级问题,线程的启动,AfxBeginThread问题
- 关于单线程和多线程问题?
- 请问出现LINK : fatal error LNK1561: entry point must be defined怎么回事?
- 请问如何在vc6.0的标签控件中改变caption的字体大小,为什么在属性中没有提供设置,难道还要用代码实现吗?
- 难题求解,为什么TCP可以用127.0.0.1上本机调试,而UDP不可以
- 如何获取一个对话控件的指针
- 如何在代码中获取工具条和状态条的指针
下面是读取灰度BMP的函数,256色,24位色类似.//---------
COLORREF SwitchColor(unsigned char c);
bool ShowImage(unsigned char *image,int height,int width,int x,int y)
bool ReadBMP(char *file,BITMAPFILEHEADER_ *bmph,BITMAPINFO_ *bmpi,unsigned char **data);bool ReadBMP(char *file,BITMAPFILEHEADER_ *bmph,BITMAPINFO_ *bmpi,unsigned char **data)
{
int i,pos; FILE *f=fopen(file,"rb"); if(f==NULL) //判断文件是否打开成功
{
return false;
} fseek(f,0,0);//移动到开头 //----------读BMP文件头
if(fread((char*)bmph,sizeof(BITMAPFILEHEADER_),1,f)==NULL)
{
return false;
} //-----------读BMP信息头
if(fread((char*)bmpi,sizeof(BITMAPINFO_),1,f)==NULL)
{
return false;
} fseek(f,bmph->Offbits,0); //定位位图矩阵 int skip=0; //填充象素数
if(bmpi->width%4==0)
skip=0;
else
skip=4-bmpi->width%4; long N=(bmpi->width)*(bmpi->height);//有效象素总数
*data=(unsigned char*)malloc(N);
for(i=0;i<(bmpi->width)*(bmpi->height);i++) //清空,变白色.
{
(*data)[i]=0xff;
} unsigned char waste[8];
pos=N-bmpi->width;
for(i=0;i<bmpi->height;i++) //读入位图矩阵
{
fread(&(*data)[pos],bmpi->width,1,f);
fread(waste,skip,1,f);
pos=pos-bmpi->width;
}
fclose(f); return true;
}//////////////-
bool ShowImage(unsigned char *image,int height,int width,int x,int y)
{
int i,j;
for(i=0;i<height;i++)
{
for(j=0;j<width;j++)
{
dc->SetPixel(j+x,i+y,SwitchColor(image[i*width+j]));
}
}
return true;
}
COLORREF SwitchColor(unsigned char c)
{
return (0x00000000 | (c<<16) | (c<<8) |c);
}
显示位图是很容易的, 只需将图形控件拖到对话中并选择适当属性即可,用户也
可以显示图标、位图以及增强型元文件。
32、如何改变对话或窗体视窗的背景颜色 调用CWinApp : : SetDialogBkColor可以改变所有应用程序的背景颜色。第
一个参数指定了背景颜色,第二个参数指定了文本颜色。下例将应用程序对话设置
为蓝色背景和黄色文本。
BOOL CSampleApp : : InitInstance ( )
{
… //use blue dialog with yellow text .
SetDialogBkColor (RGB (0, 0, 255 ), RGB ( 255 , 255 , 0 ) ) ; …
}
需要重画对话(或对话的子控件)时,Windows向对话发送消息WM_CTLCOLOR,
通常用户可以让Windows选择绘画背景的刷子,也可重置该消息指定刷子。下例说
明了创建一个红色背景对话的步骤。
首先,给对话基类增加一人成员变量CBursh :
class CMyFormView : public CFormView
{
… private :
CBrush m_ brush ; // background brush …
} ;
其次, 在类的构造函数中将刷子初始化为所需要的背景颜色。
CMyFormView : : CMyFormView ( )
{
// Initialize background brush .
m_brush .CreateSolidBrush (RGB ( 0, 0, 255 ) )
}
最后,使用ClassWizard处理WM_CTLCOLOR消息并返回一个用来绘画对话背景的
刷子句柄。注意:由于当重画对话控件时也要调用该函数,所以要检测nCtlColor
参量。
HBRUSH CMyFormView : : OnCtlColor (CDC* pDC , CWnd*pWnd , UINT nCtlColor )
{
// Determine if drawing a dialog box . If we are , return +handle to
//our own background brush . Otherwise let windows handle it .
if (nCtlColor = = CTLCOLOR _ DLG )
return (HBRUSH) m_brush .GetSafeHandle ( ) ; return CFormView : : OnCtlColor (pDC, pWnd , nCtlColor );
}