读书人

BitBlt有关问题

发布时间: 2012-04-01 17:23:46 作者: rapoo

BitBlt问题
BitBlt是不是只能复制一次啊,我想要在某处单击鼠标就把这张位图画在那个位置上,
在另一个地方单击时,另一个地方也出现这张位图,要怎样实现啊,现在我用Bitblt只能在一个地方出现,
在第二次单击鼠标时,没有出现这张位图,要怎么解决啊?

C/C++ code
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam){       static int x, y;    int wmId, wmEvent;    PAINTSTRUCT ps;    static HBRUSH hBrush;    static    HBITMAP    hbmpGreen = NULL,                hbmpPurple = NULL;    static RECT rect;     HDC hdc, hdcMem = NULL;    static bool flag = true;    static bool isPaint = true;    HINSTANCE hInst = NULL;    switch (message)    {    case WM_CREATE:         hInst = ( (LPCREATESTRUCT) lParam )->hInstance;          hbmpGreen = LoadBitmap( hInst, MAKEINTRESOURCE(IDB_GREEN));                      hbmpPurple = LoadBitmap( hInst, MAKEINTRESOURCE(IDB_PURPLE) );                    break;    case WM_LBUTTONDOWN:                 x = LOWORD(lParam);         y = HIWORD(lParam);        if ( ( x - 110 ) / 30 < DIVI && ( y - 35 ) / 30 < DIVI && !chessboard [( x - 110 ) / 30 ][( y - 35 ) / 30])        {            flag = !flag ;            chessboard[( x - 110 ) / 30 ][( y - 35 ) / 30 ] = flag ? W_Chessman : B_Chessman;            //pn [x][y] = 1;            //rect.left = x * cxblock;            rect.left = ( x - 110 ) / 30 * 30 + 110;            rect.right = ( x - 110 ) / 30 * 30 + 140;            rect.top = ( y - 35 ) / 30 * 30 + 35;            rect.bottom = ( y - 35 ) / 30 * 30 + 65;            InvalidateRect( hWnd, &rect, FALSE);        }        else             MessageBeep( 0 );        hdc = GetDC(hWnd);                ReleaseDC(hWnd,hdc);                        return 0;case WM_COMMAND:        wmId    = LOWORD(wParam);        wmEvent = HIWORD(wParam);        // 分析菜单选择:        switch (wmId)        {        case IDM_ABOUT:            DialogBox(hInst, MAKEINTRESOURCE(IDD_ABOUTBOX), hWnd, About);            break;        case IDM_EXIT:            DestroyWindow(hWnd);            break;        default:            return DefWindowProc(hWnd, message, wParam, lParam);        }        break;    case WM_PAINT:                 hdc = BeginPaint(hWnd, &ps);        // TODO: 在此添加任意绘图代码...                  if ( isPaint)                                        //画棋盘          {            hBrush = CreateSolidBrush(RGB(244,150,0));                    SelectObject(hdc,hBrush);            Rectangle( hdc, 100, 20, 570,490);            for ( int i = 0; i < 15; i++ )            {                 MoveToEx(hdc,125 + i * 30,50,NULL);               LineTo(hdc,125 + i * 30, 470);            }            for ( int j = 0; j < 15; j++ )            {                MoveToEx( hdc, 125, 50 + j * 30, NULL );                LineTo( hdc, 545, 50 + j * 30 );            }            SelectObject(hdc,GetStockObject(BLACK_BRUSH));            Ellipse(hdc,208,132.5,223,147.5);            Ellipse(hdc,448,132.5,463,147.5);            Ellipse(hdc,208,372.5,223,387.5);            Ellipse(hdc,448,372.5,463,387.5);            Ellipse (hdc,328,252.5,343,267.5);            isPaint = false;          }        hdcMem = CreateCompatibleDC( hdc );        if ( chessboard[( x - 110 ) / 30 ][( y - 35 ) / 30 ] && flag && ( x - 110 ) / 30 >= 0)            {                     SelectObject( hdcMem, hbmpWhite );                                       /***************************************                    这里为什么棋子只能画一个到棋盘上,                    第一次点击时画白棋子,第二次点击时画黑子                    但第三次以后就没有棋子画到点击的位置上了                    也就是白子和黑子都只能出现一次,为什么??                                         ****************************************/                     BitBlt(                                   // 把白棋子画到指定位置上                            hdc,                            ( x - 110 ) / 30 * 30 + 110,                                     ( y - 35 ) / 30 * 30 + 35,                            30,                            30,                            hdcMem,                            0,                            0,                            SRCCOPY );                //SelectObject(hdc,GetStockObject(WHITE_BRUSH));                //Ellipse ( hdc,( x - 110 ) / 30 * 30 + 110, ( y - 35 ) / 30 * 30 + 35, ( ( x - 110 ) / 30 + 1 ) * 30 + 110, ( y - 35 ) / 30 * 30 + 65 );                //chessboard[x][y] = W_Chessman;                            }            else if ( chessboard[( x - 110 ) / 30 ][( y - 35 ) / 30 ] && !flag && ( x - 110 ) / 30  >= 0)            {                                SelectObject( hdcMem, hbmpBlack);                            BitBlt(                                  // 把黑棋子画到指定位置上                            hdc,                            ( x - 110 ) / 30 * 30 + 110,                            ( y - 35 ) / 30 * 30 + 35,                            30,                            30,                            hdcMem,                            0,                            0,                            SRCCOPY );                              // SelectObject(hdc,GetStockObject(BLACK_BRUSH));              // Ellipse ( hdc,( x - 110 ) / 30 * 30 + 110, ( y - 35 ) / 30 * 30 + 35, ( ( x - 110 ) / 30 + 1 ) * 30 + 110, ( y - 35 ) / 30 * 30 + 65 );              // chessboard[x][y] = B_Chessman;                                            }                EndPaint(hWnd, &ps);                    if(flag && isWin(W_Chessman, ( x - 110 ) / 30, ( y - 35 ) / 30))            {                 MessageBox(hWnd,TEXT("black win"),TEXT("result"), MB_OK);            }                       else if(!flag && isWin(B_Chessman, ( x - 110 ) / 30, ( y - 35 ) / 30))            {                          MessageBox(hWnd,TEXT("Black win"),TEXT("result"),MB_OK);            }        break;    case WM_DESTROY:        PostQuitMessage(0);        break;    default:        return DefWindowProc(hWnd, message, wParam, lParam);    }    return 0;}// “关于”框的消息处理程序。INT_PTR CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam){    UNREFERENCED_PARAMETER(lParam);    switch (message)    {    case WM_INITDIALOG:        return (INT_PTR)TRUE;    case WM_COMMAND:        if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL)        {            EndDialog(hDlg, LOWORD(wParam));            return (INT_PTR)TRUE;        }        break;    }    return (INT_PTR)FALSE;}//**************************************************** 



[解决办法]
仔细调试一下,尤其是第三次进入绘图部分的时候。
BitBlt坐标换算比较麻烦,可能绘制到别的地方了,或者是图片句柄的问题。没有时能绘一次的说法

读书人网 >C++

热点推荐