A class for dynamic icons in Windows

Source: Internet
Author: User

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::D Ynamicicon () {
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 (&AMP;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, ten, Ten, "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 (&AMP;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 (&AMP;MSG);
DispatchMessage (&AMP;MSG);
}
}

return msg.wparam;
};

A class for dynamic icons in Windows

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.