这个类就相当于一个简易的文本编辑器,这个文本编辑器的窗口又绝大部分文本编辑器的特性:可以Ctrl+c,+v+x进行复制粘贴剪切等基本操作
声明方法:
hWnd = CreateWindow(TEXT("edit"),NULL,WS_CHILD|WS_VISIBLE|...,0,0,0,0,hWndPar,(HMENU)ID,hInstance,NULL)
其中窗口大小要通过MoveWindow进行修改,中间的…是可选项,这里可选项很多,就不一一例举了,并且可以通过像向这个窗口SendMessage加上相应的消息就可以让其返回一些需要的东西:
EM_GETLINECOUNT,得到行数(最后两个参数为0就好)
EM_LINELENGTH,得到一行的文本长度(wParam需要获得所需要的行的号)
EM_GETLINE,得到一整行的内容到缓冲区(wParam为行号,lParam为缓冲区指针)
同样的,可以通过SetWindowLong来设置一个额外的窗口过程来增加其消息特性(窗口子类化)
列表框就是类似于一个列表里面是可供选定的文本串
创建方法:
hWnd = CreateWindow(TEXT("lisybox"),NULL,WS_CHILD|WV_VISIBLE...,x,y,Width,Height,hWndPar,(HMENU)ID,hInstance,NULL);
现在来整理几个这个窗口类的几个特殊的消息:
LB_ADDSTRING,LB_INSTERSTRING前者是当有LBS_SORT风格时插入字符串的方法(SendMessage(hWnd,….,0.(LPARAM)szString)后者是没有的,并且,这个插入将返回一个值,当返回为LB_ERRSPACE时表示超过了可用的内存空间,LB_ERR表示其他的原因,返回0表示正确
LB_DELETESTRING,LB_RESETCONTEXT前者是删除一行wParam就是行数,后者是删除所有
下面是一个有意义的例子:
/*---------------------------------------------
HEAD.C -- Displays beginning (head) of file
(c) Charles Petzold, 1998
---------------------------------------------*/
#include
#define ID_LIST 1
#define ID_TEXT 2
#define MAXREAD 8192
#define DIRATTR (DDL_READWRITE | DDL_READONLY | DDL_HIDDEN | DDL_SYSTEM | \
DDL_DIRECTORY | DDL_ARCHIVE | DDL_DRIVES)
#define DTFLAGS (DT_WORDBREAK | DT_EXPANDTABS | DT_NOCLIP | DT_NOPREFIX)
LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM) ;
LRESULT CALLBACK ListProc (HWND, UINT, WPARAM, LPARAM) ;
WNDPROC OldList ;
int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
PSTR szCmdLine, int iCmdShow)
{
static TCHAR szAppName[] = TEXT ("head") ;
HWND hwnd ;
MSG msg ;
WNDCLASS wndclass ;
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) (COLOR_BTNFACE + 1) ;
wndclass.lpszMenuName = NULL ;
wndclass.lpszClassName = szAppName ;
if (!RegisterClass (&wndclass))
{
MessageBox (NULL, TEXT ("This program requires Windows NT!"),
szAppName, MB_ICONERROR) ;
return 0 ;
}
hwnd = CreateWindow (szAppName, TEXT ("head"),
WS_OVERLAPPEDWINDOW | WS_CLIPCHILDREN,
CW_USEDEFAULT, CW_USEDEFAULT,
CW_USEDEFAULT, CW_USEDEFAULT,
NULL, NULL, hInstance, NULL) ;
ShowWindow (hwnd, iCmdShow) ;
UpdateWindow (hwnd) ;
while (GetMessage (&msg, NULL, 0, 0))
{
TranslateMessage (&msg) ;
DispatchMessage (&msg) ;
}
return msg.wParam ;
}
LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
static BOOL bValidFile ;
static BYTE buffer[MAXREAD] ;
static HWND hwndList, hwndText ;
static RECT rect ;
static TCHAR szFile[MAX_PATH + 1] ;
HANDLE hFile ;
HDC hdc ;
int i, cxChar, cyChar ;
PAINTSTRUCT ps ;
TCHAR szBuffer[MAX_PATH + 1] ;
switch (message)
{
case WM_CREATE :
cxChar = LOWORD (GetDialogBaseUnits ()) ;
cyChar = HIWORD (GetDialogBaseUnits ()) ;
rect.left = 20 * cxChar ;
rect.top = 3 * cyChar ;
hwndList = CreateWindow (TEXT ("listbox"), NULL,
WS_CHILDWINDOW | WS_VISIBLE | LBS_STANDARD,
cxChar, cyChar * 3,
cxChar * 13 + GetSystemMetrics (SM_CXVSCROLL),
cyChar * 10,
hwnd, (HMENU) ID_LIST,
(HINSTANCE) GetWindowLong (hwnd, GWL_HINSTANCE),
NULL) ;
GetCurrentDirectory (MAX_PATH + 1, szBuffer) ;
hwndText = CreateWindow (TEXT ("static"), szBuffer,
WS_CHILDWINDOW | WS_VISIBLE | SS_LEFT,
cxChar, cyChar, cxChar * MAX_PATH, cyChar,
hwnd, (HMENU) ID_TEXT,
(HINSTANCE) GetWindowLong (hwnd, GWL_HINSTANCE),
NULL) ;
OldList = (WNDPROC) SetWindowLong (hwndList, GWL_WNDPROC,
(LPARAM) ListProc) ;
SendMessage (hwndList, LB_DIR, DIRATTR, (LPARAM) TEXT ("*.*")) ;
return 0 ;
case WM_SIZE :
rect.right = LOWORD (lParam) ;
rect.bottom = HIWORD (lParam) ;
return 0 ;
case WM_SETFOCUS :
SetFocus (hwndList) ;
return 0 ;
case WM_COMMAND :
if (LOWORD (wParam) == ID_LIST && HIWORD (wParam) == LBN_DBLCLK)
{
if (LB_ERR == (i = SendMessage (hwndList, LB_GETCURSEL, 0, 0)))
break ;
SendMessage (hwndList, LB_GETTEXT, i, (LPARAM) szBuffer) ;
if (INVALID_HANDLE_VALUE != (hFile = CreateFile (szBuffer,
GENERIC_READ, FILE_SHARE_READ, NULL,
OPEN_EXISTING, 0, NULL)))
{
CloseHandle (hFile) ;
bValidFile = TRUE ;
lstrcpy (szFile, szBuffer) ;
GetCurrentDirectory (MAX_PATH + 1, szBuffer) ;
if (szBuffer [lstrlen (szBuffer) - 1] != '\\')
lstrcat (szBuffer, TEXT ("\\")) ;
SetWindowText (hwndText, lstrcat (szBuffer, szFile)) ;
}
else
{
bValidFile = FALSE ;
szBuffer [lstrlen (szBuffer) - 1] = '\0' ;
// If setting the directory doesn't work, maybe it's
// a drive change, so try that.
if (!SetCurrentDirectory (szBuffer + 1))
{
szBuffer [3] = ':' ;
szBuffer [4] = '\0' ;
SetCurrentDirectory (szBuffer + 2) ;
}
// Get the new directory name and fill the list box.
GetCurrentDirectory (MAX_PATH + 1, szBuffer) ;
SetWindowText (hwndText, szBuffer) ;
SendMessage (hwndList, LB_RESETCONTENT, 0, 0) ;
SendMessage (hwndList, LB_DIR, DIRATTR,
(LPARAM) TEXT ("*.*")) ;
}
InvalidateRect (hwnd, NULL, TRUE) ;
}
return 0 ;
case WM_PAINT :
if (!bValidFile)
break ;
if (INVALID_HANDLE_VALUE == (hFile = CreateFile (szFile, GENERIC_READ,
FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, NULL)))
{
bValidFile = FALSE ;
break ;
}
ReadFile (hFile, buffer, MAXREAD, &i, NULL) ;
CloseHandle (hFile) ;
// i now equals the number of bytes in buffer.
// Commence getting a device context for displaying text.
hdc = BeginPaint (hwnd, &ps) ;
SelectObject (hdc, GetStockObject (SYSTEM_FIXED_FONT)) ;
SetTextColor (hdc, GetSysColor (COLOR_BTNTEXT)) ;
SetBkColor (hdc, GetSysColor (COLOR_BTNFACE)) ;
// Assume the file is ASCII
DrawTextA (hdc, buffer, i, &rect, DTFLAGS) ;
EndPaint (hwnd, &ps) ;
return 0 ;
case WM_DESTROY :
PostQuitMessage (0) ;
return 0 ;
}
return DefWindowProc (hwnd, message, wParam, lParam) ;
}
LRESULT CALLBACK ListProc (HWND hwnd, UINT message,
WPARAM wParam, LPARAM lParam)
{
if (message == WM_KEYDOWN && wParam == VK_RETURN)
SendMessage (GetParent (hwnd), WM_COMMAND,
MAKELONG (1, LBN_DBLCLK), (LPARAM) hwnd) ;
return CallWindowProc (OldList, hwnd, message, wParam, lParam) ;
}
这里涉及到了一个非常强的消息:LB_DIR这个消息是一个类之前的LB_ADDSTRING,LB_INSTERSTRING消息的东西,它的作用是把文件目录填充到列表,斌且可以通过改变之前的wParam的值来让它显示不同属性的文件(是否显示隐藏文件,系统文件,等。。。。),lParam的参数则是一个文件说明的执政,这个说明穿不影响子目录但是可以通过它修改只显示文件的类型如.cpp就是只显示cpp后缀文件,为*.*时则是所有文件类型。最后这里将子窗口类化,加了一个回车选文件的方式。