mirror of
				https://github.com/gnif/LookingGlass.git
				synced 2025-11-03 22:22:08 +00:00 
			
		
		
		
	[idd] helper: use CWindow helper to avoid global state
This commit is contained in:
		
							
								
								
									
										60
									
								
								idd/LGIddHelper/CWindow.cpp
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										60
									
								
								idd/LGIddHelper/CWindow.cpp
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,60 @@
 | 
			
		||||
#include "CWindow.h"
 | 
			
		||||
#include <strsafe.h>
 | 
			
		||||
#include <CDebug.h>
 | 
			
		||||
 | 
			
		||||
ATOM CWindow::s_atom = 0;
 | 
			
		||||
static HINSTANCE hInstance = (HINSTANCE)GetModuleHandle(NULL);
 | 
			
		||||
 | 
			
		||||
bool CWindow::registerClass()
 | 
			
		||||
{
 | 
			
		||||
  WNDCLASSEX wx = {};
 | 
			
		||||
  wx.cbSize = sizeof(WNDCLASSEX);
 | 
			
		||||
  wx.lpfnWndProc = wndProc;
 | 
			
		||||
  wx.hInstance = hInstance;
 | 
			
		||||
  wx.lpszClassName = L"LookingGlassIddHelper";
 | 
			
		||||
  wx.hIcon = LoadIcon(NULL, IDI_APPLICATION);
 | 
			
		||||
  wx.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
 | 
			
		||||
  wx.hCursor = LoadCursor(NULL, IDC_ARROW);
 | 
			
		||||
  wx.hbrBackground = (HBRUSH)COLOR_APPWORKSPACE;
 | 
			
		||||
 | 
			
		||||
  s_atom = RegisterClassEx(&wx);
 | 
			
		||||
  return s_atom;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
CWindow::CWindow()
 | 
			
		||||
{
 | 
			
		||||
  CreateWindowEx(0, MAKEINTATOM(s_atom), NULL,
 | 
			
		||||
    0, 0, 0, 0, 0, NULL, NULL, hInstance, this);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
LRESULT CWindow::wndProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
 | 
			
		||||
{
 | 
			
		||||
  CWindow *self;
 | 
			
		||||
  if (uMsg == WM_NCCREATE)
 | 
			
		||||
  {
 | 
			
		||||
    LPCREATESTRUCT lpcs = (LPCREATESTRUCT)lParam;
 | 
			
		||||
    self = (CWindow*)lpcs->lpCreateParams;
 | 
			
		||||
    self->m_hwnd = hwnd;
 | 
			
		||||
    SetWindowLongPtr(hwnd, GWLP_USERDATA, (LPARAM) self);
 | 
			
		||||
  }
 | 
			
		||||
  else
 | 
			
		||||
  {
 | 
			
		||||
    self = (CWindow*)GetWindowLongPtr(hwnd, GWLP_USERDATA);
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  if (self)
 | 
			
		||||
    return self->handleMessage(uMsg, wParam, lParam);
 | 
			
		||||
  else
 | 
			
		||||
    return DefWindowProc(hwnd, uMsg, wParam, lParam);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
LRESULT CWindow::handleMessage(UINT uMsg, WPARAM wParam, LPARAM lParam)
 | 
			
		||||
{
 | 
			
		||||
  return DefWindowProc(m_hwnd, uMsg, wParam, lParam);  
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
CWindow::~CWindow()
 | 
			
		||||
{
 | 
			
		||||
  if (m_hwnd)
 | 
			
		||||
    DestroyWindow(m_hwnd);
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										20
									
								
								idd/LGIddHelper/CWindow.h
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										20
									
								
								idd/LGIddHelper/CWindow.h
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,20 @@
 | 
			
		||||
#pragma once
 | 
			
		||||
 | 
			
		||||
#define WIN32_LEAN_AND_MEAN
 | 
			
		||||
#include <windows.h>
 | 
			
		||||
#include <shellapi.h>
 | 
			
		||||
 | 
			
		||||
#define WM_NOTIFY_ICON (WM_USER)
 | 
			
		||||
 | 
			
		||||
class CWindow {
 | 
			
		||||
  static ATOM s_atom;
 | 
			
		||||
  static LRESULT CALLBACK wndProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
 | 
			
		||||
 | 
			
		||||
  HWND m_hwnd = NULL;
 | 
			
		||||
  LRESULT handleMessage(UINT uMsg, WPARAM wParam, LPARAM lParam);
 | 
			
		||||
 | 
			
		||||
public:
 | 
			
		||||
  static bool registerClass();
 | 
			
		||||
  CWindow();
 | 
			
		||||
  ~CWindow();
 | 
			
		||||
};
 | 
			
		||||
@@ -178,11 +178,13 @@ copy /Y "$(ProjectDir)VERSION" "$(SolutionDir)$(Platform)\$(Configuration)\LGIdd
 | 
			
		||||
  <ItemGroup>
 | 
			
		||||
    <ClCompile Include="$(SolutionDir)LGCommon\*.cpp" />
 | 
			
		||||
    <ClCompile Include="CPipeClient.cpp" />
 | 
			
		||||
    <ClCompile Include="CWindow.cpp" />
 | 
			
		||||
    <ClCompile Include="main.cpp" />
 | 
			
		||||
  </ItemGroup>
 | 
			
		||||
  <ItemGroup>
 | 
			
		||||
    <CLInclude Include="$(SolutionDir)LGCommon\*.h" />
 | 
			
		||||
    <ClInclude Include="CPipeClient.h" />
 | 
			
		||||
    <ClInclude Include="CWindow.h" />
 | 
			
		||||
  </ItemGroup>
 | 
			
		||||
  <ItemGroup>
 | 
			
		||||
    <None Include="packages.config" />
 | 
			
		||||
 
 | 
			
		||||
@@ -1,4 +1,4 @@
 | 
			
		||||
#include <Windows.h>
 | 
			
		||||
#include <Windows.h>
 | 
			
		||||
#include <wrl.h>
 | 
			
		||||
#include <UserEnv.h>
 | 
			
		||||
 | 
			
		||||
@@ -11,6 +11,7 @@ using namespace Microsoft::WRL::Wrappers::HandleTraits;
 | 
			
		||||
#include "CDebug.h"
 | 
			
		||||
#include "VersionInfo.h"
 | 
			
		||||
#include "CPipeClient.h"
 | 
			
		||||
#include "CWindow.h"
 | 
			
		||||
 | 
			
		||||
#define ARRAY_SIZE(x) (sizeof(x) / sizeof*(x))
 | 
			
		||||
#define SVCNAME L"Looking Glass (IDD Helper)"
 | 
			
		||||
@@ -31,11 +32,6 @@ static std::wstring              l_exitEventName;
 | 
			
		||||
 | 
			
		||||
static void Launch();
 | 
			
		||||
 | 
			
		||||
LRESULT CALLBACK DummyWndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
 | 
			
		||||
{
 | 
			
		||||
  return DefWindowProc(hwnd, msg, wParam, lParam);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
int WINAPI WinMain(_In_ HINSTANCE hInstance, _In_opt_ HINSTANCE hPrevInstance, _In_ LPSTR lpCmdLine, _In_ int nShowCmd)
 | 
			
		||||
{
 | 
			
		||||
  g_debug.Init("looking-glass-iddhelper");
 | 
			
		||||
@@ -80,24 +76,13 @@ int WINAPI WinMain(_In_ HINSTANCE hInstance, _In_opt_ HINSTANCE hPrevInstance, _
 | 
			
		||||
    return EXIT_FAILURE;
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  WNDCLASSEX wx = {};
 | 
			
		||||
  wx.cbSize        = sizeof(WNDCLASSEX);
 | 
			
		||||
  wx.lpfnWndProc   = DummyWndProc;
 | 
			
		||||
  wx.hInstance     = hInstance;
 | 
			
		||||
  wx.lpszClassName = L"DUMMY_CLASS";
 | 
			
		||||
  wx.hIcon         = LoadIcon(NULL, IDI_APPLICATION);
 | 
			
		||||
  wx.hIconSm       = LoadIcon(NULL, IDI_APPLICATION);
 | 
			
		||||
  wx.hCursor       = LoadCursor(NULL, IDC_ARROW);
 | 
			
		||||
  wx.hbrBackground = (HBRUSH)COLOR_APPWORKSPACE;
 | 
			
		||||
  ATOM aclass;
 | 
			
		||||
  if (!(aclass = RegisterClassEx(&wx)))
 | 
			
		||||
  if (!CWindow::registerClass())
 | 
			
		||||
  {
 | 
			
		||||
    DEBUG_ERROR("Failed to register message window class");
 | 
			
		||||
    return EXIT_FAILURE;
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  HWND msgWnd = CreateWindowEx(0, MAKEINTATOM(aclass), NULL,
 | 
			
		||||
    0, 0, 0, 0, 0, NULL, NULL, hInstance, NULL);
 | 
			
		||||
  CWindow window;
 | 
			
		||||
 | 
			
		||||
  bool running = g_pipe.Init();
 | 
			
		||||
  while (running)
 | 
			
		||||
@@ -128,7 +113,6 @@ int WINAPI WinMain(_In_ HINSTANCE hInstance, _In_opt_ HINSTANCE hPrevInstance, _
 | 
			
		||||
  }
 | 
			
		||||
  g_pipe.DeInit();
 | 
			
		||||
 | 
			
		||||
  DestroyWindow(msgWnd);
 | 
			
		||||
  return EXIT_SUCCESS;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user