Click here to Skip to main content
15,902,635 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am trying to create a window where to put some text and a image ( text is the description of image ), but I succeed to put only the text over the image . Can you please tell me where is the mistake ? I want to put the text above the image!

What I have tried:

C++
#include <Windows.h>

char Window_Title [] = {"First Window"};
int X_Coordinate = 215;
int Y_Coordinate = 415;
int Width = 400;
int Height = 280;
char Text[] = {"this is a comment"};

HBITMAP bitmap;

#define ErrorMessageBox(a,b) MessageBox(a,b,"Error:",MB_ICONWARNING);

bool SetUpWindowClass (char*, int, int, int);

LRESULT CALLBACK WindowProcedure (HWND, unsigned int, WPARAM, LPARAM);

int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpsCmdLine, int iCmdShow) 
{	
	if (!SetUpWindowClass ("1", 255, 255, 255)) 
	{
		ErrorMessageBox (NULL, "Window class \"1\" failed");
		return 0;
	}

	HWND hWnd = CreateWindow ("1", Window_Title, WS_OVERLAPPEDWINDOW, X_Coordinate, Y_Coordinate, Width, Height, NULL, NULL, hInstance, NULL);	

	if (!hWnd) 
	{
		ErrorMessageBox (NULL, "Window handle = NULL");
		return 0;
	}

	ShowWindow (hWnd, SW_SHOW);
	
	MSG uMsg;

	while (GetMessage (&uMsg, NULL, 0, 0) > 0) 
	{
		TranslateMessage (&uMsg);
		DispatchMessage (&uMsg);
	}

	return 0;
}

bool SetUpWindowClass (char *cpTitle, int iR, int iG, int iB) 
{
	WNDCLASSEX WindowClass;
	WindowClass.cbClsExtra = 0;
	WindowClass.cbWndExtra = 0;
	WindowClass.cbSize = sizeof (WNDCLASSEX);
	WindowClass.style = 0;
	WindowClass.lpszClassName = cpTitle;
	WindowClass.lpszMenuName = NULL;
	WindowClass.lpfnWndProc = WindowProcedure;
	WindowClass.hInstance = GetModuleHandle (NULL);
	WindowClass.hCursor = LoadCursor (NULL, IDC_ARROW);
	WindowClass.hbrBackground = CreateSolidBrush (RGB (iR, iG, iB));
	WindowClass.hIcon = LoadIcon (NULL, IDI_APPLICATION);
	WindowClass.hIconSm = LoadIcon (NULL, IDI_APPLICATION);
	if (RegisterClassEx (&WindowClass)) return true;
	else return false;
}

LRESULT CALLBACK WindowProcedure (HWND hWnd, unsigned int uiMsg, WPARAM wParam, LPARAM lParam) 
{
	switch (uiMsg) 
	{
		case WM_CLOSE:
			DestroyWindow (hWnd);
			break;
		
		case WM_DESTROY:
			PostQuitMessage (0);
			break;

		case WM_PAINT: 
			{				
				bitmap=(HBITMAP)LoadImage(NULL,TEXT("D:\\bitmap1.bmp"),IMAGE_BITMAP,0,0,LR_LOADFROMFILE);

				PAINTSTRUCT ps;
				
										
				HDC hDC = BeginPaint (hWnd, &ps);

				HDC hMemDC=CreateCompatibleDC(hDC);
				::SelectObject(hMemDC,bitmap);
				BitBlt(hDC,0,0,400,400,hMemDC,0,0,SRCCOPY);

				char *cpaText [] = {""};

				*cpaText = Text;
				
				int iY = 5;
				
				for (int iLoopCounter = 0; cpaText [iLoopCounter] != '\0'; iLoopCounter++, iY += 20)
				{
					TextOut (hDC, 5, iY, cpaText [iLoopCounter], strlen (cpaText [iLoopCounter]));					
				}		
								
				EndPaint (hWnd, &ps);
			}
			break;
	}
	return DefWindowProc (hWnd, uiMsg, wParam, lParam);
}
Posted
Updated 9-Jul-19 1:18am
v2

What you are doing is totally wrong, because you load the image in every draw call again und have a backed in fixed path to it. Normally you load it once and store it in some app or global data.

Such data like images are in such apps in resource-files. But there are different approches, so you must look for a solution. Maybe you look into this article about images which uses resources.
 
Share this answer
 
You are writing the text starting at 5,5. But the image extends from 0,0 to 400,400. You are also assuming the height of your font is 20 pixels, which may not be true.
 
Share this answer
 
Hello .... first of all many many thanks for your advice !

Regarding this ...i did some changes but now I have some problems with scrollbars .... Will appear but are not working ... can you tell me where is the mistake ?


C++
<pre>#ifndef UNICODE
#define UNICODE
#endif 

#include <windows.h>
#include <iostream>
#include <fstream>
#include <string>


int X_Coordinate = 215;
int Y_Coordinate = 415;
int Width = 400;
int Height = 380;
char Text[] = {"this is a comment"};

HBITMAP bitmap;

LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);

int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE, PWSTR pCmdLine, int nCmdShow)
{
    // Register the window class.
    const wchar_t CLASS_NAME[]  = L"Sample Window Class";
    
    WNDCLASS wc = { };

	//Registering the Window Class
    wc.lpfnWndProc   = WindowProc;
    wc.hInstance     = hInstance;
    wc.lpszClassName = CLASS_NAME;

    RegisterClass(&wc);


    // Create the window.
    HWND hwnd = CreateWindowEx(
        0,                              // Optional window styles.
        CLASS_NAME,                     // Window class
        L"My first window",             // Window text
        WS_OVERLAPPEDWINDOW |           // Window style
            WS_VSCROLL | WS_HSCROLL ,   // Window scrollbar        

        // Size and position
        X_Coordinate, Y_Coordinate, Width, Height,

        NULL,       // Parent window    
        NULL,       // Menu
        hInstance,  // Instance handle
        NULL        // Additional application data
        );


    if (hwnd == NULL)
    {
        return 0;
    }

    ShowWindow(hwnd, nCmdShow);

    // Run the message loop.

    MSG msg = { };
    while (GetMessage(&msg, NULL, 0, 0))
    {
        TranslateMessage(&msg);
        DispatchMessage(&msg);
    }

    return 0;
}

LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
    switch (uMsg)
    {
    case WM_DESTROY:
        PostQuitMessage(0);
        return 0;


    case WM_PAINT:
        {
 
			bitmap=(HBITMAP)LoadImage(NULL,TEXT("D:\\bitmap1.bmp"),IMAGE_BITMAP,0,0,LR_LOADFROMFILE);
			
			PAINTSTRUCT ps;
            HDC hdc = BeginPaint(hwnd, &ps);


		////////////////////
				
			    int index = sizeof(Text);
			    

				wchar_t wtext[sizeof(Text)];
				memset(wtext,1,sizeof(wtext));
				mbstowcs(wtext,Text,strlen(Text)+1); 
				LPWSTR a = wtext;

			    index = index + 100;

				HDC hMemDC=CreateCompatibleDC(hdc);
				::SelectObject(hMemDC,bitmap);
				BitBlt(hdc,0,index,700,980,hMemDC,0,0,SRCCOPY);


				long retval;

				retval=SetTextAlign(hdc,TA_TOP);


				for (int x = 0; x < sizeof(Text); x++)
				{
					
					TextOut (hdc, 5, 5, a, sizeof(Text));
				
				}



         /////////////////////

            //FillRect(hdc, &ps.rcPaint, (HBRUSH) (COLOR_WINDOW+1));

            EndPaint(hwnd, &ps);
        }
        return 0;

    }
    return DefWindowProc(hwnd, uMsg, wParam, lParam);
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900