注册 登录  
 加关注
   显示下一条  |  关闭
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!立即重新绑定新浪微博》  |  关闭

沙漠里de烟雨

原创分享,禁止转载

 
 
 

日志

 
 

Qt---WIN32更改exe执行文件的图标  

2014-03-26 20:08:50|  分类: QT5.x与QML |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |

///---ChangeAppIcon.h
//如下:

#include <io.h>
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <windows.h>

#pragma pack(push, 2)
typedef struct {
  WORD Reserved1;       // reserved, must be 0
  WORD ResourceType;    // type is 1 for icons
  WORD ImageCount;      // number of icons in structure (1)
  BYTE Width;           // icon width (32)
  BYTE Height;          // icon height (32)
  BYTE Colors;          // colors (0 means more than 8 bits per pixel)
  BYTE Reserved2;       // reserved, must be 0
  WORD Planes;          // color planes
  WORD BitsPerPixel;    // bit depth
  DWORD ImageSize;      // size of structure
  WORD ResourceID;      // resource ID
} GROUPICON;
#pragma pack(pop)

std::string WcharToString(LPCWSTR wChar)
{
int len = WideCharToMultiByte(CP_ACP,0,wChar,-1,NULL,0,NULL,NULL);
char* arrPath = new char[len];
WideCharToMultiByte(CP_ACP,0,wChar,-1,arrPath,len,NULL,NULL);
std::string str(arrPath);
delete[] arrPath;
return str;
WCHAR* CharToWChar(const char* myChar)
{
int nLen = MultiByteToWideChar(CP_ACP,0,myChar,-1,NULL,0);
WCHAR *myWChar = new WCHAR[nLen];
MultiByteToWideChar(CP_ACP,0,myChar,-1,myWChar,nLen);
myWChar[nLen-1] = '\0';
return myWChar;
}
void ChangeAppIcon(LPCWSTR Where, LPCWSTR What)
{
   HANDLE hWhere = BeginUpdateResource(Where, FALSE);
 
   char *buffer;    // buffer to store raw icon data
   long buffersize; // length of buffer
   int hFile;       // file handle

   hFile = open(WcharToString(What).c_str(), O_RDONLY | O_BINARY);
   if (hFile == -1)
      return ; // if file doesn't exist, can't be opened etc.

   // calculate buffer length and load file into buffer
   buffersize = filelength(hFile);
   buffer = (char *)malloc(buffersize);
   read(hFile, buffer, buffersize);
   close(hFile);
 
   UpdateResource(
      hWhere,  // Handle to executable
      RT_ICON, // Resource type - icon
      MAKEINTRESOURCE(1), // Make the id 1
      MAKELANGID(LANG_ENGLISH,
                 SUBLANG_DEFAULT), // Default language
      (buffer+22),
      // skip the first 22 bytes because this is the
      // icon header&directory entry (if the file
      // contains multiple images the directory entries
      // will be larger than 22 bytes
      buffersize-22  // length of buffer
     );
 

   // Again, we use this structure for educational purposes.
   // The icon header and directory entries can be read from
   // the file.
   GROUPICON grData;
 
   // This is the header
   grData.Reserved1 = 0;     // reserved, must be 0
   grData.ResourceType = 1;  // type is 1 for icons
   grData.ImageCount = 1;    // number of icons in structure (1)

   // This is the directory entry
   grData.Width = 32;        // icon width (32)
   grData.Height = 32;       // icon height (32)
   grData.Colors = 0;        // colors (256)
   grData.Reserved2 = 0;     // reserved, must be 0
   grData.Planes = 2;        // color planes
   grData.BitsPerPixel = 32; // bit depth
   grData.ImageSize = buffersize - 22; // size of image
   grData.ResourceID = 1;       // resource ID is 1

   UpdateResource(
      hWhere,
      RT_GROUP_ICON,
      // RT_GROUP_ICON resources contain information
      // about stored icons
      L"MAINICON",
      // MAINICON contains information about the
      // application's displayed icon
      MAKELANGID(LANG_ENGLISH, SUBLANG_DEFAULT),
      &grData,
      // Pointer to this structure
      sizeof(GROUPICON)
     );
 
   delete buffer; // free memory

   // Perform the update, don't discard changes
   EndUpdateResource(hWhere, FALSE);
}

void ChangeAppIco(std::string appName,std::string appIco)
{
LPCWSTR appWstr = CharToWChar(appName.c_str());
LPCWSTR icoWstr = CharToWChar(appIco.c_str());
ChangeAppIcon(appWstr,icoWstr);
}

 
////-----updateappmainicon.h
///---如下:
#ifndef UPDATEAPPMAINICON_H
#define UPDATEAPPMAINICON_H

#include <QtWidgets/QWidget>

class QLineEdit;
class QPushButton;

class UpdateAppMainIcon : public QWidget
{
Q_OBJECT

public:
UpdateAppMainIcon(QWidget *parent = 0);
~UpdateAppMainIcon();

private slots:
void SetAppNameSlot();
void SetAppIcoSlot();
void UpdateAppIcoSlot();
private:
bool IsEnableUpdateBtn();
private:
QLineEdit* m_AppName;
QLineEdit* m_AppIcon;
QPushButton* m_UpdateIco;
};

#endif // UPDATEAPPMAINICON_H



///----updateappmainicon.cpp
///如下:

#include "updateappmainicon.h"
#include <QFileDialog>

#include <QLineEdit>
#include <QPushButton>
#include <QLayout>

#include "ChangeAppIcon.h"


UpdateAppMainIcon::UpdateAppMainIcon(QWidget *parent) : QWidget(parent)
{
m_AppName = new QLineEdit(this);
m_AppIcon = new QLineEdit(this);
QPushButton* appBtn = new QPushButton("App...",this);
QPushButton* icoBtn = new QPushButton("Ico...",this);
m_UpdateIco = new QPushButton("Update App's ICO",this);
m_UpdateIco->setFixedSize(120,50);

QHBoxLayout* hLayout1 = new QHBoxLayout;
QHBoxLayout* hLayout2 = new QHBoxLayout;
QHBoxLayout* hLayout3 = new QHBoxLayout;
QVBoxLayout* mainLayout = new QVBoxLayout;
hLayout1->addWidget(m_AppName);
hLayout1->addWidget(appBtn);
hLayout2->addWidget(m_AppIcon);
hLayout2->addWidget(icoBtn);
hLayout3->addStretch();
hLayout3->addWidget(m_UpdateIco);
hLayout3->addStretch();
mainLayout->addLayout(hLayout1);
mainLayout->addLayout(hLayout2);
mainLayout->addLayout(hLayout3);
this->setLayout(mainLayout);

m_AppName->setEnabled(false);
m_AppIcon->setEnabled(false);
m_UpdateIco->setEnabled(false);

this->setFixedSize(300,150);

connect(appBtn,SIGNAL(clicked()),this,SLOT(SetAppNameSlot()));
connect(icoBtn,SIGNAL(clicked()),this,SLOT(SetAppIcoSlot()));
connect(m_UpdateIco,SIGNAL(clicked()),this,SLOT(UpdateAppIcoSlot()));

}

UpdateAppMainIcon::~UpdateAppMainIcon()
{
}

void UpdateAppMainIcon::SetAppNameSlot()
{
QString appName = QFileDialog::getOpenFileName(this,"Select a App File",".","App (*.exe)");
m_AppName->setText(appName);
m_UpdateIco->setEnabled(IsEnableUpdateBtn());
}
void UpdateAppMainIcon::SetAppIcoSlot()
{
QString appIco = QFileDialog::getOpenFileName(this,"Select a Ico File",".","ICO (*.ico)");
m_AppIcon->setText(appIco);
m_UpdateIco->setEnabled(IsEnableUpdateBtn());
}
void UpdateAppMainIcon::UpdateAppIcoSlot()
{
QString appName = m_AppName->text();
QString appIco = m_AppIcon->text();
ChangeAppIco(appName.toStdString(),appIco.toStdString());
}

bool UpdateAppMainIcon::IsEnableUpdateBtn()
{
return !(m_AppName->text().isEmpty()) && !(m_AppIcon->text().isEmpty());
}

效果图:

  评论这张
 
阅读(604)| 评论(0)
推荐

历史上的今天

在LOFTER的更多文章

评论

<#--最新日志,群博日志--> <#--推荐日志--> <#--引用记录--> <#--博主推荐--> <#--随机阅读--> <#--首页推荐--> <#--历史上的今天--> <#--被推荐日志--> <#--上一篇,下一篇--> <#-- 热度 --> <#-- 网易新闻广告 --> <#--右边模块结构--> <#--评论模块结构--> <#--引用模块结构--> <#--博主发起的投票-->
 
 
 
 
 
 
 
 
 
 
 
 
 
 

页脚

网易公司版权所有 ©1997-2017