A class for dynamic icons in Windows

#include <windows.h>

class DynamicIcon {
public:
  DynamicIcon();
 ~DynamicIcon();

HICON Icon();

private:

HDC      memDC1_;
  HDC      memDC2_;
 
  HBITMAP  oldBmp_1;
  HBITMAP  oldBmp_2;
  HBITMAP  iconBmp_;
  HBITMAP  iconMaskBmp_;

HBRUSH   hCircleBrush;
  HBRUSH   hTranspBrush;
  HBRUSH   hOldBrush;

HRGN     circle;

HICON    icon_;

static int const iconWidth_;
  static int const iconHeight_;
};

int const DynamicIcon::iconWidth_  = 16;
int const DynamicIcon::iconHeight_ = 16;

DynamicIcon::DynamicIcon() {
  HDC hDC = GetDC (0);

memDC1_  = CreateCompatibleDC     (hDC);
  memDC2_  = CreateCompatibleDC     (hDC);
  iconBmp_ = CreateCompatibleBitmap (hDC, iconWidth_, iconHeight_);

iconMaskBmp_ = CreateCompatibleBitmap (hDC, iconWidth_, iconHeight_);
  oldBmp_1    = (HBITMAP) SelectObject (memDC1_, (HBITMAP) iconBmp_);
  oldBmp_2    = (HBITMAP) SelectObject (memDC2_, (HBITMAP) iconMaskBmp_);

// prepare mask
  HBRUSH hBrush = CreateSolidBrush (RGB (255, 255, 255));
  hOldBrush     = (HBRUSH) SelectObject (memDC2_, hBrush);
  PatBlt (memDC2_, 0, 0, iconWidth_, iconHeight_, PATCOPY);
  SelectObject (memDC2_, hOldBrush);
  DeleteObject (hBrush);
 
  // draw circle on both bitmaps
  circle = CreateEllipticRgn (0, 0, iconWidth_, iconHeight_);

hBrush = CreateSolidBrush (RGB (255, 0, 0));
  FillRgn (memDC1_, circle, hBrush);
  DeleteObject (hBrush);

hBrush = CreateSolidBrush (RGB (0, 0, 0));
  FillRgn (memDC2_, circle, hBrush);
  DeleteObject (hBrush);
  DeleteObject (circle);
 
  SelectObject (memDC1_, (HBITMAP) oldBmp_1);
  DeleteDC     (memDC1_);
  SelectObject (memDC2_, (HBITMAP) oldBmp_2);
  DeleteDC     (memDC2_);
  DeleteDC     (hDC);
 
  ICONINFO ii = {TRUE, 0, 0, iconMaskBmp_, iconBmp_};
  icon_ = CreateIconIndirect (&ii);

}

DynamicIcon::~DynamicIcon() {
  DestroyIcon (icon_);
  DeleteObject(iconBmp_);
  DeleteObject(iconMaskBmp_);
}

HICON DynamicIcon::Icon() {
  return icon_;
}

LRESULT CALLBACK WndProc(
    HWND   hWnd,
    UINT   msg,
    WPARAM wParam,
    LPARAM lParam ) {

switch( msg ) {
    case WM_PAINT: {
      PAINTSTRUCT ps;
      HDC hDC = BeginPaint( hWnd, &ps );
      TextOut(hDC, 10, 10, "ADP GmbH", 8 );
      EndPaint( hWnd, &ps );
    }
    break;

case WM_DESTROY:
      PostQuitMessage(0);
    break;

default:
      return DefWindowProc( hWnd, msg, wParam, lParam);
  }
  return 0;
}

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
                   LPSTR lpCmdLine, int nCmdShow) {

DynamicIcon di;

WNDCLASSEX wce;

wce.cbSize        = sizeof(wce);
  wce.style         = CS_VREDRAW | CS_HREDRAW;
  wce.lpfnWndProc   = (WNDPROC) WndProc;
  wce.cbClsExtra    = 0;
  wce.cbWndExtra    = 0;
  wce.hInstance     = hInstance;
  wce.hIcon         = <b>di.Icon()</b>;
  wce.hCursor       = LoadCursor((HINSTANCE) NULL, IDC_ARROW);
  wce.hbrBackground = (HBRUSH) GetStockObject(WHITE_BRUSH);
  wce.lpszMenuName  = 0;
  wce.lpszClassName = "ADPWinClass",
  wce.hIconSm       = 0;
 
  if (!RegisterClassEx(&wce)) return 0;
 
  HWND hWnd = CreateWindowEx(
    0,          // Ex Styles
    "ADPWinClass",
    "ADP GmbH",
     WS_OVERLAPPEDWINDOW,
     CW_USEDEFAULT,  // x
     CW_USEDEFAULT,  // y
     CW_USEDEFAULT,  // Hoehe
     CW_USEDEFAULT,  // Breite
     NULL,           // ElternWindow
     NULL,           // Menu respektive Child ID
     hInstance,      //
     NULL            // Pointer auf Daten zu diesem Window
  );

ShowWindow( hWnd, nCmdShow );

MSG msg;
  int r;
  while ((r = GetMessage(&msg, NULL, 0, 0 )) != 0) {
    if (r == -1) {
      ;  // Error!
    }
    else {
      TranslateMessage(&msg);
      DispatchMessage(&msg);
    }
  }

return msg.wParam;
};

A class for dynamic icons in Windows的更多相关文章

  1. [LeetCode] 643. Maximum Average Subarray I_Easy tag: Dynamic Programming(Sliding windows)

    Given an array consisting of n integers, find the contiguous subarray of given length k that has the ...

  2. 在WINDOWS中安装使用GSL(MinGW64+Sublime Text3 & Visual Studio)

    本文介绍在Windows下安装使用GSL库,涉及GSL两个版本(官方最新版及GSL1.8 VC版).msys shell.GCC.G++等内容,最终实现对GSL安装及示例基于MinGW64在Subli ...

  3. [Windows Azure] Developing Multi-Tenant Web Applications with Windows Azure AD

    Developing Multi-Tenant Web Applications with Windows Azure AD 2 out of 3 rated this helpful - Rate ...

  4. 安装InfoPath 2013后 SharePoint 2010 出现 “找不到 Microsoft.Office.InfoPath, Version=14.0.0....” 的错误的解决方案

    1. 症状 您的SharePoint 2010的服务器是不是最近一直出现这个错误呢? Could not load file or assembly 'Microsoft.Office.InfoPat ...

  5. Configure the max limit for concurrent TCP connections(转)

    To keep the TCP/IP stack from taking all resources on the computer, there are different parameters t ...

  6. WPF datagrid 动态增加列

    DataGrid动态增加列 <Window x:Class="WpfApplication1.MainWindow" xmlns="http://schemas.m ...

  7. 关于DLL搜索路径顺序的一个问题

    DLL的动态链接有两种方法.一种是加载时动态链接(Load_time dynamic linking).Windows搜索要装入的DLL时,按以下顺序:应用程序所在目录→当前目录→Windows SY ...

  8. 【转载】Configure the max limit for concurrent TCP connections

    转载地址:http://smallvoid.com/article/winnt-tcpip-max-limit.html To keep the TCP/IP stack from taking al ...

  9. [LeetCode] All questions numbers conclusion 所有题目题号

    Note: 后面数字n表明刷的第n + 1遍, 如果题目有**, 表明有待总结 Conclusion questions: [LeetCode] questions conclustion_BFS, ...

随机推荐

  1. 分享在github超酷超炫特效动画,不看你会懊悔的。

    有图有真相直接上效果图,有须要的朋友们能够到连接上去下载. 下载地址:https://github.com/ChrisRenke/DrawerArrowDrawable 下载地址:https://gi ...

  2. XDocument简单入门

    [+]   1.什么是XML? 2.XDocument和XmlDocument的区别? 3.XDocument 4.XmlDocument 5.LINQ to XML 6.XML序列化与反序列化 因为 ...

  3. 怎样安装解压版MySQL

    第一步: 解压包. 第二步:引入MySQL的bin路径. 第三步: 在cmd下敲入 mysqld -install 第四步:启动服务 net start mysql 第五步:空password登录 m ...

  4. Hibernate_day04--HQL多表查询_Hibernate检索策略

    HQL多表查询 Mysql里面多表查询 1 内连接 查的是两个表关联的数据 2 左外连接 查的是左边表所有数据,右边表关联数据 3 右外连接 HQL实现多表查询 Hql多表查询 (1)内连接 (2)左 ...

  5. Struts 2再曝远程代码执行漏洞S2-037

    导读今年4月份,Apache Stuts 2之上发现的S2-033远程代码执行漏洞,以迅雷不及掩耳之势席卷而来.其利用代码很快就在短时间内迅速传播.而且官方针对这个高危漏洞的修复方案还是无效的. 悲剧 ...

  6. poj_2479 动态规划

    题目大意 给定一列数,从中选择两个不相交的连续子段,求这两个连续子段和的最大值. 题目分析 典型的M子段和的问题,使用动态规划的方法来解决. f[i][j] 表示将A[1...i] 划分为j个不相交连 ...

  7. keepalive和脑裂问题

    keepalive keepalive起初专门为lvs负载均衡软件设计的,用来管理监控lvs集群系统中各个服务节点的状态,后来又加入了可以实现高可用的vrrp功能. keepalive软件通过vrrp ...

  8. 搭建jsp运行环境

    一:搭建jsp服务器端运行环境的步骤: a,下载并按照JDK(JavaDevelopmentKit),这其中就包含了编译器,虚拟机,类库:. b,配置操作系统的环境变量PATH; 补充:编辑变量  添 ...

  9. 【BZOJ3416】Poi2013 Take-out 栈

    [BZOJ3416]Poi2013 Take-out Description 小F喜欢玩一个消除游戏——take-out 保证k+1|n,保证输入数据有解这是一个单人游戏 游戏者的目标是消除初始时给定 ...

  10. 【BZOJ3894】文理分科 最小割

    [BZOJ3894]文理分科 Description 文理分科是一件很纠结的事情!(虽然看到这个题目的人肯定都没有纠结过) 小P所在的班级要进行文理分科.他的班级可以用一个n*m的矩阵进行描述,每个格 ...