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--第四章的全部內容,希望文章能夠幫你解決所遇到的問題。