sdi mfc,手头的这个win32应用程序(线程)怎么把它加到mfc的SDI里去.

手头的这个win32应用程序(线程)怎么把它加到mfc的SDI里去. - 故障解答 - 电脑教程网

手头的这个win32应用程序(线程)怎么把它加到mfc的SDI里去.

日期:2007-03-01   荐:
手头的这个win32应用程序(线程)怎么把它加到mfc的SDI里去.我想在单文档里通过菜单调用这个win32程序所实现的功能,不知怎么弄???急^^^^^^^^^#include "stdafx.h"#include "stdlib.h"#include "resource.h"#include "stdio.h"#include "winsock2.h"#include "conio.h"#include "windows.h" DWORD NewThreadId; HWND hWnd1;LRESULT CALLBACKWndProc(HWND, UINT, WPARAM, LPARAM);int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow){ // TODO: Place code here. MSG msg; int bool1; DialogBox(hInstance, (LPCTSTR)IDD_DIALOG1,NULL, (DLGPROC)WndProc); while ((bool1=GetMessage(&msg, NULL, 0, 0))!=0 && bool1!=-1) { TranslateMessage(&msg);DispatchMessage(&msg); }return 0;}DWORD ThreadFunc(char *IP_value){unsigned long IP_A=(unsigned long)IP_value;WSADATA a;if(WSAStartup(MAKEWORD( 2, 2 ),&a)!=0){MessageBox(NULL,"failure-load winsok", "Error", MB_OK); return 0;}SOCKET client; //The socket used to linksockaddr_in server_add ; //used in the function of connectclient=socket(AF_INET,SOCK_STREAM,IPPROTO_IP);if(client==INVALID_SOCKET){MessageBox(NULL,"failure-create winsok", "Error", MB_OK); return 0;} //Psockaddr_in server_add;server_add.sin_family=AF_INET;server_add.sin_port=htons(5150); server_add.sin_addr.S_un.S_addr=IP_A;//inet_addr("192.168.0.9"); if(connect(client,(sockaddr *)&server_add,sizeof(server_add))==SOCKET_ERROR){MessageBox(NULL,"failure-connect", "Error", MB_OK); return 0;} int count=0; //count the receive number of one time; BITMAP bmp;char *New_start; char buf_recv[20000]; int counter=0; //count the total number of receive char *Mem_real_pointer; char *pointerMem; int all_size; count=recv(client,(char *)&bmp,sizeof(BITMAP),0); if(count!=0 && count!=SOCKET_ERROR) { if(count==sizeof(BITMAP)) { //allocat the buffer to receive the BMP data all_size=bmp.bmWidthBytes*bmp.bmHeight; pointerMem=(char *)GlobalAlloc(GMEM_FIXED,all_size); Mem_real_pointer=(char *)GlobalLock(pointerMem); if(Mem_real_pointer==NULL) { MessageBox(NULL,"Memory allocated failure", "Error", MB_OK); //分配内存失败 } else { //allocate success counter=0; count=0; char *cpy_result; while(counter<all_size) { New_start=(char *)((char *)Mem_real_pointer counter); count=recv(client,(char *)buf_recv,20000,0); if(count==SOCKET_ERROR) //this indicate that the link is break up { break; } cpy_result=(char *)memcpy(New_start,buf_recv,count); if(cpy_result!=New_start) { break; } counter=counter count; } } } } LPBITMAPINFO lpBmpInfo; lpBmpInfo = (BITMAPINFO*) new BYTE[sizeof(BITMAPINFOHEADER)]; lpBmpInfo->bmiHeader.biSize = sizeof(BITMAPINFOHEADER); lpBmpInfo->bmiHeader.biPlanes =bmp.bmPlanes; lpBmpInfo->bmiHeader.biBitCount = bmp.bmBitsPixel; lpBmpInfo->bmiHeader.biCompression = BI_RGB; lpBmpInfo->bmiHeader.biSizeImage = 0; lpBmpInfo->bmiHeader.biClrUsed = 0; lpBmpInfo->bmiHeader.biWidth = bmp.bmWidth; lpBmpInfo->bmiHeader.biHeight = bmp.bmHeight; HDC hdc = CreateCompatibleDC(NULL); LPVOID lpBits; PBITMAP holdbmp,hbmp; hbmp=(PBITMAP)CreateDIBSection(hdc,lpBmpInfo,DIB_RGB_COLORS,&lpBits,NULL,0); delete lpBmpInfo; DeleteDC(hdc); if(hbmp==NULL) MessageBox(NULL,"create BMP failure", "Error", MB_OK); else { char *cpyresult; cpyresult=(char *)memcpy(lpBits,Mem_real_pointer,all_size); if(cpyresult!=lpBits) { printf("copy mem last failure!"); } HDC dcImage=CreateCompatibleDC(NULL); holdbmp=(PBITMAP)SelectObject(dcImage, hbmp); GetObject(hbmp,sizeof(bmp),&bmp); RECT ScreenSaved; //get IDC_EDIT1 'S screen place coordinate GetWindowRect(GetDlgItem(hWnd1,IDC_EDIT1),&ScreenSaved); StretchBlt(GetWindowDC(GetDlgItem(hWnd1,IDC_EDIT1)),0,0,ScreenSaved.right-ScreenSaved.left,ScreenSaved.bottom-ScreenSaved.top,dcImage,0,0,bmp.bmWidth,bmp.bmHeight,SRCCOPY); SelectObject(dcImage, holdbmp); DeleteDC(dcImage); }GlobalFree(pointerMem);closesocket(client);WSACleanup(); return 0;}LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) {//char string[100];hWnd1=hWnd; switch (message){case WM_INITDIALOG: return TRUE;case WM_COMMAND:if (LOWORD(wParam) == IDC_BUTTON2) {EndDialog(hWnd, LOWORD(wParam));return TRUE;}if(LOWORD(wParam) == IDC_BUTTON1) {unsigned long IP_value; char string_IP[30];GetWindowText(GetDlgItem(hWnd,IDC_EDIT3),string_IP,30);IP_value=inet_addr(string_IP); CreateThread(NULL,0,(LPTHREAD_START_ROUTINE) ThreadFunc,(LPVOID)IP_value,0,&NewThreadId);}break;case WM_DESTROY: PostQuitMessage(0); default:return DefWindowProc(hWnd, message, wParam, lParam);} return 0;}高手来啊!!创建对话框,在相应按钮的处理函数中加入以上的IDC_BUTTON2,处理内容IDC_BUTTON1
标签: