win32的基本消息机制 button按钮的添加

#include <windows.h>


HINSTANCE  g_hInstance = 0;
HWND hwndButtonCintinue;
HWND hwndButtonExit;
HWND hwndLabel;
//消息入口函数
LRESULT CALLBACK WndProc(HWND hWnd, UINT msgID, WPARAM wparam, LPARAM lParam)
{
   switch (msgID)
   {
              case  WM_CREATE:
               hwndButtonCintinue = CreateWindow(TEXT("button"),//必须为:button    
                  TEXT("远程连接"),//按钮上显示的字符    
                  WS_CHILD | WS_VISIBLE,
                  100, 240, 100, 50,  //按钮在界面上出现的位置
                  hWnd, (HMENU)1,  //设置按钮IDIDC_BUTTON_CONTINUE = 131自己定义ID
                  ((LPCREATESTRUCT)lParam)->hInstance, NULL);
                     ShowWindow(hwndButtonCintinue, true);


                hwndButtonExit = CreateWindow(TEXT("button"),//必须为:button    
                   TEXT("退出"),//按钮上显示的字符    
                   WS_CHILD | WS_VISIBLE,
                   400, 240, 100, 50,  //按钮在界面上出现的位置
                 hWnd, (HMENU)2,  //设置按钮ID IDC_BUTTON_EXIT =132自己定义ID
                   ((LPCREATESTRUCT)lParam)->hInstance, NULL);
   ShowWindow(hwndButtonExit, TRUE);


   //错误的
   /*hwndLabel = CreateWindow(TEXT("label"), TEXT("1111111111111"), WS_CHILD | WS_VISIBLE, 300, 240, 100, 50, hWnd, (HMENU)3, ((LPCREATESTRUCT)lParam)->hInstance, NULL);
   ShowWindow(hwndLabel, TRUE);*/




   break;
   case  WM_DESTROY:
   PostQuitMessage(0);
    break;
   }
   //给各种消息默认处理
   return DefWindowProc(hWnd,msgID,wparam,lParam);
}
//注册窗口
void Register(LPSTR lpClassName, /*WNDPROC WndProc*/WNDPROC wndProc)
{
WNDCLASSEX wce = { 0 };
wce.cbSize = sizeof(wce);
wce.cbClsExtra = 0;
wce.cbWndExtra = 0;
wce.hbrBackground = (HBRUSH)(COLOR_WINDOW);
wce.hCursor = NULL;
wce.hIcon = NULL;
wce.hIconSm = NULL;
wce.lpfnWndProc = wndProc;
wce.hInstance = g_hInstance;
wce.lpszClassName = lpClassName;
wce.lpszMenuName = NULL;
wce.style = CS_HREDRAW | CS_VREDRAW;
RegisterClassEx(&wce);
}
//创建窗口
HWND   createMain(LPSTR lpClassName,LPSTR lpWndName)
{
HWND hWnd = CreateWindowEx(0, lpClassName, lpWndName, WS_OVERLAPPEDWINDOW, 100, 100, 700, 500, NULL, NULL, g_hInstance, NULL); 
return hWnd;
}
//显示窗口
void  Display(HWND hWnd)
{
ShowWindow(hWnd, SW_SHOW);
UpdateWindow(hWnd);
}


//消息循环
void Messasge()
{
MSG nMsg = { 0 };
while (GetMessage(&nMsg,NULL,0,0))
{
TranslateMessage(&nMsg);
DispatchMessage(&nMsg);
}
}
//WinMain函数
int  CALLBACK WinMain(HINSTANCE hIns,HINSTANCE hPreIns,LPSTR lpCmdLine,int nCmdShow)
{
g_hInstance = hIns;
//注册窗口类
Register("Main", WndProc);
//创建窗口
HWND hWnd = createMain("Main","Window");
Display(hWnd);
Messasge();
return 0;
}



















猜你喜欢

转载自blog.csdn.net/han13018123456789/article/details/79972941