CrectTracker m_tracker;
…
};
其次,在文档类的构造函数中初始化CRectTracker 对象:
CSampleDoc:: CSampleDOC ()
{
//Initialize tracker position, size and style.
m_tracker.m_rect.SetRect (0, 0, 10, 10);
m_tracker.m_nStyle=CRectTracker:: resizeInside |
CRectTracker:: dottedLine;
}
然后,在OnDraw函数中画椭圆和踪迹矩形:
void CSampleView:: OnDraw (CDC* pDC)
{
CSampleDoc* pDoc=GetDocument ();
ASSERT_VALID (pDoc);
//Select blue brush into device context.
CBrush brush (RGB (0, 0, 255));
CBrush* pOldBrush=pDC->SelectObject (&brush);
//draw ellipse in tracking rectangle.
Crect rcEllipse;
pDoc->m_tracker.GetTrueRect (rcEllipse);
pDC->Ellipse (rcEllipse);
//Draw tracking rectangle.
pDoc->m_tracker.Draw (pDC);
//Select blue brush out of device context.
pDC->Selectobject (pOldBrush);
}
最后,使用ClassWizard 处理WM_LBUTTONDOWN 消息,并增加下述代码。该段代码根据
鼠标击键情况可以拖放、移动或者重置椭圆的大小。
void CSampleView::OnLButtonDown (UINT nFlags, CPoint point)
{
//Get pointer to document.
CSampleDoc* pDoc=GetDocument ();
ASSERT_VALID (pDoc);
//If clicked on ellipse, drag or resize it. Otherwise create a
//rubber-band rectangle nd create a new ellipse.
BOOL bResult=pDoc->m_tracker.HitTest (point)!=
CRectTracker::hitNothing;
//Tracker rectangle changed so update views.
if (bResult)
{
pDoc->m_tracker.Track (this,point,TRue);
pDoc->SetModifiedFlag ();
pDoc->UpdateAllViews (NULL);
}
else
pDoc->m-tracker.TrackRubberBand (this,point,TRUE);
CView:: onLButtonDown (nFlags,point);
}
48. 如何更新翻转背景颜色的文本
调用CDC:: SetBkmode 并传送OPAQUE 用当前的背景颜色填充背景,或者调用
CDC::SetBkMode 并传送TRANSPAARENT 使背景保持不变,这两种方法都可以设置背景模
式。下例设置背景模式为TRANSPARENT,可以两次更新串,用花色带黑阴影更新文本。
黑色串在红色串之后,但由于设置了背景模式仍然可见。
void CSampleView:: OnDraw (CDC* pDC)
{
//Determint size of view.
CRect rcView;
GetClientRect (rcVieew);
//Create sample string to display.
CString str (_T ("Awesome Shadow Text..."));
//Set the background mode to transparent.
pDC->SetBKMode (TRANSPARENT);
//Draw black shadow text.
rcView.OffsetRect (1, 1);
pDc->SetTextColor (RGB (0, 0, 0));
pDC->DrawText (str, str.GetLength (), rcView,
DT_SINGLELINE | DT_CENTER | DT_VCENTER);
//Draw red text.
rcView.OffsetRect (-1,-1);
pDc->SetTextColor (RGB (255, 0, 0));
pDC->DrawText (str, str.GetLength (), rcView,
DT_SINGLELINE | DT_CENTER | DT_VCENTER);
}
49. 如何创建一个具有特定点大小的字体
可以指定字体逻辑单位的大小,但有时指定字体的点的大小可能会更方便一些。可以如
下将字体的点转换为字体的高度:
int nHeigth=mulDiv (nPointSize, -dc.GetDeviceCaps (LOGPIXELSY), 72);
下例创建了一个8点的Apial字体:
CClientDC dc (AqfxGetMainWnd ());
m_font. CreateFont (MulDiv (8, -dc.GetDeviceCaps (LOGPIXELSY),
72),0,0,0,FW_NORMAL,0,0,0,ANSI_CHARSET,
OUT_STROKE_PRECIS,CLIP_STROKE_PRECIS,DRAFT_QUALITY,
VARIABLE_PITCH | FF-SWISS,_T ("Arial"));
50. 如何计算一个串的大小
函数CDC:: Det text Extent 根据当前选择的字体计算一个串的高度和宽度。如果使用
的不是系统字体而是其他字体,则在调用GetTextExtent之前将字体选进设备上下文中
是很重要的,否则计算高度和宽度时将依据系统字体,由此得出的结果当然是不正确的。
下述样板程序当改变下压按钮的标题时动态调整按钮的大小,按钮的大小由按钮的字体
和标题的大小而定。响应消息WM_SETTEXT 时调用OnSetText,该消息使用ON_MESSAE
宏指令定义的用户自定义消息。
LRESULT CMyButton:: OnSettext (WPARAM wParam, LPARAM lParam)
{
//Pass message to window procedure.
LRESULT bResult=CallWindowProc (*GetSuperWndProcAddr (),
m_hWnd, GetCurrentMessage () ->message,wParam,lParam);
//Get title of push button.
CString strTitle;
GetWindowText (strTitle);
//Select current font into device context.
CDC* pDC=GetDc ();
CFont*pFont=GetFont ();
CFont*pOldFont=pDC->SelectObject (pFont);
//Calculate size of title.
CSize size=pDC->GetTextExent (strTitle,strTitle.GetLength ());
//Adjust the button's size based on its title.
//Add a 5-pixel border around the button.
SetWindowPos (NULL, 0, 0, size.cx+10, size.cy+10,
SWP_NOMOVE | SWP_NOZORDER | SWP_NOACTIVATE);
//Clean up.
pDC->SelectFont (pOldFont);
ReleaseDC (pDC);
return bResult;
}
51. 如何显示旋转文本
只要用户使用TrueType或者GDI笔或字体就可以显示旋转文本(有些硬件设备也支持旋
转光栅字体)。LOGFONT 结构中的ifEscapement 成员指定了文本行和x 轴的角度,角度
的单位是十分之一度而不是度,例如,ifEscapement 为450 表示字体旋转45度。为确
保所有的字体沿坐标系统的同一方向旋转,一定要设置ifEscapement 成员的
CLIP_LH_ANGLES位,否则,有些字体可能反向旋转。下例使用了14点Arial字体每间
隔15度画一个串。
void CSampleView:: OnDraw (CDC* pDC)
{
//Determine the size of the window.
CRect rcClient;
GetClientRect (rcClient);
//Create sample string.
CString str (_T ("Wheeee...I am rotating!"));
//Draw transparent, red text.
pDC->SetBkMode (TRANSPARENT);
pDC->SetTextColor (RGB (255,0,0));
CFont font; //font object
LOGFONT stFont; //font definition
//Set font attributes that will not change.
memset (&stFont, 0, sizeof (LOGFONT));
stFont.ifheight=MulDiv (14, -pDC->GetDeviceCaps (LOGPIXELSY), 72);
stFont.ifWeight=FW_NORMAL;
stFont.ifClipPrecision=LCIP_LH_ANGLES;
strcpy (stFont.lfFaceName, "Arial");
//Draw text at 15degree intervals.
for (int nAngle=0; nAngle<3600; nAngle+=150)
{
//Specify new angle.
stFont.lfEscapement=nAngle;
//Create and select font into dc.
font.CreateFontIndirect (&stfont);
CFont* pOldFont=pDC->SelectObject (&font);
//Draw the text.
pDC->SelectObject (pOldFont);
font.DelectObjext ();
}
}
52. 如何正确显示包含标签字符的串
调用GDI文本绘画函数时需要展开标签字符,这可以通过调用CDC:: TabbedTextOut 或
者CDC:: DrawText并指定DT_EXPANDTABS标志来完成。TabbedTextOut函数允许指定标
签位的数组,下例指定每20设备单位展开一个标签:
void CSampleView:: OnDraw (CDC* pDC)
{
CTestDoc* pDoc=GetDocument ();
ASSERT_VALID (pDoC);
CString str;
str.Format (_T ("Cathy\tNorman\tOliver"));
int nTabStop=20; //tabs are every 20 pixels
pDC->TabbedtextOut (10, 10, str, 1, &nTabStop, 10);
}
53. 串太长时如何在其末尾显示一个省略号
调用CDC:: DrawText 并指定DT_END_ELLIPSIS 标志,这样就可以用小略号取代串末尾
的字符使其适合于指定的边界矩形。如果要显示路径信息,指定DT_END_ELLIPSIS标志
并省略号取代串中间的字符。
void CSampleView:: OnDraw (CDC* pDC)
{
CTestDoc* pDoc=GetDocument ();
ASSERT_VALID (pDoc);
//Add ellpsis to end of string if it does not fit
pDC->Drawtext (CString ("This is a long string"),
CRect (10, 10, 80, 30), DT_LEFT | DT_END_ELLIPSIS);
//Add ellpsis to middle of string if it does not fit
pDC->DrawText (AfxgetApp () ->m_pszhelpfilePath,
CRect (10, 40, 200, 60), DT_LEFT | DT_PATH_ELLIPSIS);
}
54. 如何快速地格式化一个CString 对象
调用CString:: Format,该函数和printf 函数具有相同的参数,下例说明了如何使用
Format函数:
//Get size of window.
CRect rcWindow;
GetWindowRect (rcWindow);
//Format message string.CString strMessage;
strMessage.Format (_T ("Window Size (%d, %d)"),
rcWindow.Width (), rcWindow.Height ());
//Display the message.
MessageBox (strmessage);*******************************************
上个帖子没有人回,只好重开帖子,终于发完了,但回头看看,37-38重复了好几遍,不好意思,Ctrl+V按多了,并且28-30缺失。补上。
******************************************
…
};
其次,在文档类的构造函数中初始化CRectTracker 对象:
CSampleDoc:: CSampleDOC ()
{
//Initialize tracker position, size and style.
m_tracker.m_rect.SetRect (0, 0, 10, 10);
m_tracker.m_nStyle=CRectTracker:: resizeInside |
CRectTracker:: dottedLine;
}
然后,在OnDraw函数中画椭圆和踪迹矩形:
void CSampleView:: OnDraw (CDC* pDC)
{
CSampleDoc* pDoc=GetDocument ();
ASSERT_VALID (pDoc);
//Select blue brush into device context.
CBrush brush (RGB (0, 0, 255));
CBrush* pOldBrush=pDC->SelectObject (&brush);
//draw ellipse in tracking rectangle.
Crect rcEllipse;
pDoc->m_tracker.GetTrueRect (rcEllipse);
pDC->Ellipse (rcEllipse);
//Draw tracking rectangle.
pDoc->m_tracker.Draw (pDC);
//Select blue brush out of device context.
pDC->Selectobject (pOldBrush);
}
最后,使用ClassWizard 处理WM_LBUTTONDOWN 消息,并增加下述代码。该段代码根据
鼠标击键情况可以拖放、移动或者重置椭圆的大小。
void CSampleView::OnLButtonDown (UINT nFlags, CPoint point)
{
//Get pointer to document.
CSampleDoc* pDoc=GetDocument ();
ASSERT_VALID (pDoc);
//If clicked on ellipse, drag or resize it. Otherwise create a
//rubber-band rectangle nd create a new ellipse.
BOOL bResult=pDoc->m_tracker.HitTest (point)!=
CRectTracker::hitNothing;
//Tracker rectangle changed so update views.
if (bResult)
{
pDoc->m_tracker.Track (this,point,TRue);
pDoc->SetModifiedFlag ();
pDoc->UpdateAllViews (NULL);
}
else
pDoc->m-tracker.TrackRubberBand (this,point,TRUE);
CView:: onLButtonDown (nFlags,point);
}
48. 如何更新翻转背景颜色的文本
调用CDC:: SetBkmode 并传送OPAQUE 用当前的背景颜色填充背景,或者调用
CDC::SetBkMode 并传送TRANSPAARENT 使背景保持不变,这两种方法都可以设置背景模
式。下例设置背景模式为TRANSPARENT,可以两次更新串,用花色带黑阴影更新文本。
黑色串在红色串之后,但由于设置了背景模式仍然可见。
void CSampleView:: OnDraw (CDC* pDC)
{
//Determint size of view.
CRect rcView;
GetClientRect (rcVieew);
//Create sample string to display.
CString str (_T ("Awesome Shadow Text..."));
//Set the background mode to transparent.
pDC->SetBKMode (TRANSPARENT);
//Draw black shadow text.
rcView.OffsetRect (1, 1);
pDc->SetTextColor (RGB (0, 0, 0));
pDC->DrawText (str, str.GetLength (), rcView,
DT_SINGLELINE | DT_CENTER | DT_VCENTER);
//Draw red text.
rcView.OffsetRect (-1,-1);
pDc->SetTextColor (RGB (255, 0, 0));
pDC->DrawText (str, str.GetLength (), rcView,
DT_SINGLELINE | DT_CENTER | DT_VCENTER);
}
49. 如何创建一个具有特定点大小的字体
可以指定字体逻辑单位的大小,但有时指定字体的点的大小可能会更方便一些。可以如
下将字体的点转换为字体的高度:
int nHeigth=mulDiv (nPointSize, -dc.GetDeviceCaps (LOGPIXELSY), 72);
下例创建了一个8点的Apial字体:
CClientDC dc (AqfxGetMainWnd ());
m_font. CreateFont (MulDiv (8, -dc.GetDeviceCaps (LOGPIXELSY),
72),0,0,0,FW_NORMAL,0,0,0,ANSI_CHARSET,
OUT_STROKE_PRECIS,CLIP_STROKE_PRECIS,DRAFT_QUALITY,
VARIABLE_PITCH | FF-SWISS,_T ("Arial"));
50. 如何计算一个串的大小
函数CDC:: Det text Extent 根据当前选择的字体计算一个串的高度和宽度。如果使用
的不是系统字体而是其他字体,则在调用GetTextExtent之前将字体选进设备上下文中
是很重要的,否则计算高度和宽度时将依据系统字体,由此得出的结果当然是不正确的。
下述样板程序当改变下压按钮的标题时动态调整按钮的大小,按钮的大小由按钮的字体
和标题的大小而定。响应消息WM_SETTEXT 时调用OnSetText,该消息使用ON_MESSAE
宏指令定义的用户自定义消息。
LRESULT CMyButton:: OnSettext (WPARAM wParam, LPARAM lParam)
{
//Pass message to window procedure.
LRESULT bResult=CallWindowProc (*GetSuperWndProcAddr (),
m_hWnd, GetCurrentMessage () ->message,wParam,lParam);
//Get title of push button.
CString strTitle;
GetWindowText (strTitle);
//Select current font into device context.
CDC* pDC=GetDc ();
CFont*pFont=GetFont ();
CFont*pOldFont=pDC->SelectObject (pFont);
//Calculate size of title.
CSize size=pDC->GetTextExent (strTitle,strTitle.GetLength ());
//Adjust the button's size based on its title.
//Add a 5-pixel border around the button.
SetWindowPos (NULL, 0, 0, size.cx+10, size.cy+10,
SWP_NOMOVE | SWP_NOZORDER | SWP_NOACTIVATE);
//Clean up.
pDC->SelectFont (pOldFont);
ReleaseDC (pDC);
return bResult;
}
51. 如何显示旋转文本
只要用户使用TrueType或者GDI笔或字体就可以显示旋转文本(有些硬件设备也支持旋
转光栅字体)。LOGFONT 结构中的ifEscapement 成员指定了文本行和x 轴的角度,角度
的单位是十分之一度而不是度,例如,ifEscapement 为450 表示字体旋转45度。为确
保所有的字体沿坐标系统的同一方向旋转,一定要设置ifEscapement 成员的
CLIP_LH_ANGLES位,否则,有些字体可能反向旋转。下例使用了14点Arial字体每间
隔15度画一个串。
void CSampleView:: OnDraw (CDC* pDC)
{
//Determine the size of the window.
CRect rcClient;
GetClientRect (rcClient);
//Create sample string.
CString str (_T ("Wheeee...I am rotating!"));
//Draw transparent, red text.
pDC->SetBkMode (TRANSPARENT);
pDC->SetTextColor (RGB (255,0,0));
CFont font; //font object
LOGFONT stFont; //font definition
//Set font attributes that will not change.
memset (&stFont, 0, sizeof (LOGFONT));
stFont.ifheight=MulDiv (14, -pDC->GetDeviceCaps (LOGPIXELSY), 72);
stFont.ifWeight=FW_NORMAL;
stFont.ifClipPrecision=LCIP_LH_ANGLES;
strcpy (stFont.lfFaceName, "Arial");
//Draw text at 15degree intervals.
for (int nAngle=0; nAngle<3600; nAngle+=150)
{
//Specify new angle.
stFont.lfEscapement=nAngle;
//Create and select font into dc.
font.CreateFontIndirect (&stfont);
CFont* pOldFont=pDC->SelectObject (&font);
//Draw the text.
pDC->SelectObject (pOldFont);
font.DelectObjext ();
}
}
52. 如何正确显示包含标签字符的串
调用GDI文本绘画函数时需要展开标签字符,这可以通过调用CDC:: TabbedTextOut 或
者CDC:: DrawText并指定DT_EXPANDTABS标志来完成。TabbedTextOut函数允许指定标
签位的数组,下例指定每20设备单位展开一个标签:
void CSampleView:: OnDraw (CDC* pDC)
{
CTestDoc* pDoc=GetDocument ();
ASSERT_VALID (pDoC);
CString str;
str.Format (_T ("Cathy\tNorman\tOliver"));
int nTabStop=20; //tabs are every 20 pixels
pDC->TabbedtextOut (10, 10, str, 1, &nTabStop, 10);
}
53. 串太长时如何在其末尾显示一个省略号
调用CDC:: DrawText 并指定DT_END_ELLIPSIS 标志,这样就可以用小略号取代串末尾
的字符使其适合于指定的边界矩形。如果要显示路径信息,指定DT_END_ELLIPSIS标志
并省略号取代串中间的字符。
void CSampleView:: OnDraw (CDC* pDC)
{
CTestDoc* pDoc=GetDocument ();
ASSERT_VALID (pDoc);
//Add ellpsis to end of string if it does not fit
pDC->Drawtext (CString ("This is a long string"),
CRect (10, 10, 80, 30), DT_LEFT | DT_END_ELLIPSIS);
//Add ellpsis to middle of string if it does not fit
pDC->DrawText (AfxgetApp () ->m_pszhelpfilePath,
CRect (10, 40, 200, 60), DT_LEFT | DT_PATH_ELLIPSIS);
}
54. 如何快速地格式化一个CString 对象
调用CString:: Format,该函数和printf 函数具有相同的参数,下例说明了如何使用
Format函数:
//Get size of window.
CRect rcWindow;
GetWindowRect (rcWindow);
//Format message string.CString strMessage;
strMessage.Format (_T ("Window Size (%d, %d)"),
rcWindow.Width (), rcWindow.Height ());
//Display the message.
MessageBox (strmessage);*******************************************
上个帖子没有人回,只好重开帖子,终于发完了,但回头看看,37-38重复了好几遍,不好意思,Ctrl+V按多了,并且28-30缺失。补上。
******************************************
工具条是一个窗口,所以可以在调用CWnd : : SetWindowText来设置标题,例子如下:
int CMainFrame : : OnCreate (LPCREATESTRUCT lpCreateStruct )
{
…
// Set the caption of the toolbar .
m_wndToolBar.SetWindowText (_T "Standdard");
30. 如何创建和使用无模式对话框
MFC 将模式和无模式对话封装在同一个类中,但是使用无模式对话需要几个对话需要几
个额处的步骤。首先,使用资源编辑器建立对话资源并使用ClassWizard 创建一个
CDialog的派生类。模式和无模式对话的中止是不一样的:
模式对话通过调用CDialog : : EndDialog 来中止,无模式对话则是调用CWnd: :
DestroyWindow 来中止的,函数CDialog : : OnOK 和CDialog : : OnCancel 调用
EndDialog ,所以需要调用DestroyWindow并重置无模式对话的函数。
void CSampleDialog : : OnOK ( )
{
// Retrieve and validate dialog data .
if (! UpdateData (TRUE) )
{
// the UpdateData rountine will set focus to correct item
TRACEO (" UpdateData failed during dialog termination .\n") ;
return ;
}
//Call DestroyWindow instead of EndDialog .
DestroyWindow ( ) ;
}
void CSampleDialog : : OnCancel ( )
{
//Call DestroyWindow instead of EndDialog .
DestroyWindow ( ) ;
}
其次,需要正确删除表示对话的C++对象。对于模式对来说,这很容易,需要创建函数
返回后即可删除C++对象;无模式对话不是同步的,创建函数调用后立即返回,因而用
户不知道何时删除C++对象。撤销窗口时工作框调用CWnd : : PostNcDestroy,可以重
置该函数并执行清除操作,诸如删除this指针。
*************
自我感觉补齐了,如果大家又发现什么问题,及时跟贴,我在改。建议版主把上一个帖子里重复的删除吧,再把这些补进去,合成一个帖子。CSDN不允许编辑自己的帖子,这一点很是不爽!!
*************
你怎么升的这么快!
//屏蔽掉系统键
SystemParametersInfo(SPI_SETSCREENSAVERRUNNING,true,&#38;bOld,
SPIF_UPDATEINIFILE);
● 启用系统热键
//恢复系统热键
SystemParametersInfo(SPI_SETSCREENSAVERRUNNING,false,&#38;bOld,
SPIF_UPDATEINIFILE);
::ShowWindow (::FindWindow("Shell_TrayWnd",NULL),SW_HIDE);
● 显示系统任务栏 //恢复WINDOWS系统任务栏正常显示
::ShowWindow (::FindWindow("Shell_TrayWnd",NULL),SW_SHOW);
//* 名称:FindTrayWnd
//* 作者:徐景周([email protected])
//* 功能:在显示窗体动画效果前,先寻找系统区位置
//********************************************************************************
BOOL CALLBACK FindTrayWnd(HWND hwnd, LPARAM lParam)
{
TCHAR szClassName[256];
GetClassName(hwnd, szClassName, 255);
// 比较窗口类名
if (_tcscmp(szClassName, _T("TrayNotifyWnd")) == 0)
{
CRect *pRect = (CRect*) lParam;
::GetWindowRect(hwnd, pRect);
return TRUE;
}
// 当找到时钟窗口时表示可以结束了
if (_tcscmp(szClassName, _T("TrayClockWClass")) == 0)
{
CRect *pRect = (CRect*) lParam;
CRect rectClock;
::GetWindowRect(hwnd, rectClock);
pRect->right = rectClock.left;
return FALSE;
}
return TRUE;
}
//********************************************************************************
//* 名称:WinAnimation
//* 作者:徐景周([email protected])
//* 功能:显示窗口动画效果的涵数
//********************************************************************************
void CScreenSnapDlg::WinAnimation(BOOL ShowFlag)
{
CRect rect(0,0,0,0);
// 查找托盘窗口
CWnd* pWnd = FindWindow("Shell_TrayWnd", NULL);
if (pWnd)
{
pWnd->GetWindowRect(rect);
EnumChildWindows(pWnd->m_hWnd, FindTrayWnd, (LPARAM)&#38;rect);
//rect 为托盘区矩形
CRect rcWnd;
GetWindowRect(rcWnd);
if(ShowFlag) //窗体滑向系统区
DrawAnimatedRects(GetSafeHwnd(),IDANI_CAPTION,rcWnd,rect);
else //窗体从系统区滑出
DrawAnimatedRects(GetSafeHwnd(),IDANI_CAPTION,rect,rcWnd);
}
}
用法如下: if(IsWindowVisible()) //窗体是否已隐藏
{
ShowWindow(SW_HIDE); //先隐藏窗体
WinAnimation(true); //窗体动画滑入到系统区中
}
else
{
WinAnimation(false); //窗体动画从系统区滑出
ShowWindow(SW_SHOW);
}
http://expert.csdn.net/Expert/topic/2849/2849355.xml?temp=.8005182
http://www.czvc.com/