Hej.
Jeg har et problem med min Toolbar. Den gider ikke at blive vist. Herunder kan I se min kode. Bemærk dog at jeg helst bare vil have at vide hvad der er galt og hvad jeg skal ændre, fremfor I ændrer koden for mig. Det lærer jeg selv mest af
#include <windows.h>
#include <richedit.h>
#include <commctrl.h>
#include <algorithm>
#define CPP_ikon 001
#define Menu 002
#define Afslut 003
#define Skrifttype 004
#define Marker 005
#define Center 006
/* Declare Windows procedure */
LRESULT CALLBACK WindowProcedure (HWND, UINT, WPARAM, LPARAM);
/* Make the class name into a global variable */
char szClassName[] = "WindowsApp";
/* Declare the window handles - must be global in case of messages */
HWND editHwnd, windowHwnd, toolHwnd;
const char* editText;
/* Function to select font */
void FontDialog() {
CHOOSEFONT cf = {sizeof(CHOOSEFONT)};
LOGFONT lf;
cf.hwndOwner = windowHwnd;
cf.Flags = CF_EFFECTS | CF_FORCEFONTEXIST | CF_BOTH | CF_INITTOLOGFONTSTRUCT;
cf.lpLogFont = &lf;
cf.rgbColors = RGB(0, 0, 0);
cf.nFontType = REGULAR_FONTTYPE;
lf.lfItalic = false;
lf.lfWeight = FW_DONTCARE;
lf.lfStrikeOut = false;
lf.lfUnderline = false;
lf.lfHeight = 16;
if (ChooseFont(&cf)) {
HDC hDC = GetDC(editHwnd);
HFONT inF = CreateFontIndirect(&lf);
SendMessage(editHwnd, WM_SETFONT, (WPARAM) inF, true);
SetTextColor(hDC, cf.rgbColors);
}
}
/* The rects should be global so that the program doesn't have to create the variable each time
That would make it run faster, I think =) */
RECT winCoordinates;
int WINAPI WinMain (HINSTANCE hThisInstance,
HINSTANCE hPrevInstance,
LPSTR lpszArgument,
int nFunsterStil)
{
//InitCommonControls();
HMODULE hMod = LoadLibrary("riched20.DLL");
if(!hMod)
{
MessageBox(0, "Failed to load dll", "TextApp", MB_OK);
}
MSG messages; /* Here messages to the application are saved */
WNDCLASSEX wincl; /* Data structure for the windowclass */
/* The Window structure */
wincl.hInstance = hThisInstance;
wincl.lpszClassName = szClassName;
wincl.lpfnWndProc = WindowProcedure; /* This function is called by windows */
wincl.style = CS_DBLCLKS; /* Catch double-clicks */
wincl.cbSize = sizeof (WNDCLASSEX);
/* Use default icon and mouse-pointer */
wincl.hIcon = LoadIcon (GetModuleHandle(0), MAKEINTRESOURCE(CPP_ikon));
wincl.hIconSm = LoadIcon (GetModuleHandle(0), MAKEINTRESOURCE(CPP_ikon));
wincl.hCursor = LoadCursor (NULL, IDC_ARROW);
wincl.lpszMenuName = MAKEINTRESOURCE(Menu); /* No menu */
wincl.cbClsExtra = 0; /* No extra bytes after the window class */
wincl.cbWndExtra = 0; /* structure or the window instance */
/* Use Windows's default color as the background of the window */
wincl.hbrBackground = (HBRUSH) COLOR_BACKGROUND;
/* Register the window class, and if it fails quit the program */
if (!RegisterClassEx (&wincl))
return 0;
/* The class is registered, let's create the program*/
windowHwnd = CreateWindowEx (
0, /* Extended possibilites for variation */
szClassName, /* Classname */
"Editor", /* Title Text */
WS_OVERLAPPEDWINDOW | WS_MAXIMIZE, /* default window */
CW_USEDEFAULT, /* Windows decides the position */
CW_USEDEFAULT, /* where the window ends up on the screen */
544, /* The programs width */
375, /* and height in pixels */
HWND_DESKTOP, /* The window is a child-window to desktop */
NULL, /* No menu */
hThisInstance, /* Program Instance handler */
NULL /* No Window Creation data */
);
/* Make the window visible on the screen */
ShowWindow (windowHwnd, nFunsterStil);
/* Run the message loop. It will run until GetMessage() returns 0 */
while (GetMessage (&messages, NULL, 0, 0))
{
/* Translate virtual-key messages into character messages */
TranslateMessage(&messages);
/* Send message to WindowProcedure */
DispatchMessage(&messages);
}
/* The program return-value is 0 - The value that PostQuitMessage() gave */
return messages.wParam;
}
/* This function is called by the Windows function DispatchMessage() */
LRESULT CALLBACK WindowProcedure (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
switch (message) /* handle the messages */
{
case WM_COMMAND: {
switch(wParam) {
case Afslut: PostQuitMessage(0); break;
case Skrifttype: FontDialog(); break;
case Marker: {
int editLen = SendMessage(editHwnd, WM_GETTEXTLENGTH, wParam, lParam);
if (editLen == 0) MessageBox(hwnd, "Du skal skrive noget tekst før du kan markere det ;)", "Fejl", MB_OK);
else SendMessage(editHwnd, EM_SETSEL, 0, editLen);
} break;
case Center: {
PARAFORMAT p;
p.cbSize = sizeof(PARAFORMAT);
p.wAlignment = PFA_CENTER;
SendMessage(editHwnd, EM_SETPARAFORMAT, wParam, (LPARAM) &p);
} break;
}
} break;
case WM_CREATE: {
DWORD editFlags = WS_CHILD | WS_VISIBLE | ES_MULTILINE | WS_HSCROLL | WS_VSCROLL | ES_NOHIDESEL | ES_WANTRETURN;
/* Create the toolbar */
toolHwnd = CreateWindowEx(
0,
TOOLBARCLASSNAME,
NULL,
WS_CHILD | WS_VISIBLE | WS_BORDER,
0, 0, 0, 0,
windowHwnd,
NULL, NULL, NULL);
SendMessage(toolHwnd, TB_BUTTONSTRUCTSIZE, (WPARAM) sizeof(TBBUTTON), 0);
TBADDBITMAP bMP;
bMP.hInst = HINST_COMMCTRL;
bMP.nID = IDB_STD_SMALL_COLOR;
SendMessage(toolHwnd, TB_ADDBITMAP, (WPARAM) 1, (LPARAM) &bMP);
TBBUTTON knap[1];
memset(&knap, 0, sizeof(knap));
int idx = SendMessage(toolHwnd, TB_ADDSTRING, 0,(LPARAM) "Fed");
knap[0].iString = idx;
knap[0].fsState = TBSTATE_ENABLED;
knap[0].fsStyle = TBSTYLE_BUTTON;
SendMessage(toolHwnd, TB_ADDBUTTONS, (WPARAM) 1, (LPARAM) &knap);
ShowWindow(toolHwnd, SW_SHOW);
/* Create the edit control */
editHwnd = CreateWindowEx(
WS_EX_CLIENTEDGE, // Window type is "edit"
RICHEDIT_CLASS,
NULL,
WS_CHILD | WS_VISIBLE | ES_MULTILINE | WS_HSCROLL | WS_VSCROLL | ES_NOHIDESEL,
0, 0, // The positions of the window
0, 0, // The dimensions of the window,
hwnd, // Owner handle,
NULL, NULL, NULL); // Some other bullshit
//InitializeFlatSB(editHwnd);
/* The system font just isn't good enough */
char skrifttype[256] = "Times New Roman";
HFONT newFont = CreateFont(19, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, skrifttype);
SendMessage(editHwnd, WM_SETFONT, (WPARAM) newFont, true);
SetFocus(editHwnd);
/* Apply some new settings for the scrollbars in the edit control */
//FlatSB_SetScrollProp(editHwnd, RGB(0, 0, 0), true);
} break;
case WM_SIZE: {
GetClientRect(hwnd, &winCoordinates);
MoveWindow(editHwnd, 0, 20, winCoordinates.right, winCoordinates.bottom-20, true);
MoveWindow(toolHwnd, 0, 0, winCoordinates.right, 100, true);
SendMessage(editHwnd, message, wParam, lParam);
} break;
case WM_DESTROY:
PostQuitMessage (0); /* send a WM_QUIT to the message queue */
break;
default: /* for messages that we don't deal with */
return DefWindowProc (hwnd, message, wParam, lParam);
}
return 0;
}
Jeg ved der er mange overflødige ting i koden, men nu er jeg også lige begyndt på C++, så bare ignorer det
Ps. Hvis du ser denne tråd, Bertel Brander, så skal du vide at jeg har læst din tutorial om Toolbars på damb
Men det fik jeg desværre ikke så meget ud af.