1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94
|
class inputButton {
HWND hWnd;
int xpos, ypos, nCmdShow, buttonState, buttonId, labelId;
LPCSTR buttonText;
public:
HWND createButton(HWND, int, int, int, int, int, int, LPCSTR);
void createLabel(HWND, int, int, int, int, LPCSTR);
void toggleButton(HWND);
};
HWND inputButton::createButton(HWND hWnd, int xpos, int ypos, int nCmdShow, int buttonState, int buttonId, int labelId, LPCSTR buttonText ) {
// Create button
HWND hButton1;
hButton1 = CreateWindowEx(
0, // _In_ DWORD dwExStyle
_T("BUTTON"), // _In_opt_ LPCTSTR lpClassName
_T("TEXT"), // _In_opt_ LPCTSTR lpWindowName
BS_ICON | WS_VISIBLE | WS_CHILD, // | SS_NOTIFY, // _In_ DWORD dwStyle
xpos, // _In_ int x
ypos, // _In_ int y
100, // _In_ int nWidth
100, // _In_ int nHeight
hWnd, // _In_opt_ HWND hWndParent
(HMENU)buttonId, // _In_opt_ HMENU hMenu
NULL, // _In_opt_ HINSTANCE hInstance
NULL // _In_opt_ LPVOID lpParam
);
// Assign image to button
Gdiplus::Bitmap* m_pBitmap;
HICON hicon;
if (buttonState == 0) { m_pBitmap = Gdiplus::Bitmap::FromFile(L"greySwitch.png"); }
if (buttonState == 1) { m_pBitmap = Gdiplus::Bitmap::FromFile(L"greenSwitch.png"); }
if (buttonState == 2) { m_pBitmap = Gdiplus::Bitmap::FromFile(L"redSwitch.png"); }
if (buttonState == 3) { m_pBitmap = Gdiplus::Bitmap::FromFile(L"yellowSwitch.png"); }
m_pBitmap->GetHICON(&hicon);
LRESULT lr = SendMessage(hButton1, BM_SETIMAGE, IMAGE_ICON, (LPARAM)hicon);
ShowWindow(hButton1, nCmdShow);
UpdateWindow(hButton1);
return (hButton1);
}
void inputButton::createLabel(HWND hWnd, int xpos, int ypos, int nCmdShow, int buttonId, LPCSTR buttonText) {
HWND hStatic;
int labelId = buttonId + 1000;
hStatic = CreateWindowEx(
WS_EX_TRANSPARENT, // _In_ DWORD dwExStyle
TEXT("STATIC"), // _In_opt_ LPCTSTR lpClassName
TEXT(buttonText), // _In_opt_ LPCTSTR lpWindowName
WS_CHILD | WS_VISIBLE | SS_OWNERDRAW | SS_CENTER, // _In_ DWORD dwStyle
xpos+10, // _In_ int x
ypos+40, // _In_ int y
80, // _In_ int nWidth
20, // _In_ int nHeight
hWnd, // _In_opt_ HWND hWndParent
(HMENU)labelId, // _In_opt_ HMENU hMenu
NULL, // _In_opt_ HINSTANCE hInstance
NULL // _In_opt_ LPVOID lpParam
);
if (hStatic == NULL) {
logText = "Failed to create label: " + std::to_string(labelId);
logTextLPSTR = const_cast<char *>(logText.c_str());
writeLog(logTextLPSTR);
}
else {
logText = "Created label: " + std::to_string(labelId);
logTextLPSTR = const_cast<char *>(logText.c_str());
writeLog(logTextLPSTR);
}
ShowWindow(hStatic, nCmdShow);
UpdateWindow(hStatic);
};
void inputButton::toggleButton(HWND hWnd) {
// need code here to know which button has been pressed.
};
.
.
.
.
.
inputButton button01;
HWND buttonHandle = button01.createButton(hWnd, 50, 50, 1, 0, AUX1_I01_BUTTON_ID, AUX1_I01_LABEL_ID, AUX1_I01_LABEL_TEXT );
button01.createLabel(buttonHandle, 50, 50, 1, AUX1_I01_BUTTON_ID, AUX1_I01_LABEL_TEXT);
|