WindowsAPI之按钮

WindowsAPI之按钮

定义一个按钮函数:

HWND CreateButton(
                   const HWND hParent,            //父窗口类
                   const HINSTANCE hInst,         //应用程序实例
                   DWORD dwStyle,                         //窗口样式
         constRECT& rc,                          //相对于父窗口的位置矩形
                   const int id,                                   //按钮ID号
                   const string&caption)             //按钮标题
{
         dwStyle |= WS_CHILD|WS_VISIBLE;
         return CreateWindowEx(
                   0,
                   TEXT("BUTTON"),                                                 //类型为BUTTON,若是文本框为EDIT
                   caption.c_str(),
                   dwStyle,
                   rc.left,
                   rc.top,
                   rc.right - rc.left,
                   rc.bottom - rc.top,
                   hParent,
                   reinterpret_cast<HMENU>(static_cast<INT_PTR>(id)),
                   hInst,
                   NULL);
}

调用按钮函数创建一个按钮,指定位置,ID号等,尤其是ID号比较重要:

#defineIDC_BUTTON_1 101
 
RECT editrc_1 = {120,30,220,100 };
CreateButton(
<span style="white-space:pre">		</span>hParentWindow,
<span style="white-space:pre">		</span>(HINSTANCE)  GetWindowLong (hParentWindow ,
<span style="white-space:pre">		</span>GWL_HINSTANCE),
<span style="white-space:pre">		</span>BS_DEFPUSHBUTTON,
<span style="white-space:pre">		</span>buttonrc,
<span style="white-space:pre">		</span>IDC_BUTTON_1,                                  //注意
<span style="white-space:pre">		</span>string("按钮1"));


在消息循环中加入:

case WM_COMMAND:
                   switch(wParam)
                   {
                   case IDC_BUTTON_1:         //ID号要于上面创建按钮时保持一致
                            break;
                   default:
                            break;
                   }
                   break;


即可,在case里写自己需要的内容


你可能感兴趣的:(String,command,null,button)