简体   繁体   中英

Drawing Issue in Win32 application

I am working on Win32 application. All drawing is done in 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. Now when i move Cmd.exe than in background drawing is not done. I update the code with CreateProcess than also the same thing is happened. 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 ).

This means that until this happens, your program is stuck at this line and is therefore not processing messages (including WM_PAINT ). CreateProcess doesn't have this problem, but were you waiting on it at all?

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.

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. While waiting for the process, you still need to process incoming messages (at least the WM_PAINT one).

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.

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").

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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