开发者

Repaint issues when switching between programs

开发者 https://www.devze.com 2023-03-17 12:47 出处:网络
MyApp (.NET c#) is triggered by OtherApp (c++). When triggered my app takes over the whole screen and gives the user two options. One option quits MyApp and returns to the OtherApp home screen. The

MyApp (.NET c#) is triggered by OtherApp (c++).

When triggered my app takes over the whole screen and gives the user two options. One option quits MyApp and returns to the OtherApp home screen. The 2nd option exits the intial screen and shows another screen for user input - after inpu开发者_开发百科t it exits and returns to the OtherApp.

On occasion the OtherApp screen does not repaint(can only see the background, not buttons) - I can not easily replicate this (when I do it seems like a fluke), but I have seen it on a number of applications.

Is there a way that MyApp can force a screen repaint of OtherApp?

What could be causing this?

CLARIFICATION - OTHER APP IS NOT OURS. Our client uses OtherApp. MyApp is triggered by a filewatcher event. When we see a file, we process it. If this is the file we are looking for we give the user two options. OtherApp does not know MyApp exists.


Try and get the hwnd of OtherApp's main window and invalidate the whole thing:

[DllImport("user32.dll")]
static extern bool InvalidateRect(IntPtr hWnd, IntPtr lpRect, bool bErase);

[DllImport("user32.dll", SetLastError = true)]
static extern IntPtr FindWindow(string lpClassName, string lpWindowName);

static void InvalidateOtherApp()
{
  IntPtr hWnd = FindWindow(null, "OtherApp's Main Window's Title");
  if (hWnd != IntPtr.Zero)
    InvalidateRect(hWnd, IntPtr.Zero, true);
}


In OtherApp, add the C++ equivalent of Application.DoEvents(). It is apparently not processing the Windows messages. You can do it like this, taken from Microsoft Vterm sample program:

void CMainFrame::DoEvents()
{
MSG msg;

// Process existing messages in the application's message queue.
// When the queue is empty, do clean up and return.
while (::PeekMessage(&msg,NULL,0,0,PM_NOREMOVE) && !m_bCancel)
{
if (!AfxGetThread()->PumpMessage())

return;
}
}


Since OtherApp is not your application, you can modify your MyApp and send a message to OtherApp using the Win32 SendMessage Function . To do this in C# check out C# Win32 messaging with SendMessage . The message you want to send is WM_PAINT . The site uses a different message, but the idea is the same. Your code will be something similar to this:

[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = false)]
private static extern IntPtr SendMessage(IntPtr hWnd, Int32 Msg, IntPtr wParam, IntPtr lParam);

int WM_PAINT = 0xF;
SendMessage(hWnd, WM_PAINT, IntPtr.Zero, IntPtr.Zero);

This will send your repaint message to the application. You need to supply hWnd with the window handle of OtherApp. To get the window handle, you need to invoke the System.Diagnostics.Process class to find your application and call the MainWindowHandle property to get the handle.

0

精彩评论

暂无评论...
验证码 换一张
取 消