C++ 如何在win32中刷新图像显示白色并保持相同的文本?

C++ 如何在win32中刷新图像显示白色并保持相同的文本?,c++,winapi,win32gui,C++,Winapi,Win32gui,我可以调用任何代码来刷新win32上的图像显示(已使用的油漆),但要维护消息吗? 我只希望只更新指纹图像并显示实时图像。文本窗口+其他图片保留 编辑: case WM_PAINT: wmId = LOWORD(wParam); hdc = BeginPaint(hWnd, &ps); refresh=!refresh; if((start==true)&&(refresh==true)&&(stop!=true))

我可以调用任何代码来刷新win32上的图像显示(已使用的油漆),但要维护消息吗? 我只希望只更新指纹图像并显示实时图像。文本窗口+其他图片保留

编辑:

case WM_PAINT:

    wmId    = LOWORD(wParam);
    hdc = BeginPaint(hWnd, &ps);

refresh=!refresh;
    if((start==true)&&(refresh==true)&&(stop!=true))
    {       
        if(cycle==0)
            {
                myKad(hWnd);
                    cycle=1;
            }
    windowName = MultiByteStringToWideString(name1, CP_ACP);
    LoadAndBlitBitmap(windowName.c_str(), hdc,500,0);
    fingerprint();
    LoadAndBlitBitmap(TEXT("outresized.bmp"), hdc,500,300);
    refresh=!refresh;
    char buffer[100]; // Just to show the # of loops it is at
    sprintf(buffer,"%d",round);
     windowName = MultiByteStringToWideString(buffer, CP_ACP);
    ::MessageBox(NULL,windowName.c_str(), __T("Error"), MB_OK);//just to show #loop.
    round++;

    ::MessageBox(NULL,__T("REFRESHED"), __T("Error"), MB_OK);

    //RedrawWindow(hWnd,&fingerprintSection,NULL,RDW_INTERNALPAINT|RDW_VALIDATE|RDW_UPDATENOW|RDW_NOCHILDREN);
    InvalidateRect( hWnd, &fingerprintSection, TRUE );
    }
    EndPaint(hWnd, &ps);

旧的

这是我尝试过但仍然不成功的方法

RECT fingerprintSection;//area that i want to refresh
fingerprintSection.left=500;
fingerprintSection.top=300;
fingerprintSection.bottom=540;
fingerprintSection.right=660;

if (wmId!=IDM_STOP)
    {

    fingerprint(); //this function will save the fingerprint to "outresized.bmp"

    LoadAndBlitBitmap(TEXT("outresized.bmp"), hdc,500,300);
    InvalidateRect( hWnd, &fingerprintSection, TRUE );
}
这是我的位图代码,如果你想看的话

bool LoadAndBlitBitmap(LPCWSTR szFileName, HDC hWinDC,int x,int y)
{
// Load the bitmap image file
HBITMAP hBitmap;
hBitmap = (HBITMAP)::LoadImage(NULL, szFileName, IMAGE_BITMAP, 0, 0,
    LR_LOADFROMFILE);
// Verify that the image was loaded
if (hBitmap == NULL) {
    ::MessageBox(NULL, __T("LoadImage Failed"), __T("Error"), MB_OK);
    return false;
}

// Create a device context that is compatible with the window
HDC hLocalDC;
hLocalDC = ::CreateCompatibleDC(hWinDC);
// Verify that the device context was created
if (hLocalDC == NULL) {
    ::MessageBox(NULL, __T("CreateCompatibleDC Failed"), __T("Error"), MB_OK);
    return false;
}

// Get the bitmap's parameters and verify the get
BITMAP qBitmap;
int iReturn = GetObject(reinterpret_cast<HGDIOBJ>(hBitmap), sizeof(BITMAP),
    reinterpret_cast<LPVOID>(&qBitmap));
if (!iReturn) {
    ::MessageBox(NULL, __T("GetObject Failed"), __T("Error"), MB_OK);
    return false;
}

// Select the loaded bitmap into the device context
HBITMAP hOldBmp = (HBITMAP)::SelectObject(hLocalDC, hBitmap);
if (hOldBmp == NULL) {
    ::MessageBox(NULL, __T("SelectObject Failed"), __T("Error"), MB_OK);
    return false;
}

// Blit the dc which holds the bitmap onto the window's dc
BOOL qRetBlit = ::BitBlt(hWinDC, x, y, qBitmap.bmWidth, qBitmap.bmHeight,
    hLocalDC, 0, 0, SRCCOPY);
if (!qRetBlit) {
    ::MessageBox(NULL, __T("Blit Failed"), __T("Error"), MB_OK);
    return false;
}

// Unitialize and deallocate resources
::SelectObject(hLocalDC, hOldBmp);
::DeleteDC(hLocalDC);
::DeleteObject(hBitmap);
return true;
}
bool LoadAndBlitBitmap(LPCWSTR szFileName,HDC hWinDC,int x,int y)
{
//加载位图图像文件
HBITMAP HBITMAP;
hBitmap=(hBitmap)::加载图像(NULL,szFileName,图像\位图,0,0,
LR_LOADFROMFILE);
//验证是否已加载映像
if(hBitmap==NULL){
::MessageBox(空,加载映像失败,错误,MB正常);
返回false;
}
//创建与窗口兼容的设备上下文
HDC-hLocalDC;
hLocalDC=::CreateCompatibleDC(hWinDC);
//验证是否已创建设备上下文
if(hLocalDC==NULL){
::MessageBox(NULL,u T(“CreateCompatibleDC失败”),u T(“错误”),MB_OK);
返回false;
}
//获取位图的参数并验证获取
位图;
int iReturn=GetObject(重新解释强制转换(hBitmap)、sizeof(位图),
重新解释投射(&Q位图));
如果(!i返回){
::MessageBox(NULL、uu T(“GetObject失败”)、u T(“错误”)、MB_uOK);
返回false;
}
//在设备上下文中选择加载的位图
HBITMAP hOldBmp=(HBITMAP)::选择对象(hLocalDC,HBITMAP);
if(hOldBmp==NULL){
::MessageBox(NULL、uu T(“SelectObject Failed”)、u T(“Error”)、MB_uOK);
返回false;
}
//Blit将位图保存在窗口的dc上的dc
BOOL qRetBlit=::BitBlt(hWinDC,x,y,qBitmap.bmWidth,qBitmap.bmHeight,
hLocalDC,0,0,SRCCOPY);
如果(!qRetBlit){
::MessageBox(NULL,uu T(“Blit Failed”),u T(“Error”),MB_OK);
返回false;
}
//统一和取消分配资源
::选择对象(hLocalDC、hOldBmp);
::DeleteDC(hLocalDC);
::删除对象(hBitmap);
返回true;
}
您可以在您的案例中使用,因为指纹上没有文本或其他图像的一部分

您只需要将指纹的长度作为第二个参数传递

从我在您的帖子中看到的,您似乎在
LoadAndBlitBitmap
中加载和blit fingerprint,因此您可以使用该函数更新一些全局
RECT
变量,以便将其作为第二个参数传递

在没有看到更多代码的情况下,我现在只能建议如下:

if (wmId!=IDM_STOP)
{
    fingerprint();

    LoadAndBlitBitmap(TEXT("outresized.bmp"), hdc,500,300);

    InvalidateRect( hWnd, &RectOfMyFingerprint, TRUE );
}
编辑: 您可以使用按钮捕获并保存位图的图像

然后,您所需要做的就是使窗口无效以重新绘制捕获的图像

尝试将您的
LoadAndBlitBitmap
移动到
WM_PAINT

case WM_PAINT:
    {
         BeginPaint();

         LoadAndBlitBitmap();

         EndPaint;
    }
您的按钮处理程序应如下所示:

if ( wmId == IDM_STARTSCANNING)
{
    fingerprint(); // capture your bitmap
}
else
    if (wmId == IDM_STOP)
    {
        // order redrawing
        InvalidateRect( hWnd, &RectOfMyFingerprint, TRUE ); 
    }

问题还不清楚。给我们一些代码,更好地解释您想要实现的目标,以及当前正在发生的事情。感谢使用以下属性创建控件/窗口
WS_EX_CLIENTEDGE,_T(“静态”),WS_CHILD | WS_VISIBLE | SS_CENTERIMAGE | SS_BITMAP
然后使用以下命令设置其图像:
SendMessage(Hwnd,STM_SETIMAGE,image_BITMAP,reinterpret_cast(hBitmap))发送消息可以在
WM_PAINT
中完成,以便在需要时重新绘制。。否则,需要手动重新绘制控件时,只需发送消息即可。。这是我为一个聊天客户端做的,它工作得非常好。我试过了,但仍然不能让人耳目一新。我更新了我的问题。请查收。thanks@user3396218当前位置我已编辑了我的答案。如果再次失败,请尝试将
invalidate
的第二个参数替换为
NULL
,以便重新绘制整个窗口。这对我们很有用,因为我们可以排除发送错误坐标的可能性。这还有一个问题。内容会发生变化,但不会真正显示在窗口上,除非每次刷新时我都弹出一个messagebox。而且,如果我移动窗口,它只会显示。你知道怎么解决这个问题吗?@user3396218:我真的不明白你想在这里实现什么。“内容变化”到底意味着什么?此时,我看不到您弹出的
MessageBox
,也看不到相关代码。至少用这些信息更新你的问题,这样我可以试着帮助你。我强烈建议你上传一个演示你的问题的小程序。你好,我更新了帖子。这是完整的代码,如果你想看的话。谢谢,指纹图像已更改,但除非弹出消息框,否则不会在windows上显示和更新。谢谢