Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
#include "SingleInstance.h"
//
CSingleInstance::CSingleInstance(TCHAR* pMutexName)
{
this->m_hMutex = CreateMutex(NULL, FALSE, pMutexName);
this->m_bExist = (ERROR_ALREADY_EXISTS == GetLastError())?TRUE:FALSE;
}
CSingleInstance::CSingleInstance(const CSingleInstance& instance)
{
if (this == &instance)
{
return;
}
//
// Do nothing
//
}
CSingleInstance& CSingleInstance::operator=(const CSingleInstance& instance)
{
//
// Do nothing
//
return *this;
}
CSingleInstance::~CSingleInstance()
{
if (this->m_hMutex != NULL)
{
CloseHandle(this->m_hMutex);
this->m_hMutex = NULL;
}
}
BOOL CSingleInstance::IsRunning(void)
{
return this->m_bExist;
}
#ifndef _SingleInstance_H_
#define _SingleInstance_H_
//
#include "Win32CppLibMacro.h"
//
// Only one instance of an App is allowed to run.
// Usage: declare a global object, it doesn't work to declare a local.
// CSingleInstance si(L"MySingleInstance");
//
class CSingleInstance
{
public:
CSingleInstance(__in_opt TCHAR* pMutexName);
CSingleInstance(__in const CSingleInstance& instance);
CSingleInstance& operator=(__in const CSingleInstance& instance);
~CSingleInstance();
BOOL IsRunning(void);
private:
HANDLE m_hMutex;
BOOL m_bExist;
};
//
#endif
#ifndef _WIN32CPPLIB_H_
#define _WIN32CPPLIB_H_
//
#include "Win32CppLibMacro.h"
#include "SingleInstance.h"
#include "WindowPosition.h"
//
#endif
#ifndef _WIN32CPPLIBMACRO_H_
#define _WIN32CPPLIBMACRO_H_
//
#include <Windows.h>
//
// MessageBox for Exit
// exit: IDOK == ExitBox(hWnd)
// continue: IDCANCEL == ExitBox(hWnd)
#define ExitBox(hWnd) MessageBox(hWnd, L"Are you sure want to exit the app?", L"Exit", MB_ICONQUESTION | MB_OKCANCEL)
//
// initialize struct
#define InitStruct(s, t) memset((void*) s, 0x0, sizeof(t))
//
// copy struct
#define CopyStruct(s_dst, s_src, t) memcpy((void*) s_dst, (void*) s_src, sizeof(t))
//
#endif
//
// Visual Studio settings:
//
// 1. Win32DialogBox/Project Dependencies.../Win32CppLib
//
// 2. Win32DialogBox/Properties/All Configurations/C/C++/General/Additional Include Directories/$(SolutionDir)Win32CppLib\Win32CppLib
//
// 3. Win32DialogBox/Properties/All Configurations/Linker/General/Additional Dependencies/$(TargetDir)Win32CppLib.lib
//
// 4. if EXE smaller than "Visual C++ 2010 Redistributable Package x86"...
// x86: Win32DialogBox/Properties/Release/C/C++/Code Generation/Runtime Library/Multi-threaded (/MT)
// else..., but you have to install "Visual C++ 2010 Redistributable Package x86"
// x86: .../Multi-threaded DLL (/MD)
//
// 5. Build Debug & Release both: Build/Batch Build.../Select All/Build
//
#include "Win32DialogBox.h"
//
CWndPos WPos;
SWndSize sWSize;
SWndPos sWPos;
//
INT_PTR CALLBACK DialogProc(HWND w, UINT m, WPARAM wp, LPARAM lp);
//
int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPWSTR lpCmdLine, int nShowCmd)
{
DialogBox(hInstance, MAKEINTRESOURCE(IDD_WIN32DIALOGBOX), NULL, DialogProc);
return 0;
}
INT_PTR CALLBACK DialogProc(HWND w, UINT m, WPARAM wp, LPARAM lp)
{
INT_PTR ipWp = LOWORD(wp);
switch (m)
{
case WM_INITDIALOG:
WPos.setDefWndSize(w);
WPos.getWndSize(sWSize);
WPos.getPosCenterBoth(sWPos);
SetWindowPos(w, NULL, sWPos.pos_x, sWPos.pos_y, sWSize.width, sWSize.height, SWP_SHOWWINDOW);
break;
case WM_COMMAND:
switch (ipWp)
{
case IDOK:
MessageBox(w, L"OK", L"Caution", MB_ICONEXCLAMATION | MB_OK);
break;
case IDCANCEL:
if (IDOK == ExitBox(w))
{
EndDialog(w, ipWp);
return (INT_PTR) TRUE;
}
break;
default:
break;
}
break;
default:
break;
}
return (INT_PTR)FALSE;
}
#ifndef _WIN32DIALOGBOX_H_
#define _WIN32DIALOGBOX_H_
//
#include <Windows.h>
//
#include "resource.h"
#include "Win32CppLib.h"
//
#endif
#include "WindowPosition.h"
//
CWndPos::CWndPos()
{
InitStruct(&this->m_pos, SWndPos);
InitStruct(&this->m_wndSize, SWndSize);
this->m_sysSize.width = GetSystemMetrics(SM_CXSCREEN);
this->m_sysSize.height = GetSystemMetrics(SM_CYSCREEN);
}
CWndPos::CWndPos(HWND hWnd)
{
RECT r;
InitStruct(&r, RECT);
InitStruct(&this->m_pos, SWndPos);
this->m_sysSize.width = GetSystemMetrics(SM_CXSCREEN);
this->m_sysSize.height = GetSystemMetrics(SM_CYSCREEN);
//
// get size of a window or a dialog box
GetWindowRect(hWnd, &r);
this->m_wndSize.width = r.right - r.left;
this->m_wndSize.height = r.bottom - r.top;
}
CWndPos::CWndPos(const SWndSize& ws)
{
InitStruct(&this->m_pos, SWndPos);
CopyStruct(&this->m_wndSize, &ws, SWndSize);
this->m_sysSize.width = GetSystemMetrics(SM_CXSCREEN);
this->m_sysSize.height = GetSystemMetrics(SM_CYSCREEN);
}
CWndPos::CWndPos(const CWndPos& wndpos)
{
if (this == &wndpos)
{
return;
}
CopyStruct(&this->m_pos, &wndpos.m_pos, SWndPos);
CopyStruct(&this->m_sysSize, &wndpos.m_sysSize, SWndSize);
CopyStruct(&this->m_wndSize, &wndpos.m_wndSize, SWndSize);
}
CWndPos& CWndPos::operator=(const CWndPos& wndpos)
{
if (this != &wndpos)
{
CopyStruct(&this->m_pos, &wndpos.m_pos, SWndPos);
CopyStruct(&this->m_sysSize, &wndpos.m_sysSize, SWndSize);
CopyStruct(&this->m_wndSize, &wndpos.m_wndSize, SWndSize);
}
return (*this);
}
CWndPos::~CWndPos()
{
InitStruct(&this->m_pos, SWndPos);
InitStruct(&this->m_sysSize, SWndPos);
InitStruct(&this->m_wndSize, SWndSize);
}
void CWndPos::setWndSize(const SWndSize& ws)
{
CopyStruct(&this->m_wndSize, &ws, SWndSize);
}
void CWndPos::setDefWndSize(HWND hWnd)
{
RECT r;
InitStruct(&r, RECT);
//
// get size of a window or a dialog box
GetWindowRect(hWnd, &r);
this->m_wndSize.width = r.right - r.left;
this->m_wndSize.height = r.bottom - r.top;
}
void CWndPos::getWndSize(SWndSize& ws)
{
ws.width = this->m_wndSize.width;
ws.height = this->m_wndSize.height;
}
void CWndPos::getSysRes(SWndSize& ws)
{
ws.width = this->m_sysSize.width;
ws.height = this->m_sysSize.height;
}
void CWndPos::getPosCenterBoth(SWndPos& wp)
{
this->m_pos.pos_x = (this->m_sysSize.width - this->m_wndSize.width) / 2;
this->m_pos.pos_y = (this->m_sysSize.height - this->m_wndSize.height) / 2;
wp.pos_x = this->m_pos.pos_x;
wp.pos_y = this->m_pos.pos_y;
}
void CWndPos::getPosCenterTop(SWndPos& wp)
{
this->m_pos.pos_x = (this->m_sysSize.width - this->m_wndSize.width) / 2;
wp.pos_x = this->m_pos.pos_x;
wp.pos_y = 0;
}
void CWndPos::getPosCenterBottom(SWndPos& wp)
{
this->m_pos.pos_x = (this->m_sysSize.width - this->m_wndSize.width) / 2;
this->m_pos.pos_y = this->m_sysSize.height - this->m_wndSize.height;
wp.pos_x = this->m_pos.pos_x;
wp.pos_y = this->m_pos.pos_y;
}
void CWndPos::getPosRightTop(SWndPos& wp)
{
this->m_pos.pos_x = this->m_sysSize.width - this->m_wndSize.width;
wp.pos_x = this->m_pos.pos_x;
wp.pos_y = 0;
}
void CWndPos::getPosRightCenter(SWndPos& wp)
{
this->m_pos.pos_x = this->m_sysSize.width - this->m_wndSize.width;
this->m_pos.pos_y = (this->m_sysSize.height - this->m_wndSize.height) / 2;
wp.pos_x = this->m_pos.pos_x;
wp.pos_y = this->m_pos.pos_y;
}
void CWndPos::getPosRightBottom(SWndPos& wp)
{
this->m_pos.pos_x = this->m_sysSize.width - this->m_wndSize.width;
this->m_pos.pos_y = this->m_sysSize.height - this->m_wndSize.height;
wp.pos_x = this->m_pos.pos_x;
wp.pos_y = this->m_pos.pos_y;
}
void CWndPos::getPosLeftTop(SWndPos& wp)
{
wp.pos_x = 0;
wp.pos_y = 0;
}
void CWndPos::getPosLeftCenter(SWndPos& wp)
{
this->m_pos.pos_y = (this->m_sysSize.height - this->m_wndSize.height) / 2;
wp.pos_x = 0;
wp.pos_y = this->m_pos.pos_y;
}
void CWndPos::getPosLeftBottom(SWndPos& wp)
{
this->m_pos.pos_y = this->m_sysSize.height - this->m_wndSize.height;
wp.pos_x = 0;
wp.pos_y = this->m_pos.pos_y;
}
#ifndef _WINDOWPOSITION_H_
#define _WINDOWPOSITION_H_
//
#include "Win32CppLibMacro.h"
//
// Window(or Dialog box) position APIs:
// 1. SetWindowPos(HWND hWnd, HWND hWndInsertAfter, int X, int Y, int cx, int cy, UINT uFlags)
// 2. CreateWindow(LPCTSTR lpClassName, LPCTSTR lpWindowName, DWORD dwStyle, int x, int y, int nWidth, int nHeight, HWND hWndParent, HMENU hMenu, HINSTANCE hInstance LPVOID lpParam)
//
// struct position
typedef struct
{
int pos_x;
int pos_y;
}SWndPos;
//
// struct window size
typedef struct
{
int width;
int height;
}SWndSize;
//
// class window position
class CWndPos
{
public:
CWndPos();
CWndPos(__in HWND hWnd);
CWndPos(__in const SWndSize& ws);
CWndPos(__in const CWndPos& wndpos);
CWndPos& operator=(__in const CWndPos& wndpos);
~CWndPos();
//
// set window size
void setWndSize(__in const SWndSize& ws);
//
// set Default window size(or get dialog box size from RC file)
void setDefWndSize(__in HWND hWnd);
//
// get window size (or default size)
void getWndSize(__out SWndSize& ws);
//
// get system resolution
void getSysRes(__out SWndSize& ws);
//
// get Center position both(horizontal and vertical)
void getPosCenterBoth(__out SWndPos& wp);
//
void getPosCenterTop(__out SWndPos& wp);
void getPosCenterBottom(__out SWndPos& wp);
//
void getPosRightTop(__out SWndPos& wp);
void getPosRightCenter(__out SWndPos& wp);
void getPosRightBottom(__out SWndPos& wp);
void getPosLeftTop(__out SWndPos& wp);
void getPosLeftCenter(__out SWndPos& wp);
void getPosLeftBottom(__out SWndPos& wp);
private:
//
// new position of the window
SWndPos m_pos;
//
// system resolution
SWndSize m_sysSize;
//
// window size
SWndSize m_wndSize;
};
//
#endif
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment