Hey Im tryin to make a simple project which is pretty simple, but I cant seem to get it right :P Anyways, it goes like this. I have the main file which creates the window and stuff. And then I have an other file which populates the main file with controls such as text boxes, labels etc. Now my problem is while trying to do this. I want that when I click the text box, that it becomes empty. As right im sending a SendMessage to populate the text box with text. But I want that when someone clicks the textbox that that text goes away.

So what Im trying to do is, Im trying to send a SendMessage from the main file to the Edit Box. But my problem is in the declaration of hEdit (the HWND variable or whatever you call it). Here is what I have.

Average Calculator.cpp

//--------------------------------------------------------------------
// Made by Abdul. Average Calculator. Also to show me into beginner
// C++ Win32 programming. I like it :)
// Average Calculator.cpp : Defines the entry point for the application.
//

#include "stdafx.h"
#include "Average Calculator.h"
#include "PaintHandler.h"




#define MAX_LOADSTRING 100

// Global Variables:
HINSTANCE hInst;								// current instance
TCHAR szTitle[MAX_LOADSTRING];					// The title bar text
TCHAR szWindowClass[MAX_LOADSTRING];			// the main window class name
HFONT hf;										// The universal font


// Forward declarations of functions included in this code module:
ATOM				MyRegisterClass(HINSTANCE hInstance);
BOOL				InitInstance(HINSTANCE, int);
LRESULT CALLBACK	WndProc(HWND, UINT, WPARAM, LPARAM);
INT_PTR CALLBACK	About(HWND, UINT, WPARAM, LPARAM);

int APIENTRY _tWinMain(HINSTANCE hInstance,
                     HINSTANCE hPrevInstance,
                     LPTSTR    lpCmdLine,
                     int       nCmdShow)
{
	UNREFERENCED_PARAMETER(hPrevInstance);
	UNREFERENCED_PARAMETER(lpCmdLine);

	MSG msg;
	HACCEL hAccelTable;

	// Initialize global strings
	LoadString(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
	LoadString(hInstance, IDC_AVERAGECALCULATOR, szWindowClass, MAX_LOADSTRING);
	MyRegisterClass(hInstance);

	// Perform application initialization:
	if (!InitInstance (hInstance, nCmdShow))
	{
		return FALSE;
	}

	hAccelTable = LoadAccelerators(hInstance, MAKEINTRESOURCE(IDC_AVERAGECALCULATOR));

	// Main message loop:
	while (GetMessage(&msg, NULL, 0, 0))
	{
		if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg))
		{
			TranslateMessage(&msg);
			DispatchMessage(&msg);
		}
	}

	return (int) msg.wParam;
}



//
//  FUNCTION: MyRegisterClass()
//
//  PURPOSE: Registers the window class.
//
//  COMMENTS:
//
//    This function and its usage are only necessary if you want this code
//    to be compatible with Win32 systems prior to the 'RegisterClassEx'
//    function that was added to Windows 95. It is important to call this function
//    so that the application will get 'well formed' small icons associated
//    with it.
//
ATOM MyRegisterClass(HINSTANCE hInstance)
{
	WNDCLASSEX wcex;

	wcex.cbSize = sizeof(WNDCLASSEX);

	wcex.style			= CS_HREDRAW | CS_VREDRAW;
	wcex.lpfnWndProc	= WndProc;
	wcex.cbClsExtra		= 0;
	wcex.cbWndExtra		= 0;
	wcex.hInstance		= hInstance;
	wcex.hIcon			= LoadIcon(hInstance, MAKEINTRESOURCE(IDI_AVERAGECALCULATOR));
	wcex.hCursor		= LoadCursor(NULL, IDC_ARROW);
	wcex.hbrBackground	= (HBRUSH)(COLOR_WINDOW+1);
	wcex.lpszMenuName	= MAKEINTRESOURCE(IDC_AVERAGECALCULATOR);
	wcex.lpszClassName	= szWindowClass;
	wcex.hIconSm		= LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_SMALL));

	return RegisterClassEx(&wcex);
}

//
//   FUNCTION: InitInstance(HINSTANCE, int)
//
//   PURPOSE: Saves instance handle and creates main window
//
//   COMMENTS:
//
//        In this function, we save the instance handle in a global variable and
//        create and display the main program window.
//
BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
   HWND hWnd;

   hInst = hInstance; // Store instance handle in our global variable

   hWnd = CreateWindow(szWindowClass, szTitle, WS_OVERLAPPED | WS_CAPTION | WS_SYSMENU | WS_MINIMIZEBOX,
      CW_USEDEFAULT, 0, win_height, win_width, NULL, NULL, hInstance, NULL);

   if (!hWnd)
   {
      return FALSE;
   }
   
   ShowWindow(hWnd, nCmdShow);
   hf = setUniversalFont(hWnd, hf); // Set the Universal Font. See Paint Handler to change	
   UpdateWindow(hWnd);	
   initializeAppContents(hWnd, hInstance); //

   return TRUE;
}

void initializeAppContents(HWND hWnd, HINSTANCE hInstance) {

}

//
//  FUNCTION: WndProc(HWND, UINT, WPARAM, LPARAM)
//
//  PURPOSE:  Processes messages for the main window.
//
//  WM_COMMAND	- process the application menu
//  WM_PAINT	- Paint the main window
//  WM_DESTROY	- post a quit message and return
//
//
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
	int wmId, wmEvent;
	PAINTSTRUCT ps;
	HDC hdc;

	switch (message)
	{
	case WM_CREATE:
		MainScreen(hWnd);
		break;
	case WM_COMMAND:
		wmId    = LOWORD(wParam);
		wmEvent = HIWORD(wParam);
		// Parse the menu selections:
		switch (wmId)
		{
		case IDM_RESET:
			//TODO: RESET
			break;
		case ID_HELP_ABOUT:
			MessageBox(hWnd, "This Program Was Made By: Abdul. D \n Educational Purposes Only \n Version: 1", "About!", MB_OK | MB_ICONINFORMATION);
			break;
		case IDM_EXIT:
			DestroyWindow(hWnd);
			break;
		case EN_CHANGE:

				MessageBox(hWnd, "You pressed", "Hey", MB_OK);
				SendMessage(hEdit, WM_SETTEXT, NULL, (LPARAM)"Insert text here...");
				break;

		default:
			return DefWindowProc(hWnd, message, wParam, lParam);
		}
		break;
	case WM_PAINT:
		hdc = BeginPaint(hWnd, &ps);
		//printText(hWnd, hdc, 250, 250, "hi", hf);
		EndPaint(hWnd, &ps);
		break;
	case WM_DESTROY:		
		PostQuitMessage(0);
		break;
	default:
		return DefWindowProc(hWnd, message, wParam, lParam);
	}
	return 0;
}

Average Calculator.h

#pragma once

#include "resource.h"
HWND hEdit;

PaintHandler.cpp

#include "stdafx.h"
#include "Average Calculator.h"

long lfHeight;


void writeText(HWND hWnd, HDC hdc, int x, int y, const char * text) {
	TextOut(hdc, x, y, text,11);
}

HFONT setUniversalFont(HWND hWnd, HFONT hf) { 
	/* Create the Universal Font as Plain Text */
	HDC hdc = GetDC(hWnd);
    lfHeight = -MulDiv(12, GetDeviceCaps(hdc, LOGPIXELSY), 72);
    ReleaseDC(hWnd, hdc);
    hf = CreateFont(lfHeight, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, "Calibri");
    if(!hf)
	{
        MessageBox(hWnd, "Font creation Failed!", "Error", MB_OK | MB_ICONEXCLAMATION);
		return hf;
    } else {
		//MessageBox(hWnd, "Font creation Succeeded!", "Success", MB_OK | MB_ICONINFORMATION);
		return hf;
	}
}
void printText(HWND hWnd, HDC hdc, int x, int y, LPCSTR text, HFONT hf) {
	RECT rect;
	HFONT hfont = (HFONT) SelectObject(hdc, hf); // TOOD: test
	rect.top = 0;
	rect.left = 0;
	rect.right = x;
	rect.bottom = y;
    DrawText(hdc, text, -1, &rect, DT_WORDBREAK);
}

void MainScreen(HWND hWnd) {


		/*EDIT BOX*/
		hEdit=CreateWindowEx(WS_EX_CLIENTEDGE,
			"EDIT",
			"",
			WS_CHILD|WS_VISIBLE|
			ES_AUTOHSCROLL|ES_NUMBER,
			10,
			10,
			100,
			25,
			hWnd,
			(HMENU)IDC_MAIN_EDIT,
			GetModuleHandle(NULL),
			NULL);
		HGDIOBJ hfDefault=GetStockObject(DEFAULT_GUI_FONT);
		SendMessage(hEdit,
			WM_SETFONT,
			(WPARAM)hfDefault,
			MAKELPARAM(FALSE,0));
		/*EDIT BOX DEFAULT TEXT */
		SendMessage(hEdit, WM_SETTEXT, NULL, (LPARAM)"Insert text here...");


}

PaintHandler.h

#pragma once
	void writeText(HWND hWnd, HDC hdc, int x, int y, char text);
	HFONT setUniversalFont(HWND hWnd, HFONT hf);
	//void printText(HWND hWnd, HDC hdc);
	void printText(HWND hWnd, HDC hdc, int x, int y, LPCSTR text, HFONT hf);
	void initializeAppContents(HWND hWnd, HINSTANCE hInstance);
	void MainScreen(HWND hWnd);

Pragma once is suppose to help, no?
Any help is appreciated. Thanks.

Also: What is the difference of including a file in a header, than including it in the CPP ?

Recommended Answers

All 2 Replies

> Pragma once is suppose to help, no?

No. Pragma once only helps against multiple inclusions into the same translation unit (read, same .cpp file), that is, at the compilation time.

Your situation is different: Average Calculator.cpp and PaintHandler.cpp both independently include Average Calculator.h; they are both independently compiled, and therefore both independently declare the instance of hEdit. Linker complains.

You need to make sure that hEdit is declared extern in all files except one.

And how do i do that. xD Sorry, I am a noob in C++. All i know is that you have to add extern. I have tried it. But I got errors.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.