1 /*程序功能:点击button按钮弹出输入框的文本*/ 2 #include3 4 struct 5 { 6 int iStyle; 7 TCHAR * szText; 8 } 9 button[]= 10 { 11 BS_PUSHBUTTON, TEXT("BS_PUSHBUTTON"), 12 BS_DEFPUSHBUTTON, TEXT("BS_DEFPUSHBUTTON") 13 }; 14 15 #define NUM (sizeof button /sizeof button[0]) //num=2 16 17 LRESULT CALLBACK WndProc(HWND,UINT,WPARAM,LPARAM); //申明函数 18 19 int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR lpCmdLine,int iCmdShow) 20 { 21 static TCHAR szAppName[]= TEXT ("app1"); 22 23 HWND hwnd; 24 MSG msg; 25 WNDCLASS wndclass; 26 27 wndclass.style = CS_HREDRAW | CS_VREDRAW; 28 wndclass.lpfnWndProc = WndProc; 29 wndclass.cbClsExtra = 0; 30 wndclass.cbWndExtra = 0; 31 wndclass.hInstance = hInstance; 32 wndclass.hIcon = LoadIcon(NULL, IDI_APPLICATION); 33 wndclass.hCursor = LoadCursor(NULL,IDC_ARROW); 34 wndclass.hbrBackground = (HBRUSH) GetStockObject(WHITE_BRUSH); 35 wndclass.lpszMenuName = NULL; 36 wndclass.lpszClassName= szAppName; 37 38 if(!RegisterClass(&wndclass)) 39 { 40 MessageBox(NULL,TEXT("程序要求 Windows NT 系统!"),szAppName,MB_ICONERROR); 41 42 return 0; 43 } 44 45 hwnd = CreateWindow (szAppName, 46 TEXT ( "WindowsAPI程序-按钮实例 "), 47 WS_OVERLAPPEDWINDOW, 48 CW_USEDEFAULT, 49 CW_USEDEFAULT, 50 CW_USEDEFAULT, 51 CW_USEDEFAULT, 52 NULL, 53 NULL, 54 hInstance, 55 NULL); 56 57 ShowWindow(hwnd,iCmdShow); 58 UpdateWindow(hwnd); 59 60 while( GetMessage( &msg,NULL,0,0)) 61 { 62 TranslateMessage(&msg); 63 DispatchMessage(&msg); 64 } 65 66 return msg.wParam; 67 68 } 69 70 //窗口过程函数 71 LRESULT CALLBACK WndProc (HWND hwnd,UINT message,WPARAM wParam, LPARAM lParam) 72 { 73 74 static HWND hwndButton[NUM]; //按钮句柄集合 75 static HWND hwndTxt; 76 static HWND hwndBtnSure; 77 static HWND hwndBtnSure2; 78 static HWND hwndChild; 79 static RECT rect; //矩形结构体 80 //char数组 81 static TCHAR szTop[] =TEXT("功能:点击按钮显示文本框内容"), 82 szUnd[] =TEXT("_______ ______ ______"), 83 szFormat[] =TEXT("%-16s%04X-%04X %04X-%04X"), //关于格式化字符串参考 http://blog.163.com/c__0/blog/static/188312066201162661240584/ 84 szBuffer[50]; 85 char constStr[]= { ",欢迎进入C++世界" }; 86 char outputStr[256]; 87 static int cxChar,cyChar; 88 HDC hdc; //绘图实例句柄 89 HDC childHdc; 90 PAINTSTRUCT ps; //绘制结构体 91 int i,len; 92 switch (message) 93 { 94 //创建 95 case WM_CREATE: 96 97 cxChar=LOWORD(GetDialogBaseUnits()); 98 cyChar=HIWORD(GetDialogBaseUnits()); 99 100 101 //文本框102 hwndTxt = CreateWindow ( 103 "edit", 104 "", 105 WS_CHILD|WS_VISIBLE|WS_BORDER|BS_TEXT, 106 250, 100, 300,22, 107 hwnd, 108 (HMENU)1, 109 ((LPCREATESTRUCT) lParam)->hInstance, 110 NULL 111 );112 113 //按钮114 hwndBtnSure2 = CreateWindow( 115 TEXT("button"), 116 "点击我2",117 WS_CHILD|WS_VISIBLE|BS_PUSHBUTTON,118 230, 119 190, 120 20*cxChar,121 7*cyChar/4,122 hwnd, 123 (HMENU)3, 124 ((LPCREATESTRUCT) lParam)->hInstance, 125 NULL 126 );127 128 129 130 hwndBtnSure=CreateWindow(131 TEXT("Button"),132 "点击我",133 WS_CHILD|WS_VISIBLE|BS_PUSHBUTTON,134 230,135 150,136 20*cxChar,137 7*cyChar/4,138 hwnd,139 (HMENU)2,140 ((LPCREATESTRUCT) lParam)->hInstance,NULL);141 142 143 144 145 return 0;146 147 case WM_SIZE:148 rect.left=24* cxChar;149 rect.top=2*cyChar;150 rect.right=LOWORD(lParam);151 rect.bottom=HIWORD(lParam);152 return 0;153 154 case WM_PAINT:155 156 InvalidateRect(hwnd,&rect,TRUE);157 158 hdc=BeginPaint(hwnd,&ps);159 160 SelectObject(hdc,GetStockObject(SYSTEM_FIXED_FONT)); //选取当前字体161 SetBkMode(hdc,TRANSPARENT) ;// 背景设置成透明162 163 // SetBkColor(hdc,GetSysColor(COLOR_BTNFACE));164 SetTextColor(hdc,GetSysColor(COLOR_BTNTEXT)); //设置文本颜色165 166 TextOut(hdc,24*cxChar,cyChar,szTop,lstrlen(szTop));167 //TextOut(hdc,24*cxChar,cyChar,szUnd,lstrlen(szUnd));168 169 170 171 EndPaint(hwnd,&ps);172 return 0;173 174 case WM_DRAWITEM:175 case WM_COMMAND:176 177 hdc=GetDC(hwnd);178 SelectObject(hdc,GetStockObject(SYSTEM_FIXED_FONT)); //选取当前字体179 180 //获取当前子窗口句柄181 hwndChild=(HWND)lParam;182 if(hwndChild==hwndBtnSure)183 {184 char szBuff[256];185 GetWindowText(hwndTxt,szBuff,sizeof(szBuff));186 if(strlen(szBuff)==0)187 return 0;188 189 for(i=0;i