Skip to content

Instantly share code, notes, and snippets.

@tkojitu
Last active November 20, 2023 04:51
Show Gist options
  • Save tkojitu/e7e178aaff088da542c7a6a3eec2589b to your computer and use it in GitHub Desktop.
Save tkojitu/e7e178aaff088da542c7a6a3eec2589b to your computer and use it in GitHub Desktop.
SetWindowRgn
#include "stdafx.h"
#include "MyWnd.h"
static CString s_wndclazz;
IMPLEMENT_DYNAMIC(MyWnd, CWnd)
BEGIN_MESSAGE_MAP(MyWnd, CWnd)
ON_WM_CREATE()
ON_WM_PAINT()
END_MESSAGE_MAP()
void MyWnd::RegisterWndClazz()
{
if (!s_wndclazz.IsEmpty())
return;
s_wndclazz = AfxRegisterWndClass(
CS_VREDRAW | CS_HREDRAW,
::LoadCursor(NULL, IDC_ARROW),
(HBRUSH)::GetStockObject(WHITE_BRUSH),
::LoadIcon(NULL, IDI_APPLICATION));
}
bool MyWnd::CreateMe()
{
RegisterWndClazz();
CRect r = GetInitialRect();
return CreateEx(
0,
s_wndclazz,
L"MyWnd",
WS_POPUP | WS_CLIPSIBLINGS | WS_CLIPCHILDREN,
{ r.left, r.top, r.right, r.bottom }, // {100, 100, 740, 630}
nullptr,
0);
}
CSize MyWnd::GetBaseSize(
) {
return { 640, 480 };
}
CRect MyWnd::GetInitialRect(
) {
int offset = 100;
CSize sz = GetBaseSize();
CRect r(0, 0, sz.cx, sz.cy + m_tri_size);
r.MoveToXY(100, 100);
return r;
}
int MyWnd::OnCreate(
LPCREATESTRUCT createStruct
) {
int ret = __super::OnCreate(createStruct);
if (ret == -1)
return ret;
GetRgn(m_rgn);
SetWindowRgn(m_rgn, TRUE);
return 0;
}
void MyWnd::GetRgn(
CRgn & rgn
) {
CSize sz = GetBaseSize();
CRect r(0, m_tri_size, sz.cx, sz.cy + m_tri_size);
int xc = (r.left + r.right) / 2;
CPoint vs[] = {
{ r.left, r.top }, // {x=0 y=50}
{ xc - m_tri_size, r.top }, // {x=270 y=50}
{ xc, r.top - m_tri_size }, // {x=320 y=0}
{ xc + m_tri_size, r.top }, // {x=370 y=50}
{ r.right, r.top }, // {x=640 y=50}
{ r.right, r.bottom }, // {x=640 y=530}
{ r.left, r.bottom } // {x=0 y=530}
};
rgn.CreatePolygonRgn(vs, _countof(vs), ALTERNATE);
}
void MyWnd::OnPaint(
) {
CPaintDC dc(this);
CRect r;
GetClientRect(r);
dc.FillSolidRect(r, RGB(255, 0, 0));
}
#pragma once
class MyWnd : public CWnd
{
DECLARE_DYNAMIC(MyWnd)
DECLARE_MESSAGE_MAP()
public:
MyWnd() {}
virtual ~MyWnd() {}
static void RegisterWndClazz();
virtual bool CreateMe();
protected:
afx_msg int OnCreate(LPCREATESTRUCT createStruct);
afx_msg void OnPaint();
virtual void GetRgn(CRgn & rgn);
virtual CSize GetBaseSize();
virtual CRect GetInitialRect();
private:
int m_tri_size = 50;
CRgn m_rgn;
};
@marry-joy-maker
Copy link

Thank you for sharing this information with me, right when I was wondering, I wish you good suika health to produce more useful articles

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment