API控件


#include<windows.h>
#include<stdio.h>
#include<stdlib.h>
#define IDB_PUSHBUTTON 10
#define IDB_DEFPUSHBUTTON 11
#define  IDB_RADIOBUTTON 12
#define IDB_AUTORADIOBUTTON 13
#define IDB_CHECKBOX 14
#define IDB_AUTOCHECKBOX 15
#define IDB_GROUPBOX 16
#define IDB_GROUPBUTTON 17


HINSTANCE hInst;
char lpszClassName1[]="BUTTON";//预定义窗口类名
int nCheckBoxSt;
HWND hwndPush,hwndDefPush,hwndRadio,hwndAutoRadio,hwndCheckBox,
hwndAutoCheckBox,hwndGroupBox;//定义控件子窗口句柄
LRESULT CALLBACK WndProc(HWND,UINT,WPARAM,LPARAM);
int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInst,LPSTR lpszCmdLine,int nCmdShow)
{
 HWND hwnd;
 MSG Msg;
 WNDCLASS wndclass;
 char lpszClassName[]="控件";
 char lpszTitle[]="子窗口按钮示例";
 wndclass.style=0;
 wndclass.lpfnWndProc=WndProc;
 wndclass.cbClsExtra=0;
 wndclass.cbWndExtra=0;
 wndclass.hInstance=hInstance;
 wndclass.hIcon=LoadIcon(NULL,IDI_APPLICATION);
 wndclass.hCursor=LoadCursor(NULL,IDC_ARROW);
 wndclass.hbrBackground=(HBRUSH)(GetStockObject(WHITE_BRUSH));
 wndclass.lpszMenuName=NULL;
 wndclass.lpszClassName=lpszClassName;

 if(!RegisterClass(&wndclass))
 {
  MessageBeep(0);
  return FALSE;
 }

 hwnd=CreateWindow(lpszClassName,
  lpszTitle,
  WS_OVERLAPPEDWINDOW,
  CW_USEDEFAULT,
  CW_USEDEFAULT,
  CW_USEDEFAULT,
  CW_USEDEFAULT,
  NULL,
  NULL,
  hInstance,
  NULL);
 hInst=hInstance;
 ShowWindow(hwnd,nCmdShow);
 UpdateWindow(hwnd);
 while(GetMessage(&Msg,NULL,0,0))
 {
  TranslateMessage(&Msg);
  DispatchMessage(&Msg);
 }
 return Msg.wParam;
}

LRESULT CALLBACK WndProc(HWND hwnd,UINT message,WPARAM wParam,LPARAM lParam)
{
 int X=50,Y=30;//控件起始坐标
 switch(message)
 {
  //通过响应WM_CREATE消息创建控件子窗口
 case WM_CREATE:
  //创建按钮子窗口
  hwndPush=CreateWindow(lpszClassName1,
   "这里是一个按钮,按我吧",
   BS_PUSHBUTTON|WS_CHILD|WS_VISIBLE,
   X,Y,
  240,50,
   hwnd,
   (HMENU)IDB_PUSHBUTTON,
   hInst,
   NULL);

  //创建默认按钮子窗口
  hwndDefPush=CreateWindow(lpszClassName1,
   "这是默认按钮,按下试试看",
   BS_DEFPUSHBUTTON|WS_CHILD|WS_VISIBLE,
   X,Y+30,
   240,25,
   hwnd,
   (HMENU)IDB_DEFPUSHBUTTON,
   hInst,
   NULL);

  //创建单选按钮控件子窗口
  hwndRadio=CreateWindow(lpszClassName1,
   "单选按钮,你喜欢吗?",
   BS_RADIOBUTTON|WS_CHILD|WS_VISIBLE,
   X,Y+60,
   240,25,
   hwnd,
   (HMENU)IDB_RADIOBUTTON,
   hInst,
   NULL);

  //创建自动单选按钮子窗口
  hwndAutoRadio=CreateWindow(lpszClassName1,
   "自动圆形按钮,看看有何区别",
   BS_AUTORADIOBUTTON|WS_CHILD|WS_VISIBLE,
   X,Y+90,
   240,25,
   hwnd,
   (HMENU)IDB_AUTORADIOBUTTON,
   hInst,
   NULL);

  //创建复选框控件子窗口
  hwndCheckBox=CreateWindow(lpszClassName1,
   "复选框",
   BS_CHECKBOX|WS_CHILD|WS_VISIBLE,
   X,Y+90,
   240,25,
   hwnd,
   (HMENU)IDB_CHECKBOX,
   hInst,
   NULL);

  //创建自动复选框控件子窗口
  hwndAutoCheckBox=CreateWindow(lpszClassName1,
   "自动复选框,你会用吗?",
   BS_AUTOCHECKBOX|WS_CHILD|WS_VISIBLE,
   X,Y+150,
   240,25,
   hwnd,
   (HMENU)IDB_AUTOCHECKBOX,
   hInst,
   NULL);
  //创建组框按钮子窗口
  hwndGroupBox=CreateWindow(lpszClassName1,
   "组框",
   BS_GROUPBOX|WS_CHILD|WS_VISIBLE,
   X+290,Y+30,
   100,140,
   hwnd,
   (HMENU)IDB_GROUPBOX,
   hInst,
   NULL);

  break;

  //处理控件消息
 case WM_COMMAND:
  switch(LOWORD(wParam))
  {
   //当用户单击按钮时,改变按钮文本
  case IDB_PUSHBUTTON:
   SetWindowText(hwndPush,"你已按下了按钮");
   break;
   //当用户单击按钮时,改变按钮文本
  case IDB_DEFPUSHBUTTON:
   SetWindowText(hwndDefPush,"按钮已被按下");
   break;
  case IDB_AUTORADIOBUTTON:
   break;
  case IDB_CHECKBOX:
   //根据复选框状态设置复选框
   //如果当前为选中状态,则设置为非选中状态
   if(nCheckBoxSt==0)
   {
    SendMessage(hwndCheckBox,BM_SETCHECK,1,0);
    nCheckBoxSt=1;
   }
   //如果当前为非选中状态,则设置为选中状态
   else
   {
    SendMessage(hwndCheckBox,BM_SETCHECK,0,0);
    nCheckBoxSt=0;
   }
   break;
  case IDB_AUTOCHECKBOX:
   break;
  case IDB_GROUPBOX:
   break;
  }
  break;
  case WM_DESTROY:
   PostQuitMessage(0);
   break;
  default:
   return DefWindowProc(hwnd,message,wParam,lParam);
   }
   return 0;
   }
  

 


 

你可能感兴趣的:(API控件)