Windows XP和Windows 7中的不同程序行为

时间:2017-02-15 11:01:34

标签: c++ multithreading winapi gdi

此代码在两个线程中从窗口的顶部到底部绘制两条垂直条纹(每个线程的相应函数为thread_func)。第一个线程绘制左边条纹的一部分,然后第二个线程绘制右边的一部分,然后再绘制第一个,依此类推。信号量和关键部分用于确保此顺序。

#include <windows.h>
#include <cstdint>

HDC hDC; 
HDC  hDCMem;
HBITMAP hbitmap;
HWND  hwnd; 
int ScreenMaxX;
int ScreenMaxY;

short pattern[8]={~0xFF, ~0xFF, ~0xFF, ~0xFF, ~0xFF, ~0xFF, ~0xFF, ~0xFF};
HBRUSH brush=::CreatePatternBrush(::CreateBitmap(8, 8, 1, 1, pattern));

void bar(int nLeft, int nTop, int nRight, int nBottom)
{
    RECT rect;
    rect.left   = nLeft;
    rect.right  = nRight;
    rect.top    = nTop;
    rect.bottom = nBottom;

    ::SetTextColor(hDCMem, 0xFF00FF);
    ::SetBkColor(hDCMem, 0xFF00FF);
    //brush=::CreatePatternBrush(::CreateBitmap(8, 8, 1, 1, pattern));
    ::FillRect(hDCMem, &rect, brush);
}

void flush(){
    ::BitBlt(hDC, 0, 0, ScreenMaxX, ScreenMaxY, hDCMem, 0, 0, SRCCOPY);
}

CRITICAL_SECTION graphics_cs;

uint8_t thread_cnt=0;
uint8_t total_threads=2;
HANDLE turnstile1=CreateSemaphoreW(nullptr, 0, 2, nullptr);

void thread_func(int num){
    int x,y;
    if(num==0){
        x=20; y=0;
    } else {
        x=110; y=0;
    }

    while(true) {
        while(true) {
            EnterCriticalSection(&graphics_cs);
            if (thread_cnt == num) {
                thread_cnt++;
                bar(x, y, x+40, y+40);
                y+=1;
                //flush();
                if(thread_cnt==total_threads){
                    thread_cnt = 0;
                    flush();
                    ReleaseSemaphore(turnstile1, total_threads, nullptr);
                }
                LeaveCriticalSection(&graphics_cs);
                break;
            } else {
                LeaveCriticalSection(&graphics_cs);
            }
        }

        WaitForSingleObject(turnstile1, INFINITE);

        Sleep(100);
    }
}

void mainx ()
{
    InitializeCriticalSection(&graphics_cs);
    for(int i=0; i<total_threads; i++){
        CreateThread (nullptr, 0, (LPTHREAD_START_ROUTINE)thread_func, (LPVOID)i, 0, nullptr);
    }
}

DWORD Th(LPVOID param)
{
    (void)param;
    ::SetWindowPos(hwnd, HWND_TOP,
                   10,
                   10,
                   400,
                   500,
                   SWP_SHOWWINDOW
    );
    mainx();
    flush();
    return 0;
}
DWORD g_nMainThreadID;

//processing main window messages
long FAR PASCAL WindowProc(HWND   hWnd,UINT   message, WPARAM wParam,LPARAM lParam )
{
    switch (message)
    {
        case WM_PAINT:   flush();
            break;
        case WM_DESTROY: PostQuitMessage(0);
            break;
    }
    return DefWindowProc(hWnd, message, wParam, lParam);
}

int PASCAL WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR  lpCmdLine,  int  nShowCmd )
{
    (void)hPrevInstance, (void)lpCmdLine;
    WNDCLASS  wc;
    MSG       msg;

    wc.style         = CS_HREDRAW | CS_VREDRAW;
    wc.lpfnWndProc   = WindowProc;
    wc.cbClsExtra    = 0;
    wc.cbWndExtra    = 0;
    wc.hInstance     = hInstance;
    wc.hIcon         = NULL;
    wc.hCursor       = LoadCursor(NULL, IDC_ARROW);
    wc.hbrBackground = (HBRUSH) GetStockObject(WHITE_BRUSH);
    wc.lpszMenuName  = "Menu_one";
    wc.lpszClassName = "NAME";

    if (!RegisterClass(&wc)) {return 0; };


    //main window
    hwnd = CreateWindow("NAME",    
                        "!", 
                        WS_OVERLAPPEDWINDOW,
                        CW_USEDEFAULT,
                        CW_USEDEFAULT,
                        CW_USEDEFAULT,
                        CW_USEDEFAULT,
                        HWND_DESKTOP, 
                        NULL, 
                        hInstance,
                        NULL
    );
    ScreenMaxX = ::GetSystemMetrics(SM_CXSCREEN);
    ScreenMaxY = ::GetSystemMetrics(SM_CYSCREEN);
    hDC = ::GetDC(hwnd);
    hDCMem  = ::CreateCompatibleDC(hDC);
    hbitmap = ::CreateCompatibleBitmap(hDC, ScreenMaxX, ScreenMaxY );
    ::SelectObject(hDCMem, hbitmap);
    auto hbrush = (HBRUSH)::GetStockObject(WHITE_BRUSH);
    ::SelectObject(hDCMem, hbrush);
    ::PatBlt(hDCMem, 0,0, ScreenMaxX, ScreenMaxY, PATCOPY );
    ::DeleteObject(hbrush);


    CreateThread(NULL,0,(LPTHREAD_START_ROUTINE)Th, (LPVOID)hwnd, 0,&g_nMainThreadID);

    ShowWindow(hwnd, nShowCmd);
    UpdateWindow(hwnd);


    while (GetMessage(&msg, NULL, 0, 0))
    {
        TranslateMessage(&msg);
        DispatchMessage(&msg);
    }
    return msg.wParam;
}

在Windows 7中,条纹以与预期相同的速度绘制。但在Windows XP中,速度是不同的:

different speed

如果我取消注释//brush=::CreatePatternBrush(::CreateBitmap(8, 8, 1, 1, pattern));//flush();行,则Windows XP中的绘图速度将相同。为什么这会解决问题,为什么初始代码的行为在不同版本的Windows中会有所不同?

更新

当我在std::cout<<"num = "<<num<<" : bar call\n";中的std::cout<<"num = "<<num<<" : flush call\n";bar来电之后添加flushthread_func时,输出为

num = 0 : bar call
num = 1 : bar call
num = 1 : flush call
num = 0 : bar call
num = 1 : bar call
num = 1 : flush call
num = 0 : bar call
num = 1 : bar call
num = 1 : flush call
num = 0 : bar call
num = 1 : bar call
num = 1 : flush call
...

订单似乎是正确的,但在刷新调用后不会立即绘制左边的条纹。

1 个答案:

答案 0 :(得分:1)

从我的评论中抽取一些答案。

请注意,GDI会对可能导致较慢计算机上的突发呈现的操作进行批处理。虽然GDI在XP和7上都进行了批量处理,但实际上这在7上似乎不再是一个问题。尝试调用GdiSetBatchLimit将限制设置为1.这将导致GDI在每次调整后刷新呼叫,总体上可能较慢,但应消除突发行为。