Click here to Skip to main content
15,888,313 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
AnswerRe: how to Convert MFC based Windows application to web application Pin
CPallini9-Apr-14 1:52
mveCPallini9-Apr-14 1:52 
AnswerRe: how to Convert MFC based Windows application to web application Pin
Munchies_Matt9-Apr-14 4:58
Munchies_Matt9-Apr-14 4:58 
AnswerRe: how to Convert MFC based Windows application to web application Pin
Richard MacCutchan9-Apr-14 6:24
mveRichard MacCutchan9-Apr-14 6:24 
GeneralRe: how to Convert MFC based Windows application to web application Pin
Munchies_Matt9-Apr-14 10:54
Munchies_Matt9-Apr-14 10:54 
Questionvisual c++ code to read number of characters in the string entered in a text box Pin
Member 107151068-Apr-14 16:14
Member 107151068-Apr-14 16:14 
AnswerRe: visual c++ code to read number of characters in the string entered in a text box Pin
David Crow8-Apr-14 16:48
David Crow8-Apr-14 16:48 
GeneralRe: visual c++ code to read number of characters in the string entered in a text box Pin
Munchies_Matt9-Apr-14 10:56
Munchies_Matt9-Apr-14 10:56 
AnswerRe: visual c++ code to read number of characters in the string entered in a text box Pin
leon de boer21-Apr-14 23:25
leon de boer21-Apr-14 23:25 
Ok here is a skeleton program it does a bit more than you asked it deals with resizing the window, closing etc and is really complete enough for you to build on and get the idea.

C++
#include <windows.h>
#include <commctrl.h>

// These are ID's to call the controls by as you add new controls you add new entries
#define IDC_TEXT	100          // Edit box ID
#define IDC_BUTTON	101          // Button ID
#define IDC_ANSWER      102          // Static text box to show answer

// THIS IS THE MAIN WINDOW HANDLER WHERE ALL THE ACTION OCCURS
LRESULT CALLBACK MainWindowProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
	switch(uMsg)
	{
		// Initialize our window and create our child controls.
		case WM_CREATE:
			{
				// Create your edit window ... we wont bother with position we will fix later
				CreateWindowEx(WS_EX_CLIENTEDGE, WC_EDIT, TEXT("SOME TEXT HERE"),
					ES_LEFT | 	WS_CHILD | WS_VISIBLE,
					0, 0, 0, 0, hWnd, (HMENU)IDC_TEXT, 0, NULL);
				// Create your button window.
				CreateWindowEx(0, WC_BUTTON, TEXT("&Count text"),
					BS_PUSHBUTTON | 
					WS_CHILD | WS_VISIBLE,
					0, 0, 0, 0, hWnd, (HMENU)IDC_BUTTON, 0, NULL);
				// Create your answer text box.
				CreateWindowEx(0, WC_STATIC, TEXT("0"),
					SS_LEFT | 
					WS_CHILD | WS_VISIBLE,
					0, 0, 0, 0, hWnd, (HMENU)IDC_ANSWER, 0, NULL);
			}
			return 0;
		
		// We accept this message so we can set a minimum window size. This only sets the users
		// tracking size. The window itself can always be resized smaller programmatically unless
		// you restrict it in WM_WINDOWPOSCHANGING/WM_WINDOWPOSCHANGED. 
		case WM_GETMINMAXINFO:
			{
				LPMINMAXINFO lpInfo = (LPMINMAXINFO)lParam;
				if(lpInfo) {
					lpInfo->ptMinTrackSize.x = 300;
					lpInfo->ptMinTrackSize.y = 300;
				};
			}
			return 0;
		// These next two messages are better to use rather than WM_MOVE/WM_SIZE.
		// Remember WM_MOVE/WM_SIZE are from 16bit windows. In 32bit windows the window
		// manager only sends these two messages and the DefWindowProc() handler actually
		// accepts them and converts them to WM_MOVE/WM_SIZE.
		// 
		// We accept this so we can scale our controls to the client size.
		case WM_WINDOWPOSCHANGING:
		case WM_WINDOWPOSCHANGED:
			{
				HDWP hDWP;
			
				// Create a deferred window handle.
				if(hDWP = BeginDeferWindowPos(3)){ // Defer 3 controls to stop flashing
					
                                        // Position edit box
					hDWP = DeferWindowPos(hDWP, GetDlgItem(hWnd, IDC_TEXT), NULL,
						10, 10, 250, 25, SWP_NOZORDER | SWP_NOREDRAW);
                                        // Position button
					hDWP = DeferWindowPos(hDWP, GetDlgItem(hWnd, IDC_BUTTON), NULL,
						10, 40, 80, 25, SWP_NOZORDER | SWP_NOREDRAW);
					// Position answer
					hDWP = DeferWindowPos(hDWP, GetDlgItem(hWnd, IDC_ANSWER), NULL,
						10, 70, 250, 25, SWP_NOZORDER | SWP_NOREDRAW);
					// Resize all windows under the deferred handle at the same time.
					EndDeferWindowPos(hDWP);
					
					// We told DeferWindowPos not to redraw the controls so we can redraw
					// them here all at once.
					RedrawWindow(hWnd, NULL, NULL, RDW_INVALIDATE | RDW_ALLCHILDREN | 
						RDW_ERASE | RDW_NOFRAME | RDW_UPDATENOW);
				}
			}
			return 0;
			
		// Handle the notifications of button presses.
		case WM_COMMAND:
			switch (LOWORD(wParam)) {
				long Li;
				char Buffer[256];
				HWND Wnd;
				case IDC_BUTTON:
					if (HIWORD(wParam) == BN_CLICKED){
                                                // Get handle to edit box
						Wnd = GetDlgItem(hWnd, IDC_TEXT);
						//Get text length in edit box
                                                Li = SendMessage(Wnd, WM_GETTEXTLENGTH, 0, 0);
                                                // Turn the length to ascii string safely
						_ltoa_s(Li, &Buffer[0], sizeof(Buffer), 10);
                                                // Get handle to answer static text
						Wnd = GetDlgItem(hWnd, IDC_ANSWER);
                                                // Send string to answer box
						SendMessage(Wnd, WM_SETTEXT, 0, (LPARAM)&Buffer[0]);
						return (0);
					};
					break;
			};
			break;
		case WM_DESTROY:
			// We post a WM_QUIT when our window is destroyed so we break the main message loop.
			PostQuitMessage(0);
			break;
	}
	
	// Not a message we wanted? No problem hand it over to the Default Window Procedure.
	return DefWindowProc(hWnd, uMsg, wParam, lParam);
}



// Program Entry Point
int APIENTRY WinMain(HINSTANCE hInst, HINSTANCE hPrev, LPSTR lpCmdLine, INT nShowCmd)
{
	OSVERSIONINFO lpVer;
	WNDCLASSEX wcex;
	DWORD dwExStyle;
	HWND hWnd;
	MSG msg;
	RECT R;

	// Zero class and message structures
	ZeroMemory(&msg,  sizeof(MSG));
	ZeroMemory(&wcex, sizeof(WNDCLASSEX));
	
	// Register our Main Window class.
	wcex.cbSize        = sizeof(WNDCLASSEX);
	wcex.hInstance     = hInst;
	wcex.lpszClassName = TEXT("TESTCLASS");
	wcex.lpfnWndProc   = MainWindowProc;
	wcex.hCursor       = LoadCursor(NULL, IDC_ARROW);
	wcex.hIcon         = LoadIcon(NULL, IDI_APPLICATION);
	wcex.hIconSm       = wcex.hIcon;
	wcex.hbrBackground = (HBRUSH) (COLOR_BTNFACE + 1);
	if(!RegisterClassEx(&wcex))
		return 1;

	// Default main window ex-style.
	dwExStyle = WS_EX_APPWINDOW;
	
	// If we are using XP or above lets 'double-buffer' the window to reduce the
	// flicker to the edit controls when drawing (notepad needs this).
	lpVer.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
	if(GetVersionEx(&lpVer) && (lpVer.dwMajorVersion > 5 || 
		(lpVer.dwMajorVersion == 5 && lpVer.dwMinorVersion == 1)))
		dwExStyle |= WS_EX_COMPOSITED;
		
	GetClientRect(GetDesktopWindow(), &R);
	// Create an instance of the Main Window.
	hWnd = CreateWindowEx(dwExStyle, wcex.lpszClassName, TEXT("TEST APPLICATION"),
		WS_OVERLAPPEDWINDOW, R.left+20, R.top+20, R.right-R.left-40, R.bottom-R.top-140,
		HWND_DESKTOP, NULL, hInst, NULL);
	
	if(hWnd){
		// Show the main window and enter the message loop.
		ShowWindow(hWnd, nShowCmd);
		UpdateWindow(hWnd);
		while(GetMessage(&msg, NULL, 0, 0))
		{
			// If the message was not wanted by the Dialog Manager dispatch it like normal.
			if(!IsDialogMessage(hWnd, &msg)){
				TranslateMessage(&msg);
				DispatchMessage(&msg);
			}
		}
	}
	
	// Free up our resources and return.
	return (int)msg.wParam;
}


modified 22-Apr-14 5:35am.

QuestionNeed help on MemDC , Drawtext() coding Pin
econy8-Apr-14 10:08
econy8-Apr-14 10:08 
AnswerRe: Need help on MemDC , Drawtext() coding Pin
leon de boer22-Apr-14 21:30
leon de boer22-Apr-14 21:30 
QuestionCDC exist or not question Pin
econy8-Apr-14 2:28
econy8-Apr-14 2:28 
AnswerRe: CDC exist or not question Pin
econy8-Apr-14 2:37
econy8-Apr-14 2:37 
GeneralRe: CDC exist or not question Pin
CPallini8-Apr-14 4:02
mveCPallini8-Apr-14 4:02 
GeneralRe: CDC exist or not question Pin
econy8-Apr-14 4:29
econy8-Apr-14 4:29 
GeneralRe: CDC exist or not question Pin
CPallini8-Apr-14 11:09
mveCPallini8-Apr-14 11:09 
GeneralRe: CDC exist or not question Pin
econy8-Apr-14 12:47
econy8-Apr-14 12:47 
GeneralRe: CDC exist or not question Pin
CPallini8-Apr-14 21:36
mveCPallini8-Apr-14 21:36 
AnswerRe: CDC exist or not question Pin
Joe Woodbury24-Apr-14 9:30
professionalJoe Woodbury24-Apr-14 9:30 
QuestionSetWindowText Blinking problem Pin
econy7-Apr-14 6:00
econy7-Apr-14 6:00 
AnswerRe: SetWindowText Blinking problem Pin
Richard MacCutchan7-Apr-14 6:06
mveRichard MacCutchan7-Apr-14 6:06 
GeneralRe: SetWindowText Blinking problem Pin
econy7-Apr-14 6:46
econy7-Apr-14 6:46 
GeneralRe: SetWindowText Blinking problem Pin
Richard MacCutchan7-Apr-14 6:55
mveRichard MacCutchan7-Apr-14 6:55 
QuestionRe: SetWindowText Blinking problem Pin
David Crow7-Apr-14 16:39
David Crow7-Apr-14 16:39 
AnswerRe: SetWindowText Blinking problem Pin
willcoxson7-Apr-14 18:17
willcoxson7-Apr-14 18:17 
GeneralRe: SetWindowText Blinking problem Pin
econy8-Apr-14 2:26
econy8-Apr-14 2:26 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.