📄 desktop-desktop-cpp.html
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"><html><head><meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"><title>Qt Toolkit - desktop/desktop.cpp example file</title><style type="text/css"><!--h3.fn,span.fn { margin-left: 1cm; text-indent: -1cm; }a:link { color: #004faf; text-decoration: none }a:visited { color: #672967; text-decoration: none }body { background: white; color: black; }--></style></head><body bgcolor="#ffffff"><table width="100%"><tr><td><a href="index.html"><img width="100" height="100" src="qtlogo.png"alt="Home" border="0"><img width="100"height="100" src="face.png" alt="Home" border="0"></a><td valign="top"><div align="right"><img src="dochead.png" width="472" height="27"><br><a href="classes.html"><b>Classes</b></a>- <a href="annotated.html">Annotated</a>- <a href="hierarchy.html">Tree</a>- <a href="functions.html">Functions</a>- <a href="index.html">Home</a>- <a href="topicals.html"><b>Structure</b> <font face="Arial,Helvetica,Geneva,Swiss,SunSans-Regular" align="center" size=32>Qte</font></a></div></table><h1 align=center>Painting on the Desktop</h1><br clear="all"> The desktop demo contains three routines, each of which draws something on the desktop. It does some nice stuff with QPainter, and also demonstrates how one can treat the desktop as a widget like any other. <hr> Implementation:<pre>/****************************************************************************** $Id: qt/examples/desktop/desktop.cpp 2.3.8 edited 2004-05-12 $**** Copyright (C) 1992-2000 Trolltech AS. All rights reserved.**** This file is part of an example program for Qt. This example** program may be used, distributed and modified without limitation.*******************************************************************************/#include <<a name="qimage.h"></a><a href="qimage-h.html">qimage.h</a>>#include <<a name="qbitmap.h"></a><a href="qbitmap-h.html">qbitmap.h</a>>#include <<a name="qpainter.h"></a><a href="qpainter-h.html">qpainter.h</a>>#include <<a name="qapplication.h"></a><a href="qapplication-h.html">qapplication.h</a>>#include <<a name="qdropsite.h"></a><a href="qdropsite-h.html">qdropsite.h</a>>#include <<a name="qdragobject.h"></a><a href="qdragobject-h.html">qdragobject.h</a>>#include <stdio.h>static double seed = 0.353535353535;static const int KINDA_RAND_MAX = 32767;static int kindaRand(){ seed = seed*147; seed = seed - (double) ((int) seed); return (int) ( seed*(KINDA_RAND_MAX + 1) );}static int velocity( int i ) // change velocity{ const int velmax = 15; const int velmin = 4; if ( i == 1 || i == 2 ) i = (kindaRand()&0x7fff % velmax)/3 + velmin; else i = (kindaRand()&0x7fff % velmax) + velmin; return i;}//// Draw polygon on desktop.//void poly(){ <a name="QWidget"></a><a href="qwidget.html">QWidget</a> *d = QApplication::desktop(); d-><a name="setBackgroundColor"></a><a href="qwidget.html#c09181">setBackgroundColor</a>( Qt::white ); // white desktop const int maxpoints = 5; const int maxcurves = 8; static int xvel[maxpoints]; static int yvel[maxpoints]; int head = 0; int tail = -maxcurves + 2; <a name="QPointArray"></a><a href="qpointarray.html">QPointArray</a> *a = new <a href="qpointarray.html">QPointArray</a>[ maxcurves ]; register QPointArray *p; <a name="QRect"></a><a href="qrect.html">QRect</a> r = d-><a name="rect"></a><a href="qwidget.html#75ae71">rect</a>(); // desktop rectangle int i; for ( i=0; i<maxcurves; i++ ) a[i].resize( maxpoints ); p = &a[0]; for ( i=0; i<maxpoints; i++ ) { // setup first polygon points p->setPoint( i, (kindaRand()&0x7fff) % r.<a name="width"></a><a href="qrect.html#45fe95">width</a>(), (kindaRand()&0x7fff) % r.<a name="height"></a><a href="qrect.html#581ab8">height</a>() ); xvel[i] = velocity(i); yvel[i] = velocity(i); } <a name="QPainter"></a><a href="qpainter.html">QPainter</a> paint; paint.<a name="begin"></a><a href="qpainter.html#02ed5d">begin</a>( d ); // start painting desktop for ( int ntimes=0; ntimes<2000; ntimes++ ) { paint.<a name="setBrush"></a><a href="qpainter.html#3e0cc8">setBrush</a>( <a name="QColor"></a><a href="qcolor.html">QColor</a>(kindaRand()%360, 180, 255, QColor::Hsv) ); paint.<a name="drawPolygon"></a><a href="qpainter.html#2efe17">drawPolygon</a>( a[head] ); if ( ++tail >= maxcurves ) tail = 0; int minx=r.<a name="left"></a><a href="qrect.html#369cab">left</a>(), maxx=r.<a name="right"></a><a href="qrect.html#896e32">right</a>(); int miny=r.<a name="top"></a><a href="qrect.html#4dd27e">top</a>(), maxy=r.<a name="bottom"></a><a href="qrect.html#2de4b0">bottom</a>(); int x, y; p = &a[head]; if ( ++head >= maxcurves ) head = 0; for ( i=0; i<maxpoints; i++ ) { // calc new curve p->point( i, &x, &y ); x += xvel[i]; y += yvel[i]; if ( x >= maxx ) { x = maxx - (x - maxx + 1); xvel[i] = -velocity(i); } if ( x <= minx ) { x = minx + (minx - x + 1); xvel[i] = velocity(i); } if ( y >= maxy ) { y = maxy - (y - maxy + 1); yvel[i] = -velocity(i); } if ( y <= miny ) { y = miny + (miny - y + 1); yvel[i] = velocity(i); } a[head].setPoint( i, x, y ); } } paint.<a name="end"></a><a href="qpainter.html#365784">end</a>(); // painting done delete[] a;}//// Rotate pattern on desktop.//void rotate(){ int i; const int w = 64; const int h = 64; <a name="QImage"></a><a href="qimage.html">QImage</a> image( w, h, 8, 128 ); // create image for ( i=0; i<128; i++ ) // build color table image.<a name="setColor"></a><a href="qimage.html#271983">setColor</a>( i, <a name="qRgb"></a><a href="qcolor.html#dab147">qRgb</a>(i,0,0) ); for ( int y=0; y<h; y++ ) { // set image pixels uchar *p = image.<a name="scanLine"></a><a href="qimage.html#7632a9">scanLine</a>(y); for ( int x=0; x<w; x++ ) *p++ = (x+y)%128; } <a name="QPixmap"></a><a href="qpixmap.html">QPixmap</a> pm; pm = image; // convert image to pixmap pm.<a name="setOptimization"></a><a href="qpixmap.html#e6ae80">setOptimization</a>( QPixmap::BestOptim ); // rotation will be faster <a href="qwidget.html">QWidget</a> *d = QApplication::desktop(); // w = desktop widget for ( i=0; i<=360; i += 2 ) { <a name="QWMatrix"></a><a href="qwmatrix.html">QWMatrix</a> m; m.<a name="rotate"></a><a href="qwmatrix.html#71020d">rotate</a>( i ); // rotate coordinate system <a href="qpixmap.html">QPixmap</a> rpm = pm.<a name="xForm"></a><a href="qpixmap.html#ff5fcf">xForm</a>( m ); // rpm = rotated pixmap d-><a name="setBackgroundPixmap"></a><a href="qwidget.html#7e417f">setBackgroundPixmap</a>( rpm ); // set desktop pixmap d-><a name="update"></a><a href="qwidget.html#a66a88">update</a>(); // repaint desktop }}//// Generates a marble-like pattern in pm.//void generateStone( <a href="qpixmap.html">QPixmap</a> *pm, const QColor &c1, const QColor &c2, const QColor &c3 ){ <a href="qpainter.html">QPainter</a> p; <a name="QPen"></a><a href="qpen.html">QPen</a> p1 ( c1, 0 ); <a href="qpen.html">QPen</a> p2 ( c2, 0 ); <a href="qpen.html">QPen</a> p3 ( c3, 0 ); p.<a href="qpainter.html#02ed5d">begin</a>( pm );
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -