刚开始学习孙鑫的《VC++深入详解》,按照上面敲了第一个窗口程序WinMain。编写完编译没错误(完全按照书上的代码),但是运行的时候却不能生成窗口,什么反应都没有,打开进程看,却有一个WinMain.exe程序在运行。
究竟是什么问题啊?望前辈们解答一下小弟!
PS:我刚开始用VS2010敲,没反应,然后把代码复制过VC6.0,但是运行还是没反应..
究竟是什么问题啊?望前辈们解答一下小弟!
PS:我刚开始用VS2010敲,没反应,然后把代码复制过VC6.0,但是运行还是没反应..
http://www.vckbase.com/code/viewcode.asp?id=1560
#include "stdafx.h"
#include <stdio.h>LRESULT CALLBACK WinSunProc(
HWND hwnd,
UINT uMsg,
WPARAM wParam,
LPARAM lParam
);int WINAPI WinMain(
HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdshow
)
{
WNDCLASS wndcls;
wndcls.cbClsExtra=0;
wndcls.cbWndExtra=0;
wndcls.hbrBackground=(HBRUSH)GetStockObject(BLACK_BRUSH);
wndcls.hCursor=LoadIcon(NULL,IDC_CROSS);
wndcls.hInstance=hInstance;
wndcls.lpfnWndProc=WinSunProc;
wndcls.lpszClassName="liuzhenyang";
wndcls.lpszMenuName=NULL;
wndcls.style=CS_HREDRAW | CS_VREDRAW;
RegisterClass (&wndcls); HWND hwnd;
hwnd=CreateWindow("liuzhenyang","love xiyao",WS_OVERLAPPEDWINDOW,0,0,600,400,NULL,NULL,hInstance,NULL); ShowWindow(hwnd,SW_SHOWNORMAL);
UpdateWindow(hwnd); MSG msg;
while(GetMessage(&msg,NULL,0,0))
{
TranslateMessage (&msg);
DispatchMessage(&msg);
}
return msg.wParam;
}LRESULT CALLBACK WinSunProc(
HWND hwnd,
UINT uMsg,
WPARAM wParam,
LPARAM lParam
)
{
switch(uMsg)
{
case WM_CHAR:
char szChar[20];
sprintf(szChar,"char code is %d",wParam);
MessageBox(hwnd,szChar,"char ",0);
break;
case WM_LBUTTONDOWN:
MessageBox(hwnd,"mouse clicked","message",0);
HDC hdc;
hdc=GetDC(hwnd);
TextOut(hdc,0,50,"love汐夭",strlen("love汐夭"));
//ReleaseDC(hwnd,hdc);
break;
case WM_PAINT:
HDC hDC;
PAINTSTRUCT ps;
hDC=BeginPaint(hwnd,&ps);
TextOut(hDC,0,0,"http://www.liuzhenyang.org",strlen("http://www.liuzhenyang.org"));
EndPaint(hwnd,&ps);
break;
case WM_CLOSE:
if(IDYES==MessageBox(hwnd,"是否要真的退出?","message",MB_YESNO))
{
DestroyWindow(hwnd);
}
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hwnd,uMsg,wParam,lParam);
}
return 0;
}这个是代码,就是就是改了一下孙鑫第一个程序输出的字符串而已..
ShowWindow(hwnd,SW_SHOWNORMAL)这个也是有的啊。搞不懂,怎么也建立不了窗口..就是没反应还是先多谢各位了,但是窗口建立不了,搞得我都没信心把这本书学下去了..
ZeroMemory( &wndcls, sizeof( WNDCLASS ) );加了这一句,直接搞定。
你Debug 把Memory 调出来看下内存就知道了