游客已登陆 (0)未知
笔行证 257310
昵称 kuan 
笔贝 Score1
加为好友 发送短信
<< << 2008 八月 >> >>
12
3456789
10111213141516
17181920212223
24252627282930
31

访问计数:19052
本文:258 今天:2 本月 258

本地音乐播放器



 
       在MFC AppWizard中确定窗口样式
2007-11-19 晴

出处或者来源:MFC Windows




在函数PreCreateWindow中修改窗口的样式

它是框架窗口类中的一个成员函数,它在创建窗口前一刻被用。原型为:
virtual BOOL PreCreatWindow(CREATESTRUCT & cs);


typedef struct tagCREATESTRUCT { // cs
LPVOID lpCreateParams;
HINSTANCE hInstance;
HMENU hMenu;
HWND hwndParent;
int cy; //窗口的高度
int cx; //窗口的宽度
int y; //窗口左上角的y值
int x; //窗口左上角的x值
LONG style; //窗口的样式
LPCTSTR lpszName;
LPCTSTR lpszClass;
DWORD dwExStyle;
} CREATESTRUCT;


style的可取值

&#8226; WS_BORDER Creates a window that has a border.
创建带有边界线的窗口
&#8226; WS_CAPTION Creates a window that has a title bar (implies the WS_BORDER style). Cannot be used with the WS_DLGFRAME style.
创建带有标题栏的窗口
&#8226; WS_CHILD Creates a child window. Cannot be used with the WS_POPUP style.
创建一个子窗口
&#8226; WS_CLIPCHILDREN Excludes the area occupied by child windows when you draw within the parent window. Used when you create the parent window.
当在父窗口上绘图时,禁止程序在子窗口上绘图。
&#8226; WS_CLIPSIBLINGS Clips child windows relative to each other; that is, when a particular child window receives a paint message, the WS_CLIPSIBLINGS style clips all other overlapped child windows out of the region of the child window to be updated. (If WS_CLIPSIBLINGS is not given and child windows overlap, when you draw within the client area of a child window, it is possible to draw within the client area of a neighboring child window.) For use with the WS_CHILD style only.
当在其他子窗口上绘图时,禁止程序在子窗口上绘图。
&#8226; WS_DISABLED Creates a window that is initially disabled.
创建一个没有功能的窗口。
&#8226; WS_DLGFRAME Creates a window with a double border but no title.
创建一个带有双线边界而没有标题栏的窗口。
&#8226; WS_GROUP Specifies the first control of a group of controls in which the user can move from one control to the next with the arrow keys. All controls defined with the WS_GROUP style FALSE after the first control belong to the same group. The next control with the WS_GROUP style starts the next group (that is, one group ends where the next begins).
指定控制组中的第一个控制
&#8226; WS_HSCROLL Creates a window that has a horizontal scroll bar.
创建带有水平流动条的窗口。
&#8226; WS_MAXIMIZE Creates a window of maximum size.
创建最大化的窗口。
&#8226; WS_MAXIMIZEBOX Creates a window that has a Maximize button.
创建带有最大化按钮的窗口。
&#8226; WS_MINIMIZE Creates a window that is initially minimized. For use with the WS_OVERLAPPED style only.
创建一个带有最小化的窗口。
&#8226; WS_MINIMIZEBOX Creates a window that has a Minimize button.
创建一个带有最小化按钮的窗口。
&#8226; WS_OVERLAPPED Creates an overlapped window. An overlapped window usually has a caption and a border.
创建带有标题栏和边界线的窗口。
&#8226; WS_OVERLAPPEDWINDOW Creates an overlapped window with the WS_OVERLAPPED, WS_CAPTION, WS_SYSMENU, WS_THICKFRAME, WS_MINIMIZEBOX, and WS_MAXIMIZEBOX styles.
组合了WS_OVERLAPPED, WS_CAPTION, WS_SYSMENU, WS_THICKFRAME, WS_MINIMIZEBOX, 和 WS_MAXIMIZEBOX。
&#8226; WS_POPUP Creates a pop-up window. Cannot be used with the WS_CHILD style.
创建一个弹出式窗口。
&#8226; WS_POPUPWINDOW Creates a pop-up window with the WS_BORDER, WS_POPUP, and WS_SYSMENU styles. The WS_CAPTION style must be combined with the WS_POPUPWINDOW style to make the Control menu visible.
组合了WS_BORDER, WS_POPUP,和 WS_SYSMENU
&#8226; WS_SYSMENU Creates a window that has a Control-menu box in its title bar. Used only for windows with title bars.
创建带有控制菜单的窗口。
&#8226; WS_TABSTOP Specifies one of any number of controls through which the user can move by using the TAB key. The TAB key moves the user to the next control specified by the WS_TABSTOP style.
指定可通知制表键选择的控件。
&#8226; WS_THICKFRAME Creates a window with a thick frame that can be used to size the window.
创建带有可伸缩框架的窗口。
&#8226; WS_VISIBLE Creates a window that is initially visible.
创建可见的窗口
&#8226; WS_VSCROLL Creates a window that has a vertical scroll bar.
创建带有垂直滑动条的窗口。


拆分窗口的同步更新

在文档类中的成员函数UpdateAllViews.它是通知该文档除了pSender之外的所有全部窗口(视图)进行更新.它的作用是调用拆分窗口的OnDraw函数,从而对视图进行重绘.它的原型为:
void UpdateAllviews(CView * pSender,
LPARAM lHit=0L,
CObject * pHint=NULL);



例如.
在文档类定义数组.
#include<afxtempl.h>

public:
CArray<CRect,CRect&> m_Rectag;
然后在其它构造函数,初始化.


void CMy6_2View::OnLButtonDown(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
CMy6_2Doc * pDoc=GetDocument();
int r=rand()%50+5;
CRect Ret(point.x-r, point.y-r, point.x+r, point.y+r);
pDoc->m_Rectag.Add(Ret);
pDoc->UpdateAllViews(NULL);//把NULL改为this则是不重绘画图本身的窗口.

CView::OnLButtonDown(nFlags, point);
}



void CMy6_2View::OnDraw(CDC* pDC)
{
CMy6_2Doc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
// TODO: add draw code for native data here
for(int i=0; i<pDoc->m_Rectag.GetSize(); i++)
{
pDC->Rectangle(pDoc->m_Rectag);
}

}

视图类成员函数InvalidateRect

视图类成员函数InvalidateRect可以用来触发OnDraw来对显示屏幕进行重绘.原型为:
InvalidateRect(LPCRECT lpRect, BOOL bErase=TRUE)
其中的第一个参数lpRect就是用来指定无效区域的.(需要重绘的区域叫无效显示区域)

如前面说的例子5_2中就有这个用法:
void CMy5_2View::OnLButtonDown(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
int r=rand()%50+50;
CRect Ret(point.x-r, point.y-r, point.x+r, point.y+r);
m_Rectag.Add(Ret);
InvalidateRect(Ret,FALSE); //触发OnDraw()函数
CView::OnLButtonDown(nFlags, point);
}




把6_2改一下.
void CMy6_2_2View::OnLButtonDown(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
CMy6_2_2Doc * pDoc=GetDocument();
int r=rand()%50+5;
CRect Ret(point.x-r, point.y-r, point.x+r, point.y+r);
pDoc->m_Rectag.Add(Ret);
InvalidateRect(Ret,FALSE);//更新本视图的无效区域
pDoc->UpdateAllViews(this);//更新其余视图 不更新画图本身的窗口.

CView::OnLButtonDown(nFlags, point);
}
注意:这样做.只有画图本身的窗口才是只更新无效区域.对对其它的窗口还是更新整个窗口的.当然可以,把无效区域传给其它窗口的InvalidateRect.只不过,是比较复杂.
文档类的成员函数UpdateAllViews之所以能够通知与文档对象对应的所有有进行显示更新. 是因为在UpdateAllViews函数中又调用了各个的成员函数OnUpdate(CView * pSender, LPARAM lHint ,CObject * pHint);
它的第三个参数是CObject类的对象.UpdateAllViews函数的原形就会发现,它的第三个参数也是一个CObject类的对象,这就我们就可以用它来传递无效区域.我们可以改写视图类的虚函数OnUpdate. ..在OnUpdate函数中再调用视图类的成员函数InvalidateRect 这样就可以把无效区域的信息通过CObject派生类对象集资传递到函数InvalidateRect.

这样我们就可以设计一个以CObject为基类.在该类中封闭无效区域的相关数据及算法,然后创建这个类的对象,并以这个对象作为实参调用函数UpdateAllViews,然后在函数OnUpdate中计算出无效区域的矩形后,再调用InvalidateRect函数来触发OnDraw对无效区域进行重绘,这样就可以,大提高重绘效率.

例如,创建一个可拆分窗口的SDI应用程序.
1在程序的头文件StdAfx.h中包含头文件.(在文档类也行)
2由CObject派生一个描述重绘区的类:
class CDrawRect : public CObject
{
public:
CRect m_DrawRect;
};
3
class CMy6_3Doc : public CDocument
{
public:
CArray<CRect,CRect&> m_Rectag;
}

CMy6_3Doc::CMy6_3Doc()
{
// TODO: add one-time construction code here
m_Rectag.SetSize(256,256);

}

class CMy6_3View : public CView
{
public:
CDrawRect * m_ViewDrRect;
virtual void OnUpdate(CView * pSender, LPARAM lHint, CObject * pHint);//注意声明这个东东
}
在视图类包含头文件
#include “DrawRect.h”

CMy6_3View::CMy6_3View()
{
// TODO: add construction code here
m_ViewDrRect=new CDrawRect;

}

void CMy6_3View::OnLButtonDown(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
CMy6_3Doc *pDoc=GetDocument();
int r=rand()%50+5;
CRect Ret(point.x-r, point.y-r, point.x+r, point.y+r);
pDoc->m_Rectag.Add(Ret);
m_ViewDrRect->m_DrawRect=Ret;
InvalidateRect(Ret,FALSE);
pDoc->UpdateAllViews(this, 0L, m_ViewDrRect);
CView::OnLButtonDown(nFlags, point);
}


4改写视图类的虚函数OnUpdate并使用重绘区对象指定重绘区域:
void CMy6_3View::OnUpdate(CView * pSender, LPARAM lHint, CObject * pHint)
{
CDrawRect *pDrawRect=(CDrawRect *)pHint;////////////////////////kuan
InvalidateRect(pDrawRect->m_DrawRect,FALSE);
}
5
void CMy6_3View::OnDraw(CDC* pDC)
{
CMy6_3Doc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
// TODO: add draw code for native data here
for(int i=0; i<pDoc->m_Rectag.GetSize(); i++)
pDC->Ellipse(pDoc->m_Rectag);
}
.
# posted by kuan @ 2007-11-19 13:44:40 评论(0)
 








 
笔 名:
*
评 论:
最多1000字。当前字数:0
*
联系方式: