Hej.
Jeg har en LISTBOX i mit program, som lister alle installerede skrifttyper. Men når man klikker rundt på den liste et par gange, så lukker programmet ned helt af sig selv.
Gennem nogle tests lokaliserede jeg fejl til at ligge her:
char szBuffer [128];
switch (HIWORD (wParam))
{
case LBN_SELCHANGE:
SendMessage (List, LB_GETTEXT,
SendMessage (List, LB_GETCURSEL, 0, 0),
(LPARAM) &szBuffer
);
SS << szBuffer;
SS >> S;
S.erase (strlen (szBuffer) - 4);
InvalidateRect (Window, NULL, TRUE);
break;
}
Altså i min WM_COMMAND. Jeg tror, det har noget med min STRINGSTREAM at gøre. Hvis jeg putter de sidste 4 linjer inden BREAK i /* kommentar */, så lukker programmet ikke ned, men så virker det naturligvis heller ikke som det skal.
Hele min kode ser således ud:
#include <windows.h>
#include <commctrl.h>
#include <string>
#include <sstream>
#define IDD_EDIT 1001
#define IDD_LIST 1002
#define IDF_1 1003
#define IDF_2 1004
#define IDF_3 1005
#define IDF_4 1006
#define IDF_5 1007
#define IDF_6 1008
#define IDF_7 1009
using std::string;
using std::stringstream;
HWND Main, List, Edit;
HINSTANCE Instance;
string S;
stringstream SS;
LRESULT CALLBACK WndProc (
HWND Window,
UINT Message,
WPARAM wParam,
LPARAM lParam
)
{
static PAINTSTRUCT ps;
static HDC hdc;
static RECT r;
static HBRUSH brush;
static HPEN pen;
static HFONT font;
static HFONT TempFont;
static int iIndex, TempSize = 16;
static char szWindowText [128];
static char szTemplate [10000];
switch (Message)
{
case WM_CREATE:
List = CreateWindowEx (
WS_EX_CLIENTEDGE,
"LISTBOX",
"",
WS_CHILD | WS_VISIBLE | LBS_SORT | LBS_NOTIFY | WS_VSCROLL | LBS_STANDARD,
0, 0, 0, 0,
Window, (HMENU) IDD_LIST, Instance, NULL
);
Edit = CreateWindowEx (
WS_EX_CLIENTEDGE,
"EDIT",
"",
WS_CHILD | WS_VISIBLE | ES_NOHIDESEL,
210, 5, 300, 20,
Window, (HMENU) IDD_EDIT, Instance, NULL
);
SetCurrentDirectory ("C:/WINDOWS/Fonts");
iIndex = SendMessage (
List,
LB_DIR,
DDL_ARCHIVE | DDL_SYSTEM | DDL_DIRECTORY | DDL_READONLY | DDL_READWRITE | DDL_EXCLUSIVE | DDL_HIDDEN,
(LPARAM) "*.ttf"
);
brush = CreateSolidBrush (RGB (255, 255, 255));
pen = CreatePen (PS_NULL, 0, RGB (255, 255, 255));
font = CreateFont (16, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, "Verdana");
wsprintf (szWindowText, "%d skrifttyper installeret", iIndex + 1);
SetWindowText (Window, szWindowText);
SetFocus (Edit);
break;
case WM_SIZE:
GetClientRect (Window, &r);
MoveWindow (
List,
r.left + 5, r.top + 30,
150, r.bottom - 30,
TRUE
);
MoveWindow (
Edit,
55, 5, r.right - 60, 20,
TRUE
);
InvalidateRect (Window, NULL, TRUE);
break;
case WM_CHAR:
GetDlgItemText (Window, IDD_EDIT, szTemplate, sizeof (szTemplate));
InvalidateRect (Window, NULL, TRUE);
break;
case WM_COMMAND:
{
char szBuffer [128];
switch (HIWORD (wParam))
{
case LBN_SELCHANGE:
SendMessage (List, LB_GETTEXT,
SendMessage (List, LB_GETCURSEL, 0, 0),
(LPARAM) &szBuffer
);
SS << szBuffer;
SS >> S;
S.erase (strlen (szBuffer) - 4);
InvalidateRect (Window, NULL, TRUE);
break;
}
switch (LOWORD (wParam))
{
case IDF_1:
TempSize = 16;
InvalidateRect (Window, NULL, TRUE);
break;
case IDF_2:
TempSize = 20;
InvalidateRect (Window, NULL, TRUE);
break;
case IDF_3:
TempSize = 25;
InvalidateRect (Window, NULL, TRUE);
break;
case IDF_4:
TempSize = 30;
InvalidateRect (Window, NULL, TRUE);
break;
case IDF_5:
TempSize = 35;
InvalidateRect (Window, NULL, TRUE);
break;
case IDF_6:
TempSize = 40;
InvalidateRect (Window, NULL, TRUE);
break;
case IDF_7:
TempSize = 45;
InvalidateRect (Window, NULL, TRUE);
break;
}
break;
case WM_PAINT:
hdc = BeginPaint (Window, &ps);
SelectObject (hdc, brush);
SelectObject (hdc, pen);
SelectObject (hdc, font);
SetBkMode (hdc, TRANSPARENT);
TextOut (
hdc,
r.left + 5, 7,
"Tekst:", 6
);
Rectangle (
hdc,
r.left + 155, r.top + 31,
r.right - 5, r.bottom - 10
);
try
{
TempFont = CreateFont (TempSize, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, S.c_str ());
SelectObject (hdc, TempFont);
}
catch (char *msg)
{
MessageBox (Window, msg, "Fejl", MB_OK);
}
TextOut (
hdc,
r.left + 180, r.top + 50,
szTemplate, GetWindowTextLength (Edit)
);
EndPaint (Window, &ps);
}
break;
case WM_DESTROY:
DeleteObject (brush);
DeleteObject (pen);
DeleteObject (font);
DeleteObject (TempFont);
PostQuitMessage (0);
break;
default:
return DefWindowProc (Window, Message, wParam, lParam);
}
}
int WINAPI WinMain
(
HINSTANCE _Instance,
HINSTANCE,
LPSTR,
int
)
{
Instance = _Instance;
WNDCLASSEX wcl;
MSG Message;
memset (&wcl, 0, sizeof (WNDCLASSEX));
InitCommonControls ();
wcl.cbSize = sizeof (WNDCLASSEX);
wcl.cbClsExtra = 0;
wcl.cbWndExtra = 0;
wcl.hInstance = Instance;
wcl.hIcon = LoadIcon (NULL, IDI_APPLICATION);
wcl.hIconSm = LoadIcon (NULL, IDI_APPLICATION);
wcl.hCursor = LoadCursor (NULL, IDC_ARROW);
wcl.hbrBackground = (HBRUSH) COLOR_BACKGROUND;
wcl.lpfnWndProc = WndProc;
wcl.lpszMenuName = NULL;
wcl.lpszClassName = "MAINWINDOW";
wcl.style = 0;
RegisterClassEx (&wcl);
//
// Menu
//
HMENU Menu = CreateMenu ();
HMENU Files = CreateMenu ();
HMENU Format = CreateMenu ();
HMENU Size = CreateMenu ();
AppendMenu (Files, MF_STRING, 0, "Opdater");
AppendMenu (Files, MF_SEPARATOR, 0, 0);
AppendMenu (Files, MF_STRING, 0, "Afslut\\tAlt+F4");
AppendMenu (Size, MF_STRING, IDF_1, "1");
AppendMenu (Size, MF_STRING, IDF_2, "2");
AppendMenu (Size, MF_STRING, IDF_3, "3");
AppendMenu (Size, MF_STRING, IDF_4, "4");
AppendMenu (Size, MF_STRING, IDF_5, "5");
AppendMenu (Size, MF_STRING, IDF_6, "6");
AppendMenu (Size, MF_STRING, IDF_7, "7");
AppendMenu (Format, MF_POPUP, (UINT) Size, "Skriftstørrelse");
AppendMenu (Menu, MF_POPUP, (UINT) Files, "&Filer");
AppendMenu (Menu, MF_POPUP, (UINT) Format, "F&ormater");
Main = CreateWindow (
"MAINWINDOW",
"Vindue",
WS_OVERLAPPEDWINDOW,
0, 0, 500, 500,
GetDesktopWindow (), Menu,
Instance, NULL
);
ShowWindow (Main, SW_SHOW);
UpdateWindow (Main);
while (GetMessage (&Message, NULL, 0, 0) > 0)
{
TranslateMessage (&Message);
DispatchMessage (&Message);
if (Message.message == WM_CHAR && Message.hwnd == Edit)
SendMessage (Main, Message.message, Message.wParam, Message.lParam);
}
return Message.wParam;
}