Win32 self-painted button, using GDI + (1), win32gdi

Source: Internet
Author: User
Tags win32 window

Win32 self-painted button, using GDI + (1), win32gdi

The first time I wrote the essay, I wanted to set the title bar of the win32 window to gradient color.

However, the api for setting the title bar attribute cannot be found. SetWindowLong is only used to increase or decrease the fixed style of the window. So I thought of an idea. I removed the title bar and drew a title bar myself, including the buttons on the title bar. The gdi + is used here, and this library has just been used.

The final implementation effect of the program is as follows.

I don't know much about the color corresponding to the color value, so it looks ugly.

Code:

# Ifndef ULONG_PTR # define ULONG_PTR unsigned long * # endif # include <windows. h> // # include <objidl. h> # include <gdiplus. h> using namespace Gdiplus; # pragma comment (lib, "Gdiplus. lib ") # define ID_BUTTON1 1 # define ID_BUTTON2 2 HINSTANCE hInst; void LoadBkImge (HDC hdc) {Graphics graphics (hdc); Image image (L" pic1.png "); graphics. drawImage (& image, 0, 0);} void FillRec (HDC hdc, Rect myRect, Color * colors, float * positions, int I) {Graphics graphics (hdc ); // colorful gradient LinearGradientBrush myLinearGradientBrush (myRect, Color (255,255,255, 0), Color (255,255,), LinearGradientModeVertical); // gradient */myLinearGradientBrush. setInterpolationColors (colors, positions, I); graphics. fillRectangle (& Records, myRect);} lresult callback WndProc (HWND, UINT, WPARAM, LPARAM); int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE, PSTR, INT iCmdShow) {HWND hWnd; MSG msg; WNDCLASS wndClass; GdiplusStartupInput gdiplusStartupInput; ULONG_PTR gdiplusToken; hInst = hInstance; // Initialize GDI +. gdiplusStartup (& gdiplusToken, & gdiplusStartupInput, NULL); wndClass. style = CS_HREDRAW | CS_VREDRAW | DS_CENTER; wndClass. lpfnWndProc = WndProc; wndClass. cbClsExtra = 0; wndClass. cbWndExtra = 0; wndClass. hInstance = hInstance; wndClass. hIcon = LoadIcon (NULL, IDI_APPLICATION); wndClass. hCursor = LoadCursor (NULL, IDC_ARROW); wndClass. hbrBackground = (HBRUSH) GetStockObject (WHITE_BRUSH); wndClass. lpszMenuName = NULL; wndClass. vertex = TEXT ("Gdiplustest"); RegisterClass (& wndClass); hWnd = CreateWindow (TEXT ("Gdiplustest"), TEXT ("Gdiplustest"), WS_OVERLAPPED | WS_POPUP, CW_USEDEFAULT, CW_USEDEFAULT, 400,250, NULL, NULL, hInstance, NULL); ShowWindow (hWnd, iCmdShow); UpdateWindow (hWnd); while (GetMessage (& msg, NULL, 0, 0 )) {TranslateMessage (& msg); DispatchMessage (& msg) ;} GdiplusShutdown (gdiplusToken); return msg. wParam;} lresult callback (HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) {static HDC hdc; static HWND hButton1, hButton2; PAINTSTRUCT ps; Notify pdis; switch (message) {case WM_CREATE: {// int scrWidth, scrHeight; RECT rect; scrWidth = GetSystemMetrics (SM_CXSCREEN); scrHeight = GetSystemMetrics (SM_CYSCREEN); GetWindowRect (hWnd, & rect ); rect. left = (scrWidth-rect.right)/2; rect. top = (scrHeight-rect.bottom)/2; SetWindowPos (hWnd, HWND_TOP, rect. left, rect. top, rect. right, rect. bottom, SWP_SHOWWINDOW); hButton1 = CreateWindow ("button", "", WS_CHILD | WS_VISIBLE | BS_OWNERDRAW, 30, hWnd, (HMENU) ID_BUTTON1, hInst, NULL ); hButton2 = CreateWindow ("button", "", WS_CHILD | WS_VISIBLE | BS_OWNERDRAW, 355, 40, 30, hWnd, (HMENU) ID_BUTTON2, hInst, NULL ); // hdc = GetDC (hWnd); LoadBkImge (hdc); return 0;} case WM_SIZE: return 0; case WM_PAINT: {hdc = BeginPaint (hWnd, & ps ); // Draw1 (hdc); Rect myRect (255,255,255, 247,247,247, 40); // Color colors [] = {Color (231,235,139), Color (), Color ), color (214,223,231), Color (181,178,181)}; float positions [] = {0.0f, 0.125f, 0.5f, 0.875f, 1.0f}; FillRec (hdc, myRect, colors, positions, 5); EndPaint (hWnd, & ps); return 0;} case WM_COMMAND: {switch (wParam) {case ID_BUTTON1: {MessageBox (NULL, "Undefined ","", MB_ OK); break;} case ID_BUTTON2: {PostQuitMessage (0); break;} //} return 0;} case WM_DRAWITEM: {pdis = (LPDRAWITEMSTRUCT) lParam; // SetBkMode (pdis-> hDC, TRANSPARENT); FillRect (pdis-> hDC, & pdis-> rcItem, NULL); FrameRect (pdis-> hDC, & pdis-> rcItem, (HBRUSH) GetStockObject (BLACK_BRUSH); int cx = pdis-> rcItem. right-pdis-> rcItem. bottom; int cy = pdis-> rcItem. bottom-pdis-> rcItem. top; switch (pdis-> CtlID) {case ID_BUTTON1: {Graphics graphics (pdis-> hDC); Rect rectt (0, 0, 40, 30 ); color colors [] = {Color (247,247,247), Color (231,235,139), Color (214,223,231),}; float positions [] = {0.0f, 0.5f, 1.0f }; fillRec (pdis-> hDC, rectt, colors, positions, 3); {SolidBrush m_pBrush (Color (255,130,130,130); PointF point1 (0.0f, 00366f); PointF point2 (30366f, 20366f); PointF point3 (10.0f, 30366f); PointF points [3] = {point1, point2, point3}; graphics. fillPolygon (& m_pBrush, points, 3, FillModeAlternate);} break;} case ID_BUTTON2: {Graphics graphics (pdis-> hDC); Rect rectt (0, 0, 40, 30 ); color colors [] = {Color (247,247,247), Color (231,235,139), Color (214,223,231),}; float positions [] = {0.0f, 0.5f, 1.0f }; fillRec (pdis-> hDC, rectt, colors, positions, 3); SolidBrush m_pBrush (Color (255,130,130,130); PointF point1 (0.0f, 7.0); PointF point2 (6.0f, 0.0f); PointF point3 (40366f, 23.0f); PointF point4 (34366f, 30366f); PointF points [4] = {point1, point2, point3, point4}; graphics. fillPolygon (& m_pBrush, points, 4, FillModeAlternate); // PointF point5 (0.0f, 23.0f); PointF point6 (34366f, 0.0f); PointF point7 (40366f, 7.0f ); pointF point8 (6.0f, 30366f); PointF points2 [4] = {point5, point6, point7, point8}; graphics. fillPolygon (& m_pBrush, points2, 4, FillModeAlternate);} break;} // if (pdis-> itemState & ODS_SELECTED) // InvertRect (pdis-> hDC, & pdis-> rcItem); // Draw a focus rectangle if the button has the focus if (pdis-> itemState & ODS_FOCUS) {pdis-> rcItem. left + = cx/16; pdis-> rcItem. top + = cy/16; pdis-> rcItem. right-= cx/16; pdis-> rcItem. bottom-= cy/16; DrawFocusRect (pdis-> hDC, & pdis-> rcItem);} return 0;} case WM_DESTROY: PostQuitMessage (0); return 0; case WM_LBUTTONDOWN: sendMessage (hWnd, WM_NCLBUTTONDOWN, HTCAPTION, 0); return 0; default: return DefWindowProc (hWnd, message, wParam, lParam );}}

Note:

There is a serious problem here. How do I set the background transparency of the button? The temporary solution is to fill the button with the same gradient. The position array value of the five gradient colors in the picturesque title bar is

      float positions[]={       0.0f,       0.125f,       0.5f,       0.875f,       1.0f      };

Color:

       Color colors[]={       Color(255,255,255),       Color(247,247,247),       Color(231,235,139),       Color(214,223,231),       Color(181,178,181)       };

The gradient color and position of the buttons are calculated based on the coordinates.

       Color colors[]={       Color(247,247,247),       Color(231,235,139),       Color(214,223,231),       };      float positions[]={       0.0f,0.5f,1.0f      };

This method looks stupid, so there are two issues at last.

Question 1: How do I set the system menu bar to gradient color?

Question 2: How is the button background transparent?

Reference: I found a lot of information.

 

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.