-
Makes Me So Blur!!!
hi,guys.here is my question.i want to type a something inside my window but cannot.The problem is,the window pop up everytimes i compiler but when i pressed any keys in my keyboards.The window is closed up already.
here is my code: pls solve it for me.
where is the error and problem?
Solution:i want to enter something inside my window.
here is the code:
pls run this program before u make any modification.as it contain two error in it.Thanks.
#include<windows.h>
long FAR PASCAL WndProc(HWND ,UINT, WPARAM,LPARAM);
int PASCAL WinMain(HANDLE hInst,HANDLE hPrevInstance,LPSTR lpszCmdLine,int nCmdShow)
{
HWND hWnd;
MSG msg;
WNDCLASS wndclass;
if(!hPrevInstance)
{
wndclass.style=0;
wndclass.lpfnWndProc=(WNDPROC)WndProc;
wndclass.cbClsExtra=0;
wndclass.cbWndExtra=0;
wndclass.hInstance=hInst;
wndclass.hIcon=LoadIcon(hInst,IDI_APPLICATION);
wndclass.hCursor=LoadCursor(NULL,IDC_ARROW);
wndclass.hbrBackground=GetStockObject(WHITE_BRUSH) ;
wndclass.lpszMenuName=MAKEINTRESOURCE(NULL); wndclass.lpszClassName="MyClass";
if(!RegisterClass(&wndclass))
return 0;
}
hWnd=CreateWindow("MyClass","Student Pre-App Form",WS_OVERLAPPEDWINDOW,10,10,300,300,NULL,NULL, hInst,NULL);
ShowWindow(hWnd,nCmdShow);
while(GetMessage(&msg,0,0,0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return(msg.wParam);
}
long FAR PASCAL WndProc(HWND hWnd ,UINT wMessage,WPARAM wParam,LPARAM lParam)
{
PAINTSTRUCT ps;
HDC hDC;
POINT pt;
TEXTMETRIC tm;
static int nCurPos=0;
static char cBuf[128];
static int nwidth,nheight;
static HFONT hFont,hOldFont;
switch(wMessage)
{
case WM_CREATE:
CreateCaret(hWnd,NULL,10,10);
ShowCaret(hWnd);
SetCaretPos(0,0);
hDC=GetDC(hWnd);
GetTextMetrics(hDC,&tm);
ReleaseDC(hWnd,hDC);
nwidth=tm.tmAveCharWidth;
nheight=tm.tmHeight;
}
break;
switch(wMessage)
{
case WM_PAINT:
hDC=BeginPaint(hWnd,&ps);
SetTextColor(hDC,RGB(0,0,255));
TextOut(hDC,0,0,cBuf,lstrlen(cBuf));
SelectObject(hDC,hOldFont);
EndPaint(hWnd,&ps);
}
break;
case WM_CHAR:
if(nCurPos<127&&(IsCharAlphaNumeric((char)wParam)) )
{
cBuf[nCurPos++]=(char)wParam;
cBuf[nCurPos]=0;
InvalidateRect(hWnd,NULL,TRUE);
}
break;
case WM_KEYDOWN:
GetCaretPos(&pt);
switch(wParam)
{
case VK_LEFT:
pt.x-=nwidth;
break;
case VK_RIGHT:
pt.x+=nwidth;
break;
case VK_UP:
pt.y-=nheight;
break;
case VK_DOWN:
pt.y+=nheight;
break;
case VK_HOME:
pt.y=0;
pt.x=0;
break;
case VK_END:
pt.y=1;
pt.x=1;
break;
case VK_BACK:
if(nCurPos>0)
{
nCurPos--;
cBuf[nCurPos]=0;
InvalidateRect (hWnd,NULL,TRUE);
}
break;
SetCaretPos(pt.x,pt.y);
break;
}
case WM_DESTROY:
DestroyCaret();
PostQuitMessage(0);
break;
default:
return DefWindowProc (hWnd,wMessage,wParam,lParam);
}
return 0;
}
-
Your wndproc is falling through to WM_DESTROY whenever it gets a WM_KEYDOWN message due to the way you've wriiten your break statements and closing braces. This will allow text input (the only WM_KEYDOWN that has any effect on the text is VK_BACK) -
Code:
long FAR PASCAL WndProc(HWND hWnd ,UINT wMessage,WPARAM wParam,LPARAM lParam)
{
PAINTSTRUCT ps;
HDC hDC;
POINT pt;
TEXTMETRIC tm;
static int nCurPos=0;
static char cBuf[128];
static int nwidth,nheight;
static HFONT hFont,hOldFont;
switch(wMessage)
{
case WM_CREATE:
CreateCaret(hWnd,NULL,10,10);
ShowCaret(hWnd);
SetCaretPos(0,0);
hDC=GetDC(hWnd);
GetTextMetrics(hDC,&tm);
ReleaseDC(hWnd,hDC);
nwidth=tm.tmAveCharWidth;
nheight=tm.tmHeight;
break;
case WM_PAINT:
hDC=BeginPaint(hWnd,&ps);
SetTextColor(hDC,RGB(0,0,255));
TextOut(hDC,0,0,cBuf,lstrlen(cBuf));
SelectObject(hDC,hOldFont);
EndPaint(hWnd,&ps);
break;
case WM_CHAR:
if(nCurPos<127&&(IsCharAlphaNumeric((char)wParam)))
{
cBuf[nCurPos++]=(char)wParam;
cBuf[nCurPos]=0;
InvalidateRect(hWnd,NULL,TRUE);
}
break;
case WM_KEYDOWN:
GetCaretPos(&pt);
switch(wParam)
{
case VK_LEFT:
pt.x-=nwidth;
break;
case VK_RIGHT:
pt.x+=nwidth;
break;
case VK_UP:
pt.y-=nheight;
break;
case VK_DOWN:
pt.y+=nheight;
break;
case VK_HOME:
pt.y=0;
pt.x=0;
break;
case VK_END:
pt.y=1;
pt.x=1;
break;
case VK_BACK:
if(nCurPos>0)
{
nCurPos--;
cBuf[nCurPos]=0;
InvalidateRect (hWnd,NULL,TRUE);
}
break;
}
SetCaretPos(pt.x,pt.y);
break;
case WM_DESTROY:
DestroyCaret();
PostQuitMessage(0);
break;
default:
return DefWindowProc (hWnd,wMessage,wParam,lParam);
}
return 0;
}