Hej,
Når jeg skriver noget tekst i min EDIT kontrol og trykker på slet-knappen (altså "backspace"), så går markøren et tegn tilbage, men uden at slette det tegn, det står ved. Det er lidt svært at forklare, men det kan være I kan se en fejl i min kode:
#include <windows.h>
#include <fstream>
#include <string>
#include <iostream>
// -----------------------------------
// Variables
// -----------------------------------
bool draw_ed = FALSE;
char fileName [256];
CHOOSEFONT cf;
COLORREF ed_clr;
HDC edit_device;
HINSTANCE instance;
HFONT CN = CreateFont (16, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, "Courier New");
HMENU MainMenu, Files, Formatting;
HWND MainWindow, EditWindow; // Windows
LOGFONT lf;
MSG message;
OPENFILENAME of;
void TestFunc () { MessageBox (NULL, "s", "s", MB_OK); };
// -----------------------------------
// Enumerations
// -----------------------------------
enum {
enum_open = 001,
enum_save = 002,
enum_formatting = 003,
enum_wordwrap = 004,
menu_edit = 005,
};
// -----------------------------------
// Program functions interface
// -----------------------------------
int ShowMsg (LPSTR txt); // Show simple MessageBox ()
bool ReadFile (const char *aFileName);
LRESULT CALLBACK MsgDispatch (HWND hwnd, unsigned int message, WPARAM wParam, LPARAM lParam);
// -----------------------------------
// Function declarations
// -----------------------------------
int ShowMsg (LPSTR txt)
{
return ((int)MessageBox (NULL, txt, "Meddelelse", MB_OK));
};
int WINAPI WinMain (HINSTANCE hInstance,
HINSTANCE,
LPSTR,
int)
{
instance = hInstance;
// Create the window menus
// Code must be inserted here due to the Menu name parameter in the CreateWindowEx function
MainMenu = CreateMenu ();
Files = CreateMenu ();
Formatting = CreateMenu ();
// Append menuitems for Files menu
AppendMenu (Files, MF_STRING, enum_open, "Åben");
AppendMenu (Files, MF_STRING, enum_save, "Gem");
// Append menuitems for Formatting menu
AppendMenu (Formatting, MF_STRING, enum_formatting, "Skrifttype");
AppendMenu (Formatting, MF_STRING, enum_wordwrap, "Tekstombrydning");
// Append menuitems for MainMenu menu
AppendMenu (MainMenu, MF_POPUP, (unsigned int)Files, "&Filer");
AppendMenu (MainMenu, MF_POPUP, (unsigned int)Formatting, "F&ormater");
// Register a new class for the window
WNDCLASSEX cls;
cls.cbSize = sizeof (WNDCLASSEX);
cls.cbWndExtra = 0;
cls.cbClsExtra = 0;
cls.lpfnWndProc = MsgDispatch;
cls.lpszMenuName = NULL;
cls.lpszClassName = "MAINWINDOW";
cls.hIcon = LoadIcon (NULL, IDI_APPLICATION);
cls.hIconSm = LoadIcon (NULL, IDI_APPLICATION);
cls.hCursor = LoadCursor (NULL, IDC_ARROW);
cls.hbrBackground = (HBRUSH)COLOR_BACKGROUND;
cls.hInstance = instance;
cls.style = 0;
if (!RegisterClassEx (&cls)) {
ShowMsg ("Registration failed!");
return 0; // Exit program in case of errors
};
// Create and show main window
MainWindow = CreateWindowEx (
WS_EX_CLIENTEDGE,
cls.lpszClassName, // Make sure to use the correct classname for the window
"Notesblok",
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, CW_USEDEFAULT, // Positions
CW_USEDEFAULT, CW_USEDEFAULT, // Dimensions
NULL, MainMenu, instance, NULL
);
if (!MainWindow) {
ShowMsg ("Creation failed!");
return 0;
} else ShowWindow (MainWindow, SW_SHOWMAXIMIZED);
// -----------------------------------
// Dialog structures
// -----------------------------------
// Due to the window handle, our dialog structures must be set here
// Open filename structure
of.lStructSize = sizeof (OPENFILENAME);
of.hwndOwner = MainWindow;
of.hInstance = instance;
of.lpstrFilter = "Tekstdokumenter\\0*.txt\\0Alle filer\\0*.*\\0";
of.lpstrTitle = "Vælg det dokument, du vil åbne :o)";
of.lpstrFile = fileName;
of.nMaxFile = sizeof (fileName);
of.Flags = OFN_FILEMUSTEXIST | OFN_HIDEREADONLY;
of.lpstrDefExt = "txt";
// Choose font structure
cf.lStructSize = sizeof (CHOOSEFONT);
cf.hwndOwner = MainWindow;
cf.lpLogFont = &lf;
cf.iPointSize = 16;
cf.Flags = CF_BOTH | CF_EFFECTS | CF_INITTOLOGFONTSTRUCT | CF_FORCEFONTEXIST;
cf.rgbColors = RGB (0, 0, 0);
cf.hInstance = instance;
cf.nFontType = REGULAR_FONTTYPE;
// Translate and handle (dispatch) messages
// The dispatching function calls the WindowProcedure function to handle the messages
// Condition is that the messages value must be greater than 0
// Because errormessages will have the value of -1 which in boolean is true
while (GetMessage (&message, NULL, 0, 0) > 0)
{
TranslateMessage (&message);
DispatchMessage (&message);
};
return message.wParam;
};
// -----------------------------------
// Function to read a given file
// Thanks to Bertel Brander for this piece of code
// -----------------------------------
bool ReadFile (const char *aFileName)
{
std::ifstream File (aFileName);
if (!File)
return false;
std::string Line, Text;
while (std::getline (File, Line))
Text += Line.c_str ();
SendMessage (EditWindow, WM_SETTEXT, 0, (LPARAM)Text.c_str ());
return true;
};
// -----------------------------------
// Message dispatcher
// -----------------------------------
LRESULT CALLBACK MsgDispatch (HWND hwnd,
unsigned int message,
WPARAM wParam,
LPARAM lParam)
{
switch (message)
{
case WM_CREATE:
EditWindow = CreateWindow (
"edit",
NULL,
WS_CHILD | WS_VISIBLE | ES_MULTILINE | WS_HSCROLL | WS_VSCROLL | ES_NOHIDESEL,
0, 0, 0, 0, // No dimensions needed; window will be maximized
hwnd, (HMENU)menu_edit, NULL, NULL
);
SendMessage (EditWindow, WM_SETFONT, (WPARAM)CN, NULL);
SendMessage (EditWindow, EM_SETMARGINS, EC_LEFTMARGIN | EC_RIGHTMARGIN, 2);
SetFocus (EditWindow);
break;
case WM_COMMAND:
switch (LOWORD (wParam)) {
case enum_open:
of.lpstrTitle = "Vælg det dokument, du vil åbne :o)";
if (GetOpenFileName (&of))
ReadFile (of.lpstrFile);
break;
case enum_save:
of.lpstrTitle = "Vælg hvor l*rtet skal gemmes";
if (GetSaveFileName (&of)) {
std::ofstream saveConn (of.lpstrFile);
int txtLen = SendMessage (EditWindow, WM_GETTEXTLENGTH, NULL, NULL);
saveConn << SendMessage (EditWindow, WM_GETTEXT, txtLen, txtLen);
};
break;
case enum_formatting:
if (ChooseFont (&cf)) {
// Create and apply new font
HFONT cfFont = CreateFont (
lf.lfHeight,
lf.lfWidth,
lf.lfEscapement,
lf.lfOrientation,
lf.lfWeight,
lf.lfItalic,
lf.lfUnderline,
lf.lfStrikeOut,
lf.lfCharSet,
lf.lfOutPrecision,
lf.lfClipPrecision,
lf.lfQuality,
lf.lfPitchAndFamily,
lf.lfFaceName
);
SendMessage (EditWindow, WM_SETFONT, (WPARAM)cfFont, TRUE); // Redraw immediately (TRUE)
// Create and apply new color
draw_ed = TRUE;
ed_clr = cf.rgbColors;
SendMessage (MainWindow, WM_CTLCOLOREDIT, (WPARAM)edit_device, TRUE);
};
break;
};
break;
case WM_SIZE:
MoveWindow (EditWindow, 0, 0, LOWORD (lParam), HIWORD (lParam), TRUE);
break;
case WM_SETFOCUS:
SetFocus (EditWindow);
break;
case WM_CTLCOLOREDIT:
if (draw_ed) {
edit_device = (HDC)wParam;
SetTextColor (edit_device, ed_clr);
};
HDC device_bk = (HDC)wParam;
SetBkMode (device_bk, TRANSPARENT);
return ((LRESULT)CreateSolidBrush (RGB (255, 255, 255)));
break;
case WM_DESTROY:
PostQuitMessage (0); // Send WM_QUIT to the queue
break;
default:
return DefWindowProc (hwnd, message, wParam, lParam);
};
};