[SDK programming] similar to Microsoft notepad V2.0

Source: Internet
Author: User

Update:
ICO replaced .....
Added the font and color selection function;
Fixed:
The resource files have been packaged into the project directory, and the paths have been changed to relative paths;
It solves the problem that data will be lost when files larger than Kb are saved.






 

Core code:
#include "resource.h"
#include <windows.h>
#include <stdio.h>
#include <Richedit.h>
#include <shlwapi.h>
#pragma comment (lib, "shlwapi.lib")
#define WM_FILENAME WM_USER + 1 // Custom message, this message is used to send the full path of the file selected by the user
#define OPEN WM_USER + 2 // A Flag to indicate whether this file is selected in the open dialog or save dialog
#define SAVE WM_USER + 3

INT_PTR ExitCode1; // Used to save the exit code of the main dialog
INT_PTR ExitCode2; // Exit code of About dialog
HINSTANCE hMain; // Used to save the instance handle
// The main dialog procedure function declaration
INT_PTR CALLBACK DialogProc (HWND hwndDlg, UINT uMsg, WPARAM wParam, LPARAM lParam);
// About dialog procedure function declaration
INT_PTR CALLBACK AboutProc (HWND hwndDlg, UINT uMsg, WPARAM wParam, LPARAM lParam);

BOOL OnInitDialog (HWND hwndDlg); // The function called when the corresponding WM_ONINITDIALOG message
BOOL OnOpen (HWND hwndDlg, DWORD Flag); // Called when the user clicks the Open menu item
// void GetTitle (char * pSourceBuf, char * pDestBuf);
void ChooseFont (HWND hwndDlg, CHOOSEFONT & font, LOGFONT & log);

int WINAPI WinMain (HINSTANCE hIns, HINSTANCE hPrev, LPSTR lpCmd, int nShowCmd) // Entry function
{
    LoadLibrary ("Riched20.dll"); // To use the Rich Edit control, you must first load this library, otherwise the program cannot run
    hMain = hIns;
    ExitCode1 = DialogBox (hIns, MAKEINTRESOURCE (IDD_DIALOG1), NULL, DialogProc); // Create the main dialog
    return 0;
}

// The main dialog procedure function
INT_PTR CALLBACK DialogProc (HWND hwndDlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
    switch (uMsg)
    {
    case WM_INITDIALOG: // initialization
        OnInitDialog (hwndDlg);
        break;
    // When the user changes the size of the main dialog, the size of the Edit control should also change.
    case WM_SIZE:
        {
            RECT rect;
            ZeroMemory (& rect, sizeof (RECT));
            GetWindowRect (hwndDlg, & rect); // Get the size of the main dialog window, see MSDN for details
            HWND hEdit = GetDlgItem (hwndDlg, IDC_TEXT);
            // Change the size of the Edit control
            SetWindowPos (hEdit, HWND_TOP, 0,0, rect.right-rect.left-5, rect.bottom-rect.top-50, SWP_SHOWWINDOW);
            CloseHandle (hEdit);

        }
        break;
    case WM_COMMAND:
        if (IDM_OPEN == wParam) // Open is clicked by the user
            OnOpen (hwndDlg, OPEN);
        if (IDM_EXIT == wParam) // Exit is clicked by the user
            SendMessage (hwndDlg, WM_CLOSE, NULL, NULL); // Send WM_CLOSE message
        if (IDM_ABOUT == wParam) // About is clicked to create an ABOUT dialog
            ExitCode2 = DialogBox (hMain, MAKEINTRESOURCE (IDD_ABOUT), hwndDlg, AboutProc);
        if (IDM_SAVE == wParam) // The user clicks Save
        {
          
            OnOpen (hwndDlg, SAVE);
        }
        if (IDM_CLOSEFILE == wParam)
        {
            SetDlgItemText (hwndDlg, IDC_TEXT, "");
            SetWindowText (hwndDlg, "NotePadV2.0 By 小 司");
        }
        if (IDM_FORMAT == LOWORD (wParam))
        {
            LOGFONT log;
            CHOOSEFONT font;
            CHARFORMAT2 cm;
            ZeroMemory (& cm, sizeof (cm));

            ChooseFont (hwndDlg, font, log);
            if (ChooseFont (& font))
            {

                strcpy (cm.szFaceName, log.lfFaceName);
                cm.cbSize = sizeof (cm);
                cm.dwMask = CFM_FACE | CFM_SIZE | CFM_WEIGHT | CFM_COLOR;
                cm.yHeight = log.lfHeight * log.lfHeight;
                cm.wWeight = log.lfWeight;
                cm.bPitchAndFamily = log.lfPitchAndFamily;
                cm.crTextColor = font.rgbColors;

                HWND hText = GetDlgItem (hwndDlg, IDC_TEXT);
                SendMessage (hText, EM_SETCHARFORMAT, SCF_ALL, (LPARAM) & cm);
            }
           
        }
        break;
    case WM_FILENAME: // custom message, used to receive file path
        if (OPEN == lParam)
        {
            // The purpose of opening the file is to get the file size using the GetFileSize function
            HANDLE hFile = CreateFile ((LPCSTR) wParam, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING,
                                FILE_ATTRIBUTE_NORMAL, NULL);
            if (INVALID_HANDLE_VALUE == hFile)
            {
                MessageBox (hwndDlg, "Open File Failed!", "Error", 0);
                CloseHandle (hFile);
                break;
            }
            DWORD dwFileSize = GetFileSize (hFile, NULL); // Get file size
            CloseHandle (hFile);

            TCHAR * pszData = new TCHAR [dwFileSize];
            memset (pszData, 0, dwFileSize);
           
            FILE * fp = fopen ((char *) wParam, "r"); // The path of the file saved in wParam
            fread (pszData, dwFileSize, 1, fp);
            fclose (fp);
            SetDlgItemText (hwndDlg, IDC_TEXT, pszData); // Display data
        }

        if (SAVE == lParam)
        {
            HWND hText = GetDlgItem (hwndDlg, IDC_TEXT);
            int len = GetWindowTextLength (hText);

            TCHAR * pBuf = new TCHAR [len];
            memset (pBuf, 0, len);
            GetDlgItemText (hwndDlg, IDC_TEXT, pBuf, len);

            char szFilePath [MAX_PATH + 10];
            sprintf_s (szFilePath, "% s.txt", (char *) wParam); // To make up.txt

            FILE * fp = fopen (szFilePath, "w");
            fwrite (pBuf, 1, len, fp);
            fclose (fp);
        }
        break;
        // This message is useless, for debugging ..
    case WM_LBUTTONDOWN:

        break;
    case WM_CLOSE:
        if (IDYES == MessageBox (hwndDlg, "Are you sure to exit?", "Warming", MB_YESNO | MB_ICONWARNING)))
        {
            EndDialog (hwndDlg, ExitCode1);
            break;
        }
        break;
    default:
        break;
    }
    return FALSE;
}

// about window procedure function
INT_PTR CALLBACK AboutProc (HWND hwndDlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
    switch (uMsg)
    {
    case WM_CLOSE:
        EndDialog (hwndDlg, ExitCode2);
        break;
    case WM_COMMAND:
        if (IDOK == wParam)
        {
            SendMessage (hwndDlg, WM_CLOSE, NULL, NULL);
        }
        break;
    default:
        break;
    }
    return FALSE;
}
BOOL OnInitDialog (HWND hwndDlg)
{
    HICON hIcon = LoadIcon (hMain, MAKEINTRESOURCE (IDI_ICON1)); // Load icon
    SendMessage (hwndDlg, WM_SETICON, ICON_SMALL, (LPARAM) hIcon); // Set icon
    return TRUE;
}
BOOL OnOpen (HWND hwndDlg, DWORD Flag)
{
    // See the description of GetOpenFileName in MSDN
    TCHAR szFile [MAX_PATH];
    TCHAR szFileTitle [MAX_PATH];
    OPENFILENAME op;
    memset (szFile, 0, MAX_PATH);
    memset (szFileTitle, 0, MAX_PATH);

    ZeroMemory (& op, sizeof (op));
    op.lStructSize = sizeof (op);
    op.hwndOwner = hwndDlg;
    op.lpstrFile = szFile;
    op.nMaxFile = MAX_PATH;
    op.lpstrFilter = "All \ 0 *. * \ 0 text file (* .txt) \ 0 * .txt \ 0";
    op.nFilterIndex = 2;
    op.lpstrFileTitle = NULL;
    op.nMaxFileTitle = 0;
    op.lpstrInitialDir = NULL;
    op.Flags = OFN_EXPLORER | OFN_PATHMUSTEXIST | OFN_FILEMUSTEXIST | OFN_OVERWRITEPROMPT;

    if (OPEN == Flag)
    {
        if (GetOpenFileName (& op))
        {// Send the file path to the main dialog
            StrCpy (szFileTitle, szFile);
            PathStripPath (szFileTitle);
            SetWindowText (hwndDlg, szFileTitle);
            SendMessage (hwndDlg, WM_FILENAME, (WPARAM) szFile, (LPARAM) Flag);
        }
    }
    if (SAVE == Flag)
    {
        if (GetSaveFileName (& op))
        {
            SendMessage (hwndDlg, WM_FILENAME, (WPARAM) szFile, (LPARAM) Flag);
        }
    }
    return TRUE;
}

void ChooseFont (HWND hwndDlg, CHOOSEFONT & font, LOGFONT & log)
{
    TCHAR szStyle [256];
    HDC dc = GetDC (NULL);
    memset (szStyle, 0,256);
    WORD wFontType = 0;
    COLORREF color = RGB (0,0,0);
    ZeroMemory (& log, sizeof (log));

    ZeroMemory (& font, sizeof (font));
    font.lStructSize = sizeof (font);
    font.hwndOwner = hwndDlg;
    font.lpLogFont = & log;
    font.Flags = CF_EFFECTS | CF_FORCEFONTEXIST | CF_BOTH;
    font.rgbColors = color;
    font.lpszStyle = szStyle;
    font.hDC = dc;
    font.nFontType = wFontType;

}

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.