转载地址: http://blog.csdn.net/gumanren/article/details/6129416
代码如下:
1 # -*- coding: utf-8 -*- 2 import sys 3 import win32ui 4 import win32gui 5 from PyQt4 import QtCore 6 from PyQt4 import QtGui 7 class testWindow(QtGui.QMainWindow): 8 def __init__(self): 9 super(testWindow, self).__init__() 10 self.setGeometry(180.0, 130.0, 280.0, 200.0) 11 self.setMouseTracking(True) 12 large, small = win32gui.ExtractIconEx(r"netbeans.exe", 0) 13 win32gui.DestroyIcon(small[0]) 14 self.pixmap = QtGui.QPixmap.fromWinHBITMAP(self.bitmapFromHIcon(large[0]), 2) 15 16 self.pixmap.save("a.ico","ico") 17 18 def bitmapFromHIcon(self, hIcon): 19 hdc = win32ui.CreateDCFromHandle(win32gui.GetDC(0)) 20 hbmp = win32ui.CreateBitmap() 21 hbmp.CreateCompatibleBitmap(hdc, 32, 32) 22 hdc = hdc.CreateCompatibleDC() 23 hdc.SelectObject(hbmp) 24 hdc.DrawIcon((0, 0), hIcon) 25 hdc.DeleteDC() 26 return hbmp.GetHandle() 27 # paintevent事件 28 def paintEvent(self, event): 29 # palette 调色板多个pix显示, 30 painter = QtGui.QPainter() 31 palette = QtGui.QPalette() 32 palette.setBrush(self.backgroundRole(), QtGui.QBrush(QtGui.QPixmap(self.pixmap))) # 设置背景图片 33 self.setPalette(palette) 34 #painter绘图类只显示一个 35 #painter.begin(self) 36 #painter.setRenderHint(QtGui.QPainter.Antialiasing) 37 #painter.setPen(QtCore.Qt.NoPen) 38 #painter.setBrush(QtGui.QBrush(QtGui.QColor(255.0, 255.0, 255.0, 255.0), QtCore.Qt.SolidPattern)) 39 #painter.drawRect(QtCore.QRect(0.0, 0.0, 280.0, 400.0)) 40 #painter.drawPixmap(QtCore.QRect(0.0, 0.0, 32.0, 32.0), self.pixmap) 41 #painter.end() 42 if __name__ == "__main__": 43 app = QtGui.QApplication(sys.argv) 44 mainWindow = testWindow() 45 mainWindow.show() 46 app.exec_()
显示效果: