C++中怎样设置一个窗口,使在窗口中输入数据 5
1个回答
展开全部
我是用C++的api做的,有点麻烦!
#include <windows.h>
#include "resource.h"
#include "stdlib.h"
#define BUFFER(x,y)*(pBuffer+y*cxBuffer+x)//预定义即申请一个新的内存空间
LRESULT CALLBACK WndProc(HWND,UINT,WPARAM,LPARAM);
char szAppName[]="first";
int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,PSTR szCmdLine,int iCmdShow)//winmain主函数
{
// static char szAppName[]="实例";//窗口类名
HWND hwnd;//窗口句柄
MSG msg;//Windows 消息
WNDCLASSEX wndclass;//窗口类
wndclass.cbSize=sizeof(wndclass);
wndclass.style=CS_HREDRAW|CS_VREDRAW;
wndclass.lpfnWndProc=WndProc;//窗口处理函数
wndclass.cbClsExtra=0;
wndclass.cbWndExtra=0;
wndclass.hInstance=hInstance;
wndclass.hIcon=LoadIcon(hInstance,MAKEINTRESOURCE(MYICON1));
wndclass.hCursor=LoadCursor(NULL,IDC_ARROW);
wndclass.hbrBackground=(HBRUSH)GetStockObject(WHITE_BRUSH);
wndclass.lpszMenuName=MAKEINTRESOURCE(IDR_MENU1);
wndclass.lpszClassName=szAppName;
// wndclass.hIconSm=LoadIcon(NULL,MAKEINTRESOURCE(MYICON2));
wndclass.hIconSm=LoadIcon(hInstance,MAKEINTRESOURCE(MYICON2));
if(!RegisterClassEx(&wndclass))//如果注册失败则发出警告
{
MessageBeep(0);
return FALSE;
}
HMENU hMenu;
hMenu=LoadMenu(hInstance,MAKEINTRESOURCE(IDR_MENU1));
hwnd=CreateWindow(szAppName,
"我的实例9-1之第一个窗口",
WS_OVERLAPPEDWINDOW,
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 iMsg,WPARAM wParam,LPARAM lParam)//窗口处理函数|消息处理函数
{
static char *pBuffer=NULL;
static int cxChar,cyChar,cxClient,cyClient,cxBuffer,cyBuffer,xCaret,yCaret;
HDC hdc;
int x,y,i;
PAINTSTRUCT ps;
TEXTMETRIC tm;
static int iColorID[5]={WHITE_BRUSH,LTGRAY_BRUSH,GRAY_BRUSH,DKGRAY_BRUSH,BLACK_BRUSH};
static int iSelection=ID_MENUITEM40013;
HMENU hMenu;
switch(iMsg)
{
case WM_COMMAND:
hMenu=GetMenu(hwnd);
switch(LOWORD(wParam))
{
case ID_MENUITEM40001:
case ID_MENUITEM40002:
case ID_MENUITEM40003:
MessageBeep(0);
return 0;
case ID_MENUITEM40004:
SendMessage(hwnd,WM_CLOSE,0,0L);
return 0;
case ID_MENUITEM40005:
case ID_MENUITEM40006:
case ID_MENUITEM40007:
case ID_MENUITEM40008:
case ID_MENUITEM40018:
case ID_MENUITEM40019:
case ID_MENUITEM40020:
case ID_MENUITEM40021:
case ID_MENUITEM40022:
CheckMenuItem(hMenu,iSelection,MF_UNCHECKED);
iSelection=LOWORD(wParam);
CheckMenuItem(hMenu,iSelection,MF_CHECKED);
SetClassLong(hwnd,GCL_HBRBACKGROUND,(LONG)GetStockObject(iColorID[LOWORD(wParam)-ID_MENUITEM40018]));
InvalidateRect(hwnd,NULL,TRUE);
return 0;
case ID_MENUITEM40011:
MessageBox(hwnd,"Help not yet implemented!",szAppName,MB_ICONEXCLAMATION|MB_OK);
return 0;
case ID_MENUITEM40012:
MessageBox(hwnd,"本记事本运用C++技术制作,版权为本公司霹雳小组所有,违者必究!",szAppName,MB_ICONINFORMATION|MB_OK);
return 0;
}
break;
/* case WM_TIMER:
MessageBeep(0);
return 0;
case WM_DESTROY:
PostQuitMessage(0);//调用函数postquitmessage退出程序
return 0;
case WM_CREATE:
hdc=GetDC(hwnd);
SelectObject(hdc,GetStockObject(SYSTEM_FIXED_FONT));
GetTextMetrics(hdc,&tm);
cxChar=tm.tmAveCharWidth;
cyChar=tm.tmHeight;
ReleaseDC(hwnd,hdc);
return 0;
case WM_SIZE:
cxClient=LOWORD(lParam);
cyClient=HIWORD(lParam);
cxBuffer=max(1,cxClient/cxChar);
cyBuffer=max(1,cyClient/cyChar);
if(pBuffer!=NULL)
free(pBuffer);
if((pBuffer=(char*)malloc(cxBuffer*cyBuffer))==NULL)
MessageBox(hwnd,"窗口太大,没有足够的内存,请你适当的减小一点","Typer",MB_ICONEXCLAMATION|MB_OK);
else
for(y=0;y<cyBuffer;y++)
for(x=0;x<cxBuffer;x++)
BUFFER(x,y)=' ';
xCaret=0;
yCaret=0;
if(hwnd==GetFocus())//获得焦点
SetCaretPos(xCaret*cxChar,yCaret*cyChar);//设置光标
return 0;
case WM_SETFOCUS:
CreateCaret(hwnd,NULL,cxChar,cyChar);
SetCaretPos(xCaret*cxChar,yCaret*cyChar);
ShowCaret(hwnd);
return 0;
case WM_KILLFOCUS:
HideCaret(hwnd);
DestroyCaret();
return 0;
case WM_KEYDOWN:
switch(wParam)//扫描码翻译成虚拟码,虚拟码存在键盘的wparam参数中
{
case VK_HOME://home键
xCaret=0;
break;
case VK_END://end键
xCaret=cxBuffer-1;
break;
case VK_PRIOR://pageup键
yCaret=0;
break;
case VK_NEXT://pagedown键
yCaret=cyBuffer-1;
break;
case VK_LEFT://左箭头键
xCaret=max(xCaret-1,0);
break;
case VK_RIGHT://右箭头键
xCaret=min(xCaret+1,0);
break;
case VK_UP://上箭头键
yCaret=max(yCaret-1,0);
break;
case VK_DOWN://下箭头键
yCaret=min(yCaret+1,cyBuffer-1);
break;
case VK_DELETE://del键
for(x=xCaret;x<cxBuffer-1;x++)
BUFFER(x,yCaret)=BUFFER(x+1,yCaret);
BUFFER(cxBuffer-1,yCaret)=' ';
HideCaret(hwnd);
hdc=GetDC(hwnd);
SelectObject(hdc,GetStockObject(SYSTEM_FIXED_FONT));
TextOut(hdc,xCaret*cxChar,yCaret*cyChar,&BUFFER(xCaret,yCaret),cxBuffer-xCaret);
ShowCaret(hwnd);
ReleaseDC(hwnd,hdc);
break;
}
SetCaretPos(xCaret*cxChar,yCaret*cyChar);
return 0;
case WM_CHAR://
for(i=0;i<(int)LOWORD(lParam);i++)
{
switch(wParam)
{
case '\b':
if(xCaret>0)
{
xCaret--;
SendMessage(hwnd,WM_KEYDOWN,VK_DELETE,1L);
}
break;
case '\t':
do
{
SendMessage(hwnd,WM_CHAR,' ',1L);
}
while(xCaret % 8!=0);
break;
case '\n':
if(++yCaret==cyBuffer)
yCaret=0;
break;
case '\r':
xCaret=0;
if(++yCaret==cyBuffer)
yCaret=0;
break;
case '\x1B':
for(y=0;y<cyBuffer;y++)
for(x=0;x<cxBuffer;x++)
BUFFER(x,y)=' ';
xCaret=0;
yCaret=0;
InvalidateRect(hwnd,NULL,FALSE);
break;
default:
BUFFER(xCaret,yCaret)=(char)wParam;
HideCaret(hwnd);
hdc=GetDC(hwnd);
SelectObject(hdc,GetStockObject(SYSTEM_FIXED_FONT));
TextOut(hdc,xCaret*cxChar,yCaret*cyChar,&BUFFER(xCaret,yCaret),1);
ShowCaret(hwnd);
ReleaseDC(hwnd,hdc);
if(++xCaret==cxBuffer)
{
xCaret=0;
if(++yCaret==cyBuffer)
yCaret=0;
}
break;
}
}
SetCaretPos(xCaret*cxChar,yCaret*cyChar);
return 0;
case WM_PAINT:
hdc=BeginPaint(hwnd,&ps);
SelectObject(hdc,GetStockObject(SYSTEM_FIXED_FONT));
for(y=0;y<cyBuffer;y++)
TextOut(hdc,0,y*cyChar,&BUFFER(0,y),cxBuffer);
EndPaint(hwnd,&ps);
return 0;*/
}
return DefWindowProc(hwnd,iMsg,wParam,lParam);
}
#include <windows.h>
#include "resource.h"
#include "stdlib.h"
#define BUFFER(x,y)*(pBuffer+y*cxBuffer+x)//预定义即申请一个新的内存空间
LRESULT CALLBACK WndProc(HWND,UINT,WPARAM,LPARAM);
char szAppName[]="first";
int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,PSTR szCmdLine,int iCmdShow)//winmain主函数
{
// static char szAppName[]="实例";//窗口类名
HWND hwnd;//窗口句柄
MSG msg;//Windows 消息
WNDCLASSEX wndclass;//窗口类
wndclass.cbSize=sizeof(wndclass);
wndclass.style=CS_HREDRAW|CS_VREDRAW;
wndclass.lpfnWndProc=WndProc;//窗口处理函数
wndclass.cbClsExtra=0;
wndclass.cbWndExtra=0;
wndclass.hInstance=hInstance;
wndclass.hIcon=LoadIcon(hInstance,MAKEINTRESOURCE(MYICON1));
wndclass.hCursor=LoadCursor(NULL,IDC_ARROW);
wndclass.hbrBackground=(HBRUSH)GetStockObject(WHITE_BRUSH);
wndclass.lpszMenuName=MAKEINTRESOURCE(IDR_MENU1);
wndclass.lpszClassName=szAppName;
// wndclass.hIconSm=LoadIcon(NULL,MAKEINTRESOURCE(MYICON2));
wndclass.hIconSm=LoadIcon(hInstance,MAKEINTRESOURCE(MYICON2));
if(!RegisterClassEx(&wndclass))//如果注册失败则发出警告
{
MessageBeep(0);
return FALSE;
}
HMENU hMenu;
hMenu=LoadMenu(hInstance,MAKEINTRESOURCE(IDR_MENU1));
hwnd=CreateWindow(szAppName,
"我的实例9-1之第一个窗口",
WS_OVERLAPPEDWINDOW,
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 iMsg,WPARAM wParam,LPARAM lParam)//窗口处理函数|消息处理函数
{
static char *pBuffer=NULL;
static int cxChar,cyChar,cxClient,cyClient,cxBuffer,cyBuffer,xCaret,yCaret;
HDC hdc;
int x,y,i;
PAINTSTRUCT ps;
TEXTMETRIC tm;
static int iColorID[5]={WHITE_BRUSH,LTGRAY_BRUSH,GRAY_BRUSH,DKGRAY_BRUSH,BLACK_BRUSH};
static int iSelection=ID_MENUITEM40013;
HMENU hMenu;
switch(iMsg)
{
case WM_COMMAND:
hMenu=GetMenu(hwnd);
switch(LOWORD(wParam))
{
case ID_MENUITEM40001:
case ID_MENUITEM40002:
case ID_MENUITEM40003:
MessageBeep(0);
return 0;
case ID_MENUITEM40004:
SendMessage(hwnd,WM_CLOSE,0,0L);
return 0;
case ID_MENUITEM40005:
case ID_MENUITEM40006:
case ID_MENUITEM40007:
case ID_MENUITEM40008:
case ID_MENUITEM40018:
case ID_MENUITEM40019:
case ID_MENUITEM40020:
case ID_MENUITEM40021:
case ID_MENUITEM40022:
CheckMenuItem(hMenu,iSelection,MF_UNCHECKED);
iSelection=LOWORD(wParam);
CheckMenuItem(hMenu,iSelection,MF_CHECKED);
SetClassLong(hwnd,GCL_HBRBACKGROUND,(LONG)GetStockObject(iColorID[LOWORD(wParam)-ID_MENUITEM40018]));
InvalidateRect(hwnd,NULL,TRUE);
return 0;
case ID_MENUITEM40011:
MessageBox(hwnd,"Help not yet implemented!",szAppName,MB_ICONEXCLAMATION|MB_OK);
return 0;
case ID_MENUITEM40012:
MessageBox(hwnd,"本记事本运用C++技术制作,版权为本公司霹雳小组所有,违者必究!",szAppName,MB_ICONINFORMATION|MB_OK);
return 0;
}
break;
/* case WM_TIMER:
MessageBeep(0);
return 0;
case WM_DESTROY:
PostQuitMessage(0);//调用函数postquitmessage退出程序
return 0;
case WM_CREATE:
hdc=GetDC(hwnd);
SelectObject(hdc,GetStockObject(SYSTEM_FIXED_FONT));
GetTextMetrics(hdc,&tm);
cxChar=tm.tmAveCharWidth;
cyChar=tm.tmHeight;
ReleaseDC(hwnd,hdc);
return 0;
case WM_SIZE:
cxClient=LOWORD(lParam);
cyClient=HIWORD(lParam);
cxBuffer=max(1,cxClient/cxChar);
cyBuffer=max(1,cyClient/cyChar);
if(pBuffer!=NULL)
free(pBuffer);
if((pBuffer=(char*)malloc(cxBuffer*cyBuffer))==NULL)
MessageBox(hwnd,"窗口太大,没有足够的内存,请你适当的减小一点","Typer",MB_ICONEXCLAMATION|MB_OK);
else
for(y=0;y<cyBuffer;y++)
for(x=0;x<cxBuffer;x++)
BUFFER(x,y)=' ';
xCaret=0;
yCaret=0;
if(hwnd==GetFocus())//获得焦点
SetCaretPos(xCaret*cxChar,yCaret*cyChar);//设置光标
return 0;
case WM_SETFOCUS:
CreateCaret(hwnd,NULL,cxChar,cyChar);
SetCaretPos(xCaret*cxChar,yCaret*cyChar);
ShowCaret(hwnd);
return 0;
case WM_KILLFOCUS:
HideCaret(hwnd);
DestroyCaret();
return 0;
case WM_KEYDOWN:
switch(wParam)//扫描码翻译成虚拟码,虚拟码存在键盘的wparam参数中
{
case VK_HOME://home键
xCaret=0;
break;
case VK_END://end键
xCaret=cxBuffer-1;
break;
case VK_PRIOR://pageup键
yCaret=0;
break;
case VK_NEXT://pagedown键
yCaret=cyBuffer-1;
break;
case VK_LEFT://左箭头键
xCaret=max(xCaret-1,0);
break;
case VK_RIGHT://右箭头键
xCaret=min(xCaret+1,0);
break;
case VK_UP://上箭头键
yCaret=max(yCaret-1,0);
break;
case VK_DOWN://下箭头键
yCaret=min(yCaret+1,cyBuffer-1);
break;
case VK_DELETE://del键
for(x=xCaret;x<cxBuffer-1;x++)
BUFFER(x,yCaret)=BUFFER(x+1,yCaret);
BUFFER(cxBuffer-1,yCaret)=' ';
HideCaret(hwnd);
hdc=GetDC(hwnd);
SelectObject(hdc,GetStockObject(SYSTEM_FIXED_FONT));
TextOut(hdc,xCaret*cxChar,yCaret*cyChar,&BUFFER(xCaret,yCaret),cxBuffer-xCaret);
ShowCaret(hwnd);
ReleaseDC(hwnd,hdc);
break;
}
SetCaretPos(xCaret*cxChar,yCaret*cyChar);
return 0;
case WM_CHAR://
for(i=0;i<(int)LOWORD(lParam);i++)
{
switch(wParam)
{
case '\b':
if(xCaret>0)
{
xCaret--;
SendMessage(hwnd,WM_KEYDOWN,VK_DELETE,1L);
}
break;
case '\t':
do
{
SendMessage(hwnd,WM_CHAR,' ',1L);
}
while(xCaret % 8!=0);
break;
case '\n':
if(++yCaret==cyBuffer)
yCaret=0;
break;
case '\r':
xCaret=0;
if(++yCaret==cyBuffer)
yCaret=0;
break;
case '\x1B':
for(y=0;y<cyBuffer;y++)
for(x=0;x<cxBuffer;x++)
BUFFER(x,y)=' ';
xCaret=0;
yCaret=0;
InvalidateRect(hwnd,NULL,FALSE);
break;
default:
BUFFER(xCaret,yCaret)=(char)wParam;
HideCaret(hwnd);
hdc=GetDC(hwnd);
SelectObject(hdc,GetStockObject(SYSTEM_FIXED_FONT));
TextOut(hdc,xCaret*cxChar,yCaret*cyChar,&BUFFER(xCaret,yCaret),1);
ShowCaret(hwnd);
ReleaseDC(hwnd,hdc);
if(++xCaret==cxBuffer)
{
xCaret=0;
if(++yCaret==cyBuffer)
yCaret=0;
}
break;
}
}
SetCaretPos(xCaret*cxChar,yCaret*cyChar);
return 0;
case WM_PAINT:
hdc=BeginPaint(hwnd,&ps);
SelectObject(hdc,GetStockObject(SYSTEM_FIXED_FONT));
for(y=0;y<cyBuffer;y++)
TextOut(hdc,0,y*cyChar,&BUFFER(0,y),cxBuffer);
EndPaint(hwnd,&ps);
return 0;*/
}
return DefWindowProc(hwnd,iMsg,wParam,lParam);
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询