简体   繁体   English

在Win32应用程序中绘制问题

[英]Drawing Issue in Win32 application

I am working on Win32 application. 我正在研究Win32应用程序。 All drawing is done in WM_PAINT. 所有绘图都在WM_PAINT中完成。 It is working fine. 它工作正常。 i added one more functionality in it. 我在其中添加了一个功能。 When i click on button than Command prompt is executed. 当我点击按钮时,执行命令提示符。 This i can done using WinExec. 我可以使用WinExec完成。 Now when i move Cmd.exe than in background drawing is not done. 现在当我移动Cmd.exe而不是在背景绘图中没有完成。 I update the code with CreateProcess than also the same thing is happened. 我用CreateProcess更新代码比发生同样的事情。 Can anyone please help me whats wrong with this code. 任何人都可以帮我解决这个代码的错误。 Is it because when i focus on This window than focus is losed and drawing is not done. 是因为当我专注于这个窗口时,焦点被丢失并且没有完成绘图。

Code

    STARTUPINFO si;
    PROCESS_INFORMATION pi;
ZeroMemory( &si, sizeof(si) );          
    si.cb = sizeof(si);
ZeroMemory( &pi, sizeof(pi) );

TCHAR  wchCmdPath[MAX_PATH];
memset(wchCmdPath,_T('\0'),MAX_PATH);
GetSystemDirectory(wchCmdPath,MAX_PATH);
wcscat(wchCmdPath,_T("\\cmd.exe"));
// Start the child process. 
    if( !CreateProcess( NULL,   // No module name (use command line)
    wchCmdPath,        // Command line
    NULL,           // Process handle not inheritable
    NULL,           // Thread handle not inheritable
    FALSE,          // Set handle inheritance to FALSE
    0,              // No creation flags
    NULL,           // Use parent's environment block
    NULL,           // Use parent's starting directory 
    &si,            // Pointer to STARTUPINFO structure
    &pi )           // Pointer to PROCESS_INFORMATION structure
) 
{
    printf( "CreateProcess failed (%d).\n", GetLastError() );
    return;
}



// Close process and thread handles. 
CloseHandle( pi.hProcess );
CloseHandle( pi.hThread );


        InvalidateRect(hwnd,NULL,TRUE);
        UpdateWindow(hwnd);

Thanks in advance 提前致谢

It may be because WinExec returns when the spawned executable calls GetMessage or until a timeout is specified (see: http://msdn.microsoft.com/en-us/library/ms687393(v=vs.85).aspx ). 这可能是因为当生成的可执行文件调用GetMessage或指定超时时WinExec返回(请参阅: http//msdn.microsoft.com/en-us/library/ms687393 (v = vs.85.aspx )。

This means that until this happens, your program is stuck at this line and is therefore not processing messages (including WM_PAINT ). 这意味着在此之前,您的程序会停留在此行,因此不会处理消息(包括WM_PAINT )。 CreateProcess doesn't have this problem, but were you waiting on it at all? CreateProcess没有这个问题,但你在等它吗?

The problem most likely lies outside of the code you have posted. 问题很可能在您发布的代码之外。 Your InvalidateRect / UpdateWindow will paint the window once after the child process is spawned, but it will not handle redrawing as you move it later. 您的InvalidateRect / UpdateWindow将在生成子进程后绘制一次窗口,但在稍后移动时它将不会处理重绘。

What you need to solve the "Now when i move Cmd.exe than in background drawing is not done" issue is to think about what are you doing while you are waiting for the process. 您需要解决的问题是“现在当我移动Cmd.exe而不是在后台绘图时没有完成”问题是要考虑在等待过程中你在做什么。 While waiting for the process, you still need to process incoming messages (at least the WM_PAINT one). 在等待进程时,您仍然需要处理传入的消息(至少WM_PAINT消息)。

You cannot use WaitForSingleObject to wait for the process, you need to use some function which will let you handle messages as well, like MsgWaitForMultipleObjects - it is your application who needs to repaint the window each time WM_PAINT is sent, nobody will do it for you. 你不能使用WaitForSingleObject来等待进程,你需要使用一些能让你处理消息的函数,比如MsgWaitForMultipleObjects - 你的应用程序需要在每次发送WM_PAINT时重新绘制窗口,没有人会为你做。

Another approach is not to wait for the process at all, but implement handling its result in an event-driven fashion, right in the main message loop (what you do now can be viewed as a "modal" process, perhaps you can rethink your application to make it "modeless"). 另一种方法是不要等待进程,而是实现以事件驱动的方式处理其结果,就在主消息循环中(现在你可以将其视为“模态”过程,也许你可以重新思考你的应用程序使其“无模式”)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM