| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 908 人关注过本帖
标题:Win记事本C代码
只看楼主 加入收藏
q1182665752
Rank: 1
等 级:新手上路
帖 子:3
专家分:0
注 册:2013-6-13
结帖率:0
收藏
已结贴  问题点数:20 回复次数:9 
Win记事本C代码
/***************************************************
   
    program wing_ed.c
    wing.edit 1.19   
    (c) 2008 lingluoz  -lingluoz@
   
****************************************************/
  
#include <windows.h>
#include <string.h>

#define ID_EDIT 1

#define ID_M_FILE_NEW 40001
#define ID_M_FILE_OPEN 40002
#define ID_M_FILE_SAVE 40003
#define ID_M_FILE_SAVE_AS 40004
#define ID_M_FILE_EXIT 40005

#define ID_M_HELP_ABOUT 40007


LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM);

TCHAR szAppName[] ="wing.edit";
TCHAR szCntFile[MAX_PATH]="";
TCHAR szCntTitle[MAX_PATH]="Untitled.txt";
TCHAR szCommand[MAX_PATH]="";
BOOL bTextUpdate=FALSE;


void DoCaption(HWND hwnd)
{
    TCHAR szCap[MAX_PATH+16]="";
    strcpy(szCap,szAppName);
    strcat(szCap," -");
    strcat(szCap,szCntTitle);
    if (bTextUpdate==TRUE)
        strcat(szCap,"*");
    SetWindowText(hwnd,szCap);
}




BOOL FileWrite(HWND hwndEdit,PSTR sFileName)
{
   
    PBYTE p;
    HANDLE hFile;
    DWORD dwBytesWritten;
    hFile=CreateFile(
        sFileName,
        GENERIC_WRITE,
        0,
        NULL,
        CREATE_ALWAYS,
        0,
        NULL
        );
    if (hFile==INVALID_HANDLE_VALUE)
        return FALSE;
    p=(PBYTE)malloc(GetWindowTextLength(hwndEdit)+1);
    GetWindowText (hwndEdit,(LPSTR)p,GetWindowTextLength(hwndEdit)+1);
    WriteFile (hFile,p,GetWindowTextLength(hwndEdit),&dwBytesWritten,NULL);
    SetWindowText(hwndEdit,(PTSTR)p);
    free(p);
    CloseHandle(hFile);
    return TRUE;
}


BOOL WingSaveAs(HWND hWndEdit,HWND hwnd)
{

    TCHAR szFilter[] = "文本 (*.txt)\0*.txt\ flie (*.*) \0*.*\0\0";
    TCHAR szTitle[MAX_PATH]="";
    TCHAR szFile[MAX_PATH]="";
    OPENFILENAME ofn;

    strcpy(szFile,szCntFile);
    strcpy(szTitle,szCntTitle);

    if (*szCntFile==0)
        strcpy(szFile,"Untitled.txt");

       ofn.lStructSize = sizeof (OPENFILENAME) ;
    ofn.hwndOwner = hwnd ;
    ofn.hInstance = NULL ;
    ofn.lpstrFilter = szFilter ;
    ofn.lpstrCustomFilter = NULL ;
    ofn.nMaxCustFilter = 0 ;
    ofn.nFilterIndex = 0 ;
    ofn.lpstrFile = szFile;
    ofn.nMaxFile = MAX_PATH ;
    ofn.lpstrFileTitle = szTitle;
    ofn.nMaxFileTitle = MAX_PATH ;
    ofn.lpstrInitialDir = NULL ;
    ofn.lpstrTitle = NULL ;
    ofn.Flags = OFN_OVERWRITEPROMPT ;
    ofn.nFileOffset = 0 ;
    ofn.nFileExtension = 0 ;
    ofn.lpstrDefExt = "txt";
    ofn.lCustData = 0L ;
    ofn.lpfnHook = NULL ;
    ofn.lpTemplateName = NULL ;
   
    GetSaveFileName(&ofn);
    if (*szFile!=0) {
        if (FileWrite(hWndEdit,szFile)==TRUE) {
            strcpy(szCntFile,szFile);
            strcpy(szCntTitle,szTitle);
            bTextUpdate=FALSE;
            return TRUE;
        } else {
            MessageBox(
                hwnd,
                "Can't save this file.",
                szAppName,
                MB_ICONERROR
            );
            return FALSE;
        }
    } else
        return FALSE;
}



BOOL WingSave(HWND hWndEdit,HWND hwnd)
{
    if (*szCntFile!=0) {
        if (FileWrite(hWndEdit,szCntFile)!=TRUE) {
            MessageBox(
                hwnd,
                "Can't save this file.",
                szAppName,
                MB_ICONERROR
            );
            return FALSE;
        } else {
            bTextUpdate=FALSE;
            return TRUE;
        }
    } else
        return WingSaveAs(hWndEdit,hwnd);

}

BOOL CheckUpdataToSave(HWND hwndEdit,HWND hwnd)
{
    BOOL bReturn;
    if (bTextUpdate==TRUE) {
        switch (MessageBox(
            hwnd,
            "This file has been changed,will you save it?",
            szAppName,
            MB_ICONINFORMATION|MB_YESNOCANCEL|MB_SYSTEMMODAL
        )) {
        case IDYES:
            bReturn=WingSave(hwndEdit,hwnd);
            break;
        case IDNO:
            bReturn=TRUE;
            break;
        case IDCANCEL:
            bReturn=FALSE;
            break;

        }
    } else
        bReturn=TRUE;
    return bReturn;
}

int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,PSTR szCmdLine, int iCmdShow)
{
    HWND hwnd ;
    MSG msg ;
    WNDCLASS wndclass ;
    HMENU hMenu,hMenuPopup;
        
 
    hMenu = CreateMenu () ;
    hMenuPopup = CreateMenu () ;
    AppendMenu(hMenuPopup, MF_STRING, ID_M_FILE_NEW, "新建&N");
    AppendMenu(hMenuPopup, MF_STRING, ID_M_FILE_OPEN, "打开&O");
    AppendMenu(hMenuPopup, MF_STRING, ID_M_FILE_SAVE, "保存&S");
    AppendMenu(hMenuPopup, MF_STRING, ID_M_FILE_SAVE_AS, "另存为&A");
    AppendMenu(hMenuPopup, MF_SEPARATOR, 0, NULL) ;
    AppendMenu(hMenuPopup, MF_STRING, ID_M_FILE_EXIT, "退出&E") ;
    AppendMenu(hMenu, MF_POPUP, (UINT)hMenuPopup, "文件&F") ;

    hMenuPopup = CreateMenu () ;
    AppendMenu (hMenuPopup, MF_STRING, ID_M_HELP_ABOUT, "关于&A") ;
    AppendMenu (hMenu, MF_POPUP, (UINT)hMenuPopup, "帮助&H") ;
 
    wndclass.style=CS_HREDRAW|CS_VREDRAW;
    wndclass.lpfnWndProc=WndProc;
    wndclass.cbClsExtra=0;
    wndclass.cbWndExtra=0;
    wndclass.hInstance=hInstance;
    wndclass.hIcon=LoadIcon(NULL,IDI_APPLICATION);
    wndclass.hCursor=LoadCursor(NULL,IDC_ARROW);
    wndclass.hbrBackground=(HBRUSH)GetStockObject(WHITE_BRUSH);
    wndclass.lpszMenuName=NULL;
    wndclass.lpszClassName = szAppName;
 
    if (!RegisterClass (&wndclass)) {
        MessageBox ( NULL, TEXT ("Unkonow Error!"),
        szAppName, MB_ICONERROR) ;
        return 0 ;
    }

    if (strlen(szCmdLine)<MAX_PATH)
        strcpy(szCommand,szCmdLine);


    hwnd=CreateWindow(szAppName,szAppName,
        WS_OVERLAPPEDWINDOW,
        CW_USEDEFAULT,CW_USEDEFAULT,
        CW_USEDEFAULT,CW_USEDEFAULT,
        NULL,hMenu,hInstance,NULL
    );
   
    ShowWindow(hwnd,iCmdShow);
    UpdateWindow(hwnd);

    while(GetMessage(&msg,NULL,0,0)){
        TranslateMessage(&msg);
        DispatchMessage(&msg);
    }
    return msg.wParam;
}

BOOL FileRead(HWND hwndEdit,PSTR sFileName)
{
   
    PBYTE p;
    HANDLE hFile;
    DWORD dwBytesRead;
    hFile=CreateFile(
        sFileName,
        GENERIC_READ,
        FILE_SHARE_READ,
        NULL,OPEN_EXISTING,
        0,
        NULL
        );
    if (hFile==INVALID_HANDLE_VALUE)
        return FALSE;
    p=(PBYTE)malloc(GetFileSize(hFile,NULL)+1);
    ReadFile(hFile,p,GetFileSize(hFile,NULL),&dwBytesRead, NULL);
    *(p+GetFileSize(hFile,NULL))=0;
    SetWindowText(hwndEdit,(PTSTR)p);
    free(p);
    CloseHandle(hFile);
    return TRUE;
}


void WingOpenFile(HWND hwndEdit,HWND hwnd)
{
    OPENFILENAME ofn;
    TCHAR szFilter[]="Text Files (*.txt)\0*.txt\0All Files (*.*) \0*.*\0\0";
    TCHAR szTitle[MAX_PATH]="";
    TCHAR szFile[MAX_PATH]="";
     
       ofn.lStructSize=sizeof(OPENFILENAME);
    ofn.hwndOwner=hwnd;
    ofn.hInstance=NULL;
    ofn.lpstrFilter=szFilter;
    ofn.lpstrCustomFilter=NULL;
    ofn.nMaxCustFilter=0;
    ofn.nFilterIndex=0;
    ofn.lpstrFile=szFile;
    ofn.nMaxFile=MAX_PATH;
    ofn.lpstrFileTitle=szTitle;
    ofn.nMaxFileTitle=MAX_PATH;
    ofn.lpstrInitialDir=NULL;
    ofn.lpstrTitle=NULL;
    ofn.Flags=OFN_HIDEREADONLY;
    ofn.nFileOffset=0;
    ofn.nFileExtension=0;
    ofn.lpstrDefExt="*";
    ofn.lCustData=0L;
    ofn.lpfnHook=NULL;
    ofn.lpTemplateName=NULL;
   
    GetOpenFileName(&ofn);

    if (*szFile!=0) {
        if (FileRead(hwndEdit,szFile)==TRUE) {
            strcpy(szCntFile,szFile);
            strcpy(szCntTitle,szTitle);
            bTextUpdate=FALSE;
        } else
            MessageBox(
                hwnd,
                "Can't open this file.",
                szAppName,
                MB_SYSTEMMODAL|MB_ICONERROR
            );
    }
}


LRESULT CALLBACK WndProc(HWND hwnd,UINT message,WPARAM wParam,LPARAM lParam)
{
    static HWND hwndEdit;
    LOGFONT logFont;
    HFONT hFont;
    switch (message) {
    case WM_CREATE :
        hwndEdit=CreateWindow(TEXT("edit"),NULL,
            WS_CHILD|WS_VISIBLE|WS_HSCROLL|WS_VSCROLL|
            ES_LEFT|ES_MULTILINE|
            ES_AUTOHSCROLL|ES_AUTOVSCROLL,
            0,0,0,0,hwnd,(HMENU)ID_EDIT,
            ((LPCREATESTRUCT)lParam)->hInstance,NULL
        );
        if (*szCommand!=0) {
            if (FileRead(hwndEdit,szCommand)==TRUE) {
                strcpy(szCntFile,szCommand);
                strcpy(
                    szCntTitle,
                    strrchr(szCommand,'\\')==NULL?szCommand:strrchr(szCommand,'\\')+1);
                bTextUpdate=FALSE;
            } else
                MessageBox(
                    hwnd,
                    "Can't open this file.",
                   szAppName,
                   MB_ICONERROR
                );
        }
        DoCaption(hwnd);
        
           GetObject(
            GetStockObject(SYSTEM_FONT),
            sizeof(LOGFONT),
            (LPVOID)&logFont
        );
        strcpy(logFont.lfFaceName,"Fixedsys");
        logFont.lfHeight=0;
        logFont.lfWidth=0;
        logFont.lfWeight=FW_NORMAL;
           hFont=CreateFontIndirect(&logFont);
           SendMessage(hwndEdit,WM_SETFONT,(WPARAM)hFont,0);

        return 0 ;
    case WM_SETFOCUS:
        SetFocus (hwndEdit);
        return 0;
    case WM_SIZE:
        MoveWindow(hwndEdit,0,0,LOWORD(lParam),HIWORD(lParam),TRUE);
        return 0;
    case WM_COMMAND:
        switch(LOWORD(wParam)){
        case ID_EDIT:
            if (HIWORD(wParam)==EN_ERRSPACE||HIWORD(wParam)==EN_MAXTEXT)
                MessageBox (hwnd, TEXT ("Edit control out of space."),szAppName, MB_OK | MB_ICONSTOP) ;
            if (HIWORD(wParam)==EN_UPDATE) {
                bTextUpdate=TRUE;
                DoCaption(hwnd);
            }
            break;
        case ID_M_FILE_OPEN:
            if (CheckUpdataToSave(hwndEdit,hwnd)==FALSE)
                return 0;
            WingOpenFile(hwndEdit,hwnd);
            DoCaption(hwnd);
            break;
        case ID_M_FILE_SAVE_AS:
            WingSaveAs(hwndEdit,hwnd);
            DoCaption(hwnd);
            break;
        case ID_M_FILE_SAVE:
            WingSave(hwndEdit,hwnd);
            DoCaption(hwnd);   
            break;
        case ID_M_FILE_NEW:
            if (CheckUpdataToSave(hwndEdit,hwnd)==FALSE)
                return 0;
            SetWindowText(hwndEdit,"");
            strcpy(szCntFile,"");
            strcpy(szCntTitle,"Untitled.txt");
            DoCaption(hwnd);
            break;
        case ID_M_FILE_EXIT:
            SendMessage(hwnd,WM_CLOSE,0,0);
            break;
        case ID_M_HELP_ABOUT:
            MessageBox(
                hwnd,
                "wing.edit 1.19\ncopyright (c) 2008 lingluoz\nlingluoz@,
                szAppName,
                MB_ICONINFORMATION
            );
            break;
        }
        return 0;
    case WM_CLOSE:
        if (CheckUpdataToSave(hwndEdit,hwnd)!=TRUE)
            return 0;
    case WM_DESTROY:
        PostQuitMessage(0);
        return 0 ;
    }
    return DefWindowProc(hwnd,message,wParam,lParam);
}




以上代码用C-Free 5.0直接通过  
C-Free 5.0注册码
用户名:tianfang

电子邮件:quart@

注册码:2NnUqd3shO2agta0xNjcusfK1LXO
C-Free 5.0上百度一下就有
搜索更多相关主题的帖子: include 记事本 
2013-06-13 23:11
q1182665752
Rank: 1
等 级:新手上路
帖 子:3
专家分:0
注 册:2013-6-13
收藏
得分:0 
Dev-C++下直接通过
2013-06-14 03:25
pauljames
Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19
等 级:千里冰封
威 望:9
帖 子:1555
专家分:10000
注 册:2011-5-8
收藏
得分:3 
谢谢分享

经常不在线不能及时回复短消息,如有c/单片机/运动控制/数据采集等方面的项目难题可加qq1921826084。
2013-06-14 07:01
邓士林
Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19
来 自:淮河河畔
等 级:贵宾
威 望:61
帖 子:2392
专家分:13384
注 册:2013-3-3
收藏
得分:3 
谢谢

Maybe
2013-06-14 16:19
天地霸气诀
Rank: 2
等 级:禁止发言
帖 子:21
专家分:21
注 册:2013-6-14
收藏
得分:3 
回复 2楼 q1182665752
提示: 作者被禁止或删除 内容自动屏蔽
2013-06-14 16:37
赵疯子
Rank: 8Rank: 8
来 自:dream
等 级:蝙蝠侠
威 望:2
帖 子:365
专家分:737
注 册:2013-5-5
收藏
得分:3 
会用windows.h 的人才一般很厉害了
2013-06-14 16:44
tigerball
Rank: 2
等 级:论坛游民
帖 子:58
专家分:32
注 册:2013-6-10
收藏
得分:3 
牛鼻,C F 5.0亲测可用
2013-06-14 16:53
dengluoy
Rank: 3Rank: 3
等 级:论坛游侠
帖 子:127
专家分:165
注 册:2013-2-5
收藏
得分:3 

一同学习, 一同进步
2013-06-15 15:45
雨中浊玉
Rank: 2
等 级:论坛游民
帖 子:11
专家分:41
注 册:2013-3-22
收藏
得分:3 
好像很复杂
2013-06-15 17:02
曾经的梦想
Rank: 2
等 级:论坛游民
帖 子:19
专家分:27
注 册:2012-12-10
收藏
得分:3 
很简单!!
2013-06-15 17:05
快速回复:Win记事本C代码
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.022106 second(s), 9 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved