Advertisement
Guest User

Untitled

a guest
Dec 17th, 2013
204
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 11.15 KB | None | 0 0
  1. #include <windows.h>
  2. #include <windowsx.h> // for TransparentBlt()
  3. #include <commctrl.h>
  4. #include <gdiplus.h>
  5.  
  6. #pragma comment( linker, "/manifestdependency:\"type='win32' \
  7.                          name='Microsoft.Windows.Common-Controls' version='6.0.0.0' \
  8.                          processorArchitecture='*' publicKeyToken='6595b64144ccf1df' \
  9.                          language='*'\"")
  10.  
  11. #pragma comment( lib, "comctl32.lib")
  12. #pragma comment( lib, "Msimg32.lib" ) // for TransparentBlt()
  13. #pragma comment( lib, "Gdiplus.lib" )
  14.  
  15. using namespace Gdiplus;
  16.  
  17. // variable for storing the instance
  18.  
  19. static HINSTANCE hInst;
  20.  
  21. // variables for painting the window
  22.  
  23. static HBRUSH hbPozadina, // gray background brush for grid on the top
  24.             BlueFrame, // needed to frame blue static controls
  25.             hbr; // orange brush for orange panel
  26.  
  27. /****************************** helper functions for WM_PAINT **************************/
  28.  
  29. // Fills triangle with gradient brush
  30.  
  31. void GradientTriangle( HDC MemDC, LONG x1, LONG y1, LONG x2, LONG y2, LONG x3, LONG y3,
  32.                       COLORREF top, COLORREF bottom )
  33. {
  34.     TRIVERTEX vertex[3];
  35.  
  36.     vertex[0].x     = x1;
  37.     vertex[0].y     = y1;
  38.     vertex[0].Red   = GetRValue(bottom) << 8;
  39.     vertex[0].Green = GetGValue(bottom) << 8;
  40.     vertex[0].Blue  = GetBValue(bottom) << 8;
  41.     vertex[0].Alpha = 0x0000;
  42.  
  43.     vertex[1].x     = x2;
  44.     vertex[1].y     = y2;
  45.     vertex[1].Red   = GetRValue(top) << 8;
  46.     vertex[1].Green = GetGValue(top) << 8;
  47.     vertex[1].Blue  = GetBValue(top) << 8;
  48.     vertex[1].Alpha = 0x0000;
  49.  
  50.     vertex[2].x     = x3;
  51.     vertex[2].y     = y3;
  52.     vertex[2].Red   = GetRValue(bottom) << 8;
  53.     vertex[2].Green = GetGValue(bottom) << 8;
  54.     vertex[2].Blue  = GetBValue(bottom) << 8;
  55.     vertex[2].Alpha = 0x0000;
  56.  
  57.     // Create a GRADIENT_TRIANGLE structure that
  58.     // references the TRIVERTEX vertices.
  59.            
  60.     GRADIENT_TRIANGLE gTriangle;
  61.  
  62.     gTriangle.Vertex1 = 0;
  63.     gTriangle.Vertex2 = 1;
  64.     gTriangle.Vertex3 = 2;
  65.  
  66.     // Draw a shaded triangle.
  67.            
  68.     GradientFill( MemDC, vertex, 3, &gTriangle, 1, GRADIENT_FILL_TRIANGLE);
  69. }
  70.  
  71. // draws the background for the part of the window between header and footer
  72.  
  73. void drawBackground( HDC MemDC, RECT r )
  74. {
  75.     /******************** main window's gradient background ***************/
  76.  
  77.     GradientTriangle( MemDC, r.right, r.bottom - r.top - 30, r.left, r.bottom - r.top - 30,
  78.         r.left, r.top + 120, RGB( 0x95, 0xB3, 0xD7 ), RGB( 0xDB, 0xE5, 0xF1 ) );
  79.  
  80.     GradientTriangle( MemDC, r.right, r.bottom - r.top - 30, r.right, r.top + 120,
  81.         r.left, r.top + 120, RGB( 0x95, 0xB3, 0xD7 ), RGB( 0xDB, 0xE5, 0xF1 ) );
  82.  
  83. }
  84.  
  85. // draws the header of the main window
  86.  
  87. void drawHeader( HDC MemDC, RECT rect, HBRUSH hbPozadina )
  88. {
  89.     FillRect( MemDC, &rect, hbPozadina );
  90. }
  91.  
  92. // fills rectangle with gradient brush
  93.  
  94. void GradientRectangle( HDC MemDC, LONG x1, LONG y1, LONG x2, LONG y2, COLORREF top,
  95.                        COLORREF bottom )
  96. {
  97.     // vertexes for static's gradient color
  98.  
  99.     TRIVERTEX vertexS[2];
  100.  
  101.     vertexS[0].x     = x1;
  102.     vertexS[0].y     = y1;
  103.     vertexS[0].Red   = GetRValue(top) << 8;
  104.     vertexS[0].Green = GetGValue(top) << 8;
  105.     vertexS[0].Blue  = GetBValue(top) << 8;
  106.     vertexS[0].Alpha = 0x0000;
  107.  
  108.     vertexS[1].x     = x2;
  109.     vertexS[1].y     = y2;
  110.     vertexS[1].Red   = GetRValue(bottom) << 8;
  111.     vertexS[1].Green = GetGValue(bottom) << 8;
  112.     vertexS[1].Blue  = GetBValue(bottom) << 8;
  113.     vertexS[1].Alpha = 0x0000;
  114.  
  115.     // Create a GRADIENT_RECT structure that
  116.     // references the TRIVERTEX vertices.
  117.  
  118.     GRADIENT_RECT gRect;
  119.  
  120.     gRect.UpperLeft  = 0;
  121.     gRect.LowerRight = 1;
  122.  
  123.     // Draw a shaded rectangle.
  124.  
  125.     GradientFill( MemDC, vertexS, 2, &gRect, 1, GRADIENT_FILL_RECT_V );
  126. }
  127.  
  128. // fills the "button" with blue gradient and frames it with blue brush
  129.  
  130. void FillButton( HDC MemDC, RECT rect, HBRUSH BlueFrame )
  131. {
  132.     // fill upper half of the rectangle
  133.  
  134.     GradientRectangle( MemDC, rect.left, rect.top, rect.right,
  135.         rect.top + ( rect.bottom - rect.top ) / 2,
  136.         RGB( 0x95, 0xB3, 0xD7 ), RGB( 0x4F, 0x8B, 0xBD ) );
  137.  
  138.     // fill bottom half of the rectangle
  139.  
  140.     GradientRectangle( MemDC, rect.left, rect.top + ( rect.bottom - rect.top ) / 2,
  141.         rect.right, rect.bottom, RGB( 0x4F, 0x8B, 0xBD ), RGB( 0x95, 0xB3, 0xD7 ) );
  142.  
  143.     FrameRect( MemDC, &rect, BlueFrame );
  144. }
  145.  
  146. // draws the "status bar" at the bottom of the main window
  147.  
  148. void drawFooter( HDC MemDC, RECT r, COLORREF top, COLORREF bottom )
  149. {
  150.     // down triangle
  151.  
  152.     GradientTriangle( MemDC,
  153.         r.right, r.bottom,
  154.         ( r.right - r.left ) / 2,
  155.         r.bottom - r.top - 15,
  156.         r.left, r.bottom,
  157.         top, bottom );
  158.  
  159.     // upper triangle
  160.  
  161.     GradientTriangle( MemDC,
  162.         r.right, r.bottom - r.top - 30,
  163.         ( r.right - r.left ) / 2, r.bottom - r.top - 15,
  164.         r.left, r.bottom - r.top - 30,
  165.         top, bottom );
  166.  
  167.     // left triangle
  168.  
  169.     GradientTriangle( MemDC,
  170.         r.left, r.bottom,
  171.         ( r.right - r.left ) / 2, r.bottom - r.top - 15,
  172.         r.left, r.bottom - r.top - 30,
  173.         top, bottom );
  174.  
  175.     // right triangle
  176.  
  177.     GradientTriangle( MemDC,
  178.         r.right, r.bottom - r.top - 30,
  179.         ( r.right - r.left ) / 2, r.bottom - r.top - 15,
  180.         r.right, r.bottom,
  181.         top, bottom );
  182. }
  183.  
  184. // draw orange panel on which map and 3 static controls will be drawn
  185.  
  186. void drawOrangePanel( HDC MemDC, RECT r, COLORREF top, COLORREF bottom )
  187. {
  188.     // down triangle
  189.  
  190.     GradientTriangle( MemDC,
  191.         r.right, r.bottom,
  192.         r.left + ( r.right - r.left ) / 2,
  193.         r.top + ( r.bottom - r.top ) / 2,
  194.         r.left, r.bottom,
  195.         top, bottom );
  196.  
  197.     // upper triangle
  198.  
  199.     GradientTriangle( MemDC,
  200.         r.right, r.top,
  201.         r.left + ( r.right - r.left ) / 2,
  202.         r.top + ( r.bottom - r.top ) / 2,
  203.         r.left, r.top,
  204.         top, bottom );
  205.  
  206.     // left triangle
  207.  
  208.     GradientTriangle( MemDC,
  209.         r.left, r.bottom,
  210.         r.left + ( r.right - r.left ) / 2,
  211.         r.top + ( r.bottom - r.top ) / 2,
  212.         r.left, r.top,
  213.         top, bottom );
  214.  
  215.     // right triangle
  216.  
  217.     GradientTriangle( MemDC,
  218.         r.right, r.top,
  219.         r.left + ( r.right - r.left ) / 2, r.top + ( r.bottom - r.top ) / 2,
  220.         r.right, r.bottom,
  221.         top, bottom );
  222. }
  223.  
  224. void onPaint( HWND hwnd, WPARAM wParam, LPARAM lParam )
  225. {
  226.     PAINTSTRUCT ps;
  227.  
  228.     HDC hdc = BeginPaint( hwnd, &ps);
  229.  
  230.     RECT r; // rectangle for main window's client area
  231.            
  232.     GetClientRect( hwnd, &r);
  233.  
  234.     HDC MemDC = CreateCompatibleDC(hdc); // back buffer
  235.  
  236.     // compatible bitmap for MemDC
  237.  
  238.     HBITMAP bmp = CreateCompatibleBitmap( hdc, r.right - r.left, r.bottom - r.top ),
  239.         oldBmp = (HBITMAP)SelectObject( MemDC, bmp ); // needed for cleanup
  240.  
  241.     // draw background for middle part of the window
  242.  
  243.     drawBackground( MemDC, r );
  244.  
  245.     // draw header with grid lines
  246.    
  247.     RECT rect; // position it properly at the top
  248.  
  249.     rect.left = r.left;
  250.     rect.top = r.top;
  251.     rect.right = r.right;
  252.     rect.bottom = 120;
  253.  
  254.     drawHeader( MemDC, rect, hbPozadina );
  255.  
  256.     // draw "status bar"
  257.  
  258.     drawFooter( MemDC, r, RGB( 0x48, 0xAC, 0xC6), RGB( 0x31, 0x83, 0x99 ) );
  259.  
  260.     /****************** draw static control's background ******************/
  261.  
  262.     //=============== top left static control ================//
  263.  
  264.     //position it properly
  265.  
  266.     rect.left = ( 3 * ( r.right - r.left ) / 4 - 340 ) / 3;
  267.     rect.top = 120 + ( r.bottom - r.top - 450 ) / 3;
  268.     rect.right = 150 + ( 3 * ( r.right - r.left ) / 4 - 340 ) / 3;
  269.     rect.bottom = 270 + ( r.bottom - r.top - 450 ) / 3;
  270.  
  271.     // draw gradient button
  272.  
  273.     FillButton( MemDC, rect, BlueFrame );
  274.  
  275.     //======================== draw orange panel =================//
  276.        
  277.     //position it properly
  278.  
  279.     rect.left = 3 * ( r.right - r.left ) / 4 - 40;
  280.     rect.top = r.top + 140;
  281.     rect.right = rect.left + ( r.right - r.left ) / 4;
  282.     rect.bottom = rect.top + ( r.bottom - r.top - 190 );
  283.  
  284.     drawOrangePanel( MemDC, rect, RGB( 0xFF, 0xC8, 0xAA ), RGB( 0xFF, 0x96, 0x48 ) );
  285.  
  286.     /****************** draw back buffer on the screen DC *****************/
  287.  
  288.     BitBlt( hdc, 0, 0, r.right - r.left, r.bottom - r.top, MemDC, 0, 0, SRCCOPY );
  289.  
  290.     /************** cleanup *******************/
  291.  
  292.     SelectObject( MemDC, oldBmp );
  293.  
  294.     DeleteObject(bmp); // compatible bitmap for MemDC
  295.  
  296.     DeleteDC(MemDC);
  297.  
  298.     EndPaint( hwnd, &ps);
  299. }
  300.  
  301. // WinMain's procedure
  302.  
  303. LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
  304. {
  305.     switch(msg)
  306.     {
  307.     case WM_CREATE:
  308.         {
  309.             //******** brushes ***********//
  310.            
  311.             // load gray background brush for the top banner
  312.  
  313.             hbPozadina = CreateSolidBrush( RGB( 230, 230, 230 ) );
  314.  
  315.             // brush for orange panel that holds 3 static controls and a map
  316.  
  317.             hbr = CreateSolidBrush( RGB( 255, 163, 94 ) );
  318.  
  319.             // blue frame for blue static controls
  320.  
  321.             BlueFrame = CreateSolidBrush( RGB(79, 129, 189) );
  322.  
  323.             /*******************************************/
  324.         }
  325.         return (LRESULT)0;
  326.  
  327.     case WM_ERASEBKGND:
  328.         return (LRESULT)1; // so we avoid flicker ( all painting is in WM_PAINT )
  329.  
  330.     case WM_PAINT:
  331.         {
  332.             // paint the picture
  333.             onPaint( hwnd, wParam, lParam );
  334.         }
  335.         return (LRESULT)0;
  336.  
  337.     case WM_SIZE:
  338.         InvalidateRect( hwnd, NULL, FALSE );
  339.         return (LRESULT)0;
  340.  
  341.     case WM_CLOSE:
  342.  
  343.         // destroy brushes
  344.        
  345.         DeleteObject(hbPozadina);
  346.         DeleteObject(hbr);
  347.         DeleteObject(BlueFrame);
  348.  
  349.         DestroyWindow(hwnd);
  350.  
  351.         return (LRESULT)0;
  352.  
  353.     case WM_DESTROY:
  354.         PostQuitMessage(0);
  355.         return (LRESULT)0;
  356.  
  357.     default:
  358.         return DefWindowProc(hwnd, msg, wParam, lParam);
  359.     }
  360.     return 0;
  361. }
  362.  
  363. // WinMain
  364.  
  365. int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine,
  366.                    int nCmdShow)
  367. {
  368.     // store hInstance in global variable for later use
  369.  
  370.     hInst = hInstance;
  371.  
  372.     WNDCLASSEX wc;
  373.     HWND hwnd;
  374.     MSG Msg;
  375.  
  376.     /*********** variables for GDI+ initialization *****************/
  377.  
  378.     GdiplusStartupInput gdiplusStartupInput;
  379.     ULONG_PTR           gdiplusToken;
  380.  
  381.     /************** Initialize GDI+. *************************/
  382.  
  383.     GdiplusStartup(&gdiplusToken, &gdiplusStartupInput, NULL);
  384.  
  385.     /*************** finished GDI+ initialisation ********************/
  386.  
  387.     // initialize common controls
  388.  
  389.     INITCOMMONCONTROLSEX iccex;
  390.     iccex.dwSize = sizeof(INITCOMMONCONTROLSEX);
  391.     iccex.dwICC = ICC_STANDARD_CLASSES ;
  392.     InitCommonControlsEx(&iccex);
  393.    
  394.     // register main window class
  395.  
  396.     wc.cbSize = sizeof(WNDCLASSEX);
  397.     wc.style = 0;
  398.     wc.lpfnWndProc = WndProc;
  399.     wc.cbClsExtra = 0;
  400.     wc.cbWndExtra = 0;
  401.     wc.hInstance = hInst;
  402.     wc.hIcon = LoadIcon (NULL, IDI_APPLICATION);
  403.     wc.hCursor = LoadCursor( NULL, IDC_ARROW );
  404.     wc.hbrBackground = NULL;//(HBRUSH)GetStockObject( WHITE_BRUSH );
  405.     wc.lpszMenuName = NULL;
  406.     wc.lpszClassName = L"Main_Window";
  407.     wc.hIconSm = LoadIcon (NULL, IDI_APPLICATION);
  408.  
  409.     if(!RegisterClassEx(&wc))
  410.     {
  411.         MessageBox(NULL, L"Window Registration Failed!", L"Error!", MB_ICONEXCLAMATION |
  412.             MB_OK);
  413.  
  414.         return 0;
  415.     }
  416.  
  417.     // create main window
  418.  
  419.     hwnd = CreateWindowEx( 0, // WS_EX_COMPOSITED "improved" drawing of the edges
  420.         L"Main_Window",
  421.         L"Геотермист",
  422.         WS_OVERLAPPEDWINDOW,
  423.         ( GetSystemMetrics(SM_CXMAXIMIZED) - 1020 ) / 2,
  424.         ( GetSystemMetrics(SM_CYMAXIMIZED) - 600 ) / 2,
  425.         1020, 600, NULL, NULL, hInstance, 0 );
  426.  
  427.     if(hwnd == NULL)
  428.     {
  429.         MessageBox(NULL, L"Window creation failed!", L"Error!", MB_ICONEXCLAMATION |
  430.             MB_OK);
  431.  
  432.         return 0;
  433.     }
  434.  
  435.     ShowWindow(hwnd, nCmdShow);
  436.     UpdateWindow(hwnd);
  437.  
  438.     while(GetMessage(&Msg, NULL, 0, 0) > 0)
  439.     {
  440.         TranslateMessage(&Msg);
  441.         DispatchMessage(&Msg);
  442.     }
  443.  
  444.     // shutdownd GDI+
  445.  
  446.     GdiplusShutdown(gdiplusToken);
  447.  
  448.     return Msg.wParam;
  449. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement