如何在对话框中显示一个位图

解决方案 »

  1.   

    读取文件,得到BMP位图矩阵,在对话框中逐点描绘.
    下面是读取灰度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);
    }
      

  2.   

    这要归功于Win 32先进的静态控件和Microsoft的资源编辑器, 在对话框中
    显示位图是很容易的, 只需将图形控件拖到对话中并选择适当属性即可,用户也
    可以显示图标、位图以及增强型元文件。
    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 );
    }