Windows SDK programming RichEdit Create, display files like hex editor

Source: Internet
Author: User

Compilation environment: Windows 7 64-bit VS2010, project creation Select "Win32 Project"

Note Add several header files

#include <WinBase.h>
#include <commctrl.h>
#include <Commdlg.h>
#include <Richedit.h>
#include <malloc.h>

Found in main file: main function added

Loading RichEdit Control Library files
HINSTANCE Hrich;
Hrich = LoadLibrary (TEXT ("Riched20.dll")); RICHED20.dll

The main function is added last:

Uninstalling the RichEdit control library file
FreeLibrary (Hrich);

Add a message in the message loop:

Note in the main file header, define a RichEdit control handle variable, static HWND hrichedit;

Wm_create:

Create a RichEdit control and set the default font, and size

Hrichedit =createwindowex (Ws_ex_clientedge,text ("richedit20a"), NULL,
Ws_child | ws_visible |                                                           Ws_vscroll | Ws_hscroll | Es_multiline | Es_nohidesel,
0,0,0,0,hwnd,0,hinst,null);

stcf.cbsize = sizeof (STCF);
Stcf.yheight = 10 * 20;
Stcf.dwmask = Cfm_face | Cfm_size | Cfm_bold;
wcscpy (stcf.szfacename,_t ("The Song Body"));

SendMessage (hrichedit,em_setcharformat,0, (LPARAM) &stcf);
SendMessage (hrichedit,em_exlimittext,0,-1);
Break

----Read the file and display the-------------
void Doreadfile (HWND hrichedit,const wchar_t Strfilename[max_path])
{
Open File ***********
if (hfile = CreateFile (Strfilename,generic_read | generic_write,file_share_read,null,open_existing,0,null)) = = Invalid_handle_value)
{

Return
}
Get file length *******
if (! ( Ifilelen = GetFileSize (hfile,null)))
{
MessageBox (null,_t ("hint"), _t ("file length is zero"), NULL);
Return
}


The following function establishes a memory-mapped file;
Hfilemap = CreateFileMapping (hfile,null,page_readwrite,0,0,null);
if (!HFILEMAP)
{

Return
}
Establish a mapping view and point to the first byte of the memory-mapped file with pbuffer.
Pbuffer= pbyte (MapViewOfFile (Hfilemap,file_map_read | file_map_write,0,0,0));
if (!pbuffer)
{

Return
}

wchar_t sz[4],szr[512];

Charrange stcf; Define structure, Em_exsetsel message requires this message,
memset (&stcf,0,sizeof (STCF)); and setting the member variable to 1 is the trailing text of the cursor
Stcf.cpmax =-1;
Stcf.cpmin =-1;
int count;

The following handles the byte stream and displays it in hexadecimal in the control

for (int j = 0;j<ifilelen/16;j++)
{
memset (sz,0,sizeof (SZ));
memset (szr,0,sizeof (SzR));
Count = 0;
for (int i = 0;i<16;i++)
{
wsprintf (sz,_t ("%02x"), Pbuffer[i]);
Wcscat (SZR,SZ);
count++;
}
Pbuffer + = count;
Wcscat (szr,_t ("\ r \ n")); The text is added to the newline escape character, which is implemented in the downstream output;

SendMessage (hrichedit,em_exsetsel,0, (LPARAM) &stcf);
SendMessage (hrichedit,em_replacesel,0, (LPARAM) SzR);
}

Closes the file handle, the memory-mapped file handle, and frees the memory;
UnmapViewOfFile (pbuffer);
CloseHandle (HFILEMAP);
CloseHandle (hfile);
}

Windows SDK programming RichEdit Create, display files like hex editor

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.