VC + + Play Cool suspension Windows 3---GDI + perfect implementation of irregular forms

Source: Internet
Author: User

??

Goal
The previous irregular form, although realized, but the graphics are jagged, giving people over the past century of the antique feeling, with cool not to lap. Today it is necessary to use advanced techniques to make perfect smooth, non-aliased irregular forms.
Planning & Solutions
PNG image itself is a transparent effect, the picture as a form, with GDI + to implement it.
So what is GDI +? Let's start with the GDI, graphics Devices Interface, graphics device interface, which is responsible for exchanging information between the system and the drawing program, and processing the output of all Windows graphics programs. While GDI + is its enhanced version, the XP era is a subsystem that is responsible for outputting information on display screens and print devices. Programmers implement graphical programming based on the many functions they provide, without concern for the implementation details of the graphics hardware.
So-called: let the weak water 3,000, I only take a scoop of drink. This is my first time to contact GDI +, only in the application of some functions, tiger will use a few functions.
The key functions of this paper are: Updatelayeredwindow,

BOOL WINAPI Updatelayeredwindow (  _in_      HWND hwnd,//window handle  _in_opt_  hdc hdcdst,//Current window hdc  _in_opt_ Point  *pptdst,//  _in_opt_  SIZE *psize,  _in_opt_  HDC hdcsrc,  _in_opt_ point  *pptsrc,  _in_      COLORREF Crkey,  _in_opt_  blendfunction *pblend,  _in_      DWORD dwFlags);

See official website: http://msdn.microsoft.com/en-us/library/windows/desktop/ms633556 (v=vs.85). aspx


Practice
I. The configuration of GDI + on VS2012
GDI + support is already on the VS2012, and it is not necessary to download the installation package separately. Now all you need to do is introduce Gdiplus.lib and add header files to your project. Properties-? Configuration Properties-? Linker-? Input-? Additional Dependencies added "Gdiplus.lib".
In order to use GDI + in full engineering, include and use namespaces for header files in stdafx.h.

#include "Gdiplus.h"   


Second, the initialization of GDI +

When the app is initialized, start Gdi+bool cxxxapp::initinstance () {...        Use GDIplus begingdiplusstartupinput gdiplusstartupinput; Gdiplusstartup (&m_gdiplustoken, &gdiplusstartupinput, NULL);        Use GDIplus end ...} When the program exits, close Gdi+int cxxxapp::exitinstance () {    //close gdiplus environmentgdiplusshutdown (m_gdiplustoken); return CWinApp::ExitInstance ();}


Third, add PNG pictures to project resources
When we add resources in the resource interface, the Add Resource interface only icon and bitmap, and no PNG optional, do not ignore it, we just import it, you will find that after adding the PNG image, the PNG folder will magically appear in the resource interface.

Iv. loading images into memory
Borrow a cool function that loads pictures of various formats into memory. As follows:

Resurceid:resource Id///imgtype:type of Image///pimg:pointer to Imagebool Cafloatwindowdlg::imagefromidresource ( UINT resurceid,lpctstr Imgtype,image * &pimg) {hinstance hInst = AfxGetResourceHandle (); Hrsrc hrsrc =:: FindResource (Hinst,makeintresource (Resurceid), imgtype);    Typeif (HRSRC) {//load resource into memory DWORD len = Sizeofresource (HInst, HRSRC);    byte* lprsrc = (byte*) loadresource (HInst, HRSRC); if (LPRSRC) {//Allocate global memory on which to create stream hglobal M_hmem = GlobalAlloc (GM        Em_fixed, Len);        byte* Pmem = (byte*) globallock (M_HMEM);        memcpy (Pmem,lprsrc,len);        istream* pstm;        CreateStreamOnHGlobal (m_hmem,false,&pstm);        Load from Stream Pimg=gdiplus::image::fromstream (pstm);        Free/release Stuff GlobalUnlock (M_HMEM);        Pstm->release ();            FreeResource (LPRSRC);        return TRUE; }} return FALSE;}


V.   Replace the window background with your picture
refer to the butterfly Clock source code, the most important thing is to see the use of Updatelayeredwindow.

BOOL Cafloatwindowdlg::updatedisplay (Image *image, int Transparent) {int imagewidth = 240; int imageheight = 240;//magic number of my image width and heighthdc hdctemp=getdc ()->m_hdc;m_hdcmemory=createcompatib Ledc (hdctemp); Hbitmap Hbitmap=createcompatiblebitmap (hdctemp,imagewidth,imageheight); SelectObject (M_hdcmemory,hbitmap); if (transparent<0| | transparent>100) Transparent=100;m_blend.sourceconstantalpha=int (transparent*2.55)//1~255, if you want the change Transparent, modify this. HDC Hdcscreen=::getdc (m_hwnd); RECT RCT; GetWindowRect (&AMP;RCT); Point Ptwinpos={rct.left,rct.top}; Graphics graph (m_hdcmemory);    Point points[] = {Point (0, 0), point (imagewidth, 0),//width Point (0, imageheight)//height}; Graph. DrawImage (image,points,3);//do it! SIZE Sizewindow={imagewidth,imageheight}; Point ptsrc={0,0};D Word Dwexstyle=getwindowlong (M_hwnd,gwl_exstyle), if (dwexstyle&0x80000)!=0x80000 SetWindowLong (m_hwnd,gwl_exstyle,dwexstyle^0x80000); BOOL bret=false;bret= upDatelayeredwindow (m_hwnd,hdcscreen,&ptwinpos,&sizewindow,m_hdcmemory,&ptsrc,0,&m_blend,2); Graph. RELEASEHDC (M_hdcmemory);:: ReleaseDC (M_hwnd,hdcscreen); Hdcscreen=null;::releasedc (m_hwnd,hdctemp); HdcTemp=NULL ;D Eleteobject (HBITMAP);D Eletedc (m_hdcmemory); M_hdcmemory=null;return BRet;}


Vi. End of
At this point we have completed important functions, such as the results of the operation.


Don't worry about the jagged stuff.
The source code is here, and the animation is added. Please refer to.
Reference:
The GDI + transparent window. Updatelayeredwindow "http://blog.csdn.net/zdl1016/article/details/3298744
"GDI + Programming Summary" http://blog.csdn.net/byxdaz/article/details/5972759







VC + + Play Cool suspension Windows 3---GDI + perfect implementation of irregular forms

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.