Expand|Select|Wrap|Line Numbers
- #include "stdafx.h"
- #include<Windows.h>
- #include<commctrl.h>
- #include<Winuser.h>
- //#include <atlstr.h>
- #define IDC_BUTTON 101
- TCHAR szClassName[ ] = _T("WindowsApp");
- LRESULT CALLBACK WindowProcedure (HWND , UINT , WPARAM , LPARAM);
- HWND hEdit;
- HWND hWnd;
- void AddControls(HWND);
- int _tmain(int argc, _TCHAR* argv[])
- {
- HWND hWnd=0;
- MSG msg;
- WNDCLASSEX wincl;
- HINSTANCE hThisInstance = (HINSTANCE) GetWindowLong(hWnd, GWL_HINSTANCE);
- wincl.hInstance = hThisInstance;
- wincl.lpszClassName = L"szClassName";
- wincl.lpfnWndProc = WindowProcedure;
- wincl.style = CS_DBLCLKS;
- wincl.cbSize =sizeof (WNDCLASSEX);
- wincl.hIcon = LoadIcon (NULL, IDI_APPLICATION);
- wincl.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
- wincl.hCursor = LoadCursor (NULL, IDC_ARROW);
- wincl.lpszMenuName = NULL;
- wincl.cbClsExtra = 0;
- wincl.cbWndExtra = 0;
- wincl.hbrBackground = GetSysColorBrush (COLOR_3DFACE);
- if (!RegisterClassEx (&wincl))
- return -1;
- hWnd = CreateWindowEx(
- 0,
- L"szClassName",
- L"WindowsApp",
- WS_OVERLAPPEDWINDOW | WS_VISIBLE,
- 100,
- 100,
- 500,
- 500,
- HWND_DESKTOP,
- NULL,
- hThisInstance,
- NULL
- );
- while (GetMessage(&msg, NULL, NULL, NULL))
- {
- TranslateMessage(&msg);
- DispatchMessage(&msg);
- }
- return 0;
- }
- //HBRUSH CreateGradientBrush(COLORREF top, COLORREF bottom, LPNMCUSTOMDRAW item)
- // {
- // HBRUSH Brush = NULL;
- // HDC hdcmem = CreateCompatibleDC(item->hdc);
- // HBITMAP hbitmap = CreateCompatibleBitmap(item->hdc, item->rc.right-item->rc.left, item->rc.bottom-item->rc.top);
- // SelectObject(hdcmem, hbitmap);
- //
- // int r1 = GetRValue(top), r2 = GetRValue(bottom), g1 = GetGValue(top), g2 = GetGValue(bottom), b1 = GetBValue(top), b2 = GetBValue(bottom);
- // for(int i = 0; i < item->rc.bottom-item->rc.top; i++)
- // {
- // RECT temp;
- // int r,g,b;
- // r = int(r1 + double(i * (r2-r1) / item->rc.bottom-item->rc.top));
- // g = int(g1 + double(i * (g2-g1) / item->rc.bottom-item->rc.top));
- // b = int(b1 + double(i * (b2-b1) / item->rc.bottom-item->rc.top));
- // Brush = CreateSolidBrush(RGB(r, g, b));
- // temp.left = 0;
- // temp.top = i;
- // temp.right = item->rc.right-item->rc.left;
- // temp.bottom = i + 1;
- // FillRect(hdcmem, &temp, Brush);
- // DeleteObject(Brush);
- // }
- // HBRUSH pattern = CreatePatternBrush(hbitmap);
- //
- // DeleteDC(hdcmem);
- // DeleteObject(Brush);
- // DeleteObject(hbitmap);
- //
- // return pattern;
- // }
- //
- void AddControls(HWND hWnd)
- {
- // HINSTANCE hThisInstance = (HINSTANCE) GetWindowLong(hWnd, GWL_HINSTANCE);
- HWND h= CreateWindow(L"Button", L"Click to save text", WS_VISIBLE |WS_CHILD | BS_OWNERDRAW | BS_PUSHBUTTON, 200, 300, 150, 50, hWnd,(HMENU)IDC_BUTTON, NULL, NULL);
- //SetWindowSubclass(h, &WindowProcedure, IDC_BUTTON, NULL);
- }
- LRESULT CALLBACK WindowProcedure (HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
- { /* static HBRUSH defaultbrush = NULL;
- static HBRUSH hotbrush = NULL;
- static HBRUSH selectbrush = NULL;
- static HBRUSH push_uncheckedbrush = NULL;
- static HBRUSH push_checkedbrush = NULL;
- static HBRUSH push_hotbrush1 = NULL;
- static HBRUSH push_hotbrush2 = NULL; */
- switch (msg)
- {
- case WM_CREATE:
- AddControls(hWnd);
- break;
- case WM_DESTROY:
- // CloseHandle(hWnd);
- PostQuitMessage (0);
- break;
- // /* case BN_CLICKED:
- // MessageBox(NULL,L"Click",L"Button",0);
- // break;
- //*/
- // case WM_DRAWITEM:
- //
- //case WM_NOTIFY:
- // {
- //
- // LPNMHDR some_item = (LPNMHDR)lParam;
- // if (some_item->idFrom == IDC_BUTTON && some_item->code == NM_CUSTOMDRAW)
- // {
- // LPNMCUSTOMDRAW item = (LPNMCUSTOMDRAW)some_item;
- // if (item->uItemState & CDIS_SELECTED)
- // {
- // //Select our color when the button is selected
- // if (selectbrush == NULL)
- // selectbrush = CreateGradientBrush(RGB(180, 0, 0), RGB(255, 180, 0), item);
- // //Create pen for button border
- // HPEN pen = CreatePen(PS_INSIDEFRAME, 0, RGB(0, 0, 0));
- // //Select our brush into hDC
- // HGDIOBJ old_pen = SelectObject(item->hdc, pen);
- // HGDIOBJ old_brush = SelectObject(item->hdc, selectbrush);
- // //If you want rounded button, then use this, otherwise use FillRect().
- // RoundRect(item->hdc, item->rc.left, item->rc.top, item->rc.right, item->rc.bottom, 5, 5);
- // //Clean up
- // SelectObject(item->hdc, old_pen);
- // SelectObject(item->hdc, old_brush);
- // DeleteObject(pen);
- // //Now, I don't want to do anything else myself (draw text) so I use this value for return:
- // return CDRF_DODEFAULT;
- // //Let's say I wanted to draw text and stuff, then I would have to do it before return with
- // //DrawText() or other function and return CDRF_SKIPDEFAULT
- // }
- // else
- // {
- // if (item->uItemState & CDIS_HOT) //Our mouse is over the button
- // {
- // //Select our color when the mouse hovers our button
- // if (hotbrush == NULL)
- // hotbrush = CreateGradientBrush(RGB(255, 230, 0), RGB(245, 0, 0), item);
- // HPEN pen = CreatePen(PS_INSIDEFRAME, 0, RGB(0, 0, 0));
- // HGDIOBJ old_pen = SelectObject(item->hdc, pen);
- // HGDIOBJ old_brush = SelectObject(item->hdc, hotbrush);
- // RoundRect(item->hdc, item->rc.left, item->rc.top, item->rc.right, item->rc.bottom, 5, 5);
- // SelectObject(item->hdc, old_pen);
- // SelectObject(item->hdc, old_brush);
- // DeleteObject(pen);
- // return CDRF_DODEFAULT;
- // }
- // //Select our color when our button is doing nothing
- // if (defaultbrush == NULL)
- // defaultbrush = CreateGradientBrush(RGB(255, 180, 0), RGB(180, 0, 0), item);
- // HPEN pen = CreatePen(PS_INSIDEFRAME, 0, RGB(0, 0, 0));
- // HGDIOBJ old_pen = SelectObject(item->hdc, pen);
- // HGDIOBJ old_brush = SelectObject(item->hdc, defaultbrush);
- // RoundRect(item->hdc, item->rc.left, item->rc.top, item->rc.right, item->rc.bottom, 5, 5);
- // SelectObject(item->hdc, old_pen);
- // SelectObject(item->hdc, old_brush);
- // DeleteObject(pen);
- // return CDRF_DODEFAULT;
- // }
- // }
- // else if (some_item->idFrom == IDC_PUSHLIKE_BUTTON && some_item->code == NM_CUSTOMDRAW)
- // {
- // LPNMCUSTOMDRAW item = (LPNMCUSTOMDRAW)some_item;
- // if (IsDlgButtonChecked(hwnd, some_item->idFrom))
- // {
- // if (item->uItemState & CDIS_HOT)
- // {
- // if (push_hotbrush1 == NULL)
- // push_hotbrush1 = CreateGradientBrush(RGB(0, 0, 245), RGB(0, 230, 255), item);
- // HPEN pen = CreatePen(PS_INSIDEFRAME, 0, RGB(0, 0, 0));
- // HGDIOBJ old_pen = SelectObject(item->hdc, pen);
- // HGDIOBJ old_brush = SelectObject(item->hdc, push_hotbrush1);
- // RoundRect(item->hdc, item->rc.left, item->rc.top, item->rc.right, item->rc.bottom, 10, 10);
- // SelectObject(item->hdc, old_pen);
- // SelectObject(item->hdc, old_brush);
- // DeleteObject(pen);
- // return CDRF_DODEFAULT;
- // }
- // if (push_checkedbrush == NULL)
- // push_checkedbrush = CreateGradientBrush(RGB(0, 0, 180), RGB(0, 222, 200), item);
- // HPEN pen = CreatePen(PS_INSIDEFRAME, 0, RGB(0, 0, 0));
- // HGDIOBJ old_pen = SelectObject(item->hdc, pen);
- // HGDIOBJ old_brush = SelectObject(item->hdc, push_checkedbrush);
- // RoundRect(item->hdc, item->rc.left, item->rc.top, item->rc.right, item->rc.bottom, 10, 10);
- // SelectObject(item->hdc, old_pen);
- // SelectObject(item->hdc, old_brush);
- // DeleteObject(pen);
- // return CDRF_DODEFAULT;
- // }
- // else
- // {
- // if (item->uItemState & CDIS_HOT)
- // {
- // if (push_hotbrush2 == NULL)
- // push_hotbrush2 = CreateGradientBrush(RGB(255, 230, 0), RGB(245, 0, 0), item);
- // HPEN pen = CreatePen(PS_INSIDEFRAME, 0, RGB(0, 0, 0));
- // HGDIOBJ old_pen = SelectObject(item->hdc, pen);
- // HGDIOBJ old_brush = SelectObject(item->hdc, push_hotbrush2);
- // RoundRect(item->hdc, item->rc.left, item->rc.top, item->rc.right, item->rc.bottom, 10, 10);
- // SelectObject(item->hdc, old_pen);
- // SelectObject(item->hdc, old_brush);
- // DeleteObject(pen);
- // return CDRF_DODEFAULT;
- // }
- // if (push_uncheckedbrush == NULL)
- // push_uncheckedbrush = CreateGradientBrush(RGB(255, 180, 0), RGB(180, 0, 0), item);
- // HPEN pen = CreatePen(PS_INSIDEFRAME, 0, RGB(0, 0, 0));
- // HGDIOBJ old_pen = SelectObject(item->hdc, pen);
- // HGDIOBJ old_brush = SelectObject(item->hdc, defaultbrush);
- // RoundRect(item->hdc, item->rc.left, item->rc.top, item->rc.right, item->rc.bottom, 10, 10);
- // SelectObject(item->hdc, old_pen);
- // SelectObject(item->hdc, old_brush);
- // DeleteObject(pen);
- // return CDRF_DODEFAULT;
- // }
- // }
- // return CDRF_DODEFAULT;
- // }
- //
- // break;
- // case WM_PAINT:
- // {
- // PAINTSTRUCT ps;
- // BeginPaint(hWnd, &ps);
- //
- //
- // // RECT rcButton;
- // // rcButton.left = 10;
- // // rcButton.top = 10;
- // // rcButton.right = rcButton.left + 5;
- // // rcButton.bottom = rcButton.top + 50;
- //
- // // Set the DC's background color to whatever color we want the button to be.
- // /// In this case, we use a nice orange. You can use anything you want.
- //
- // COLORREF clrOriginal = SetBkColor(ps.hdc, RGB(0, 255,0));
- // // if (clrOriginal != CLR_INVALID)
- // // {
- // // Fill the button's rectangle with a solid color.
- // // ExtTextOut(ps.hdc, 10, 300, ETO_OPAQUE, &rcButton,NULL, 0, NULL);
- //
- // // Draw the button's border.
- //// DrawEdge(ps.hdc, &rcButton, EDGE_RAISED, BF_TOPLEFT | BF_BOTTOMRIGHT|BS_PUSHBUTTON);
- //
- // // Restore the DC's original background color.
- //// SetBkColor(ps.hdc, clrOriginal);
- // // }
- //
- // // EndPaint(hWnd, &ps);
- // break;
- // }
- /*#define WM_MOUSEMOVE 0x0200*/
- case WM_DRAWITEM:
- LPDRAWITEMSTRUCT Item;
- Item = (LPDRAWITEMSTRUCT)lParam;
- //SelectObject(Item->hDC, CreateFont(16, 0, 0, 0, FW_NORMAL, 0, 0, 0,
- //DEFAULT_CHARSET, OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS, DEFAULT_QUALITY, DEFAULT_PITCH, L"Arial Black"));
- FillRect(Item->hDC, &Item->rcItem, CreateSolidBrush(0));
- SelectObject(Item->hDC, CreateSolidBrush(0));
- if (Item->itemState & ODS_SELECTED)
- {
- SetTextColor(Item->hDC, 0);
- SelectObject(Item->hDC, CreateSolidBrush(0xFF00));
- SelectObject(Item->hDC, CreatePen(PS_SOLID, 255, 0xFF00));
- }
- /*else if(Item->itemState & ODT_STATIC)
- {
- SetTextColor(Item->hDC, 255);
- SelectObject(Item->hDC, CreateSolidBrush(0xFF00));
- SelectObject(Item->hDC, CreatePen(PS_SOLID, 0, 0xFF00));
- }*/
- else
- {
- SetTextColor(Item->hDC, 0x00FF00);
- SelectObject(Item->hDC, CreatePen(PS_SOLID, 2, 0x00FF00));
- }
- SetBkMode(Item->hDC, TRANSPARENT);
- RoundRect(Item->hDC, Item->rcItem.left, Item->rcItem.top, Item->rcItem.right, Item->rcItem.bottom, 20, 20);
- //int len;
- //len = GetWindowTextLength(Item->hwndItem);
- //LPSTR lpBuff;
- //lpBuff = new char[len+1];
- //GetWindowTextA(Item->hwndItem, lpBuff, len+1);
- //DrawTextA(Item->hDC, lpBuff, len, &Item->rcItem, DT_CENTER);
- break;
- case WM_MOUSEMOVE:
- LPDRAWITEMSTRUCT Item;
- Item = (LPDRAWITEMSTRUCT)lParam;
- //SelectObject(Item->hDC, CreateFont(16, 0, 0, 0, FW_NORMAL, 0, 0, 0,
- //DEFAULT_CHARSET, OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS, DEFAULT_QUALITY, DEFAULT_PITCH, L"Arial Black"));
- FillRect(Item->hDC, &Item->rcItem, CreateSolidBrush(0));
- SelectObject(Item->hDC, CreateSolidBrush(0));
- if (Item->itemState & ODS_SELECTED)
- {
- SetTextColor(Item->hDC, 0);
- SelectObject(Item->hDC, CreateSolidBrush(0xFF00));
- }
- break;
- default:
- return DefWindowProc (hWnd, msg, wParam, lParam);
- }