博文
关于在OnTimer中连续弹出对话框的讨论(2009-02-05 22:46:00)
摘要:
CxxxDlg::OnTimer(UINT nIDEvent)
{
static int i = 0,j;
j = i++;
if (i==2) KillTimer(nIDEvent);
MessageBox("!");
i++;
CString str;
str.Format("%d,%d ",j,i);
::OutputDebugString(str);
}
以上代码在执行的时候,会弹出2个messagebox。很多人都有1个疑问,那就是当第一个消息框弹出的时候,应该相当于是模态对话框,怎么还会第二次进入OnTimer,并且再次弹出messagebox?
首先,让我们来了解一下计时器的原理。计时器可以响应事件,而用户界面也可以响应。但是程序却只有一个线程。CPU不得不分配一个时间片给应用程序专门处理定时器事件,应用程序不能在外面操作定时器。因为定时器处理是由CPU统一处理。
让我们再来看看常见模态对话框的domodal到底干了些什么。MSDN解释如下:
The function displays the dialog box , disables the owner window, and starts its own message loop to retrieve and dispatch ......
DrawText如何使多行文字居中(2009-02-03 17:24:00)
摘要:(1)DT_WORDBREAK
只能截断单词。例如如果输入一连串英文字符,那么它会当做一个单词来处理,而不会自动换行。而对于中文字符则可以。如果要对所有字符都可以像Edit控件中那样自动换行,那么可以使用DT_WORDBREAK | DT_EDITCONTROL
DT_EDITCONTROL
Duplicates the text-displaying characteristics of a multiline edit control. Specifically, the average character width is calculated in the same manner as for an edit control, and the function does not display a partially visible last line.
(2)DT_CALRECT的使用
对于一段text,要计算他的显示大小,那么可以使用DT_CALRECT标志。其中的rect参数属于IN/OUT类型。输出时,左上角坐标不变,右下角坐标改变。函数返回值是文本的高度。当然,它要与不同格式标志一起使用得到的结果是不一样的。例如,DT_CALRECT | DT_SINGLELINE 时,它只扩展传入rect的width,而在多行显示的时候,即DT_WORDBREAK | DT_WORDBREAK | DT_EDITCONTROL,仅仅扩展height,width不变。
DT_CALCRECT Determines the width and height of the rectangle. If there are multiple lines of text, DrawText will use the width of the rectangle pointed to by lpRect and extend the base of the rectangle to bound the last line of text. If there is only one line of text, DrawText will modify the right side of the r......
Custom Draw ListView Controls(7)(2009-01-20 12:00:00)
摘要:
// do we want to do any drawing after
// the list control is finished
virtual bool IsSubItemPostDraw() { return false; }
// if we are doing the drawing afterwards ourselves
// override and put the code in here
// the return value is not used here
virtual bool OnSubItemPostDraw(CDC* /*pDC*/,
int /*nItem*/,
int /*nSubItem*/,
UINT /*nState*/,
 ......
Custom Draw ListView Controls(6)(2009-01-20 11:53:00)
摘要:
// do we want to handle custom draw for
// individual sub items
virtual bool IsNotifySubItemDraw(int /*nItem*/,
UINT /*nState*/,
LPARAM /*lParam*/)
{ return false; }
// do we want to be notified when the
// painting has finished
virtual bool IsNotifyItemPostPaint(int /*nItem*/,
UINT /*nState*/,
 ......
Custom Draw ListView Controls(5)(2009-01-20 11:46:00)
摘要:
protected:
CFont* m_pOldItemFont;
CFont* m_pOldSubItemFont;
//
// Callbacks for whole control
//
// do we want to do the drawing ourselves?
virtual bool IsDraw() { return false; }
// if we are doing the drawing ourselves
// override and put the code in here
// and return TRUE if we did indeed do
// all the drawing ourselves
virtual bool OnDraw(CDC* /*pDC*/, const CRect& /*r*/)
{ return false; }
// do we want to handle custom draw for
// individual items
virtual bool IsNotifyItemDraw() { return false; }
// do we want to be notified when the
// painting has finished
virtual bool IsNotifyPostPaint() { return false; }
// do we want to do any drawing after
// the list control is finished
virtual bool IsPostDraw() { return false; }
// if we are doing the drawing afterwards ourselves
// override and put the code in here
......
Custom Draw ListView Controls(4)(2009-01-20 11:43:00)
摘要:
case CDDS_ITEMPOSTPAINT:
{
// Item PostPaint
// restore old font if any
if (m_pOldItemFont) {
if (! pDC) pDC = CDC::FromHandle(hdc);
pDC->SelectObject(m_pOldItemFont);
m_pOldItemFont = NULL;
}
// do we want to do any extra drawing?
if (IsItemPostDraw()) {
if (! pDC) pDC = CDC::FromHandle(hdc);
OnItemPostDraw(pDC,nItem,nState,lParam);
}
}
break;
case CDDS_POSTPAINT:
{
// Item PostPaint
// do we want to do any extra drawing?
if (IsPostDraw()) {
if (! pDC) pDC = CDC::FromHandle(hdc);
CRect r(pNMLVCUSTOMDRAW->nmcd.rc);
OnPostDraw(pDC,r);
}
}
break;
}
AS......
Custom Draw ListView Controls(3)(2009-01-20 11:40:00)
摘要:
case CDDS_ITEMPREPAINT|CDDS_SUBITEM:
{
// Sub Item PrePaint
// set sub item number (data will be valid now)
int nSubItem = pNMLVCUSTOMDRAW->iSubItem;
m_pOldSubItemFont = NULL;
bNotifyPostPaint =
IsNotifySubItemPostPaint(nItem, nSubItem, nState, lParam);
// set up the colors to use
pNMLVCUSTOMDRAW->clrText =
TextColorForSubItem(nItem,nSubItem,nState,lParam);
pNMLVCUSTOMDRAW->clrTextBk =
BkColorForSubItem(nItem,nSubItem,nState,lParam);
// set up a different font to use, if any
CFont* pNewFont =
FontForSubItem(nItem, nSubItem, nState, lParam);
if (pNewFont) {
if (! pDC) pDC = CDC::FromHandle(hdc);
m_pOldSubItemFont = pDC->SelectObject(pNew......
Custom Draw ListView Controls(2)(2009-01-20 11:38:00)
摘要:
The CListCtrlWithCustomDraw Class
In the OnCustomdraw for CListCtrlWithCustomDraw, I provide a generic handler for custom draw notifications. The bulk of the code is a switch statement on the draw stage we are up to (refer to my previous article). At each of the pre-paint stages, I call virtual functions to get color information, fonts required, and to take over (or augment) the drawing process. I also allow for extra drawing during the post-paint stages.
Let's look at the fleshed out OnCustomdraw function for CListCtrlWithCustomDraw:
void CListCtrlWithCustomDraw::OnCustomdraw(NMHDR* pNMHDR,LRESULT* pResult)
{
// first, lets extract data from
// the message for ease of use later
NMLVCUSTOMDRAW* pNMLVCUSTOMDRAW = (NMLVCUSTOMDRAW*)pNMHDR;
// we'll copy the device context into hdc
// but won't convert it to a pDC* until (and if)
// we need it as this requires ......
Custom Draw ListView Controls(1)(2009-01-20 11:35:00)
摘要:
Custom Draw ListView Controls
The first is the most extreme. You handle the WM_PAINT messages and do all the painting yourself. You get no help at all from Windows with this method. You have to create a device context, determine where and how big your control is, what state it is in, and then draw it all yourself. Most programmers tend to shy away from this.
Owner-draw (or self-draw) controls are a little easier. Here, Windows sets up the device context for you. It also fills in a structure that gives you the rectangle that the control occupies, the state the control is in, and flags to say how much drawing you need to do. You still need to do all the drawing yourself, but with all this information handed to you, it is not such a chore. In particular, for controls like list boxes and ListView controls, Windows will go line by line through the control and ask you to draw each individual item; you don't need to know how to draw the entire control. To make things easy for owner-dra......
如何重载CListview中的CListctrl 讨论(2009-01-20 11:27:00)
摘要:经常有一个已经重载的CListCtrl,然后想把它用到CListView中,但是问题多多。
首先,利用Subclasswindow是绝对不行的,原因不详。
其次,这种方法非常复杂, 在listview中定义一个listctrl成员变量,然后在listview中响应wm_create消息,在oncreate中把listctrl创建出来,再响应wm_size消息,在onsize中将listctrl movewindow填充listview就可以了,一点都不方便。
再次,不用CListView,而是用CFormView。这时可以用Subclasswindow子类化
最后,如果CListCtrl使用了DrawItem的自绘方法,那么只能使用CListView的CustomDraw,把CListCtrl的绘制代码copy过来。
如果有更好的方法,请联系我:
qq: 281417474
......