CUI & GUI in C++

int CUI Application

#include <windows.h> #include <stdio.h> DWORD WINAPI mainGUI( LPVOID lp) { HMODULE hInstance= 0; hInstance = GetModuleHandle(NULL); HWND hWnd; MSG msg; hWnd = CreateWindow("EDIT", "This window was created from main() and can get messages from the console below.", WS_OVERLAPPEDWINDOW,CW_USEDEFAULT,CW_USEDEFAULT, CW_USEDEFAULT,CW_USEDEFAULT,NULL,NULL,hInstance,NULL); if (!hWnd) return (1); *(HWND*)lp = hWnd; ShowWindow(hWnd, SW_SHOW); UpdateWindow(hWnd); while (GetMessage(&msg, NULL, NULL, NULL)) { TranslateMessage(&msg); DispatchMessage(&msg); } return (msg.wParam); } int main(int argc, char* argv[]) { DWORD ID; HWND hWindow; char szOutput[64]; CreateThread(NULL,0,mainGUI, &hWindow, NULL,&ID); printf("This console created the window above./nType in the message you want to send to the window./n/"quit/" to exit./n/n"); while (true) { scanf("%s", szOutput); if (!strcmp(szOutput, "quit")) break; SendMessage(hWindow,WM_SETTEXT, strlen(szOutput), (LPARAM)szOutput); } return 0; }

 

in GUI. to control the CONSOLE Window

 

#include <windows.h> #include <stdio.h> #include <io.h> #include <fcntl.h> int __stdcall WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) { DWORD dwBytesWritten; char temp[1]; char* msg1 = "This console was created in WinMain() /nand the messages below were generated by different handles./n/"return/" to exit./n/n"; char* msg2 = "This line was writen by WriteFile() through operating system handle./n"; char* msg3 = "This line was writen by write() through run time handle./n"; char* msg4 = "This line was writen by fprintf()through stream./n"; char* msg5 = "This line was writen by printf()through standard output stream./n"; AllocConsole(); HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE); // return an OS file handle WriteFile(handle, msg1, strlen(msg1), &dwBytesWritten, NULL); WriteFile(handle, msg2, strlen(msg2), &dwBytesWritten, NULL); int hCrt = _open_osfhandle((long)handle,_O_TEXT); // return a runtime file handle write(hCrt, msg3, strlen(msg3)); // for files opened by _open() FILE * hf = _fdopen( hCrt, "w" ); // stream char buf[2]; setvbuf( hf, buf, _IONBF, 1 ); *stdout = *hf; fprintf(hf, msg4); // for files opened by fopen printf(msg5); HANDLE hIn = GetStdHandle(STD_INPUT_HANDLE); // return an OS file handle ReadFile(hIn, temp, 1, &dwBytesWritten, NULL); FreeConsole(); return (0); }

你可能感兴趣的:(Stream,File,null,System,output,winapi)