|
Good Morning.
In my view class in the onDraw() function, I am drawing the board and filling in the squares as follows:
pDC->SetMapMode(MM_LOENGLISH);
pDC->SetWindowExt(800, 800);
for (int i = 0; i < 8; i++)
{
for (int j = 0; j < 8; j++)
{
COLORREF color = pBC->getSquare(i, j);
CBrush brush(color);
int x1 = (j * 70) + 35;
int y1 = (i * -70) - 35;
int x2 = x1 + 70;
int y2 = y1 - 70;
CRect rect(x1, y1, x2, y2);
pDC->FillRect(rect, &brush);
}
}
for (int x = 35; x <= 595; x += 70)
{
pDC->MoveTo(x, -35);
pDC->LineTo(x, -595);
}
for (int y = -35; y >= -595; y -= 70)
{
pDC->MoveTo(35, y);
pDC->LineTo(595, y);
}
This all works well, and I have my board coming up.
Now I want to draw the chess pieces on the board, and have written some code, just to load 1 bitgmap and display it on the screen. However, nothing gets drawn to the screen. (All I see is just the chessboard)
This is the test code to display my bitmap on the screen:
void CMyChessTestView::drawImage(CDC* pDC, int x, int y)
{
CPaintDC dc(this);
CBitmap BmpLady;
CDC MemDCLady;
BmpLady.LoadBitmap(IDB_BISHOP_WHITE);
MemDCLady.CreateCompatibleDC(pDC);
CBitmap *BmpPrevious = MemDCLady.SelectObject(&BmpLady);
pDC->BitBlt(20, 10, 436, 364, &MemDCLady, 0, 0, SRCCOPY);
pDC->SelectObject(BmpPrevious);
The bitmap is not drwan to the screen. I have tried using both the CPaintDC dc or the pDC* but to no avail. I cannot get the bitmap to display.
Any suggestions will be greatly appreciated.
|
|
|
|
|
How do you call the drawImage method?
"In testa che avete, Signor di Ceprano?"
-- Rigoletto
|
|
|
|
|
drawImage(pDC, 100, 100);
|
|
|
|
|
BigSteve-O wrote: drawImage(pDC, 100, 100);
And where are you calling this method from?
|
|
|
|
|
From the onDraw() funtion.
|
|
|
|
|
A similar code draws the bitmap, on my system. Why didn't you check the return value of the LoadBitmap call?
"In testa che avete, Signor di Ceprano?"
-- Rigoletto
|
|
|
|
|
It looks like loadBitmap returns NULL. I have no idea as to why though.
Do you have a code snippet you can share?
|
|
|
|
|
BigSteve-O wrote: It looks like loadBitmap returns NULL. I have no idea as to why though.
According to Docs in CBitmap Class | Microsoft Learn :
Quote: If the bitmap identified by lpszResourceName does not exist or if there is insufficient memory to load the bitmap, the function returns 0.
|
|
|
|
|
|
|
Thank You.
I copied the sample code over, and I have noticed the following:
1) If I draw the bitmap first, and then draw the board afterwards, I can see the board and the test bitmap.
2) If I draw the board first and then the test bitmap, I only see the board. The loadBitmap call is successfull)
|
|
|
|
|
That's rather strange. Do you use the same DC for the board and bitmap?
"In testa che avete, Signor di Ceprano?"
-- Rigoletto
|
|
|
|
|
|
|
Here is my onDraw()
void CMyChessTestView::OnDraw(CDC* pDC)
{
CMyChessTestDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
if (!pDoc)
return;
drawImage(pDC, 100, 100);
drawEmptyBoard(pDC);
}
Here is the drawImage(...)
void CMyChessTestView::drawImage(CDC* pDC, int x, int y)
{
CBitmap bmp;
if (bmp.LoadBitmap(IDB_LADYPIC))
{
BITMAP bmpInfo;
bmp.GetBitmap(&bmpInfo);
CDC dcMemory;
dcMemory.CreateCompatibleDC(pDC);
CBitmap *pOldBitmap = dcMemory.SelectObject(&bmp);
CRect rect;
GetClientRect(&rect);
int nX = rect.left + (rect.Width() - bmpInfo.bmWidth) / 2;
int nY = rect.top + (rect.Height() - bmpInfo.bmHeight) / 2;
pDC->BitBlt(nX, nY, bmpInfo.bmWidth, bmpInfo.bmHeight, &dcMemory,
0, 0, SRCCOPY);
dcMemory.SelectObject(pOldBitmap);
}
else
{
TRACE0("ERROR: Where's IDB_LADYPIC?\n");
}
}
Here is the drawEmptyBoard()
void CMyChessTestView::drawEmptyBoard(CDC* pDC)
{
CMyChessTestDoc* pBC = GetDocument();
ASSERT_VALID(pBC);
if (!pBC)
return;
pDC->SetMapMode(MM_LOENGLISH);
pDC->SetWindowExt(800, 800);
for (int i = 0; i < 8; i++)
{
for (int j = 0; j < 8; j++)
{
COLORREF color = pBC->getSquare(i, j);
CBrush brush(color);
int x1 = (j * 70) + 35;
int y1 = (i * -70) - 35;
int x2 = x1 + 70;
int y2 = y1 - 70;
CRect rect(x1, y1, x2, y2);
pDC->FillRect(rect, &brush);
}
}
for (int x = 35; x <= 595; x += 70)
{
pDC->MoveTo(x, -35);
pDC->LineTo(x, -595);
}
for (int y = -35; y >= -595; y -= 70)
{
pDC->MoveTo(35, y);
pDC->LineTo(595, y);
}
|
|
|
|
|
Really?
You draw the chess piece first and then draw the chess board on top of it?
|
|
|
|
|
No, It serves to demonstrate that if I draw the board first, the pieces will not show up, and that by drawing the piece first and then the board, all shows up.
|
|
|
|
|
BigSteve-O wrote: if I draw the board first, the pieces will not show up, and that by drawing the piece first and then the board, all shows up.
Then why do you try to draw first the board? 
|
|
|
|
|
OK, I was able to reproduce your issue, and with a bit of luck, to fix it.
The problem arises when you change the DC mapping mode. Now while I am not an expert of mapping modes, this code displays the bitmap over the empty board:
Inside OnDraw (is actually OnPaint in my test app)
drawEmptyBoard(&dc);
drawImage(&dc, 105, -105);
While drawImage becomes
::drawImage(CDC* pDC, int x, int y)
{
CBitmap bmp;
if (bmp.LoadBitmap(IDB_LADYPIC))
{
BITMAP bmpInfo;
bmp.GetBitmap(&bmpInfo);
CDC dcMemory;
dcMemory.CreateCompatibleDC(pDC);
dcMemory.SetMapMode(pDC->GetMapMode());
CBitmap* pOldBitmap = dcMemory.SelectObject(&bmp);
pDC->StretchBlt(x, y, bmpInfo.bmWidth, bmpInfo.bmHeight, &dcMemory,0, 0, bmpInfo.bmWidth, -bmpInfo.bmHeight, SRCCOPY);
dcMemory.SelectObject(pOldBitmap);
}
}
To make things more explicit, I would move the SetMapMode call out of drawEmptyBoard .
"In testa che avete, Signor di Ceprano?"
-- Rigoletto
|
|
|
|
|
Thank you so much for your suggestions. It is much appreciated!
I am going to try it soon.
|
|
|
|
|
You are welcome.
"In testa che avete, Signor di Ceprano?"
-- Rigoletto
|
|
|
|
|
Restoring the old bitmap should happen on the MemDCLady DC where you called the first SelectObject() , not the one passed in as pDC .
|
|
|
|
|
I tried the following test, just to see if i can display anything on my board,
CBitmap bitmap;
CDC dcMemory;
bitmap.LoadBitmap(IDB_LADYPIC);
dcMemory.CreateCompatibleDC(pDC);
dcMemory.SelectObject(&bitmap);
pDC->BitBlt(100, 200, 54, 96, &dcMemory, 0, 0, SRCCOPY);
The bitmap does not show. I only see my board.
|
|
|
|
|
pDC->SelectObject(BmpPrevious);
I do not think you should be restoring the old bitmap into pDC , since it came out of MemDCLady .
|
|
|
|
|
Been a while since I futzed with Windows drawing. There are some tricks to it.
Let me go find some old code. Right off the bat, beware working with DCs and bitmaps.
MS' implementation can be subtle.
Charlie Gilley
“They who can give up essential liberty to obtain a little temporary safety deserve neither liberty nor safety.” BF, 1759
Has never been more appropriate.
|
|
|
|
|