#include<windows.h>
LRESULT CALLBACK WindowProc( HWND hwnd, // handle to window UINT uMsg, // message identifier WPARAM wParam, // first message parameter LPARAM lParam // second message parameter ); int WINAPI WinMain( HINSTANCE hInstance, // handle to current instance HINSTANCE hPrevInstance, // handle to previous instance LPSTR lpCmdLine, // pointer to command line int nCmdShow // show state of window ) { char lpszClassName[]="lll"; char lpszTitle[]="lllll"; HWND hwnd; MSG msg; WNDCLASS wndclass;
wndclass.cbClsExtra=0; wndclass.cbWndExtra=0; wndclass.hbrBackground=(HBRUSH)GetStockObject(WHITE_BRUSH); wndclass.hCursor=LoadCursor(NULL,IDC_ARROW); wndclass.hIcon=LoadIcon(NULL,IDI_APPLICATION); wndclass.hInstance=hInstance; wndclass.lpfnWndProc=WindowProc; wndclass.lpszClassName=lpszClassName; wndclass.lpszMenuName=NULL; wndclass.style=CS_HREDRAW | CS_VREDRAW;
if(!RegisterClass(&wndclass)) { MessageBeep(0); return FALSE; } hwnd=CreateWindow(lpszClassName, lpszTitle, WS_OVERLAPPEDWINDOW, 50,50, 700,500, NULL, NULL, hInstance, NULL); ShowWindow(hwnd,nCmdShow); UpdateWindow(hwnd); while(GetMessage(&msg,NULL,0,0)) { TranslateMessage(&msg); DispatchMessage(&msg); } return msg.wParam; } LRESULT CALLBACK WindowProc( HWND hwnd, // handle to window UINT uMsg, // message identifier WPARAM wParam, // first message parameter LPARAM lParam // second message parameter ) { switch(uMsg) { case WM_DESTROY: PostQuitMessage(0); break; default: return DefWindowProc(hwnd,uMsg,wParam,lParam); } return 0; } //////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////
--------------------Configuration: llll - Win32 Debug-------------------- Compiling... ddd.cpp Linking... LIBCD.lib(crt0.obj) : error LNK2001: unresolved external symbol _main Debug/llll.exe : fatal error LNK1120: 1 unresolved externals Error executing link.exe.
llll.exe - 2 error(s), 0 warning(s)
上面为什么会出现这两个错误呢?
加上以下的"按钮"代码后编译虽然没有了错误,但是只有框架,却没有按钮,这是不是又是初学者的错误啊!
#include <windows.h> #define IDB_PUSHBUTTON 10 HWND hwndPush; HINSTANCE hInst; char lpszClassName1[]="liitlepig"; LRESULT CALLBACK WindowProc( HWND hwnd, // handle to window UINT uMsg, // message identifier WPARAM wParam, // first message parameter LPARAM lParam // second message parameter ); int WINAPI WinMain( HINSTANCE hInstance, // handle to current instance HINSTANCE hPrevInstance, // handle to previous instance LPSTR lpCmdLine, // pointer to command line int nCmdShow // show state of window ) { char lpszClassName[]="lll"; char lpszTitle[]="lllll"; HWND hwnd; MSG msg; WNDCLASS wndclass;
wndclass.cbClsExtra=0; wndclass.cbWndExtra=0; wndclass.hbrBackground=(HBRUSH)GetStockObject(WHITE_BRUSH); wndclass.hCursor=LoadCursor(NULL,IDC_ARROW); wndclass.hIcon=LoadIcon(NULL,IDI_APPLICATION); wndclass.hInstance=hInstance; wndclass.lpfnWndProc=WindowProc; wndclass.lpszClassName=lpszClassName; wndclass.lpszMenuName=NULL; wndclass.style=CS_HREDRAW | CS_VREDRAW;
if(!RegisterClass(&wndclass)) { MessageBeep(0); return FALSE; } hwnd=CreateWindow(lpszClassName, lpszTitle, WS_OVERLAPPEDWINDOW, 50,50, 700,500, NULL, NULL, hInstance, NULL); ShowWindow(hwnd,nCmdShow); UpdateWindow(hwnd); while(GetMessage(&msg,NULL,0,0)) { TranslateMessage(&msg); DispatchMessage(&msg); } return msg.wParam; } LRESULT CALLBACK WindowProc( HWND hwnd, // handle to window UINT uMsg, // message identifier WPARAM wParam, // first message parameter LPARAM lParam // second message parameter ) { switch(uMsg) { case WM_CREATE: hwndPush=CreateWindow(lpszClassName1, "litt", BS_PUSHBUTTON|WS_CHILD|WS_VISIBLE, 50,50, 120,25, hwnd, (HMENU)IDB_PUSHBUTTON, hInst, NULL); break; case WM_DESTROY: PostQuitMessage(0); break; default: return DefWindowProc(hwnd,uMsg,wParam,lParam); } return 0; }