15,979
社区成员
BOOL AppendTextToRich(TCHAR* text, int len)
{
//1、自动换行
//2、自动滚动
//3、尾行添加文本
//4、重新开始时自动清除所有内容
if (TxtEditor != NULL)
{
int nLen = GetWindowTextLength(TxtEditor);
//SendMessage(hwndrich, EM_SETSEL, (WPARAM)nLen, (LPARAM)-1);
//::SendMessage(hEdit, EM_SETSEL, -1, -1);
SendMessage(TxtEditor, EM_SETSEL, -1, -1);
SendMessage(TxtEditor, EM_REPLACESEL, -1, (LPARAM)(text));
int cxPhys = GetDeviceCaps(GetDC(TxtEditor), PHYSICALWIDTH);
SendMessage(TxtEditor, EM_SETTARGETDEVICE, (WPARAM)GetDC(TxtEditor), cxPhys / 2);
// Create "print preview".
//SendMessage(hwnd, EM_SETTARGETDEVICE, (WPARAM)hdc, cxPhys / 2);
/*SendMessage(RichText.hWnd, EM_SETTARGETDEVICE, _
GetDC(RichText.hWnd), RichText.Width / 15)*/
//SendMessage(TxtEditor, EM_SETSEL, -1, -1);
//SendMessage(TxtEditor, EM_REPLACESEL, -1, (LPARAM)(text));
//自动滚动到最后一行
SendMessage(TxtEditor, WM_VSCROLL, SB_BOTTOM, 0);
return TRUE;
}
else
return FALSE;
}
// Exec.cpp : 定义应用程序的入口点。
//
#include "stdafx.h"
#include "Exec.h"
#include <Commdlg.h>
#include <iostream>
#include <iostream>
#include <string>
#include <fstream>
#include <Richedit.h>
#include <shellapi.h>
//模块句柄
HINSTANCE gInstance = NULL;
//主窗口句柄
HWND hWnd = NULL;
BOOL AppendTextToRich(TCHAR *text, int len)
{
//1、自动换行
//2、自动滚动
//3、尾行添加文本
//4、重新开始时自动清除所有内容
if (hWnd != NULL)
{
HWND hwndrich=GetDlgItem(hWnd, IDC_RICHEDIT2_LOG);
int nLen = GetWindowTextLength(hwndrich);
//SendMessage(hwndrich, EM_SETSEL, (WPARAM)nLen, (LPARAM)-1);
//::SendMessage(hEdit, EM_SETSEL, -1, -1);
SendMessage(hwndrich, EM_SETSEL, -1, -1);
SendMessage(hwndrich, EM_REPLACESEL, 0, (LPARAM)(text));
SendMessage(hwndrich, EM_SETSEL, -1, -1);
SendMessage(hwndrich, EM_REPLACESEL, 0, (LPARAM)(text));
//自动滚动到最后一行
SendMessage(hwndrich, WM_VSCROLL, SB_BOTTOM, 0);
return TRUE;
}
else
return FALSE;
}
BOOL CALLBACK DialogProc(HWND hDlg, UINT uMsg,
WPARAM wParam, LPARAM lParam)
{
BOOL fReturn = TRUE;
HICON hIcon = NULL;
HANDLE hfile = NULL;
DWORD dwsize = 0;
LOGFONT LogFont = {0};
HFONT hFont = NULL;
HWND hWndStatic = NULL;
hWnd = hDlg;//保持窗口句柄
switch(uMsg)
{
case WM_CLOSE:
EndDialog(hDlg, NULL);
break;
case WM_INITDIALOG:
hIcon = LoadIcon(gInstance, MAKEINTRESOURCE(IDI_ICON));
SendMessage(hDlg, WM_SETICON, ICON_BIG, (LPARAM)hIcon);
//设置控件字体
lstrcpy(LogFont.lfFaceName, _T("宋体"));
LogFont.lfWeight = 400;
LogFont.lfHeight = -20; // 字体大小
LogFont.lfCharSet = 134;
LogFont.lfOutPrecision = 3;
LogFont.lfClipPrecision = 2;
LogFont.lfOrientation = 45;
LogFont.lfQuality = 1;
LogFont.lfPitchAndFamily = 2;
// 创建字体
hFont = CreateFontIndirect(&LogFont);
// 取得控件句柄
hWndStatic = GetDlgItem(hDlg, IDC_FILE_PATH);
// 设置控件字体
SendMessage(hWndStatic, WM_SETFONT, (WPARAM)hFont, 0);
//设置Log窗口的字体大小
LogFont.lfHeight = -15;
SendMessage(GetDlgItem(hDlg, IDC_RICHEDIT2_LOG), WM_SETFONT, (WPARAM)hFont, 0);
AppendTextToRich(_T("你好\r\n"), wcslen(_T("你好\r\n")));
break;
case WM_COMMAND:
switch(wParam)
{
case IDC_BUTTON_OPEN:
break;
case ID_RUN:
break;
case IDC_EXIT:
EndDialog(hDlg, 0);
break;
default:break;
}
break;
default:
fReturn = FALSE;
break;
}
return fReturn;
}
int APIENTRY _tWinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPTSTR lpCmdLine,
int nCmdShow)
{
UNREFERENCED_PARAMETER(hPrevInstance);
UNREFERENCED_PARAMETER(lpCmdLine);
//不装载此dll的话richedit控件不会显示
LoadLibrary(_T("RichEd20.dll"));
gInstance = hInstance;
DialogBoxParam(hInstance, MAKEINTRESOURCE(IDD_DIALOG_MAIN), NULL, DialogProc, NULL);
ExitProcess(0);
}
资源文件:
// Microsoft Visual C++ generated resource script.
//
#include "resource.h"
#define APSTUDIO_READONLY_SYMBOLS
/////////////////////////////////////////////////////////////////////////////
//
// Generated from the TEXTINCLUDE 2 resource.
//
#ifndef APSTUDIO_INVOKED
#include "targetver.h"
#endif
#define APSTUDIO_HIDDEN_SYMBOLS
#include "windows.h"
#undef APSTUDIO_HIDDEN_SYMBOLS
/////////////////////////////////////////////////////////////////////////////
#undef APSTUDIO_READONLY_SYMBOLS
/////////////////////////////////////////////////////////////////////////////
// 中文(简体,中国) resources
#if !defined(AFX_RESOURCE_DLL) || defined(AFX_TARG_CHS)
LANGUAGE LANG_CHINESE, SUBLANG_CHINESE_SIMPLIFIED
/////////////////////////////////////////////////////////////////////////////
//
// Dialog
//
IDD_DIALOG_MAIN DIALOGEX 0, 0, 316, 180
STYLE DS_SETFONT | DS_MODALFRAME | DS_FIXEDSYS | DS_NOFAILCREATE | DS_CENTER | WS_POPUP | WS_VISIBLE | WS_CAPTION | WS_SYSMENU
CAPTION "Dialog"
FONT 8, "MS Shell Dlg", 400, 0, 0x1
BEGIN
DEFPUSHBUTTON "执行",ID_RUN,259,79,50,14
PUSHBUTTON "退出",IDC_EXIT,259,134,50,14
GROUPBOX "打开配置文件",IDC_STATIC,2,2,313,38
EDITTEXT IDC_FILE_PATH,6,12,241,23,ES_AUTOHSCROLL | ES_READONLY
PUSHBUTTON "打开",IDC_BUTTON_OPEN,258,12,50,22
GROUPBOX "",IDC_STATIC,3,44,310,134
CONTROL "",IDC_RICHEDIT2_LOG,"RichEdit20W",ES_AUTOHSCROLL | ES_READONLY | WS_BORDER | WS_TABSTOP,8,53,241,121
END
/////////////////////////////////////////////////////////////////////////////
//
// DESIGNINFO
//
#ifdef APSTUDIO_INVOKED
GUIDELINES DESIGNINFO
BEGIN
IDD_DIALOG_MAIN, DIALOG
BEGIN
END
END
#endif // APSTUDIO_INVOKED
#ifdef APSTUDIO_INVOKED
/////////////////////////////////////////////////////////////////////////////
//
// TEXTINCLUDE
//
1 TEXTINCLUDE
BEGIN
"resource.h\0"
END
2 TEXTINCLUDE
BEGIN
"#ifndef APSTUDIO_INVOKED\r\n"
"#include ""targetver.h""\r\n"
"#endif\r\n"
"#define APSTUDIO_HIDDEN_SYMBOLS\r\n"
"#include ""windows.h""\r\n"
"#undef APSTUDIO_HIDDEN_SYMBOLS\r\n"
"\0"
END
3 TEXTINCLUDE
BEGIN
"\r\n"
"\0"
END
#endif // APSTUDIO_INVOKED
/////////////////////////////////////////////////////////////////////////////
//
// Icon
//
// Icon with lowest ID value placed first to ensure application icon
// remains consistent on all systems.
IDI_ICON ICON "Icon.ico"
#endif // 中文(简体,中国) resources
/////////////////////////////////////////////////////////////////////////////
#ifndef APSTUDIO_INVOKED
/////////////////////////////////////////////////////////////////////////////
//
// Generated from the TEXTINCLUDE 3 resource.
//
/////////////////////////////////////////////////////////////////////////////
#endif // not APSTUDIO_INVOKED
资源头文件
//{{NO_DEPENDENCIES}}
// Microsoft Visual C++ 生成的包含文件。
// 供 Exec.rc 使用
//
#define IDC_MYICON 2
#define IDD_EXEC_DIALOG 102
#define IDM_ABOUT 104
#define IDR_MAINFRAME 128
#define IDD_DIALOG_MAIN 129
#define IDI_ICON1 134
#define IDI_ICON 134
#define IDC_EDIT1 1001
#define IDC_FILE_PATH 1001
#define IDC_BUTTON_OPEN 1002
#define IDC_RICHEDIT21 1003
#define IDC_RICHEDIT2_LOG 1003
#define ID_RUN 1004
#define IDC_EXIT 1005
#define IDC_EDIT3 1008
#define IDC_STATIC -1
// Next default values for new objects
//
#ifdef APSTUDIO_INVOKED
#ifndef APSTUDIO_READONLY_SYMBOLS
#define _APS_NO_MFC 1
#define _APS_NEXT_RESOURCE_VALUE 135
#define _APS_NEXT_COMMAND_VALUE 32771
#define _APS_NEXT_CONTROL_VALUE 1009
#define _APS_NEXT_SYMED_VALUE 110
#endif
#endif
BOOL AppendTextToRich(TCHAR *text, int len)
{
//1、自动换行
//2、自动滚动
//3、尾行添加文本
//4、重新开始时自动清除所有内容
if (hWnd != NULL)
{
HWND hwndrich=GetDlgItem(hWnd,IDC_RICHEDIT2_LOG);
for (int i = 0; i < 2; i++)
{
SETTEXTEX stex = { ST_NEWCHARS, 1200 };
SendMessage(hwndrich, EM_SETTEXTEX, (WPARAM)&stex, (LPARAM)L"\r\n");
SendMessage(hwndrich, EM_SETTEXTEX, (WPARAM)&stex, (LPARAM)L"h a");
}
return TRUE;
}
else
return FALSE;
}
以上是我的代码,麻烦知道的给个参考