Draw Image On DeskTop with GDI Plus (PureBasic)

 
PureBasic 编的桌面泡泡 从D5转化来的。都得用GDI+ ,不然不好转换颜色和实现完美透明图片。
By the way,PB编的CPU占用率明显高于D5编的,不知道什么原因。

 

 

  1 ; Draw Image on DeskTop with GDI plus  2 #ColorAdjustTypeDefault=0
  3 #ColorMatrixFlagsDefault=0
  4 Declare checkCollision(bub0, bub1)
  5 If OpenLibrary(0, "gdiplus.dll") = 0
  6   MessageRequester("Error","Required component gdiplus.dll is not found. Please install it and retry    ", #MB_ICONERROR)
  7   End
  8 EndIf
  9 Structure GdiplusStartupInput
 10   GdiPlusVersion.i
 11   *DebugEventCallback.DebugEventProc
 12   SuppressBackgroundThread.i
 13   SuppressExternalCodecs.i
 14 EndStructure
 15 Structure RectF
 16   x.f
 17   y.f
 18   width.f
 19   height.f
 20 EndStructure
 21 Structure ColorMatrix ;{ 
 22   M00.f 
 23   M01.f 
 24   M02.f 
 25   M03.f 
 26   M04.f 
 27   M10.f 
 28   M11.f 
 29   M12.f 
 30   M13.f 
 31   M14.f 
 32   M20.f 
 33   M21.f 
 34   M22.f 
 35   M23.f 
 36   M24.f 
 37   M30.f 
 38   M31.f 
 39   M32.f 
 40   M33.f 
 41   M34.f 
 42   M40.f 
 43   M41.f 
 44   M42.f 
 45   M43.f 
 46   M44.f 
 47 EndStructure ;}
 48 
 49 Import "gdiplus.lib"
 50   GdiplusStartup(token, *input.GdiplusStartupInput, output)
 51   GdiplusShutdown(token)
 52   GdipCreateFromHDC(hdc, *graphics)
 53   GdipLoadImageFromFile(filename.p-unicode, *image)
 54   GdipGetImageHeight(*image, *height.integer)
 55   GdipGetImageWidth(*image, *width.integer)
 56   GdipImageRotateFlip(*image, rotateFlipType)
 57   GdipGetImageBounds(*image, *srcRect.RectF, *srcUnit.integer)
 58   GdipDrawImage(*graphics, *image, x.f, y.f)
 59   GdipDrawImageI(*graphics, *image, x.l, y.l)
 60   GdipDrawImageRectI(*graphics, *image, x.l, y.l, width.l, height.l)
 61   GdipDrawImageRect(*graphics, *image, x.f, y.f, width.f, height.f)
 62   GdipGraphicsClear(*graphics, color)
 63   GdipDisposeImage(image)
 64   GdipDeleteGraphics(*graphics) 
 65   GdipCreateImageAttributes(*imageattr)
 66   GdipDisposeImageAttributes(*imageattr)
 67   GdipResetImageAttributes(*imageattr, type.l)
 68   GdipSetImageAttributesColorMatrix(*imageattr, type.l, enableFlag.l, *colorMatrix.ColorMatrix, *grayMatrix.ColorMatrix, flags.l)
 69   GdipDrawImageRectRect(*graphics, *image, dstx.f, dsty.f, dstwidth.f, dstheight.f, srcx.f, srcy.f, srcwidth.f, srcheight.f, srcUnit.l, *ImageAttributes, *callback, *callbackData)
 70 EndImport 
 71 ;UsePNGImageDecoder()
 72 ;LoadImage(1,"popobluesmall.png")
 73 #Count=10
 74 Global NColor.ColorMatrix
 75 Global D.w=180
 76 Global Dw.w,Dh.w
 77 Global Dim Bx.w(#Count),Dim By.w(#Count)
 78 Global Dim adx.w(#Count),Dim ady.w(#Count)
 79 Global Dim mcolor.f(#Count,3)
 80 Global HDcs
 81 
 82 With NColor
 83   \M00=1
 84   \M11=1
 85   \M22=1
 86   \M33=1
 87   \M44=1
 88 EndWith
 89 
 90 input.GdiplusStartupInput
 91 input\GdiPlusVersion = 1.1
 92 ExamineDesktops()
 93 Dw=DesktopWidth(0)
 94 Dh=DesktopHeight(0)
 95 
 96 GdiplusStartup(@token, @input, #Null)
 97 If GdipLoadImageFromFile("popobluesmall.png", @image) = 0
 98   dial=CreateImage(1,Dw,Dh,32,#PB_Image_Transparent)
 99   hdcs=StartDrawing(ImageOutput(1))
100   GdipCreateFromHDC(hdcs,@gfx)
101   GdipDrawImage(gfx,image,0,0)
102   StopDrawing()
103 Else
104   MessageRequester("Error","No Loaded Image ...",#PB_MessageRequester_Ok|#MB_ICONERROR)
105 EndIf
106 
107 Procedure BlitWindow(window, image)
108   hDC = StartDrawing(ImageOutput(image))
109   With sz.SIZE
110     \cx = ImageWidth(image)
111     \cy = ImageHeight(image)
112   EndWith
113   With BlendMode.BLENDFUNCTION
114     \SourceConstantAlpha = 255
115     \AlphaFormat = 1
116   EndWith     
117   UpdateLayeredWindow_(WindowID(window),0,0,@sz,hDC,@ContextOffset.POINT,0,@BlendMode,2)
118   StopDrawing()
119 EndProcedure
120 
121 OpenWindow(0,0,0,Dw,Dh,"Gdiplus Drawing",#PB_Window_ScreenCentered|#PB_Window_BorderLess|#DS_3DLOOK)
122 
123 SetWindowLongPtr_(WindowID(0), #GWL_EXSTYLE, #WS_EX_LAYERED)
124 SetWindowPos_(WindowID(0),#HWND_TOPMOST,#Null,#Null,#Null,#Null,#SWP_NOMOVE|#SWP_NOSIZE)
125 
126 For i=1 To #Count
127   adx(i)=6:ady(i)=6
128 Next i
129 
130 RandomSeed(Date())
131 For i=1 To #Count
132   Bx(i)=Random(dw,0)
133   By(i)=Random(dh,0)
134 Next i
135 
136 Repeat
137   EventID = WindowEvent()
138   Select EventID 
139     Case #WM_LBUTTONDOWN
140       SendMessage_(WindowID(0), #WM_NCLBUTTONDOWN, #HTCAPTION, 0)
141     Case #WM_KEYDOWN
142       If EventwParam()=27
143         End
144       EndIf
145   EndSelect
146   GdipCreateImageAttributes(@attr) 
147   GdipGraphicsClear(gfx,0)
148   
149   For i=1 To #Count
150     If Random(100)<3
151       mcolor(i,0)=Random(200,0)/100-1
152       mcolor(i,1)=Random(200,0)/100-1
153       mcolor(i,2)=Random(200,0)/100-1
154     EndIf
155     With ncolor
156       \M40=mcolor(i,0)
157       \M41=mcolor(i,1)
158       \M42=mcolor(i,2)
159     EndWith
160     
161     Bx(i)=Bx(i)+Adx(i) : By(i)=By(i)+Ady(i)
162     If Bx(i)>dw-D
163       Bx(i)=Dw-D
164       Adx(i)=-Adx(i)
165     EndIf
166     If Bx(i)<0
167       Bx(i)=0
168       Adx(i)=-Adx(i)
169     EndIf
170     If By(i)>dh-D
171       By(i)=Dh-D
172       Ady(i)=-Ady(i)
173     EndIf
174     If By(i)<0
175       By(i)=0
176       Ady(i)=-Ady(i)
177     EndIf
178     ;Debug bx(i)
179     For j=2 To #Count      
180       checkCollision(i,j)
181     Next j
182     If Adx(i)=0
183       Adx(i)=6
184     EndIf
185     If Ady(i)=0
186       Ady(i)=6
187     EndIf    
188     GdipSetImageAttributesColorMatrix(attr,0,#True,ncolor,#Null,0)
189     GdipDrawImageRectRect(gfx,image,Bx(i),By(i),D,D,0,0,D,D,2,attr,0,0)
190     
191   Next i
192   
193   BlitWindow(0,1) 
194   GdipDisposeImageAttributes(attr)
195   
196   ;Delay(500)
197 Until EventID = #PB_Event_CloseWindow
198 GdiplusShutdown(token)
199 
200 Procedure checkCollision(bub0, bub1)
201   Protected dx.i,dy.i,Dist.i,px1.i,px2.i,py1.i,py2.i,dx2.i,dy2.i,d2.i
202   dx=Abs(Bx(bub1)-Bx(bub0))
203   dy=Abs(By(bub1)-By(bub0))
204   dist=Sqr(dx*dx+dy*dy);
205   If dist = 0 
206     dist= 1
207   EndIf
208   D2=D-Dist
209   If d2>=0 
210     ;  p的速度的交换分
211     py1=(dy* dy) *Ady(bub1)/dist / dist +(dy * dx)*Adx(Bub1)/dist /dist
212     px1=(dx* dx) *Adx(Bub1)/dist / dist +(dx * dy)*Ady(Bub1)/dist /dist
213     
214     ; this的速度的交换分量
215     py2=(dy* dy) *Ady(Bub0)/dist /dist +(dy * dx)*Adx(Bub0)/dist /dist
216     px2=(dx* dx) *Adx(Bub0)/dist /dist +(dx * dy)*Ady(Bub0)/dist /dist
217     
218     Adx(Bub0)=Adx(Bub0)+px1-px2
219     Ady(Bub0)=Ady(Bub0)+py1-py2
220     
221     Adx(Bub1)=Adx(Bub1)-px1+px2
222     Ady(Bub1)=Ady(Bub1)-py1+py2
223   EndIf
224   If d2>0
225     ;移动第二个圆到延长线上去。
226     dx2=D2 * (Bx(Bub1) -Bx(Bub0)) / Dist
227     dy2=D2 * (By(Bub1) -By(Bub0)) / Dist
228     Bx(Bub1)=Bx(Bub1)+dx2
229     By(Bub1)=By(Bub1)+dy2
230     ;//this._pCenter.X -= (float)(dx2 / 2.0);
231     ;//this._pCenter.Y -= (float)(dy2 / 2.0);
232   EndIf    
233 EndProcedure

 

 

看到创建窗口那一句了吗?OpenWindow(0,0,0,Dw,Dh,"Gdiplus Drawing",......     对了,只要加上#DS_3DLOOK,任务栏就没有程序名称了,这点比Delphi简单多了。

posted @ 2022-11-25 15:54  PBlover  阅读(94)  评论(0编辑  收藏  举报