|
圆形窗口(VC++)
int SetWindowRgn(
HWND hWnd, // handle to window whose window region is to be set
HRGN hRgn, // handle to region
BOOL bRedraw // window redraw flag
);
只用上面这个API函数,就能创建一个椭圆形状的窗口。为了做编程笔记,做一个功能极简单的窗口。椭圆形状的窗口上贴上按钮,创建一个时间间隔为一秒的计时器,用计时器消息的触发像電子時計那样在按钮上显示当前的时刻。 1.创建CWnd的派生类CEllipseWnd。 #define ELLIPSE_WIDTH 200
#define ELLIPSE_HEIGHT 150
#define IDC_BUTTON1 1001
/////////////////////////////////////////////////////////////////////////////
// CEllipseWnd
CEllipseWnd::CEllipseWnd()
{
CString strClassName
= AfxRegisterWndClass(NULL,
theApp.LoadStandardCursor(IDC_CROSS),
(HBRUSH)::GetStockObject(LTGRAY_BRUSH));
CreateEx(0,
strClassName,
"Ellipse Window",
WS_POPUP,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
NULL,
NULL,
NULL);
SetWindowPos(NULL,
0,
0,
ELLIPSE_WIDTH,
ELLIPSE_HEIGHT,
SWP_NOZORDER | SWP_NOMOVE | SWP_NOREDRAW);
HRGN hRgn = CreateEllipticRgn(0,
0,
ELLIPSE_WIDTH - 1,
ELLIPSE_HEIGHT - 1);
SetWindowRgn(hRgn, TRUE);
DeleteObject(hRgn);
m_btnClock.Create("00:00:00", WS_CHILD | WS_VISIBLE,
CRect(ELLIPSE_WIDTH / 2 - 60,
ELLIPSE_HEIGHT / 2 - 20,
ELLIPSE_WIDTH / 2 + 60 ,
ELLIPSE_HEIGHT / 2 + 20),
this, IDC_BUTTON1);
SetTimer(1, 1000, NULL);
CenterWindow();
}
CEllipseWnd::~CEllipseWnd()
{
}
BEGIN_MESSAGE_MAP(CEllipseWnd, CWnd)
//{{AFX_MSG_MAP(CEllipseWnd)
ON_WM_NCHITTEST()
ON_BN_CLICKED(IDC_BUTTON1, OnButton1)
ON_WM_TIMER()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CEllipseWnd メッセージ ハンドラ
UINT CEllipseWnd::OnNcHitTest(CPoint point)
{
return HTCAPTION;
}
void CEllipseWnd::OnButton1()
{
SendMessage(WM_CLOSE);
}
void CEllipseWnd::OnTimer(UINT nIDEvent)
{
CTime tm = CTime::GetCurrentTime();
CString strText;
strText.Format("%02d:%02d:%02d", tm.GetHour(), tm.GetMinute(), tm.GetSecond());
m_btnClock.SetWindowText(strText);
CWnd::OnTimer(nIDEvent);
}
6.创建一个普通的CWinApp的派生类。在InitInstance()函数里,生成CEllipseWnd的实体,显示窗口。 BOOL CEllipseAppliApp::InitInstance()
{
m_pMainWnd = new CEllipseWnd();
m_pMainWnd->ShowWindow(m_nCmdShow);
m_pMainWnd->UpdateWindow();
return TRUE;
}
|