删去很多无足轻重的代码,伪代码如下:int WINAPI MainWndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)// HWND hWnd;                      /* window handle                 */
//UINT message;                   /* type of message               */
//WPARAM wParam;                  /* additional information        */
//LPARAM lParam;                  /* additional information        */
{
PAINTSTRUCT stPaintStruct;
HDC hDC;
int i, j;
RECT rtClientRect;
/*?*/
FILE *fp = fopen("c:\\test.txt","wb+");
    switch (message)
    {
case WM_LBUTTONDOWN:
bMouseDown = TRUE;
flag = 0;
num_flag = 0;
if( nStatus == DRAW_LINE || nStatus == DRAW_RECTANGLE) {
ptStart.x = LOWORD(lParam);
ptStart.y = HIWORD(lParam);
ptEnd.x = ptStart.x;
ptEnd.y = ptStart.y;
}
else if(nStatus == DRAW_NONE) {
ptStart.x = LOWORD(lParam);
ptStart.y = HIWORD(lParam);
ptEnd.x = ptStart.x;
ptEnd.y = ptStart.y;
for( i = 0; i < nTotalLines;i ++ ) { //线段优先拾取,找到符合要求的线段break
//看点(ptStart.x, ptStart.y)到直线距离是否小于1

float len1 = pow((pow((abs(ptStart.x-aLine[i].ptStart.x)),2) + pow((abs(ptStart.y-aLine[i].ptStart.y)),2)), 0.5);
float len2 = pow((pow((abs(ptStart.x-aLine[i].ptEnd.x)),2) + pow((abs(ptStart.y-aLine[i].ptEnd.y)),2)), 0.5);
float len = pow((pow((abs(aLine[i].ptEnd.x-aLine[i].ptStart.x)),2) + pow((abs(aLine[i].ptEnd.y-aLine[i].ptStart.y)),2)), 0.5);
if(len1 + len2 - 1.5 <= len) {
break;
}
}
if(i < nTotalLines) { //有相邻线段可选
flag = 1;
num_flag = i;
sln = aLine[i];
}

else if(i == nTotalLines) { //没有相邻线段,再找相邻矩形
for( i = 0; i < nTotalRectangles;i ++ ) {
if( abs(ptStart.x-aRect[i].left)<=1.5 || abs(ptStart.x-aRect[i].right)<=1.5 || abs(ptStart.y-aRect[i].top)<=1.5 || abs(ptStart.y-aRect[i].bottom)<=1.5) {
break;
}
}
if(i < nTotalRectangles) {
flag = 2;
num_flag = i;
rct = aRect[i];
}
}
}
break;
case WM_LBUTTONUP:
bMouseDown = FALSE;
if( nStatus == DRAW_LINE) {
aLine[nTotalLines].ptStart = ptStart;
aLine[nTotalLines++].ptEnd = ptEnd;
}
else if( nStatus == DRAW_RECTANGLE) {
aRect[nTotalRectangles].left  = ptStart.x;
aRect[nTotalRectangles].top   = ptStart.y;
aRect[nTotalRectangles].right = ptEnd.x;
aRect[nTotalRectangles++].bottom  = ptEnd.y;
}
break;
case WM_MOUSEMOVE:
if ( bMouseDown ) {
if( nStatus == DRAW_LINE || nStatus == DRAW_RECTANGLE) {
ptEnd.x = LOWORD(lParam);
ptEnd.y = HIWORD(lParam);
InvalidateRect( hWnd,NULL,TRUE);  //指定整个窗口都需要重画
UpdateWindow(hWnd);  //刷新窗口
}
else if( nStatus == DRAW_NONE) {
ptEnd.x = LOWORD(lParam);
ptEnd.y = HIWORD(lParam);

if(flag == 1) {
aLine[num_flag].ptStart.x =sln.ptStart.x + ptEnd.x - ptStart.x;
aLine[num_flag].ptStart.y =sln.ptStart.y + ptEnd.y - ptStart.y;
aLine[num_flag].ptEnd.x =sln.ptEnd.x + ptEnd.x - ptStart.x;
aLine[num_flag].ptEnd.y =sln.ptEnd.y + ptEnd.y - ptStart.y;
}
else if(flag == 2) {
aRect[num_flag].left =rct.left + ptEnd.x - ptStart.x;
aRect[num_flag].right =rct.right + ptEnd.x - ptStart.x;
aRect[num_flag].top =rct.top + ptEnd.y - ptStart.y;
aRect[num_flag].bottom =rct.bottom + ptEnd.y - ptStart.y;
}

InvalidateRect( hWnd,NULL,TRUE);  //指定整个窗口都需要重画
UpdateWindow(hWnd);  //刷新窗口
}
}
break;
case WM_CHAR: //敲击字母、数字、标点符号键时产生
if ( nTotalChars < MAX_UNITS  ) {
szString[nTotalChars++] = wParam;
InvalidateRect( hWnd,NULL,TRUE);
UpdateWindow(hWnd);
}
else {
nTotalChars = 0;
}
break;

case WM_COMMAND:       /* message: command from application menu */
switch( wParam) {
case IDM_ABOUT:
//显示对话框
DialogBox(hInst,        /* current instance          */
"AboutBox",         /* resource to use   .rc文件中的对话框名        */
hWnd,               /* parent handle     父窗口        */
About);             /* About() instance address  对话框窗口消息处理函数 */

break;
case IDM_EXIT:  //用户选 exit菜单
DestroyWindow( hWnd);  //向消息队列里放 WM_DESTROY消息,相当于用户点关闭窗口按钮
break;
case ID_RECTANGLE:
nStatus = DRAW_RECTANGLE;
SetWindowText(hWnd,"正在画矩形");
break;
case ID_LINE:
nStatus = DRAW_LINE;
SetWindowText(hWnd,"正在画线");
break;
case  ID_NONE:
nStatus = DRAW_NONE;
SetWindowText(hWnd,"什么也不画");
break;
case ID_CLEAR:
nTotalLines = 0;
nTotalRectangles = 0;
InvalidateRect(hWnd,NULL,TRUE);
break;

/*?*/
case ID_SAVE:
fwrite(&nTotalLines, sizeof(nTotalLines), 1, fp);
/*
int i;
fout << nTotalLines << ' ';
for( i = 0; i < nTotalLines; i ++ ) {
fout << aLine[i].ptStart.x << ' ' << aLine[i].ptStart.y << ' ' << aLine[i].ptEnd.x << ' ' << aLine[i].ptEnd.y << ' ';
}
fout << nTotalRectangles << ' ';
for( i = 0; i < nTotalRectangles; i ++ ) {
fout << aRect[i].left << ' ' << aRect[i].top << ' ' << aRect[i].right << ' ' << aRect[i].bottom << ' ';
}
*/
break;
default:
return (DefWindowProc(hWnd, message, wParam, lParam));
//WM_COMMAND来自其他来源则用Windows缺省的消息处理函数处理
}
break;
        case WM_DESTROY:          /* message: window being destroyed */
PostQuitMessage(0);  //向消息队列里放 WM_QUIT消息
            break;
case WM_PAINT:
//注意,一般情况下可以用 GetDC函数来取得DC句柄并绘图,但是处理WM_PAINT消息的时候最好用
// BeginPaint  EndPaint函数对
hDC = BeginPaint( hWnd, & stPaintStruct);
TextOut( hDC ,0,0,szString, nTotalChars);

for( i = 0; i < nTotalLines;i ++ ) {
MoveToEx(hDC,aLine[i].ptStart.x,aLine[i].ptStart.y,NULL);
LineTo(hDC,aLine[i].ptEnd.x,aLine[i].ptEnd.y);
}
for( i = 0; i < nTotalRectangles;i ++ ) {
SelectObject(hDC,GetStockObject(HOLLOW_BRUSH));
Rectangle( hDC,aRect[i].left,aRect[i].top,aRect[i].right,aRect[i].bottom );
}

if (bMouseDown) {
if( nStatus == DRAW_LINE) {
MoveToEx(hDC,ptStart.x,ptStart.y,NULL);
LineTo(hDC,ptEnd.x,ptEnd.y);
}
else if( nStatus == DRAW_RECTANGLE) {
Rectangle( hDC,ptStart.x, ptStart.y,ptEnd.x,ptEnd.y);
}

}
EndPaint( hWnd, & stPaintStruct);
break;
        default:                  /* Passes it on if unproccessed    */
            return (DefWindowProc(hWnd, message, wParam, lParam));
// 其他消息则用Windows缺省的消息处理函数处理
    }
fclose(fp);
    return (0);
}现在想在点击菜单栏里的“保存”项(ID_SAVE)时能够输出文件到c:\test.txt,但现在这个程序点击保存后错误是:某内存不能为"read",请问在这样的程序中应如何输出数据(全局的)到文件中,并且能载入回来?谢谢!