steckel Temat założony przez niniejszego użytkownika |
WinApi - znak końca lini » 2009-09-12 21:32:48 Znak końca linii wstawiam przed zmienną 'nick' typu string i mam problem przy zapisie, bo ten znak tak jakby jest przenoszony za zmienną 'nick'. Kod używa standardowych bibliotek, więc każdy może go przetestować. #include <windows.h> #include <string>
void InitWindow( int width, int height, std::string name ); LRESULT CALLBACK WndProc( HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam ); LRESULT CALLBACK NewWndProc( HWND hwnd, UINT mesg, WPARAM wParam, LPARAM lParam );
void Copy(); void LoadFile(); void Chat(); void Load(); void SaveFile();
HWND hWnd; HWND hMessageBox; HWND hChatBox; WNDPROC g_OldWndProc;
using namespace std;
string text; string pathOfFile; string nick; string message; string textAktual; int proces = 0;
int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow ) { InitWindow( 400, 500, "Chat" ); MSG message; while( GetMessage( & message, NULL, 0, 0 ) ) { TranslateMessage( & message ); DispatchMessage( & message ); } return message.wParam; }
void InitWindow( int width, int height, std::string name ) { HINSTANCE hProg = GetModuleHandle( 0 ); WNDCLASSEX wc; wc.cbSize = sizeof( WNDCLASSEX ); wc.style = 0; wc.lpfnWndProc = WndProc; wc.cbClsExtra = 0; wc.cbWndExtra = 0; wc.hInstance = hProg; wc.hIcon = LoadIcon( NULL, IDI_APPLICATION ); wc.hCursor = LoadCursor( NULL, IDC_ARROW ); wc.hbrBackground =( HBRUSH )( COLOR_WINDOW + 1 ); wc.lpszMenuName = NULL; wc.lpszClassName = "WndClass"; wc.hIconSm = LoadIcon( NULL, IDI_APPLICATION ); RegisterClassEx( & wc ); hWnd = CreateWindowEx( 0, "WndClass", name.c_str(), WS_OVERLAPPEDWINDOW, 0, 0, width, height, 0, 0, hProg, 0 ); ShowWindow( hWnd, 5 ); UpdateWindow( hWnd ); hMessageBox = CreateWindowEx( WS_EX_CLIENTEDGE, "EDIT", NULL, WS_CHILD | WS_VISIBLE | WS_BORDER | WS_VSCROLL | ES_MULTILINE | ES_AUTOVSCROLL, 5, height - 120, width - 20, 80, hWnd, NULL, hProg, NULL ); hChatBox = CreateWindowEx( WS_EX_CLIENTEDGE, "EDIT", NULL, WS_CHILD | WS_VISIBLE | WS_BORDER | WS_VSCROLL | ES_MULTILINE | ES_AUTOVSCROLL, 5, 5, width - 20, height - 140, hWnd, NULL, hProg, NULL ); g_OldWndProc =( WNDPROC ) SetWindowLong( hMessageBox, GWL_WNDPROC,( LONG ) NewWndProc ); SetWindowText( hMessageBox, "twoj nick" ); textAktual = ""; }
LRESULT CALLBACK WndProc( HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam ) { Load(); switch( msg ) { case WM_CLOSE: DestroyWindow( hwnd ); break; case WM_DESTROY: PostQuitMessage( 0 ); break; case WM_KEYDOWN: switch(( int ) wParam ) { case VK_RETURN: Copy(); Chat(); break; case VK_ESCAPE: DestroyWindow( hwnd ); break; } break; default: return DefWindowProc( hwnd, msg, wParam, lParam ); } return 0; }
void Copy() { DWORD dlugosc = GetWindowTextLength( hMessageBox ); LPSTR Bufor =( LPSTR ) GlobalAlloc( GPTR, dlugosc + 1 ); GetWindowText( hMessageBox, Bufor, dlugosc + 1 ); message = Bufor; GlobalFree( Bufor ); }
LRESULT CALLBACK NewWndProc( HWND hwnd, UINT mesg, WPARAM wParam, LPARAM lParam ) { switch( mesg ) { case WM_KEYDOWN: { CallWindowProc( WndProc, hwnd, mesg, wParam, lParam ); } break; } return CallWindowProc( g_OldWndProc, hwnd, mesg, wParam, lParam ); }
void LoadFile() { LPSTR Bufor; DWORD dwRozmiar, dwPrzeczyt; HANDLE hPlik; hPlik = CreateFile( pathOfFile.c_str(), GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_ALWAYS, 0, NULL ); dwRozmiar = GetFileSize( hPlik, NULL ); Bufor =( LPSTR ) GlobalAlloc( GPTR, dwRozmiar + 1 ); ReadFile( hPlik, Bufor, dwRozmiar, & dwPrzeczyt, NULL ); Bufor[ dwRozmiar ] = 0; text = Bufor; GlobalFree( Bufor ); CloseHandle( hPlik ); } void Chat() { switch( proces ) { case 0: nick = message; SetWindowText( hMessageBox, "" ); SetWindowText( hMessageBox, "temat" ); proces++; break; case 1: pathOfFile = message; SetWindowText( hMessageBox, "" ); proces++; break; default: SaveFile(); SetWindowText( hMessageBox, "" ); break; } }
void Load() { LoadFile(); if( textAktual != text ) { SetWindowText( hChatBox, text.c_str() ); textAktual = text; } }
void SaveFile() { string txt = textAktual + char( '\n' ); txt += "[" + nick + "]" + message; LPSTR Bufor; DWORD dwRozmiar = txt.length(); Bufor =( LPSTR ) GlobalAlloc( GPTR, dwRozmiar + 1 ); Bufor =( LPSTR ) txt.c_str(); DWORD dwZapisane; HANDLE hPlik; hPlik = CreateFile( pathOfFile.c_str(), GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_ALWAYS, 0, NULL ); WriteFile( hPlik, Bufor, dwRozmiar, & dwZapisane, NULL ); CloseHandle( hPlik ); }
|