0lt
0lt

Reputation: 313

How to make custom Visual C++ button?

To be precise, I don't want to use standard rectangular buttons. Is there a way to create your own buttons and incorporate them into Visual C++ application? For example, a button in a shape of a fruit, animal, random object...? How can this be achieved?

Upvotes: 0

Views: 1984

Answers (2)

user3656076
user3656076

Reputation: 1

define your own callback function for button SetWindowLong(hMyButtonWnd,GWL_WNDPROC,(LONG)&MyButtonProc);

and here example of button callback LRESULT CALLBACK MyButtonProc(HWND hWnd,UINT message,WPARAM wParam,LPARAM lParam) { if (message == WM_LBUTTONDOWN) { bBtnClicked = true; bBtnDown = true; SetCapture(hWnd); nMouseX = LOWORD(lParam); nMouseY = HIWORD(lParam); InvalidateRect(hWnd,NULL,false); UpdateWindow(hWnd); } else if (message == WM_LBUTTONUP) { bBtnClicked = false; bBtnDown = false; InvalidateRect(hWnd,NULL,false); UpdateWindow(hWnd); RECT rec; GetClientRect(hWnd,&rec); if (nMouseX > rec.left && nMouseY > rec.top && nMouseX < rec.right && nMouseY < rec.bottom) { MessageBox(NULL,L"Button clicked",L"Test",MB_OK); } ReleaseCapture(); } else if (message == WM_MOUSEMOVE) { nMouseX = LOWORD(lParam); nMouseY = HIWORD(lParam); RECT rec; GetClientRect(hWnd,&rec); if (nMouseX <= rec.left || nMouseY <= rec.top || nMouseX >= rec.right || nMouseY >= rec.bottom) bBtnDown = false; InvalidateRect(hWnd,NULL,false); UpdateWindow(hWnd); } if (message == WM_PAINT) { PAINTSTRUCT ps; HDC dc = BeginPaint(hWnd,&ps); HDC c_dc = CreateCompatibleDC(NULL); if (bBtnDown) SelectObject(c_dc,hBtnDown); else SelectObject(c_dc,hBtnUp); RECT rec; GetClientRect(hWnd,&rec); BitBlt(dc,0,0,rec.right,rec.bottom,c_dc,0,0,SRCCOPY); DeleteDC(c_dc); EndPaint(hWnd,&ps); return 0; } else return CallWindowProc(DefProc,hWnd,message,wParam,lParam); return 1; }

Upvotes: 0

ScottMcP-MVP
ScottMcP-MVP

Reputation: 10415

There are two common approaches to custom buttons. The first is to use the owner-draw optional style on a standard button. With this style set you receive a message (WM_DRAWITEM) when the button needs to be painted and you paint it yourself any way you like. A second approach is to draw the image on an existing window and analyze mouse messages to determine when a click is on the image. The common toolbar uses this approach: It doesn't really have button controls on it, just pictures that look like buttons. With this second approach you are not limited to clicks on rectangular areas.

Upvotes: 1

Related Questions