好文档 - 专业文书写作范文服务资料分享网站

C++学习笔记与开发技巧与典型列子

天下 分享 时间: 加入收藏 我要投稿 点赞

关于如何使用位图在2.4 在窗口中绘制设备相关位图,图标,设备无关位图中会详细讲解。还有一种特殊的GUI对象是多边形,利用多边形可以很好的限制作图区域或是改变窗口外型。关于如何创建和使用多边形在2.6 多边形和剪贴区域中会详细讲解。在Windows中使用GUI对象必须遵守一定的规则。首先需要创建一个合法的对象,不同的对象创建方法不同。然后需要将该GUI对象选入DC中,同时保存DC中原来的GUI对象。如果选入一个非法的对象将会引起异常。在使用完后应该恢复原来的对象,这一点特别重要,如果保存一个临时对象在DC中,而在临时对象被销毁后可能引起异常。有一点必须注意,每一个对象在重新创建前必须销毁,下面的代码演示了这一种安全的使用方法:

OnDraw(CDC* pDC) { CPen pen1,pen2; pen1.CreatePen(PS_SOLID,2,RGB(128,128,128));//创建对象 pen2.CreatePen(PS_SOLID,2,RGB(128,128,0));//创建对象 CPen* pPenOld=(CPen*)pDC->SelectObject(&pen1);//选择对象进DC drawWithPen1... (CPen*)pDC->SelectObject(&pen2);//选择对象进DC drawWithPen2... pen1.DeleteObject();//再次创建前先销毁 pen1.CreatePen(PS_SOLID,2,RGB(0,0,0));//再次创建对象 (CPen*)pDC->SelectObject(&pen1);//选择对象进DC drawWithPen1... pDC->SelectObject(pOldPen);//恢复 }

此外系统中还拥有一些库存GUI对象,你可以利用

CDC::SelectStockObject(SelectStockObject( int nIndex )选入这些对象,它们包括一些固定颜色的刷子,画笔和一些基本字体。

? ? ? ? ? ? ? ?

BLACK_BRUSH Black brush. DKGRAY_BRUSH Dark gray brush. GRAY_BRUSH Gray brush. HOLLOW_BRUSH Hollow brush. LTGRAY_BRUSH Light gray brush. NULL_BRUSH Null brush. WHITE_BRUSH White brush. BLACK_PEN Black pen.

? ? ? ? ? ? ?

NULL_PEN Null pen. WHITE_PEN White pen.

ANSI_FIXED_FONT ANSI fixed system font. ANSI_VAR_FONT ANSI variable system font. DEVICE_DEFAULT_FONT Device-dependent font. OEM_FIXED_FONT OEM-dependent fixed font.

SYSTEM_FONT The system font. By default, Windows uses the system font to draw menus, dialog-box controls, and other text. In Windows versions 3.0 and later, the system font is proportional width; earlier versions of Windows use a fixed-width system font.

? SYSTEM_FIXED_FONT The fixed-width system font used in Windows prior to version 3.0. This object is available for compatibility with earlier versions of Windows.

? DEFAULT_PALETTE Default color palette. This palette consists of the 20 static colors in the system palette.

这些对象留在DC中是安全的,所以你可以利用选入库存对象来作为恢复DC中GUI对象。 大家可能都注意到了绘图时都需要一个DC对象,DC(Device Context设备环境)对象是一个抽象的作图环境,可能是对应屏幕,也可能是对应打印机或其它。这个环境是设备无关的,所以你在对不同的设备输出时只需要使用不同的设备环境就行了,而作图方式可以完全不变。这也就是Windows耀眼的一点设备无关性。如同你将对一幅画使用照相机或复印机将会产生不同的输出,而不需要对画进行任何调整。DC的使用会穿插在本章中进行介绍。

c++继承经典例子 #include

class Base {

private:

int b_number; public:

Base( ){}

Base(int i) : b_number (i) { } int get_number( ) {return b_number;}

void print( ) {cout << b_number << endl;} };

class Derived : public Base {

private:

int d_number; public:

// constructor, initializer used to initialize the base part of a Derived object. Derived( int i, int j ) : Base(i), d_number(j) { };

// a new member function that overrides the print( ) function in Base void print( ) {

cout << get_number( ) << \ // access number through get_number( ) cout << d_number << endl; } };

int main( ) {

Base a(2);

Derived b(3, 4);

cout << \

a.print( ); // print( ) in Base cout << \

b.print( ); // print( ) in Derived cout << \

b.Base::print( ); // print( ) in Base

return 0; }

--------------------------------------------------------------------------------

没有虚析构函数,继承类没有析构

//Example: non- virtual destructors for dynamically allocated objects. #include #include

class Thing { public:

virtual void what_Am_I( ) {cout << \

~Thing(){cout<<\};

class Animal : public Thing { public:

virtual void what_Am_I( ) {cout << \~Animal(){cout<<\};

void main( ) {

Thing *t =new Thing; Animal*x = new Animal; Thing* array[2];

array[0] = t; // base pointer array[1] = x;

for (int i=0; i<2; i++) array->what_Am_I( ) ;

delete array[0]; delete array[1]; return ; }

--------------------------------------------------------------------------------

纯虚函数,多态 #include #include

class Point {

private:

double x; double y; public:

Point(double i, double j) : x(i), y(j) { } void print( ) const

{ cout << \};

class Figure

{

private:

Point center; public:

Figure (double i = 0, double j = 0) : center(i, j) { } Point& location( ) {

return center;

} // return an lvalue void move(Point p) {

center = p; draw( ); }

virtual void draw( ) = 0; // draw the figure virtual void rotate(double) = 0; // rotate the figure by an angle };

class Circle : public Figure {

private:

double radius; public:

Circle(double i = 0, double j = 0, double r = 0) : Figure(i, j), radius(r) { } void draw( ) {

cout << \ circle with center \ location( ).print( );

cout << \ }

void rotate(double) {

cout << \ } // must be defined };

class Square : public Figure

{

private:

double side; // length of the side

C++学习笔记与开发技巧与典型列子

关于如何使用位图在2.4在窗口中绘制设备相关位图,图标,设备无关位图中会详细讲解。还有一种特殊的GUI对象是多边形,利用多边形可以很好的限制作图区域或是改变窗口外型。关于如何创建和使用多边形在2.6多边形和剪贴区域中会详细讲解。在Windows中使用GUI对象必须遵守一定的规则。首先需要创建一个合法的对象,不同的对象创建方法不同。然后需要将该GUI对象选入DC中,同时保存DC中原来的GU
推荐度:
点击下载文档文档为doc格式
0u2es5eecp3x5if1kmzt
领取福利

微信扫码领取福利

微信扫码分享