【问题标题】:Win32 Paint Text still there after rebuilding重建后Win32 Paint Text仍然存在
【发布时间】:2012-11-24 02:21:22
【问题描述】:

在为我的 windows 类完成包装后,我用一些文本进行了测试,以确保一切正常。但是,无论我删除或注释掉“This is a test!!!!”文本,在运行程序时,它在可执行文件运行期间仍然存在。

LRESULT CALLBACK WinMsgHandler(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
    {
        switch (uMsg)
        {
        case WM_PAINT:
            PAINTSTRUCT ps;
            HDC hdc;

            //hdc = BeginPaint(hwnd, &ps); 

            //TextOut(hdc, 0, 0, L"This is a TEST!!!", 17);

            //EndPaint(hwnd, &ps);
            break;
        case WM_DESTROY:
            bWindowClosed = TRUE;
            break;
        case WM_CREATE:
            MessageBox(NULL, L"Create", L"test", MB_OK);
            break;
        default:
            return CBaseWindow::WinMsgHandler(hwnd, uMsg, wParam, lParam);
        }

        return 0;
    };

编辑:

这里是winmain源文件。我觉得这与我将所有内容括起来的方式有关。 CDerivedWindow 是一个封装类,用于封装大部分窗口初始化过程。

int APIENTRY _tWinMain(HINSTANCE hInstance,
                     HINSTANCE hPrevInstance,
                     LPTSTR    lpCmdLine,
                     int       nCmdShow)
{

    CDerivedWindow mainWnd(hInstance);

    WNDCLASSEX wcx; 

    // Fill in the window class structure with default parameters 
    wcx.cbSize = sizeof(WNDCLASSEX);                            // size of structure 
    wcx.style = CS_HREDRAW | CS_VREDRAW;                        // redraw if size changes 
    wcx.lpfnWndProc = CBaseWindow::stWinMsgHandler;             // points to window procedure 
    wcx.cbClsExtra = 0;                                         // no extra class memory 
    wcx.cbWndExtra = 0;                                         // no extra window memory 
    wcx.hInstance = hInstance;                                  // handle to instance 
    wcx.hIcon = LoadIcon(NULL, IDI_APPLICATION);                // predefined app. icon 
    wcx.hCursor = LoadCursor(NULL, IDC_ARROW);                  // predefined arrow 
    wcx.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);    // white background brush 
    wcx.lpszMenuName = NULL;                                    // name of menu resource 
    wcx.lpszClassName = L"True Wild";                           // name of window class 
    wcx.hIconSm = LoadIcon(NULL, IDI_APPLICATION);              // small class icon 

    initSprites();

    // register the window
    if (mainWnd.RegisterWindow(&wcx))
    {
        DWORD dwError = 0;
        DWORD dwStyle = WS_OVERLAPPEDWINDOW | WS_VISIBLE;
        RECT rc;

        rc.top = 100;
        rc.left = 100;
        rc.right = SCREEN_WIDTH;
        rc.bottom = SCREEN_HEIGHT;

        // create the window and start the message loop
        // we will get kicked out of the message loop when the window closes
        if (mainWnd.Create(dwStyle, &rc))
        {
            // message loop
            MSG msg;


            //game Loop
            while (TRUE)
            {
                while(PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
                { 
                    // Translate the message and dispatch it to WindowProc()
                    TranslateMessage(&msg);
                    DispatchMessage(&msg);

                    if (mainWnd.IsWindowClosed())
                        return 0;

                }


                //Run game code
                render();
            }
            return 0;
        }
        else
            return -1;
    }
    else
        return -2;

    return 0;
}

编辑答案 1:

// the message handler for this window
LRESULT CALLBACK WinMsgHandler(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
    switch (uMsg)
    {
    case WM_DESTROY:
        bWindowClosed = TRUE;
        break;
    default:
        return DefWindowProc(hwnd, uMsg, wParam, lParam);
    }

    return DefWindowProc(hwnd, uMsg, wParam, lParam);
};

【问题讨论】:

  • 如果它是你唯一拥有它的地方,并且它被注释掉并且仍然出现,那么你没有构建那个源。
  • 尝试清理解决方案并再次构建解决方案
  • 是的,我之前进行了清理和重建。没有任何改变。
  • 您确定 (a) 编译成功并且 (b) 您正在执行新编译的二进制文件吗?
  • 我确定。这是一个相当离奇的问题。某处没有得到清除。

标签: c++ visual-studio-2010 winapi


【解决方案1】:

如果您不处理 WM_PAINT,则应将其传递给 DefWindowProc() 以验证客户区并重新绘制窗口。

看起来您正在脱离开关/外壳并返回 0。我将替换:

return 0;

与

return DefWindowProc(hwnd, uMsg, wParam, lParam);

【讨论】:

  • 这已经通过default: return CBaseWindow::WinMsgHandler(hwnd, uMsg,wParam, lParam);完成,它调用了DefWindowProc()的基类函数。除非你告诉我在PAINT 案例中也调用它?
  • 啊,我不敢相信我没有解决这个问题。但是,唉,还是同样的问题。我可能不得不暂停制作一个包装类,以便继续我的实际项目。
  • 看起来您正在打破 WM_PAINT 消息上的开关/大小写,并且从未达到默认大小写。此外,每当您在案例中声明变量时,您都希望用大括号将整个案例括起来:stackoverflow.com/questions/92396/…
  • 是的,我已经解决了。我有时会忘记开关盒的规则,所以我应该养成总是把它们扔在那里的习惯。然而,即使在安排订单时,它也没有解决它。这只是涉及某些问题的问题之一。 Windows 从一开始就很严格。
  • 另一件奇怪的事情是你处理 WM_DESTROY 的方式。通常通过调用 PostQuitMessage(0); 来处理。以便在您的消息队列中插入一条 WM_QUIT 消息。我不知道bWindowClosed 发生了什么,但你可能已经知道了。我只是在想,也许窗口从未被正确销毁,因为 WM_DESTROY 是在窗口从屏幕上删除之后但在它被销毁之前发送的。
猜你喜欢
  • 2013-10-03
  • 2013-05-11
  • 1970-01-01
  • 2019-02-05
  • 1970-01-01
  • 2019-10-22
  • 1970-01-01
  • 2010-10-19
  • 2021-11-21
相关资源
最近更新 更多