|
Hi,
all
I am a beginner in vc programming.Now I have some problems
in my program.I want to display a bitmap of 24 bits,but It
seems that there are some colors the system can't display
correctly. My source code follows below.
CClientDC dc(this);
HBITMAP hBitmap=(HBITMAP)::LoadImage(AfxGetInstanceHandle (),"E:\\Bg.BMP",IMAGE_BITMAP,0,0,LR_LOADFROMFILE|LR_CREATEDIBSECTION);
CBitmap bmBG;
bmBG.Attach(hBitmap);
BITMAP bm;
bmBG.GetBitmap(&bm);
CDC dcMem;
dcMem.CreateCompatibleDC(&dc);
CBitmap* pOldBitmap=(CBitmap*)dcMem.SelectObject(&bmBG);
CRect lRect;
GetClientRect(&lRect);
lRect.NormalizeRect();
DIBSECTION ds;
BITMAPINFOHEADER &bmInfo=ds.dsBmih;
bmBG.GetObject(sizeof(ds),&ds);
CPalette pal;
int nColors = bmInfo.biClrUsed ?bmInfo.biClrUsed : 1 << bmInfo.biBitCount;
if( nColors > 256 ) pal.CreateHalftonePalette( &dc );
else{
// Create the palette
RGBQUAD *pRGB = new RGBQUAD[nColors];
::GetDIBColorTable( dcMem, 0, nColors, pRGB );
UINT nSize = sizeof(LOGPALETTE) + (sizeof(PALETTEENTRY) * nColors);
LOGPALETTE *pLP = (LOGPALETTE *) new BYTE[nSize];
pLP->palVersion = 0x300;
pLP->palNumEntries = nColors;
for( int i=0; i<ncolors;i++)
{
="" plp-="">palPalEntry[i].peRed = pRGB[i].rgbRed;
pLP->palPalEntry[i].peGreen = pRGB[i].rgbGreen;
pLP->palPalEntry[i].peBlue = pRGB[i].rgbBlue;
pLP->palPalEntry[i].peFlags = 0;
}
pal.CreatePalette( pLP );
delete[] pLP;
delete[] pRGB;
}
dc.StretchBlt(10,10,lRect.Width()-10,lRect.Height()-10,&dcMem,0,0,bm.bmWidth,bm.bmHeight,SRCCOPY);
I will appreciate your advanced help.
Best Regards.
|
|
|
|
|
If the system cannot display 'some colors' correctly, it's because it's running in 8 or 16 bit.
What's the point of the lines between DIBSECTION ds and delete[]pRGB ?
Because you're using stretchblt, you might like to try dc.SetStretchBltMode(COLORONCOLOR) to get the best possible result.
Christian
After all, there's nothing wrong with an elite as long as I'm allowed to be part of it!! - Mike Burston Oct 23, 2001
Sonork ID 100.10002:MeanManOzI live in Bob's HungOut now
|
|
|
|
|
Thank you for your help very much, It is ok now .
The point of the lines between DIBSECTION ds adn delete[]
pRGB is to create the logic Palette.I only copy it from
the book ,I really did not know why.
|
|
|
|
|
Any bitmap greater than 8 bit does not need a palette - the bits contain the colour information.
Christian
After all, there's nothing wrong with an elite as long as I'm allowed to be part of it!! - Mike Burston Oct 23, 2001
Sonork ID 100.10002:MeanManOzI live in Bob's HungOut now
|
|
|
|
|
I have data in CStringArray. Can someody tell me how to sort it so that i can put it in text files sorted.
Help me with some code.
In my dream, I was dorwning in my §orrow§
But my §orrow§, they learned to §wim
|
|
|
|
|
Another reason MFC containers suck hard. STL containers do this stuff with ease.
Here's some code from MSDN - why they couldn't build it in is beyond me. If you *must* use MFC containers, then this is how to do it:
#include <afx.h>
#include <iostream.h>
#include <afxcoll.h>
class CSortStringArray : public CStringArray {
public:
void Sort();
private:
BOOL CompareAndSwap(int pos);
};
void CSortStringArray::Sort()
{
BOOL bNotDone = TRUE;
while (bNotDone)
{
bNotDone = FALSE;
for(int pos = 0;pos < GetUpperBound();pos++)
bNotDone |= CompareAndSwap(pos);
}
}
BOOL CSortStringArray::CompareAndSwap(int pos)
{
CString temp;
int posFirst = pos;
int posNext = pos + 1;
if (GetAt(posFirst).CompareNoCase(GetAt(posNext)) > 0)
{
temp = GetAt(posFirst);
SetAt(posFirst, GetAt(posNext));
SetAt(posNext, temp);
return TRUE;
}
return FALSE;
}
void main()
{
CSortStringArray sortArray;
sortArray.Add(CString("Zebra"));
sortArray.Add(CString("Bat"));
sortArray.Add(CString("Apple"));
sortArray.Add(CString("Mango"));
for (int i = 0; i <= sortArray.GetUpperBound(); i++)
cout << sortArray[i] << endl;
sortArray.Sort();
cout << endl;
for (int j = 0; j <= sortArray.GetUpperBound(); j++)
cout << sortArray[j] << endl;
}
Christian
After all, there's nothing wrong with an elite as long as I'm allowed to be part of it!! - Mike Burston Oct 23, 2001
Sonork ID 100.10002:MeanManOzI live in Bob's HungOut now
|
|
|
|
|
Be warned, that's a nice implementation of a Bubble sort, which is not particularly quick with large lists.
Sorry to dissapoint you all with my lack of a witty or poignant signature.
|
|
|
|
|
Your idea is just fine but i'll be having lots of strings in it so isn't there any way to make it fast.What about converting the whole array into char** and then using qsort. how can i do that plz help me out.Also how STL does it i dont have to stuck to MFC.
In my dream, I was dorwning in my §orrow§
But my §orrow§, they learned to §wim
|
|
|
|
|
STL is the way to go. Just #include<vector> and you can create a CString vector like so:
vector<CString> m_MyVector;
Then you can do all sort of things not just sorting. SGI has a lot of STL docs online, you could look there.
Christian
After all, there's nothing wrong with an elite as long as I'm allowed to be part of it!! - Mike Burston Oct 23, 2001
<Center><A href = "http://www.sonork.com" target = "_Blank">Sonork</A> ID 100.10002:MeanManOz</Center><Center>I live in <U><B>Bob's HungOut</B></U> now</center>
|
|
|
|
|
In case you're still interested, I have a nice global method that uses qsort to do it for you:
static int CompareStringsAscending(const void* pElement1, const void* pElement2)
{
return ((CString*)pElement1)->Compare(*(CString*)pElement2);
}
static int CompareStringsDescending(const void *pElement1, const void* pElement2)
{
return -(((CString*)pElement1)->Compare(*(CString*)pElement2));
}
static int CompareStringsAscendingNoCase(const void* pElement1, const void* pElement2)
{
return ((CString*)pElement1)->CompareNoCase(*(CString*)pElement2);
}
static int CompareStringsDescendingNoCase(const void *pElement1, const void* pElement2)
{
return -(((CString*)pElement1)->CompareNoCase(*(CString*)pElement2));
}
void AMS_SortArray(CStringArray* pArray, bool bCaseSensitive = true, bool bAscending = true)
{
ASSERT(pArray);
int nElements = pArray->GetSize();
if (nElements <= 0)
return;
int nSize = sizeof(CString*);
void* pFirstElement = (void*)&((*pArray)[0]);
if (bCaseSensitive)
{
if (bAscending)
qsort(pFirstElement, nElements, nSize, CompareStringsAscending);
else
qsort(pFirstElement, nElements, nSize, CompareStringsDescending);
}
else
{
if (bAscending)
qsort(pFirstElement, nElements, nSize, CompareStringsAscendingNoCase);
else
qsort(pFirstElement, nElements, nSize, CompareStringsDescendingNoCase);
}
}
AMS_SortArray is the one to use, but you should probably rename it to something more appropriate.
Regards,
Alvaro
|
|
|
|
|
I have a dialog that is a CDialog (CVirtualDirDlg) and I need it to return 4 values when the user hits ok. How would I do this?
-Matt Newman
-Matt Newman
|
|
|
|
|
Make the values member variables and get them from the dlg after DoModal returns if OK was pressed or not.
Christian
After all, there's nothing wrong with an elite as long as I'm allowed to be part of it!! - Mike Burston Oct 23, 2001
Sonork ID 100.10002:MeanManOzI live in Bob's HungOut now
|
|
|
|
|
I'll try that.
-Matt Newman
-Matt Newman
|
|
|
|
|
How would you compare the contents of one CDC with the the contents of a selected area in another CDC? I'm looking for the fastest way to do this.
Thanks!
Nathan
|
|
|
|
|
The fastest way to do it is to use a DIBSECTION instead of a CBitmap, so that you can access the bits directly. Otherwise you're in GetPixel/SetPixel hell - read VERY slow.
Christian
After all, there's nothing wrong with an elite as long as I'm allowed to be part of it!! - Mike Burston Oct 23, 2001
Sonork ID 100.10002:MeanManOzI live in Bob's HungOut now
|
|
|
|
|
I can't seem to get my code to work. It should display pRightDC as all black shouldn't it?, instead it draws what i've already placed in pRightDC.
CDC* pRightDC = pBitmap->GetCurrentBitmap();
CSize s = pBitmap->GetBitmapDimension();
int nSize = pRect.Width() * pRect.Height();
CBitmap *pBitmap = pRightDC->GetCurrentBitmap();
char* hBytes = new char[nSize];
memset(hBytes, 0, nSize);
pBitmap->SetBitmapBits(nSize, hBytes);
(CDC *pDraw = handle to desktop area)
pDraw->BitBlt(10, 10, 5, pRect.Height(), pRightDC, 0, 0, SRCCOPY);
Nathan
|
|
|
|
|
According to MSDN GetCurrentBitmap can return a temporary object, which I presume means a copy.
Christian
After all, there's nothing wrong with an elite as long as I'm allowed to be part of it!! - Mike Burston Oct 23, 2001
Sonork ID 100.10002:MeanManOzI live in Bob's HungOut now
|
|
|
|
|
Hi,
How can I get the disabled buttons of a 256-color toolbar to look good?
Thanks in advance,
Erik Hammar
|
|
|
|
|
I banged them into a paint program, made them greyscale, then turned the contrast down and messed with the curves.
--
Andrew.
|
|
|
|
|
Use Hot and Cold bitmaps
See sample here
http://www.codeproject.com/docking/toolbar_hotbuttons.asp
|
|
|
|
|
Hi all (again), I`m stuck (again). I just can`t figure out how to copy a HBITMAP to a completely new HBITMAP (i.e. I don`t want them handling the same image, rather I want a new image which contains the same colours). Anyone know? I`ve tried copying the handle but that doesn`t do what I want it to do (rather obviously) since when I try modifying the new image the old one gets modified too, this is what i *don't* want. I have heard something about memcopy, but haven`t got a clue how I`d use it in conjunction with the HBITMAP and "the bits" of the DIBSection. Any help, or links or ideas or anything = very many thanks.
Cheers guys (and gals)
Alan.
"When I left you I was but the learner, now I am the Master" - Darth Vader
|
|
|
|
|
You need to create the second HBITMAP to be the same size, copy them both into DC's and then do a BitBlt between them. If they are both DIBSections, I'd presume you can create the second one the same size as the first and memcpy the bits from one to the other.
Christian
After all, there's nothing wrong with an elite as long as I'm allowed to be part of it!! - Mike Burston Oct 23, 2001
Sonork ID 100.10002:MeanManOzI live in Bob's HungOut now
|
|
|
|
|
Thanks for the response Christian, I would love to be able to memcpy the bits from the one HBITMAP to the other (they will both be DIBSections and they will both be the same size). But how does it work, do I have to create a new HBITMAP first using CreateDIBSection and then use memcpy? If this is correct, how do I use memcpy in that situation, without creating a memory leak from losing the pointers to the original "bits". I would very much like it if you could elaborate a little bit on this direction of solution,
Many thanks, again
Alan.
"When I left you I was but the learner, now I am the Master" - Darth Vader
|
|
|
|
|
memcpy copies the contents of the memory, so what happens is this:
1/ You create a new DIBSECTION the same size and bit depth as the first.
2/ You now have two DIBSECTIONs and two pointers to byte arrays, which are the same size. You use memcpy, which copies the contents of one byte array into the other. You're not allocating memory, you're copying the values of the pixels into the location that already holds the values of the bitmap as it was created, making them identical to the first.
Christian
After all, there's nothing wrong with an elite as long as I'm allowed to be part of it!! - Mike Burston Oct 23, 2001
Sonork ID 100.10002:MeanManOzI live in Bob's HungOut now
|
|
|
|
|
Many thanks Christian, I`m off to implement your suggestion, I`ll get back to you and amend this message with the results.
Thanking you lots,
Alan.
"When I left you I was but the learner, now I am the Master" - Darth Vader
|
|
|
|
|