Guest User

IApplicationDesignModeSettings

a guest
Feb 7th, 2013
233
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.58 KB | None | 0 0
  1. #include "stdafx.h"
  2. #include <Windows.h>
  3. #include <ShObjIdl.h>
  4. #include <atlbase.h>
  5. #include <string>
  6.  
  7. LPCWSTR g_szClassName = L"myWindowClass";
  8.  
  9. LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
  10. {
  11.     switch(msg)
  12.     {
  13.         case WM_CLOSE:
  14.             DestroyWindow(hwnd);
  15.         break;
  16.         case WM_DESTROY:
  17.             PostQuitMessage(0);
  18.         break;
  19.         default:
  20.             return DefWindowProc(hwnd, msg, wParam, lParam);
  21.     }
  22.     return 0;
  23. }
  24.  
  25. HRESULT LaunchApp(const std::wstring strAppUserModelId, PDWORD pdwProcessId)
  26. {
  27.     IApplicationActivationManager *spAppActivationManager;
  28.     HRESULT hrResult = E_INVALIDARG;
  29.     if (!strAppUserModelId.empty())
  30.     {
  31.         // Instantiate IApplicationActivationManager
  32.         hrResult = CoCreateInstance(CLSID_ApplicationActivationManager,
  33.             NULL,
  34.             CLSCTX_LOCAL_SERVER,
  35.             IID_IApplicationActivationManager,
  36.             (LPVOID*)&spAppActivationManager);
  37.  
  38.         if (SUCCEEDED(hrResult))
  39.         {
  40.             // This call ensures that the app is launched as the foreground window
  41.             hrResult = CoAllowSetForegroundWindow(spAppActivationManager, NULL);
  42.            
  43.             // Launch the app
  44.             if (SUCCEEDED(hrResult))
  45.             {
  46.                 hrResult = spAppActivationManager->ActivateApplication(strAppUserModelId.c_str(),
  47.                     NULL,
  48.                     AO_DESIGNMODE,
  49.                     pdwProcessId);
  50.             }
  51.         }
  52.     }
  53.  
  54.     return hrResult;
  55. }
  56.  
  57. int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
  58.     LPSTR lpCmdLine, int nCmdShow)
  59. {
  60.     WNDCLASSEX wc;
  61.     HWND hwnd;
  62.     MSG Msg;
  63.  
  64.     // Window Class
  65.     wc.cbSize        = sizeof(WNDCLASSEX);
  66.     wc.style         = CS_GLOBALCLASS;
  67.     wc.lpfnWndProc   = WndProc;
  68.     wc.cbClsExtra    = 0;
  69.     wc.cbWndExtra    = 0;
  70.     wc.hInstance     = hInstance;
  71.     wc.hIcon         = LoadIcon(NULL, IDI_APPLICATION);
  72.     wc.hCursor       = LoadCursor(NULL, IDC_ARROW);
  73.     wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
  74.     wc.lpszMenuName  = NULL;
  75.     wc.lpszClassName = g_szClassName;
  76.     wc.hIconSm       = LoadIcon(NULL, IDI_APPLICATION);
  77.  
  78.     if(!RegisterClassEx(&wc))
  79.     {
  80.         MessageBox(NULL, L"Window Class Registration Failed!", L"Error!",
  81.             MB_ICONEXCLAMATION | MB_OK);
  82.         return 0;
  83.     }
  84.  
  85.     //Create the Window
  86.     hwnd = CreateWindowEx(
  87.         WS_EX_CLIENTEDGE,
  88.         g_szClassName,
  89.         L"Modern",
  90.         WS_OVERLAPPEDWINDOW,
  91.         CW_USEDEFAULT, CW_USEDEFAULT, 1366, 768,
  92.         NULL, NULL, hInstance, NULL);
  93.  
  94.     if(hwnd == NULL)
  95.     {
  96.         MessageBox(NULL, L"Window Creation Failed!", L"Error!",
  97.             MB_ICONEXCLAMATION | MB_OK);
  98.         return 0;
  99.     }
  100.  
  101.     ShowWindow(hwnd, nCmdShow);
  102.     UpdateWindow(hwnd);
  103.    
  104.     //initialize the designmodesettings
  105.  
  106.     IApplicationDesignModeSettings *pDesignModeSettings;
  107.  
  108.     //CoInitializeEx(NULL,COINIT_MULTITHREADED);
  109.  
  110.     // CoCreate the design mode settings object
  111.     HRESULT hr = CoCreateInstance(CLSID_ApplicationDesignModeSettings, nullptr, CLSCTX_INPROC, IID_PPV_ARGS(&pDesignModeSettings));
  112.     if (SUCCEEDED(hr))
  113.     {
  114.         IInitializeWithWindow *pInitializeWithWindow;
  115.         //hr = pDesignModeSettings->QueryInterface(IID_PPV_ARGS(&pInitializeWithWindow));
  116.         hr = pDesignModeSettings->QueryInterface(&pInitializeWithWindow); // Retreive the implementation for IInitializeWithWindow
  117.         if (SUCCEEDED(hr))
  118.         {
  119.             // Before we spoof any state, we must first initialize the design
  120.             // mode settings object with a proxy core window. Since apps
  121.             // running in design mode don't have an actual core window, we must
  122.             // supply an HWND that can be used as a proxy.
  123.             hr = pInitializeWithWindow->Initialize(hwnd); //E_ACCESSDENIED. Dunno why.
  124.             pInitializeWithWindow->Release();
  125.         }
  126.         BOOL fSupported=false;
  127.         SIZE sizeNativeDisplay = {1366, 768};
  128.         if (SUCCEEDED(hr))
  129.         {
  130.             // Verify that our desired spoofed settings are supported.
  131.        
  132.             //SCALE_FACTOR scaleFactor = SCALE_100_PERCENT;
  133.             APPLICATION_VIEW_STATE viewState = AVS_FULLSCREEN_LANDSCAPE;
  134.             fSupported=true;
  135.             hr = pDesignModeSettings->IsApplicationViewStateSupported(viewState,
  136.                                                                       sizeNativeDisplay,
  137.                                                                       SCALE_100_PERCENT,
  138.                                                                       &fSupported);
  139.         }
  140.         if (SUCCEEDED(hr) && fSupported)
  141.         {
  142.             // Set the spoofed native display size.
  143.             hr = pDesignModeSettings->SetNativeDisplaySize(sizeNativeDisplay);
  144.  
  145.             if (SUCCEEDED(hr))
  146.             {
  147.                 // Set the spoofed scale factor to 100%.
  148.                 hr = pDesignModeSettings->SetScaleFactor(SCALE_100_PERCENT);
  149.             }
  150.  
  151.             if (SUCCEEDED(hr))
  152.             {
  153.                 // Set the spoofed application view state to full-screen landscape.
  154.                 hr = pDesignModeSettings->SetApplicationViewState(AVS_FULLSCREEN_LANDSCAPE);
  155.             }
  156.  
  157.             if (SUCCEEDED(hr))
  158.             {
  159.                 // Now that all the necessary state has been spoofed, calculate
  160.                 // the size that the app should occupy.
  161.                 SIZE sizeApplication;
  162.                 hr = pDesignModeSettings->ComputeApplicationSize(&sizeApplication);
  163.             }
  164.         }
  165.  
  166.         pDesignModeSettings->Release();
  167.     }
  168.  
  169.     //launch the desired app
  170.     HRESULT hrResult = S_OK;
  171.     if (SUCCEEDED(CoInitializeEx(NULL, COINIT_APARTMENTTHREADED)))
  172.     {
  173.         std::wstring tes;
  174.         tes=L"microsoft.windowscommunicationsapps_8wekyb3d8bbwe!Microsoft.WindowsLive.Mail"; //fixed const
  175.         DWORD dwProcessId = 0;
  176.         hrResult = LaunchApp(tes, &dwProcessId);
  177.     }
  178.  
  179.  
  180.     //Window Message Loop
  181.     while(GetMessage(&Msg, NULL, 0, 0) > 0)
  182.     {
  183.         TranslateMessage(&Msg);
  184.         DispatchMessage(&Msg);
  185.     }
  186.     return Msg.wParam;
  187. }
Advertisement
Add Comment
Please, Sign In to add comment