找了几个月工作,未果,今天试着写了一个简单的记事本程序,不过出现了一个问题,程序运行的窗口不可以编辑,这个问题很困惑,我对比源代码找了好久,始终找不到问题所在,希望好心人帮我看一下。为了方便调试,我把完整代码发出来,代码如下:
// 记事本程序.cpp : Defines the entry point for the application.
//
#include "stdafx.h"
#include <windows.h>
#include "resource.h"
LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM);
TCHAR szAppName[] = TEXT ("ClipBoard") ;
int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
PSTR szCmdLine, int iCmdShow)
{
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 (hInstance, szAppName) ;
wndclass.hCursor = LoadCursor (NULL, IDC_ARROW) ;
wndclass.hbrBackground = (HBRUSH) GetStockObject (WHITE_BRUSH) ;
wndclass.lpszMenuName = szAppName ;
wndclass.lpszClassName = szAppName ;
if (!RegisterClass (&wndclass))
{
MessageBox (NULL, TEXT ("This program requires Windows NT!"),
szAppName, MB_ICONERROR) ;
return 0 ;
}
hwnd = CreateWindow (szAppName, szAppName,
WS_OVERLAPPEDWINDOW,
GetSystemMetrics (SM_CXSCREEN) / 4,
GetSystemMetrics (SM_CYSCREEN) / 4,
GetSystemMetrics (SM_CXSCREEN) / 2,
GetSystemMetrics (SM_CYSCREEN) / 2,
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 HWND hwndedit;
int iselect,iEnable;
switch(message)
{
case WM_CREATE:
hwndedit = CreateWindow("NOTEPAD",NULL,WS_CHILD | WS_VISIBLE |
WS_BORDER | ES_LEFT | ES_MULTILINE ,
0, 0, 0, 0, hwnd, NULL,
LPCREATESTRUCT(lparam)->hInstance, HMENU(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_INITMENUPOPUP:
EnableMenuItem((HMENU)wparam,IDM_UNDO,
SendMessage(hwndedit,EM_CANUNDO,0,0)?MF_ENABLED:MF_GRAYED);
EnableMenuItem((HMENU)wparam,IDM_PASTE,
IsClipboardFormatAvailable(CF_TEXT)?MF_ENABLED:MF_GRAYED);
iselect = SendMessage(hwndedit,EM_GETSEL,0,0);
iEnable = (LOWORD(iselect)==HIWORD(iselect))?MF_GRAYED:MF_ENABLED;
EnableMenuItem((HMENU)wparam,IDM_CUT,iEnable);
EnableMenuItem((HMENU)wparam,IDM_COPY,iEnable);
EnableMenuItem((HMENU)wparam,IDM_SELECT,iEnable);
return 0;
break;
case WM_COMMAND:
switch(LOWORD(wparam))
{
case IDM_FILE_OPEN:
case IDM_FILE_SAVE:
case IDM_FILE_SAVE_AS:
case IDM_FILE_CLOSE:
return 0;
case IDM_UNDO:
SendMessage(hwnd,WM_UNDO,0,0);
return 0;
case IDM_COPY:
SendMessage(hwnd,WM_COPY,0,0);
return 0;
case IDM_PASTE:
SendMessage(hwnd,WM_PASTE,0,0);
return 0;
case IDM_CUT:
SendMessage(hwnd,WM_CUT,0,0);
return 0;
case IDM_SELECT:
SendMessage(hwnd,EM_SETSEL,0,-1);
return 0;
}
break;
case WM_DESTROY:
PostQuitMessage(0);
return 0;
}
return DefWindowProc(hwnd,message,wparam,lparam);
}