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. 360 奇酷行车记录仪12967p 安霸a7

    http://www.qikoo.com/che?utm_source=xingchejiluyi_360daohang_xialareci_0528&utm_medium=inside ht ...

  2. php -- memcached 内存缓存

    一.memcached 简介 在很多场合,我们都会听到 memcached 这个名字,但很多同学只是听过,并没有用过或实际了解过,只知道它是一个很不错的东东.这里简单介绍一下,memcached 是高 ...

  3. 【BZOJ】1636: [Usaco2007 Jan]Balanced Lineup(rmq+树状数组)

    http://www.lydsy.com/JudgeOnline/problem.php?id=1636 (我是不会说我看不懂题的) 裸的rmq.. #include <cstdio> # ...

  4. 禁止Apache显示目录索引的常见方法

    禁止Apache显示目录索引,禁止Apache显示目录结构列表,禁止Apache浏览目录,这是网上提问比较多的,其实都是一个意思.下面说下禁止禁止Apache显示目录索引的常见的3种方法. 要实现禁止 ...

  5. wpf中用户控件的属性重用

    我们经常会抽取一些可重用的控件,某个属性是否需要重用,直接决定了这个属性的绑定方式. 1.完全不可重用的控件 有一些与业务强相关的控件,它们的属性完全来自ViewModel,越是相对复杂的控件,越容易 ...

  6. Android音视频学习第7章:使用OpenSL ES进行音频解码

    /* * *这里使用了transcode-1.1.7对wav文件进行解码.然后使用opensl es进行播放 * */ //用到的变量和结构体 WAV wav; //wav文件指针 SLObjectI ...

  7. Linux安装好系统后优化

    author:headsen chen date: 2018-05-14 15:35:49 1.快速更改国内yum源 mv /etc/yum.repos.d/CentOS-Base.repo /etc ...

  8. python中的函数的执行分类

    author:headsen  chen date: 2018-03-21  17:42:13 notice:This  article created by headsen chen himself ...

  9. 【BZOJ4881】5月月赛D 线段游戏 树状数组+set

    Description quailty和tangjz正在玩一个关于线段的游戏.在平面上有n条线段,编号依次为1到n.其中第i条线段的两端点坐 标分别为(0,i)和(1,p_i),其中p_1,p_2,. ...

  10. LeetCode 笔记系列12 Trapping Rain Water [复杂的代码是错误的代码]

    题目:Given n non-negative integers representing an elevation map where the width of each bar is 1, com ...