Det ligner noget fra din teksteditor. Det gør heller ikke noget, overvejede selv at prøve med noget af den kode, men det for det første bruger du jo ikke en edit kontrol, og for det andet er pointere ikke specielt attraktive, synes jeg
Men jeg kan sgu ikke få det til at virke. Den indlæser ikke noget i mit tekstfelt (edit):
#include <windows.h>
#include <commctrl.h>
#include <richedit.h>
#include <fstream>
#define IDM_NY 001
#define IDM_AABEN 002
#define IDM_AFSLUT 004
/* Declare Windows procedure */
LRESULT CALLBACK WindowProcedure (HWND, UINT, WPARAM, LPARAM);
/* Make the class name into a global variable */
char szClassName [] = "CodeBlocksWindowsApp";
HWND hwnd, tool, edit;
RECT client;
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 ();
Text += "\\r\\n";
}
SendMessage (edit, WM_SETTEXT, 0, (LPARAM) Text.c_str ());
return true;
}
int WINAPI WinMain (HINSTANCE hThisInstance,
HINSTANCE hPrevInstance,
LPSTR lpszArgument,
int)
{
InitCommonControls ();
LoadLibrary ("Riched20.dll");
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 (NULL, IDI_APPLICATION);
wincl.hIconSm = LoadIcon (NULL, IDI_APPLICATION);
wincl.hCursor = LoadCursor (NULL, IDC_ARROW);
wincl.lpszMenuName = NULL; /* 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;
/* Create a menu */
HMENU filer = CreateMenu ();
HMENU main = CreateMenu ();
/* MenuItems til Filer */
AppendMenu (filer, MF_STRING, IDM_NY, "Nyt dokument");
AppendMenu (filer, MF_STRING, IDM_AABEN, "Åben...");
AppendMenu (filer, MF_STRING, 0, "Gem");
AppendMenu (filer, MF_STRING, 0, "Gem som...");
AppendMenu (filer, MF_SEPARATOR, 0, 0);
AppendMenu (filer, MF_STRING, IDM_AFSLUT, "Afslut");
/* Popups til hovedmenu */
AppendMenu (main, MF_POPUP, (WPARAM) filer, "&Filer");
/* The class is registered, let's create the program*/
hwnd = CreateWindowEx (
0, /* Extended possibilites for variation */
szClassName, /* Classname */
"Program", /* Title Text */
WS_OVERLAPPEDWINDOW, /* 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 */
main, /* No menu */
hThisInstance, /* Program Instance handler */
NULL /* No Window Creation data */
);
/* Make the window visible on the screen */
ShowWindow (hwnd, SW_SHOWNORMAL);
/* 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)
{
/* File handling dialogs */
OPENFILENAME ofn;
memset (&ofn, 0, sizeof (ofn));
ofn.lStructSize = sizeof (ofn);
ofn.lpstrFilter = "Tekstdokumenter\\0*.txt\\0Alle filer\\0*.*\\0";
ofn.hwndOwner = hwnd;
ofn.lpstrDefExt = "txt";
ofn.Flags = OFN_FILEMUSTEXIST | OFN_HIDEREADONLY;
switch (message) /* handle the messages */
{
case WM_CREATE:
/* Crate the toolbar */
tool = CreateWindowEx (
0,
TOOLBARCLASSNAME,
NULL,
WS_VISIBLE | WS_CHILD,
0, 0, 0, 0,
hwnd,
NULL, NULL, NULL
);
/* Add buttons to toolbar */
TBBUTTON knapper [3];
TBADDBITMAP bmp;
bmp.hInst = HINST_COMMCTRL;
bmp.nID = IDB_STD_SMALL_COLOR;
SendMessage (tool, TB_ADDBITMAP, 0, (LPARAM) &bmp);
memset (knapper, 0, sizeof (knapper));
SendMessage (tool, TB_BUTTONSTRUCTSIZE, (WPARAM) sizeof (TBBUTTON), 0);
/* Buttons */
knapper [0].fsStyle = TBSTYLE_BUTTON;
knapper [0].fsState = TBSTATE_ENABLED;
knapper [0].iBitmap = STD_FILENEW;
knapper [0].idCommand = IDM_NY;
knapper [1].fsStyle = TBSTYLE_BUTTON;
knapper [1].fsState = TBSTATE_ENABLED;
knapper [1].iBitmap = STD_FILEOPEN;
knapper [2].fsStyle = TBSTYLE_BUTTON;
knapper [2].fsState = TBSTATE_ENABLED;
knapper [2].iBitmap = STD_FILESAVE;
SendMessage (tool, TB_ADDBUTTONS, sizeof (knapper)/sizeof (TBBUTTON), (LPARAM) &knapper);
/* Create the RichEdit control */
edit = CreateWindowEx (
WS_EX_CLIENTEDGE,
RICHEDIT_CLASS,
NULL,
WS_CHILD | WS_VISIBLE | ES_MULTILINE | WS_VSCROLL | WS_HSCROLL /*| WS_OVERLAPPEDWINDOW*/,
0, 0, 0, 0,
hwnd,
NULL, NULL, NULL
);
SetFocus (edit);
/* Apply the RichEdit default font */
HFONT tnr = CreateFont (19, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, "Times New Roman");
SendMessage (edit, WM_SETFONT, (WPARAM) tnr, 0);
break;
case WM_SIZE:
GetClientRect (hwnd, &client);
SendMessage (tool, message, wParam, lParam);
MoveWindow (edit, 0, 28, client.right, client.bottom-28, true);
break;
case WM_COMMAND:
switch (LOWORD (wParam)) {
case IDM_NY:
SendMessage (edit, WM_SETTEXT, 0, 0);
SendMessage (hwnd, WM_SETTEXT, 0, (LPARAM) "Unavngivet");
break;
case IDM_AABEN:
if (GetOpenFileName (&ofn)) {
ReadFile (ofn.lpstrFile);
};
break;
case IDM_AFSLUT:
PostQuitMessage (0);
break;
};
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;
}