首页
社区
课程
招聘
[求助]c++ 在SDK下动态创建按钮和编辑框
发表于: 2008-5-2 15:14 10248

[求助]c++ 在SDK下动态创建按钮和编辑框

2008-5-2 15:14
10248
因为初学C++ 高手勿笑偶哦...
创建窗口会了~~ 可是创建按钮和创建编辑框 不知道在哪里写代码...

只要加上 CreateWindow("btn"....) 就出错了~

哪位大大能帮我在我的原代码上创建一下呢~

谢谢大大们了~~

#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,          // command line
				   int nCmdShow              // show state
				   )
{
	WNDCLASS ws;
	ws.cbClsExtra=0;
	ws.cbWndExtra=0;
	ws.hbrBackground=(HBRUSH)GetStockObject(WHITE_BRUSH);
	ws.hCursor=LoadCursor(NULL,IDC_ARROW);
	ws.hIcon=LoadIcon(NULL,IDI_APPLICATION);
	ws.hInstance=0;
	ws.lpszClassName="学习";
	ws.lpszMenuName=NULL;
	ws.style=CS_HREDRAW|CS_VREDRAW;
	ws.lpfnWndProc=WindowProc;
	RegisterClass(&ws);

	HWND hwnd;
	int x,y;
    x=(GetSystemMetrics(SM_CXFULLSCREEN))/2-100;
    y=(GetSystemMetrics(SM_CYFULLSCREEN))/2-50;
	hwnd=CreateWindow("学习","hello word!",WS_OVERLAPPEDWINDOW&~WS_MAXIMIZEBOX,x,y,200,100,0,0,hInstance,0);
	
	ShowWindow(hwnd,SW_NORMAL);
	UpdateWindow(hwnd);

	MSG msg;
	while (GetMessage(&msg,NULL,0,0))
	{
          TranslateMessage(&msg);
		  DispatchMessage(&msg);
	}
	return 0;
}

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_CLOSE:
		if (IDYES==MessageBox(0,"是否关闭?","提示",MB_YESNO))
		{
			DestroyWindow(hwnd);
		}
		break;
	case WM_DESTROY:
		PostQuitMessage(0);
		break;
	default:
		return DefWindowProc(hwnd,uMsg,wParam,lParam);
	}
	return 0;
}

[培训]科锐逆向工程师培训第53期2025年7月8日开班!

收藏
免费 0
支持
分享
最新回复 (3)
雪    币: 223
活跃值: (231)
能力值: ( LV4,RANK:50 )
在线值:
发帖
回帖
粉丝
2
在LRESULT CALLBACK WindowProc(
              HWND hwnd,      // handle to window
              UINT uMsg,      // message identifier
              WPARAM wParam,  // first message parameter
              LPARAM lParam   // second message parameter
              )
中处理WM_CREATE消息时,调用CreateWindow去创建,记得加上WS_CHILD属性
2008-5-2 15:39
0
雪    币: 2384
活跃值: (766)
能力值: (RANK:410 )
在线值:
发帖
回帖
粉丝
3
#include <windows.h>

HINSTANCE g_hInstance;
HWND hEdit, hButton;


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,          // command line
           int nCmdShow              // show state
           )
{
  g_hInstance = hInstance;
  WNDCLASS ws;
  ws.cbClsExtra=0;
  ws.cbWndExtra=0;
  ws.hbrBackground=(HBRUSH)GetStockObject(WHITE_BRUSH);
  ws.hCursor=LoadCursor(NULL,IDC_ARROW);
  ws.hIcon=LoadIcon(NULL,IDI_APPLICATION);
  ws.hInstance=hInstance;
  ws.lpszClassName="学习";
  ws.lpszMenuName=NULL;
  ws.style=CS_HREDRAW|CS_VREDRAW;
  ws.lpfnWndProc=WindowProc;
  RegisterClass(&ws);

  HWND hwnd;
  int x,y;
    x=(GetSystemMetrics(SM_CXFULLSCREEN))/2-100;
    y=(GetSystemMetrics(SM_CYFULLSCREEN))/2-50;
  hwnd=CreateWindow("学习","hello word!",WS_OVERLAPPEDWINDOW&~WS_MAXIMIZEBOX,x,y,200,100,0,0,hInstance,0);
  
  ShowWindow(hwnd,SW_NORMAL);
  UpdateWindow(hwnd);

  MSG msg;
  while (GetMessage(&msg,NULL,0,0))
  {
          TranslateMessage(&msg);
      DispatchMessage(&msg);
  }
  return 0;
}

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:
    hEdit = CreateWindowEx(0,"Edit","Edit",WS_VISIBLE | WS_CHILD,10,10,50,20,hwnd,(HMENU)1000,g_hInstance,NULL);
    hButton = CreateWindowEx(0,"Button","Button",WS_VISIBLE | WS_CHILD,15,30,35,20,hwnd,(HMENU)1001,g_hInstance,NULL);
    break;

  case WM_CLOSE:
    if (IDYES==MessageBox(0,"是否关闭?","提示",MB_YESNO))
    {
      DestroyWindow(hwnd);
    }
    break;
  case WM_DESTROY:
    PostQuitMessage(0);
    break;
  default:
    return DefWindowProc(hwnd,uMsg,wParam,lParam);
  }
  return 0;
}
2008-5-2 15:52
0
雪    币: 208
活跃值: (10)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
4
先谢谢一楼的朋友...

小虾班班~~~承蒙您多次的帮忙,让我有了很大的进步~~~  你实在太伟大了~~~
2008-5-2 18:43
0
游客
登录 | 注册 方可回帖
返回