![]() |
#2
yangang22010-10-06 18:35
建议你自己先学会编写一个应用程序类和窗体类,用他们来派生,从而创建窗体。你的那些代码屏蔽了太多的底层信息,自己编一个应用程序类和窗体类有助于你了解那些屏蔽的底层信息到底是什么,同时也可提高你的编程能力。你的那些代码从某种程度上说对你的编程能力没有任何帮助。自己可以先初步了解一下windows编程和它的运行机理(消息响应机制)然后学会用c++的类对它们进行封装,到那时你会发现你自己也会写类似于上面的代码,达到你目的。我这里有写好的程序,你可以参考一下:
#include<windows.h> HINSTANCE hInst; HINSTANCE hInstance; MSG msg; char lpszClassName[]="window_class"; char *ShowText; LRESULT CALLBACK WndProc( HWND,UINT,WPARAM,LPARAM); void OnLButtonDown(HWND hWnd, UINT message,WPARAM wParam ,LPARAM lParam); void OnPaint(HWND hWnd, UINT message,WPARAM wParam ,LPARAM lParam); void OnDestroy(HWND hWnd, UINT message,WPARAM wParam ,LPARAM lParam); class CFrameWnd { public: HWND hWnd; public: int RegisterWindow(); void Create(LPCTSTR lpClassName, LPCTSTR lpWindowName); void ShowWindow(int nCmdShow); void UpdateWindow(); }; int CFrameWnd::RegisterWindow() { WNDCLASS wc; wc.style=0; wc.lpfnWndProc=WndProc; wc.cbClsExtra=0; wc.cbWndExtra=0; wc.hInstance=hInstance; wc.hIcon=LoadIcon(NULL,IDI_APPLICATION); wc.hCursor=LoadCursor(NULL,IDC_ARROW); wc.hbrBackground=(HBRUSH)GetStockObject(WHITE_BRUSH); wc.lpszMenuName=NULL; wc.lpszClassName=lpszClassName; return RegisterClass(&wc); } void CFrameWnd::Create(LPCTSTR lpClassName, LPCTSTR lpWindowName) { RegisterWindow(); hInst=hInstance; hWnd=CreateWindow(lpszClassName,lpWindowName, WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInstance, NULL); } void CFrameWnd::ShowWindow(int nCmdShow) { ::ShowWindow(hWnd,nCmdShow); } void CFrameWnd::UpdateWindow() { ::UpdateWindow(hWnd); } class CWinApp { public: CFrameWnd*m_pMainWnd; CWinApp *m_pCurrentWinApp; public: CWinApp(); virtual BOOL InitInstance(int nCmdShow); int Run(); ~CWinApp(); }; CWinApp::CWinApp() {m_pCurrentWinApp=this;} BOOL CWinApp::InitInstance(int nCmdShow) { m_pMainWnd=new CFrameWnd; m_pMainWnd->Create(NULL,"封装的Window程序"); m_pMainWnd->ShowWindow(nCmdShow); m_pMainWnd->UpdateWindow(); return TRUE; } int CWinApp::Run() { while(GetMessage(&msg,NULL,0,0)) { TranslateMessage(&msg); DispatchMessage(&msg); } return msg.wParam; } CWinApp::~CWinApp(){delete m_pMainWnd;} class CMyWnd:public CFrameWnd { }; class CMyApp:public CWinApp { public: BOOL InitInstance(int nCmdShow); }; CMyApp::InitInstance(int nCmdShow) { CMyWnd *pMainWnd; pMainWnd=new CMyWnd; pMainWnd->Create(NULL,"应用窗体的派生类程序"); pMainWnd->ShowWindow(nCmdShow); pMainWnd->UpdateWindow(); m_pMainWnd=pMainWnd; return TRUE; } CMyApp MyApp; CWinApp*AfxGetApp() { return MyApp.m_pCurrentWinApp; } int APIENTRY WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) { int ResultCode=-1; CWinApp *pApp; pApp=AfxGetApp(); pApp->InitInstance(nCmdShow); return ResultCode=pApp->Run(); } LRESULT CALLBACK WndProc( HWND hWnd, UINT message,WPARAM wParam,LPARAM lParam) { switch(message) { case WM_LBUTTONDOWN: OnLButtonDown(hWnd,message,wParam,lParam); break; case WM_PAINT: OnPaint(hWnd,message,wParam,lParam); break; case WM_DESTROY: OnDestroy(hWnd,message,wParam,lParam); break; default: return DefWindowProc(hWnd,message,wParam,lParam); } return 0; } void OnLButtonDown(HWND hWnd, UINT message,WPARAM wParam, LPARAM lParam) { ShowText="Hello!"; InvalidateRect(hWnd,NULL,1); } void OnPaint(HWND hWnd, UINT message,WPARAM wParam ,LPARAM lParam) { PAINTSTRUCT ps; HDC hdc; hdc=BeginPaint(hWnd,&ps); TextOut(hdc,50,50,ShowText,6); EndPaint(hWnd,&ps); } void OnDestroy(HWND hWnd, UINT message,WPARAM wParam ,LPARAM lParam) { PostQuitMessage(0); } |
最近在学习《c++核心思想》一书
在522页有这样一个程序:
#include<wx/wx.h>
/* a basic application that shows an empty frame
*/
class BasicApp:public wxApp //wxwindow框架的函数名都是以大写的字母开头的
{
public:
/*constructs the frame.*/
BasicApp();
/*shows the frame. return true*/
virtual bool OnInit();
private:
wxFrame* frame;
};
DECLARE_APP(BasicApp)
IMPLEMENT_APP(BasicApp)
BasicApp::BasicApp()
{
frame=new wxFrame(NULL,-1,"Yes,I can!");
}
bool BasicApp::OnInit ()
{
frame->Show(true);
return true;
}
我在网上下了个wxWidgets-2.8.7编译并搭建了开发环境:
编译通过
但build:
------------------Configuration: 图形界面test001 - Win32 Debug--------------------
Linking...
MSVCRTD.lib(MSVCRTD.dll) : error LNK2005: _free already defined in LIBCD.lib(dbgheap.obj)
MSVCRTD.lib(MSVCRTD.dll) : error LNK2005: _calloc already defined in LIBCD.lib(dbgheap.obj)
MSVCRTD.lib(MSVCRTD.dll) : error LNK2005: _malloc already defined in LIBCD.lib(dbgheap.obj)
MSVCRTD.lib(MSVCRTD.dll) : error LNK2005: _toupper already defined in LIBCD.lib(toupper.obj)
MSVCRTD.lib(MSVCRTD.dll) : error LNK2005: _strncpy already defined in LIBCD.lib(strncpy.obj)
MSVCRTD.lib(MSVCRTD.dll) : error LNK2005: _strchr already defined in LIBCD.lib(strchr.obj)
MSVCRTD.lib(MSVCRTD.dll) : error LNK2005: _strncmp already defined in LIBCD.lib(strncmp.obj)
MSVCRTD.lib(MSVCRTD.dll) : error LNK2005: _tolower already defined in LIBCD.lib(tolower.obj)
MSVCRTD.lib(MSVCRTD.dll) : error LNK2005: _sprintf already defined in LIBCD.lib(sprintf.obj)
MSVCRTD.lib(MSVCRTD.dll) : error LNK2005: _strstr already defined in LIBCD.lib(strstr.obj)
MSVCRTD.lib(MSVCRTD.dll) : error LNK2005: _memmove already defined in LIBCD.lib(memmove.obj)
MSVCRTD.lib(MSVCRTD.dll) : error LNK2005: _exit already defined in LIBCD.lib(crt0dat.obj)
MSVCRTD.lib(MSVCRTD.dll) : error LNK2005: _realloc already defined in LIBCD.lib(dbgheap.obj)
MSVCRTD.lib(MSVCRTD.dll) : error LNK2005: _strtol already defined in LIBCD.lib(strtol.obj)
MSVCRTD.lib(MSVCRTD.dll) : error LNK2005: _strtoul already defined in LIBCD.lib(strtol.obj)
MSVCRTD.lib(MSVCRTD.dll) : error LNK2005: _abort already defined in LIBCD.lib(abort.obj)
MSVCRTD.lib(MSVCRTD.dll) : error LNK2005: _fflush already defined in LIBCD.lib(fflush.obj)
MSVCRTD.lib(MSVCRTD.dll) : error LNK2005: __close already defined in LIBCD.lib(close.obj)
MSVCRTD.lib(MSVCRTD.dll) : error LNK2005: __write already defined in LIBCD.lib(write.obj)
MSVCRTD.lib(MSVCRTD.dll) : error LNK2005: __commit already defined in LIBCD.lib(commit.obj)
MSVCRTD.lib(MSVCRTD.dll) : error LNK2005: _fclose already defined in LIBCD.lib(fclose.obj)
MSVCRTD.lib(MSVCRTD.dll) : error LNK2005: __get_osfhandle already defined in LIBCD.lib(osfinfo.obj)
MSVCRTD.lib(MSVCRTD.dll) : error LNK2005: __open_osfhandle already defined in LIBCD.lib(osfinfo.obj)
MSVCRTD.lib(MSVCRTD.dll) : error LNK2005: __snprintf already defined in LIBCD.lib(snprintf.obj)
MSVCRTD.lib(MSVCRTD.dll) : error LNK2005: __CrtSetDbgFlag already defined in LIBCD.lib(dbgheap.obj)
LINK : warning LNK4098: defaultlib "MSVCRTD" conflicts with use of other libs; use /NODEFAULTLIB:library
wxmsw28d_core.lib(app.obj) : error LNK2001: unresolved external symbol __imp__InitCommonControls@0
wxmsw28d_core.lib(statbr95.obj) : error LNK2001: unresolved external symbol __imp__CreateStatusWindowA@16
wxmsw28d_core.lib(spinbutt.obj) : error LNK2001: unresolved external symbol __imp__CreateUpDownControl@48
wxmsw28d_core.lib(listctrl.obj) : error LNK2001: unresolved external symbol __imp__ImageList_GetIconSize@12
wxmsw28d_core.lib(imaglist.obj) : error LNK2001: unresolved external symbol __imp__ImageList_GetIconSize@12
wxmsw28d_core.lib(listctrl.obj) : error LNK2001: unresolved external symbol __imp__ImageList_Draw@24
wxmsw28d_core.lib(imaglist.obj) : error LNK2001: unresolved external symbol __imp__ImageList_Draw@24
wxmsw28d_core.lib(listctrl.obj) : error LNK2001: unresolved external symbol __imp__ImageList_GetImageCount@4
wxmsw28d_core.lib(imaglist.obj) : error LNK2001: unresolved external symbol __imp__ImageList_GetImageCount@4
wxmsw28d_core.lib(imaglist.obj) : error LNK2001: unresolved external symbol __imp__ImageList_Create@20
wxmsw28d_core.lib(dragimag.obj) : error LNK2001: unresolved external symbol __imp__ImageList_Create@20
wxmsw28d_core.lib(imaglist.obj) : error LNK2001: unresolved external symbol __imp__ImageList_Destroy@4
wxmsw28d_core.lib(dragimag.obj) : error LNK2001: unresolved external symbol __imp__ImageList_Destroy@4
wxmsw28d_core.lib(imaglist.obj) : error LNK2001: unresolved external symbol __imp__ImageList_Add@12
wxmsw28d_core.lib(dragimag.obj) : error LNK2001: unresolved external symbol __imp__ImageList_Add@12
wxmsw28d_core.lib(imaglist.obj) : error LNK2001: unresolved external symbol __imp__ImageList_AddMasked@12
wxmsw28d_core.lib(imaglist.obj) : error LNK2001: unresolved external symbol __imp__ImageList_ReplaceIcon@12
wxmsw28d_core.lib(dragimag.obj) : error LNK2001: unresolved external symbol __imp__ImageList_ReplaceIcon@12
wxmsw28d_core.lib(imaglist.obj) : error LNK2001: unresolved external symbol __imp__ImageList_Replace@16
wxmsw28d_core.lib(imaglist.obj) : error LNK2001: unresolved external symbol __imp__ImageList_Remove@8
wxmsw28d_core.lib(imaglist.obj) : error LNK2001: unresolved external symbol __imp__ImageList_SetBkColor@8
wxmsw28d_core.lib(imaglist.obj) : error LNK2001: unresolved external symbol __imp__ImageList_GetIcon@12
wxmsw28d_core.lib(dragimag.obj) : error LNK2001: unresolved external symbol __imp__ImageList_SetDragCursorImage@16
wxmsw28d_core.lib(dragimag.obj) : error LNK2001: unresolved external symbol __imp__ImageList_BeginDrag@16
wxmsw28d_core.lib(dragimag.obj) : error LNK2001: unresolved external symbol __imp__ImageList_EndDrag@0
wxmsw28d_core.lib(dragimag.obj) : error LNK2001: unresolved external symbol __imp__ImageList_DragMove@8
wxmsw28d_core.lib(dragimag.obj) : error LNK2001: unresolved external symbol __imp__ImageList_DragEnter@12
wxmsw28d_core.lib(dragimag.obj) : error LNK2001: unresolved external symbol __imp__ImageList_DragLeave@4
wxmsw28d_core.lib(uuid.obj) : error LNK2001: unresolved external symbol __imp__UuidToStringA@8
wxmsw28d_core.lib(uuid.obj) : error LNK2001: unresolved external symbol __imp__RpcStringFreeA@4
wxmsw28d_core.lib(uuid.obj) : error LNK2001: unresolved external symbol __imp__UuidCreate@4
wxmsw28d_core.lib(uuid.obj) : error LNK2001: unresolved external symbol __imp__UuidFromStringA@8
Debug/图形界面test001.exe : fatal error LNK1120: 25 unresolved externals
执行 link.exe 时出错.
图形界面test001.exe - 1 error(s), 0 warning(s)
这是怎么回事呀?
跪求各位高手看看,指点指点~~~~~!!!!