是否可以将一个应用程序嵌入到 Windows 中的另一个应用程序中?

问题描述:

我正在用 Visual C++ 2008 编写一个 Windows 应用程序,我想在其中嵌入 Windows 附带的计算器 (calc.exe).有谁知道这是否可行,如果可行,您能给我一些有关如何实现这一目标的提示吗?

I'm writing a Windows application in Visual C++ 2008 and I want to embed the calculator (calc.exe) that comes with Windows in it. Does anyone know if that is possible, and if it is, can you give me hints on how I could achieve that?

是的,可以将 calc 嵌入到您自己的应用程序中,但它仍将在其自己的进程空间中运行.UAC 可能还会施加一些限制,但这取决于 calc 的启动方式.您需要做的就是更改主计算窗口的父级并将其样式更改为 WS_CHILD.

Yes, it's possible to embed the calc in your own application but it will still run in it's own process space. There may also be some restrictions imposed by UAC but that will depend on how calc is launched. All you need to do is change the parent of the main calc window and change it's style to WS_CHILD.

void EmbedCalc(HWND hWnd)
{
    HWND calcHwnd = FindWindow(L"CalcFrame", NULL);
    if(calcHwnd != NULL)
    {
        // Change the parent so the calc window belongs to our apps main window 
        SetParent(calcHwnd, hWnd);

        // Update the style so the calc window is embedded in our main window
        SetWindowLong(calcHwnd, GWL_STYLE, GetWindowLong(calcHwnd, GWL_STYLE) | WS_CHILD);

        // We need to update the position as well since changing the parent does not
        // adjust it automatically.
        SetWindowPos(calcHwnd, NULL, 0, 0, 0, 0, SWP_NOSIZE | SWP_NOZORDER);
    }
}