Skip to content

Instantly share code, notes, and snippets.

@johnhmj
Last active April 4, 2017 00:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save johnhmj/7c00187495c9bbfc16db996a63f00eb9 to your computer and use it in GitHub Desktop.
Save johnhmj/7c00187495c9bbfc16db996a63f00eb9 to your computer and use it in GitHub Desktop.
#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 <Windows.h>
//
// 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))
//
// 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