使用C++封装API失败...

diablox0147 2009-09-04 04:00:34
我试着用C++封装了3个API,分别是WNDCLASS,CreateWindow和CreateMenu..其实也没什么大东西,就是把一些不常用的东西在声明的时候加上了默认值。。。

然后在WinMain里:

WinClass wnclass(hInstance, winProc, "Playeur");
Window wnd("Playeur","Test", hInstance);
Show(wnd, winMode);
MSG msg;
while(GetMessage(&msg,NULL,0,0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}

编译时没错误,但是运行的时候除了那个黒框跳出来其他什么反映都没。。。咋回事?有什么头绪么?
...全文
116 16 打赏 收藏 转发到动态 举报
写回复
用AI写文章
16 条回复
切换为时间正序
请发表友善的回复…
发表回复
thy38 2009-09-05
  • 打赏
  • 举报
回复
if(menu.GetMenuHandle() != NULL) _menu = menu.GetMenuHandle();


你的Menu没有默认构造函数,那么menu.GetMenuHandle可能是个任意值,所以menu.GetMenuHandle() != NULL成立,所以_menu=menu.GetMenuHandle(),而这又是个任意值。

匆匆一看,不知说得对不对。

另外,请LZ贴多处代码时要标明文件名,也方便别人给你调试。
papaofdoudou 2009-09-05
  • 打赏
  • 举报
回复
       case WM_PAINT:
hdc = BeginPaint(hwnd,&ps);

EndPaint(hwnd, &ps);
return 0;
break;

问题可能处在这儿,客户区处于无效状态,进入死循环,你尝试画一个圈圈试试!
ketet 2009-09-05
  • 打赏
  • 举报
回复
代码很不规范。也没有错误检查。调用 api以后一定要先检查返回值,这样错了你也知道在哪里。
gx168853 2009-09-05
  • 打赏
  • 举报
回复
真是一帮牛人来着,学习来了~~~~~~~~
diablox0147 2009-09-05
  • 打赏
  • 举报
回复
上菜啦:
LRESULT CALLBACK winProc(HWND hwnd, UINT message, WPARAM lParam , LPARAM wParam)
{
PAINTSTRUCT ps;
HDC hdc;

switch(message)
{
case WM_CREATE:

return 0;
break;
case WM_PAINT:
hdc = BeginPaint(hwnd,&ps);

EndPaint(hwnd, &ps);
return 0;
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default: break;
}
return DefWindowProc(hwnd, message, wParam, lParam);
}
diablox0147 2009-09-05
  • 打赏
  • 举报
回复
可以了。。。的确是那个MENU的问题,
我把那个MENU的constructor加上NULL默认值然后,WINDOW那里去掉了NULL检查。。。
fox000002 2009-09-05
  • 打赏
  • 举报
回复
WinProc 的代码呢?
diablox0147 2009-09-05
  • 打赏
  • 举报
回复
会是什么问题呢?
我传了函数指针进去,然后赋予WNDCLASS了阿、、、
fox000002 2009-09-05
  • 打赏
  • 举报
回复
窗口没有创建成功啊

多半是 WinProc 有问题
diablox0147 2009-09-05
  • 打赏
  • 举报
回复
顶下,求救!!!!!!
fox000002 2009-09-05
  • 打赏
  • 举报
回复
除了 14 楼的错误之外,还有 winProc 的参数名乱写,DefWindowProc 就乱了

LRESULT CALLBACK winProc(HWND hwnd, UINT message, WPARAM lParam , LPARAM wParam)
godspeed_plus 2009-09-04
  • 打赏
  • 举报
回复
MFC的目的和意义不在于封装,而在于提供一个框架 (Framework), 让一切事务处理拥有统一可循的步骤
diablox0147 2009-09-04
  • 打赏
  • 举报
回复
想问下。。。API和MFC除了一个是封装过后的其他有区别么?
2个的学习价值差不多?
diablox0147 2009-09-04
  • 打赏
  • 举报
回复
最后是menu的
#ifndef LJMENU_
#define LJMENU_
#include <windows.h>
namespace LJ
{
class Menu
{
public:
Menu(HMENU menu){_menuHandle = menu;}
~Menu(){};
HMENU GetMenuHandle(){return _menuHandle;}
private:
HMENU _menuHandle;
};
}

#endif
diablox0147 2009-09-04
  • 打赏
  • 举报
回复
好的,一个个来:
首先是:
ifndef WNDCLASS_
#define WNDCLASS_
#include <windows.h>
#include "LJMenu.h"
namespace LJ
{
class WinClass
{
public:
WinClass(HINSTANCE instance,LRESULT CALLBACK (*winProc)(HWND hwnd, UINT message, WPARAM lParam , LPARAM wParam) ,LPSTR name,UINT style = CS_VREDRAW|CS_HREDRAW|CS_OWNDC|CS_DBLCLKS);
~WinClass(){}
void SetStyle(DWORD);
void SetMenu(Menu);

HINSTANCE GetInstance(){return _instance;}

private:
HINSTANCE _instance;
WNDCLASS _winClass;
HANDLE _icon;
HANDLE _cursor;
HMENU _menu;
};
}
#endif

上面那个的cpp文件:
#include "LJApplication.h"

LJ::WinClass::WinClass(HINSTANCE instance,LRESULT CALLBACK (*winProc)(HWND hwnd, UINT message, WPARAM lParam , LPARAM wParam) ,LPSTR name,UINT _style)
{
_instance = instance;

_winClass.style = _style;
_winClass.lpfnWndProc = winProc;
_winClass.cbClsExtra = 0;
_winClass.cbWndExtra = 0;
_winClass.hInstance = instance;
_winClass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
_winClass.hCursor = LoadCursor(NULL, IDC_ARROW);
_winClass.hbrBackground= (HBRUSH)COLOR_BACKGROUND;
_winClass.lpszMenuName = NULL;
_winClass.lpszClassName= name;
if(!RegisterClass(&_winClass)) MessageBox(NULL,"注册失败","错误",MB_OK);

}
void LJ::WinClass::SetStyle(DWORD)
{
}

void LJ::WinClass::SetMenu(Menu)
{
}

///---------------分割线------------------------///

#ifndef LJWINDOW_
#define LJWINDOW_
#include <windows.h>
#include "LJMenu.h"
namespace LJ
{
class Window
{
public:
Window(LPSTR winClass, LPSTR caption, HINSTANCE hInstance,int width = CW_USEDEFAULT, int height = CW_USEDEFAULT,DWORD style = WS_OVERLAPPEDWINDOW|WS_VISIBLE, HWND paren = NULL, LJ::Menu meun = NULL);
~Window(){}
void SetCaption(LPSTR);
void SetSize(int, int);

HWND& GetHandle(){return _winHandle;}
int GetWidth(){return _width;}
int GetHeight(){return _height;}
HWND GetParenHandle(){return _parentHandle;}

private:
HWND _winHandle;
HWND _parentHandle;
HMENU _menu;
LPSTR _titre;
int _width;
int _height;
};

void Show(Window& win, int mode);
}


#endif


上面那个的cpp
#include "LJWindow.h"

LJ::Window::Window(LPSTR winClass, LPSTR caption, HINSTANCE hInstance,int width, int height,DWORD style, HWND parent, LJ::Menu menu)
{
if(menu.GetMenuHandle() != NULL) _menu = menu.GetMenuHandle();
_parentHandle = parent;
_winHandle = CreateWindow(winClass,
caption,
style,
CW_USEDEFAULT,
CW_USEDEFAULT,
width,
height,
parent,
_menu,
hInstance,
NULL);
}
void LJ::Window::SetCaption(LPSTR caption)
{
}
void LJ::Window::SetSize(int width, int height)
{
}
void LJ::Show(Window& win, int mode)
{
ShowWindow(win.GetHandle(), mode);
UpdateWindow(win.GetHandle());
}
野男孩 2009-09-04
  • 打赏
  • 举报
回复
帮顶,贴代码吧

64,648

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧