• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

如何创建一个不规则形状的窗口

c++ 搞代码 7年前 (2018-08-07) 126次浏览 已收录 0个评论

可以使用新的SDK函数SetWindowRgn。该函数将绘画和鼠标消息限定在窗口的一个指定的区域,实际上使窗口成为指定的不规则形状。

使用AppWizard创建一个基于对话框的应用程序并使用资源编辑器从主对话资源中删除有的缺省控件、标题以及边界。

给对话类增加一个CRgn数据成员,以后要使用该数据成员建立窗口区域。
Class CRoundDlg : public CDialog
{ …
private :
Crgn m_rgn : // window region
…}

http://www.gaodaima.com/65049.html如何创建一个不规则形状的窗口

修改OnInitDialog函数建立一个椭圆区域并调用SetWindowRgn将该区域分配给窗口:
BOOL CRoundDlg : : OnInitDialog ( )
{

CDialog : : OnInitDialog ( )

//Get size of dialog .

CRect rcDialog ;

GetClientRect (rcDialog )

// Create region and assign to window .

m_rgn . CreateEllipticRgn (0 , 0 , rcDialog.Width( ),

rcDialog.Height ( ) )

SetWindowRgn (GetSafeHwnd ( ) , (HRGN) m_ rgn ,TRUE );

return TRUE
}
通过建立区域和调用SetWindowRgn,已经建立一个不规则形状的窗口,下面的例子程序是修改OnPaint函数使窗口形状看起来象一个球形体。
voik CRoundDlg : : OnPaint ( )
{
CPaintDC de (this) // device context for painting.
//draw ellipse with out any border
dc. SelecStockObject (NULL_PEN)
//get the RGB colour components of the sphere color
COLORREF color= RGB( 0 , 0 , 255)
BYTE byRed =GetRValue (color)
BYTE byGreen = GetGValue (color)
BYTE byBlue = GetBValue (color)
// get the size of the view window Crect
rect GetClientRect (rect)
// get minimun number of units
int nUnits =min (rect.right , rect.bottom )
//calculate he horiaontal and vertical step size
float fltStepHorz = (float) rect.right /nUnits
float fltStepVert = (float) rect.bottom /nUnits
int nEllipse = nUnits/3 // calculate how many todraw int nIndex
// current ellipse that is being draw
CBrush brush
// bursh used for ellipse fill color
CBrush *pBrushOld // previous brush that was selected into dc
//draw ellipse , gradually moving towards upper-rightcorner
for (nIndex = 0 nIndes < + nEllipse nIndes++)
{ //creat solid brush brush .
CreatSolidBrush (RGB ( ( (nIndex*byRed ) /nEllipse ).

( ( nIndex * byGreen ) /nEllipse ), ( (nIndex*byBlue)/nEllipse ) ) )
//select brush into dc
pBrushOld= dc .SelectObject (&brhsh)
//draw ellipse
dc .Ellipse ( (int) fltStepHorz * 2, (int)fltStepVert * nIndex ,
rect. right -( (int)fltStepHorz * nIndex )+ 1, rect . bottom -( (int)
fltStepVert * (nIndex *2) ) +1)
//delete the brush
brush.DelecteObject ( )
} }
最后,处理WM_NCHITTEST消息,使当击打窗口的任何位置时能移动窗口。
UINT CRoundDlg : : OnNchitTest (Cpoint point )
{
//Let user move window by clickign anywhere on thewindow .
UINT nHitTest = CDialog : : OnNcHitTest (point)
rerurn (nHitTest = = HTCLIENT)? HTCAPTION: nHitTest

欢迎大家阅读《如何创建一个不规则形状的窗口》,跪求各位点评,若觉得好的话请收藏本文,by 搞代码


搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:如何创建一个不规则形状的窗口

喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址