Я пишу программу на C + + win32 api, и мне нужно читать из файла текст. Но там несовместимость кодирования. Пишу методы которые я использовал, и скажите как их исправить.
Код:
#include<windows.h>
#include<vector>
#include<string>
#include<fstream>
#include<tchar.h>
using namespace std;
class KDocument
{
private:
vector<string> lines;
public:
int cxChar;
int yStep;
int lineLenMax;
SCROLLINFO vsi;
int vertRange;
SCROLLINFO hsi;
int horzRange;
BOOL Open(const char* file)
{
ifstream finp(file);
char buf[300];
if(!finp.good())
{
MessageBox(NULL, _T("Cannot found file"), _T("ERROR"), MB_OK);
return false;
}
while(!finp.eof())
{
finp.getline(buf, 299);
buf[199]=0;
lines.push_back(string(buf));
}
return TRUE;
}
void Initialize(LPTEXTMETRIC tm)
{
cxChar=tm->tmAveCharWidth;
yStep=tm->tmHeight+tm->tmExternalLeading;
vsi.nMin=vsi.nPos=0;
hsi.nMin=hsi.nPage=0;
}
void ScrollSettings(HWND hWnd, int width, int height)
{
vsi.cbSize=sizeof(vsi);
vsi.fMask=SIF_RANGE | SIF_PAGE | SIF_POS;
vsi.nPage=height/yStep-1;
vsi.nMax=lines.size()-1;
if(vsi.nPage>vsi.nMax)
vsi.nPos=vsi.nMin;
vertRange=vsi.nMax-vsi.nMin+1;
SetScrollInfo(hWnd, SB_VERT, &vsi, TRUE);
hsi.cbSize=sizeof(SCROLLINFO);
hsi.fMask=SIF_RANGE | SIF_PAGE | SIF_POS;
hsi.nPage=width/cxChar-2;
hsi.nMax=lineLenMax;
if(hsi.nPage>hsi.nMax)
hsi.nPos=hsi.nMin;
horzRange=hsi.nMax-hsi.nMin+1;
SetScrollInfo(hWnd, SB_HORZ, &hsi, TRUE);
}
void UpdateVsroll(HWND hWnd, int yInc)
{
yInc=min(yInc, vertRange-(int)vsi.nPage-vsi.nPos);
yInc=max(yInc, vsi.nMin-vsi.nPos);
if(yInc)
{
ScrollWindow(hWnd, 0, -yStep*yInc, NULL, NULL);
vsi.nPos+=yInc;
SetScrollInfo(hWnd, SB_VERT, &vsi, TRUE);
InvalidateRect(hWnd, NULL, TRUE);
UpdateWindow(hWnd);
}
}
void UpdateHscroll(HWND hWnd, int xInc)
{
xInc=min(xInc, horzRange-(int)hsi.nPage-hsi.nPos);
xInc=max(xInc, hsi.nMin-hsi.nPos);
if(xInc)
{
ScrollWindow(hWnd, -cxChar* xInc, 0, NULL, NULL);
hsi.nPos+-xInc;
SetScrollInfo(hWnd, SB_HORZ, &hsi, TRUE);
InvalidateRect(hWnd, NULL, TRUE);
UpdateWindow(hWnd);
}
}
void PutText(HWND hWnd, HDC hdc)
{
RECT rect;
GetClientRect(hWnd, &rect);
rect.left+=cxChar;
rect.right-=cxChar;
HRGN hRgn=CreateRectRgnIndirect(&rect);
SelectClipRgn(hdc, hRgn);
int x=cxChar*(hsi.nMin-hsi.nPos+1);
int y=yStep;
int amountLines=lines.size();
int iBeg=vsi.nPos;
int iEnd=(vsi.nPos+vsi.nPage<amountLines)? vsi.nPos+vsi.nPage : amountLines;
for(int i=iBeg; i<iEnd; ++i)
{
int iTabPos=lines[i].find('\t');
if(-1==iTabPos)
TextOut(hdc, x, y,(LPCWSTR)lines[i].c_str(), 300);
else
TabbedTextOut(hdc, x, y,(LPCWSTR)lines[i].c_str(), lines[i].size(), 0, 0, x);
y+=yStep;
}
SelectClipRgn(hdc, NULL);
}
}