⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 read.cgi-read=9954.htm

📁 随书类文件![随书类]MFC_SOURCEBOOK
💻 HTM
字号:
<HTML><HEAD><TITLE>Q: Why Cannot display a bitmap nicely?</TITLE>
</HEAD><BODY background="../fancyhome/back.gif" tppabs="http://www.codeguru.com/fancyhome/back.gif" bgcolor="#FFFFFF">
 <table WIDTH="100%"> <tr WIDTH="100%"> <td><table>
		<tr><td><img src="../mfc_sourcebook.jpg" tppabs="http://www.codeguru.com/mfc_sourcebook.jpg" 
		ALT="MFC Programmer's SourceBook" WIDTH="256" 
		HEIGHT="88"><td></tr>
		<tr><td valign="bottom"><font SIZE="+1" 
		color="#a0a0ff"><b>Discussion Board</b></font></td></tr>
		</table></td> <td width="40"></td>
		<td  align="right" valign="top"><A HREF="http://209.66.99.126/cgi/ads.cgi?advert=catalyst"><IMG SRC="../banners/catalyst.jpg" tppabs="http://www.codeguru.com/banners/catalyst.jpg" HEIGHT=60 WIDTH=468 ALT="Catalyst Development" BORDER=2></A><BR><SMALL><A HREF="http://209.66.99.126/cgi/ads.cgi?advert=catalyst">Click here for Free ActiveX Control</A></SMALL><td> </tr> </table> <hr><P ALIGN=CENTER>[ <A HREF="#Responses">Read Responses</A> | <A HREF="#PostResponse">Post a New Response</A> | <A HREF="index.cgi.htm" tppabs="http://www.codeguru.com/mfc_bbs/index.cgi">Return to the Index</A> ]
<A HREF="read.cgi-read=10075.htm" tppabs="http://www.codeguru.com/mfc_bbs/read.cgi?read=10075">Next in Thread</A><P ALIGN=CENTER><BIG><BIG><BIG><STRONG>Q: Why Cannot display a bitmap nicely?</STRONG></BIG></BIG></BIG>
<P ALIGN=CENTER><EM>Posted by <STRONG><A HREF="mailto:zda01@engr.uky.edu">Z.J.Da</A></STRONG> on <STRONG>4/21/98 7:49a.m.</STRONG></EM></P>
<!-- REMOTE_HOST: 192.146.101.2; REMOTE_ADDR: 192.146.101.2-->
<P>Hi All,
<P>I tried to create a bitmap for a contour plot then display it as the folloeing:
<P>//derive CContourPlot from CMDIChildWnd
<BR>IMPLEMENT_DYNCREATE(CContourPlot, CMDIChildWnd)
<P>//in the constructor create a color palette with 48 entries 
<BR>CContourPlot::CContourPlot()
<BR>{
<BR>    RGBQUAD *pRGB = new RGBQUAD[48];
<BR>    
<BR>    UINT nSize = sizeof(LOGPALETTE) + (sizeof(PALETTEENTRY) * 48);
<BR>    LOGPALETTE *pLP = (LOGPALETTE *) new BYTE[nSize];
<P>    pLP-&gt;palVersion = 0x300;
<BR>    pLP-&gt;palNumEntries = 48;
<P>    for(int i=0; i&lt;8; i++) {
<BR>	pLP-&gt;palPalEntry[i].peRed = 255;
<BR>        pLP-&gt;palPalEntry[i].peGreen = 0;
<BR>        pLP-&gt;palPalEntry[i].peBlue = i*32;
<BR>        pLP-&gt;palPalEntry[i].peFlags = 0;
<P>	pLP-&gt;palPalEntry[i+8].peRed = 255-i*32;
<BR>        pLP-&gt;palPalEntry[i+8].peGreen = 0;
<BR>        pLP-&gt;palPalEntry[i+8].peBlue = 255;
<BR>        pLP-&gt;palPalEntry[i+8].peFlags = 0;
<P>	pLP-&gt;palPalEntry[i+16].peRed = 0;
<BR>        pLP-&gt;palPalEntry[i+16].peGreen = i*32;
<BR>        pLP-&gt;palPalEntry[i+16].peBlue = 255;
<BR>        pLP-&gt;palPalEntry[i+16].peFlags = 0;
<P>	pLP-&gt;palPalEntry[i+24].peRed = 0;
<BR>        pLP-&gt;palPalEntry[i+24].peGreen = 255;
<BR>        pLP-&gt;palPalEntry[i+24].peBlue = 255-i*32;
<BR>        pLP-&gt;palPalEntry[i+24].peFlags = 0;
<P>	pLP-&gt;palPalEntry[i+32].peRed = i*32;
<BR>        pLP-&gt;palPalEntry[i+32].peGreen = 255;
<BR>        pLP-&gt;palPalEntry[i+32].peBlue = 0;
<BR>        pLP-&gt;palPalEntry[i+32].peFlags = 0;
<P>	pLP-&gt;palPalEntry[i+40].peRed = 223;
<BR>        pLP-&gt;palPalEntry[i+40].peGreen = 255-i*32;
<BR>        pLP-&gt;palPalEntry[i+40].peBlue = 0;
<BR>        pLP-&gt;palPalEntry[i+40].peFlags = 0;
<BR>    }
<P>    myPalette.CreatePalette( pLP );
<P>    delete[] pLP;
<BR>}
<P>//create the bitmap using the same color as the palette
<BR>CContourPlot::CreatMyBitmap()
<BR>{
<BR>    COLORREF NEAR colorArray[48];
<BR>    for(i=0; i&lt;8; i++) {
<BR>	colorArray[i] = RGB(255, 0, i*32);
<BR>	colorArray[i+8] = RGB(255-i*32, 0, 255);
<BR>	colorArray[i+16] = RGB(0, i*32, 255);
<BR>	colorArray[i+24] = RGB(0, 255, 255-i*32);
<BR>	colorArray[i+32] = RGB(i*32, 255, 0);
<BR>	colorArray[i+40] = RGB(223, 255-i*32, 0);
<BR>    }
<BR>	
<BR>	if(m_myBitmap.m_hObject != NULL)
<BR>		m_myBitmap.DeleteObject();
<P>	CClientDC dc(this);
<BR>	CDC dcMem;
<P> 	dcMem.CreateCompatibleDC(&amp;dc);
<BR>	m_myBitmap.CreateCompatibleBitmap(&amp;dc, 600, 410);
<BR>	ASSERT(m_myBitmap.m_hObject != NULL);
<P>	CBitmap* pOldBitmap = dcMem.SelectObject(&amp;m_myBitmap);
<P>	//draw a rectangle in the backgorund window color
<BR>	CBrush brBackground(::GetSysColor(COLOR_WINDOW));
<BR>	dcMem.FillRect(rect, &amp;brBackground);
<P>	CBrush myBrush[48];
<BR>	for(i=0; i&lt;48; i++)
<BR>		myBrush[i].CreateSolidBrush(colorArray[i]);
<P>	// draw contour
<BR>	RECT myRect;
<BR>	for(x=1; x&lt;100; x += 5)
<BR>	{
<BR>	     for(y=1; y&lt;100; y += 5)
<BR>		{
<BR>			myRect.left = (LONG)(x-1);
<BR>			myRect.top = (LONG)(y-1);
<BR>			myRect.right = (LONG)(x+4);
<BR>			myRect.bottom = (LONG)(y+4);
<BR>			z = x + y;  //plot linera contours
<BR>			dcMem.FillRect(&amp;myRect, &amp;myBrush[(int)48.0*z/200.0]);
<BR>		}
<BR>	}
<P>    dcMem.SelectObject(pOldBitmap);
<BR>    dcMem.DeleteDC();
<BR>}
<P>//display the bitmap
<BR>void CContourPlot::OnPaint() 
<BR>{
<BR>    if(m_myBitmap.m_hObject == NULL) 
<BR>	return;
<P>    CPaintDC dc(this);
<BR>    CBitmap* pbmOld = NULL;
<P>    CDC dcMem;
<BR>    dcMem.CreateCompatibleDC(&amp;dc);
<BR>    pbmOld = dcMem.SelectObject(&amp;m_myBitmap);
<P>    // Select and realize the palette
<BR>    if( dc.GetDeviceCaps(RASTERCAPS) &amp; RC_PALETTE &amp;&amp; myPalette.m_hObject != NULL ) {
<BR>	dc.SelectPalette( &amp;myPalette, TRUE );
<BR>	dc.RealizePalette();
<BR>  }	
<BR>	
<BR>    //dc.SetStretchBltMode(COLORONCOLOR);
<BR>	dc.StretchBlt(0, 0, m_Width, m_Height,
<BR>		&amp;dcMem, 0, 0, 100, 100, SRCCOPY);
<P>	dcMem.SelectObject(pbmOld);
<BR>	dcMem.DeleteDC();
<BR>	
<BR>	return;
<BR>}

<P>The bitmap can be displayed but it does not use the colors that I expected. Instead, the dithered colors are used.  Could anybody tell me what is wrong with my code in using bitmap and color palette?
<P>Thank you in advance.
<P>Z.J. Da
<BR>
</P>
<A NAME="Responses"><HR></A><P ALIGN=CENTER><BIG><BIG><STRONG>Responses</STRONG></BIG></BIG>
<P><UL><LI><STRONG><A HREF="read.cgi-read=10075.htm" tppabs="http://www.codeguru.com/mfc_bbs/read.cgi?read=10075">Re: Q: Why Cannot display a bitmap nicely?</A></STRONG> : Steve McAdams -- <EM>4/22/98 6:11a.m.</EM>
<UL>
<LI><STRONG><A HREF="read.cgi-read=10087.htm" tppabs="http://www.codeguru.com/mfc_bbs/read.cgi?read=10087">Re: Q: Why Cannot display a bitmap nicely?</A></STRONG> : Z.J. Da -- <EM>4/22/98 7:56a.m.</EM>
<UL>
</UL>
</UL>
</UL></P>
<A NAME="PostResponse"><HR></A><P ALIGN=CENTER><BIG><BIG><STRONG>Post a New Response</STRONG></BIG></BIG>
<P><FORM METHOD=POST ACTION="http://www.codeguru.com/mfc_bbs/index.cgi?post">
<INPUT TYPE=HIDDEN NAME="followup" VALUE="9954">
<P><CENTER><TABLE><TR>
<TD ALIGN=RIGHT><P><STRONG>Your Name:</STRONG></TD><TD><INPUT TYPE=TEXT NAME="name" SIZE=40></TD></TR><TR>
<TD ALIGN=RIGHT><P><STRONG>E-Mail Address:</STRONG></TD><TD><INPUT TYPE=TEXT NAME="email" SIZE=40></TD></TR><TR>
<TD ALIGN=RIGHT><P><STRONG>Subject:</STRONG></TD><TD><INPUT TYPE=TEXT NAME="subject" SIZE=40 VALUE="Re: Q: Why Cannot display a bitmap nicely?"></TD></TR><TR>
<TD COLSPAN=2 ALIGN=CENTER><P><STRONG>Message:</STRONG><BR><TEXTAREA COLS=80 ROWS=15 NAME="body" WRAP=PHYSICAL>
</TEXTAREA></TD></TR><TR>
<TD COLSPAN=2 ALIGN=CENTER><HR WIDTH=50%><P><SMALL>If you'd like to include a link to another page with your message,<BR>please provide both the URL address and the title of the page:</SMALL></TD></TR><TR>
<TD ALIGN=RIGHT><P><STRONG>Optional Link URL:</STRONG></TD><TD><INPUT TYPE=TEXT NAME="url" SIZE=40 VALUE="http://"></TD></TR><TR>
<TD ALIGN=RIGHT><P><STRONG>Optional Link Title:</STRONG></TD><TD><INPUT TYPE=TEXT NAME="url_title" SIZE=40></TD></TR><TR>
<TD COLSPAN=2 ALIGN=CENTER><HR WIDTH=50%><P><SMALL>If you'd like to include an image (picture) with your message,<BR>please provide the URL address of the image file:</SMALL></TD></TR><TR>
<TD ALIGN=RIGHT><P><STRONG>Optional Image URL:</STRONG></TD><TD><INPUT TYPE=TEXT NAME="imageurl" SIZE=40 VALUE="http://"></TD></TR><TR>
<TD COLSPAN=2 ALIGN=CENTER><HR WIDTH=50%><P><SMALL>If you'd like e-mail notification of responses, please check this box:</SMALL> <INPUT TYPE=CHECKBOX NAME="wantnotice" CHECKED VALUE="yes"></TD></TR><TR>
<TH COLSPAN=2><HR WIDTH=50%><P><INPUT TYPE=SUBMIT NAME="Preview" VALUE="Preview Message"> <INPUT TYPE=SUBMIT NAME="Post" VALUE="Post Message"></TH></TR></TABLE></CENTER></P></FORM>
<HR><P ALIGN=CENTER><SMALL>The MFC Discussion Board is maintained with <STRONG><A HREF="http://awsd.com/scripts/webbbs/">WebBBS 2.24</A></STRONG>.</SMALL></P>
</BODY></HTML>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -