Windows环境下32位汇编程序设计C版code--第四章
采用的编译环境为VC++6.0
(一)第一个窗口函数
FirstWindow.c#include <windows.h>LRESULT CALLBACK ProcWinMain(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam) {PAINTSTRUCT stPs;RECT stRect;HDC hDc;switch(uMsg){case WM_PAINT:hDc = BeginPaint(hWnd, &stPs);GetClientRect(hWnd, &stRect);DrawText(hDc, TEXT("Win32 Programing, Simple and Powerful"), -1, &stRect, DT_SINGLELINE | DT_CENTER | DT_VCENTER);EndPaint(hWnd, &stPs);return 0;case WM_CLOSE:DestroyWindow(hWnd);PostQuitMessage(0);return 0;}return DefWindowProc(hWnd, uMsg, wParam, lParam); }int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR szCmdLine, int iCmdShow) {WNDCLASSEX stWndCls;MSG stMsg;HWND hWnd;RtlZeroMemory(&stWndCls, sizeof(stWndCls));stWndCls.hCursor = LoadCursor(NULL, IDC_ARROW);stWndCls.hIcon = LoadIcon(NULL, IDI_APPLICATION);stWndCls.hIconSm = LoadIcon(NULL, IDI_APPLICATION);stWndCls.cbWndExtra = 0;stWndCls.cbClsExtra = 0;stWndCls.hInstance = hInstance;stWndCls.cbSize = sizeof(WNDCLASSEX);stWndCls.style = CS_HREDRAW | CS_VREDRAW;stWndCls.lpfnWndProc = ProcWinMain;stWndCls.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);stWndCls.lpszClassName = TEXT("MyClass");stWndCls.lpszMenuName = NULL;RegisterClassEx(&stWndCls);hWnd = CreateWindowEx(WS_EX_CLIENTEDGE, TEXT("MyClass"), TEXT("My First Window"), WS_OVERLAPPEDWINDOW, 100, 100, 600, 400, NULL, NULL, hInstance, NULL);CreateWindowEx(0, TEXT("Button"), TEXT("&OK"), WS_CHILD | WS_VISIBLE, 10, 10, 65, 22, hWnd, (HMENU)1, hInstance, NULL);ShowWindow(hWnd, SW_SHOWNORMAL);UpdateWindow(hWnd);while(GetMessage(&stMsg, NULL, 0, 0)){TranslateMessage(&stMsg);DispatchMessage(&stMsg);}return stMsg.wParam; }
(二)窗口间的通信
//窗口间消息发送 接收程序Receive.c 代码在FirstWindow.c代码ProcWinMain函数中添加变量定义:
TCHAR buffer[512]; TCHAR szFmt[]=TEXT("Received WM_SETTEXT message/nparam: %08x/ntext: %s");
//然后在处理WM_PAINT消息代码段后添加下述代码:
case WM_SETTEXT:wsprintf(buffer, szFmt, lParam, lParam); MessageBox(NULL, buffer, TEXT("Success!"), MB_OK | MB_ICONINFORMATION);return 0;
//发送函数Send.c代码:
#include <windows.h>HWND hWnd; TCHAR szBuffer[256];const TCHAR szCaption[] = TEXT("SendMessage"); const TCHAR szStart[] = TEXT("Press OK start SendMessage, param: %08x "); const TCHAR szReturn[] = TEXT("SendMessage returned!"); const TCHAR szDestClass[] = TEXT("MyClass"); const TCHAR szText[] = TEXT("Text send to other windows"); const TCHAR szNotFound[] = TEXT("Receive Message Window not found");int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR CmdLine, int iCmdShow) {if((hWnd = FindWindow(szDestClass, NULL)) != NULL){wsprintf(szBuffer, szStart, szText);MessageBox(NULL, szBuffer, szCaption, MB_OK);SendMessage(hWnd, WM_SETTEXT, 0, (LPARAM)szText);MessageBox(NULL, szReturn, szCaption, MB_OK);}elseMessageBox(NULL, szNotFound, szCaption, MB_OK);return 0; }
(三)窗口间数据传递
//只要将(二)中接收代码Receive.c中case: WM_SETTEXT段替换为:
case WM_COPYDATA:wsprintf(buffer, szFmt, ((COPYDATASTRUCT *)lParam)->lpData, ((COPYDATASTRUCT *)lParam)->lpData);MessageBox(NULL, buffer, TEXT("Success!"), MB_OK | MB_ICONINFORMATION);return 0;
//只要将(二)中发送文件Send.c中WinMain改为:
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR CmdLine, int iCmdShow) {COPYDATASTRUCT stCDS;stCDS.cbData = sizeof(szText);stCDS.dwData = 0;stCDS.lpData = szText;if((hWnd = FindWindow(szDestClass, NULL)) != NULL){wsprintf(szBuffer, szStart, szText);MessageBox(NULL, szBuffer, szCaption, MB_OK);//SendMessage(hWnd, WM_SETTEXT, 0, (LPARAM)szText);SendMessage(hWnd, WM_COPYDATA, 0, (LPARAM)&stCDS);MessageBox(NULL, szReturn, szCaption, MB_OK);}elseMessageBox(NULL, szNotFound, szCaption, MB_OK);return 0; }
总结
以上是生活随笔为你收集整理的Windows环境下32位汇编程序设计C版code--第四章的全部内容,希望文章能够帮你解决所遇到的问题。
- 上一篇: NS2 安装
- 下一篇: Windows环境下32位汇编程序设计C