Hi,
I need your help again, I think.
With help from some experts here in the forum, my first userdefined menue is nearly ready.
What is missing:
Reading the measurements of the buttons out of my textfile and
writing the text on the buttons, written in the textfile too.
This is what I´ve done until now:
#include <windows.h>
#include <wchar.h>
/** Globals ********************************************************/
#define WIN32_LEAN_AND_MEAN
#define IDM_BTN1 6010
#define N 14 // Number of Buttons, incl. 6 rows comments in textfile
#define LINELEN_MAX 200
#define BUTTONMAX 10 //Number of Buttons, shown in menue
BOOL WINAPI CheckRemoteDebuggerPresent( HANDLE hProcess, PBOOL pbDebuggerPresent ){ // Routine für CE5!
if( pbDebuggerPresent != NULL )
*pbDebuggerPresent = FALSE;
return TRUE;
}
LRESULT WINAPI WndProc(HWND, UINT, WPARAM, LPARAM);
BOOL OnCreate(HWND hwnd, LPCREATESTRUCT lpCreateStruct);
void OnDestroy(HWND hwnd);
void OnCommand(HWND hwnd, int id, HWND hwndCtl, UINT codeNotify);
void OnDrawItem(HWND hwnd, const DRAWITEMSTRUCT * lpDrawItem);
TCHAR szAppName[] = L"GPW_Menue";
TCHAR szFrameClass[] = L"GPW_Class";
TCHAR fname[MAX_PATH];
char fname_2[MAX_PATH];
wchar_t *fmtw = (L"%200l[^\n]\n %200l[^\n]\n %200l[^\n]\n");
HANDLE hInst;
HWND hFrame;
HWND hWndBtn0, hWndBtn1, hWndBtn2, hWndBtn3, hWndBtn4, hWndBtn5;
HWND hWndBtn6, hWndBtn7, hWndBtn8, hWndBtn9, hWndBtn10;
typedef struct button Button;
struct button
{
wchar_t text[LINELEN_MAX+1];
wchar_t pic[LINELEN_MAX+1];
wchar_t link[LINELEN_MAX+1];
};
Button buttons[N] = {0};
HBITMAP hBM[BUTTONMAX];
HBITMAP background;
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPTSTR lpCmdLine, int nCmdShow)
{
WNDCLASS wc;
MSG msg;
wc.style = CS_HREDRAW | CS_VREDRAW ;
wc.lpfnWndProc = (WNDPROC)WndProc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = hInstance;
wc.hIcon = 0;
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
wc.lpszMenuName = NULL;
wc.lpszClassName = szFrameClass;
wc.style = CS_VREDRAW | CS_HREDRAW;
if (!RegisterClass(&wc)) {
MessageBox(NULL,L"Error with RegisterClass",0,0);
return 0;
}
hInst = hInstance;
hFrame = CreateWindowEx(WS_EX_CAPTIONOKBTN, szFrameClass, szAppName,
WS_VISIBLE,
CW_USEDEFAULT, CW_USEDEFAULT,
CW_USEDEFAULT, CW_USEDEFAULT,
NULL, NULL, hInst, NULL);
if(!hFrame) return 0;
ShowWindow(hFrame, nCmdShow);
UpdateWindow(hFrame);
while(GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return msg.wParam;
}
LRESULT WINAPI WndProc(HWND hwnd, UINT wMsg, WPARAM wParam, LPARAM lParam)
{
switch(wMsg) {
case WM_COMMAND: return OnCommand(hwnd,(int)LOWORD(wParam),(HWND)lParam,(UINT)HIWORD(wParam)),0;
case WM_CREATE: return OnCreate(hwnd,(LPCREATESTRUCT)lParam),0;
case WM_DESTROY: return OnDestroy(hwnd),0;
case WM_DRAWITEM: return (OnDrawItem(hwnd,(const DRAWITEMSTRUCT*)lParam),0);
default:
return DefWindowProc(hwnd, wMsg, wParam, lParam);
}
}
BOOL OnCreate(HWND hwnd, LPCREATESTRUCT lpCreateStruct)
int n = 3, // Anzahl der zu einem Datensatz zugehörigen Zeilen.
i = 0; // Index und Zähler fürs Strukturarray.
FILE* fp = fopen ( fname_2, "r" );
if ( fp == NULL ) // Wenn die Textdatei fehlt, bringe Fehlermeldung
{
MessageBox(NULL, L"Zugehörige Textdatei fehlt!", L"Fehler", MB_SETFOREGROUND);
DestroyWindow(hwnd);
PostQuitMessage(0);
}
while ( !feof(fp) )
{
if ( i == N ){
MessageBox(NULL, L"Zugehörige Textdatei ist beschädigt! ", L"Fehler", MB_SETFOREGROUND);
DestroyWindow(hwnd);
PostQuitMessage(0);
return (0);
break;
}
if (n != fwscanf(fp, fmtw, &buttons[i].text, buttons[i].pic, buttons[i].link)){
MessageBox(NULL, L"Zugehörige Textdatei ist nicht vollständig! ", L"Fehler", MB_SETFOREGROUND);
DestroyWindow(hwnd);
PostQuitMessage(0);
break;
}
i++;
}
fclose ( fp );
OutputDebugString(L"WM_CREATE");
/*********** Buttons**************/
/***********Row 1***************/
if (_wfopen(buttons[0].link , L"r") != 0){ // prüft ob die auszuführende Exe vorhanden ist. Falls nicht, wird der Button nicht angezeigt.
hWndBtn0 = CreateWindow( L"BUTTON", L"Button 1", WS_VISIBLE | BS_OWNERDRAW, 0, 33, 239, 62, hwnd, (HMENU)IDM_BTN1, hInst, NULL);
hBM[0] = SHLoadDIBitmap(buttons[0].pic);
}
if (_wfopen(buttons[1].link , L"r") != 0){
hWndBtn1 = CreateWindow( L"BUTTON", L"Button 2", WS_VISIBLE | BS_OWNERDRAW, 241, 33, 239, 62, hwnd, (HMENU)(IDM_BTN1+1), hInst, NULL);
hBM[1] = SHLoadDIBitmap(buttons[1].pic);
}
/*
following some other buttons
*/
/*********** Background *********************/
HWND hwndback = CreateWindow( L"STATIC", NULL, WS_VISIBLE | WS_CHILD |SS_BITMAP, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,CW_USEDEFAULT, hwnd, NULL, hInst, NULL);
background = SHLoadDIBitmap(buttons[11].pic);
SendMessage(hwndback,(UINT)STM_SETIMAGE, (WPARAM)IMAGE_BITMAP,(LPARAM)background);
}
void OnDestroy(HWND hwnd)
{
OutputDebugString(L"WM_DESTROY");
for (int i = 0; i < BUTTONMAX; i++)
if (hBM[i]) DeleteObject(hBM[i]);
PostQuitMessage(0);
}
void OnCommand(HWND hwnd, int id, HWND hwndCtl, UINT codeNotify)
{
switch(id)
{
/********************Row 1***************/
case IDM_BTN1:
CreateProcess(buttons[0].link,NULL,0,0,FALSE,0, 0, 0, NULL, NULL );
PostMessage(hwnd, WM_DESTROY, 0, 0L);
return;
case IDM_BTN1+1:
CreateProcess(buttons[1].link, NULL,0,0,FALSE,0, 0,0,NULL,NULL );
PostMessage(hwnd, WM_DESTROY, 0, 0L);
return;
/*
following some other buttons
*/
}
}
void OnDrawItem(HWND hwnd, const DRAWITEMSTRUCT * lpDrawItem)
{
RECT rc;
HDC hdc;
HDC hdcMem;
HBITMAP hBM0;
BITMAP bm;
int nIdx;
if (lpDrawItem->CtlID < IDM_BTN1 || lpDrawItem->CtlID > (IDM_BTN1+BUTTONMAX))
return;
nIdx = lpDrawItem->CtlID-IDM_BTN1;
rc = lpDrawItem->rcItem;
hdc = lpDrawItem->hDC;
hBM0 = hBM[nIdx];
hdcMem = CreateCompatibleDC(hdc);
SelectObject(hdcMem, hBM0);
GetObject(hBM0, sizeof(bm), &bm);
if (lpDrawItem->itemState & ODS_SELECTED) { // clicked
BitBlt(hdc, -2, -2, bm.bmWidth-0, bm.bmHeight-0, hdcMem, -2, -2, SRCCOPY); // paints the button
DrawEdge(lpDrawItem->hDC, &rc, EDGE_SUNKEN, BF_TOPLEFT);
}
else {
BitBlt(hdc, 0, 0, bm.bmWidth, bm.bmHeight, hdcMem, 0, 0, SRCCOPY); // normal Button
// DrawEdge(lpDrawItem->hDC, &rc, EDGE_RAISED, BF_RECT); // not needed this time
}
DeleteDC(hdcMem);
}
Would you please help me again with completing the menue?
Thank you!
Greetings Peter