Advertisement

WinAPI + C > Executing a program on a window.

Started by December 16, 2018 08:59 PM
3 comments, last by timothyjlaird 5 years, 11 months ago

i don't how to begin i've a lot of problem and i've been searching for more than a month (everyday) i'm really tired so i'm gonna ask maybe some of you would help me and it will be really appreciated. Anyways i'm just gonna say one problem. I wanna execute a program from Window i mean u know with stdio.h normal C program i get the .exe file when i do my prog but in windows.h i don't get it the .exe file and idk where to type my prog. I have feeling that it's something related to createProcess function but i don't know how to use it and where to put the program. For now i just want that .exe file appears and it shows a simple thing like printf.

 To be more specific, when i launch my program i get a window i want to read some variables, like Insert a variable 'N' or something. In a normal C program we get the .exe file where i can insert my variables etc but in WinAPI i don't get any execution panel that's what i'm searching for. Program used: CodeBlocks Language : C, not C++.

Thank you.

If you post your actual code somebody should be able to help you out pretty quickly I would imagine. ;)

Advertisement

I'm going to post it but it's a long code anyways here you go:


#include <windows.h>
#include <stdio.h>
#include <conio.h>
#include "resource.h"
#include "entree_sortie.h"
#include "prog.h"
#include"TP3.h"
#define BUFSIZE 4096
#include <commctrl.h>
const char g_szClassName[] = "myWindowClass";

const int ID_TIMER = 1;

const int BALL_MOVE_DELTA = 1;

void addcontrols();
typedef struct _BALLINFO
{
	int width;
	int height;
	int x;
	int y;

	int dx;
	int dy;
}BALLINFO;


BALLINFO g_ballInfo;
HBITMAP g_hbmBall = NULL;
HBITMAP g_hbmMask = NULL;

HBITMAP hbmMask;
HDC hdcMem, hdcMem2;
HBITMAP CreateBitmapMask(HBITMAP hbmColour, COLORREF crTransparent)
{


	BITMAP bm;

	GetObject(hbmColour, sizeof(BITMAP), &bm);
	hbmMask = CreateBitmap(bm.bmWidth, bm.bmHeight, 1, 1, NULL);

	hdcMem = CreateCompatibleDC(0);
	hdcMem2 = CreateCompatibleDC(0);

	SelectObject(hdcMem, hbmColour);
	SelectObject(hdcMem2, hbmMask);

	SetBkColor(hdcMem, crTransparent);

	BitBlt(hdcMem2, 0, 0, bm.bmWidth, bm.bmHeight, hdcMem, 0, 0, SRCCOPY);

	BitBlt(hdcMem, 0, 0, bm.bmWidth, bm.bmHeight, hdcMem2, 0, 0, SRCINVERT);

	DeleteDC(hdcMem);
	DeleteDC(hdcMem2);

	return hbmMask;
}

void DrawBall(HDC hdc, RECT* prc)
{
	HDC hdcBuffer = CreateCompatibleDC(hdc);
	HBITMAP hbmBuffer = CreateCompatibleBitmap(hdc, prc->right, prc->bottom);
	HBITMAP hbmOldBuffer = (HBITMAP)SelectObject(hdcBuffer, hbmBuffer);

	HDC hdcMem = CreateCompatibleDC(hdc);
	HBITMAP hbmOld = (HBITMAP)SelectObject(hdcMem, g_hbmMask);

	FillRect(hdcBuffer, prc, GetStockObject(DKGRAY_BRUSH));

	BitBlt(hdcBuffer, g_ballInfo.x, g_ballInfo.y, g_ballInfo.width, g_ballInfo.height, hdcMem, 0, 0, SRCAND);

	SelectObject(hdcMem, g_hbmBall);
	BitBlt(hdcBuffer, g_ballInfo.x, g_ballInfo.y, g_ballInfo.width, g_ballInfo.height, hdcMem, 0, 0, SRCPAINT);

	BitBlt(hdc, 0, 0, prc->right, prc->bottom, hdcBuffer, 0, 0, SRCCOPY);

	SelectObject(hdcMem, hbmOld);
	DeleteDC(hdcMem);

	SelectObject(hdcBuffer, hbmOldBuffer);
	DeleteDC(hdcBuffer);
	DeleteObject(hbmBuffer);
/***********************************************TEXT************************************************/
	addcontrols(hdc);
}

void UpdateBall(RECT* prc)
{
	g_ballInfo.x += g_ballInfo.dx;
	g_ballInfo.y += g_ballInfo.dy;

	if(g_ballInfo.x < 0)
	{
		g_ballInfo.x = 0;
		g_ballInfo.dx = BALL_MOVE_DELTA;
	}
	else if(g_ballInfo.x + g_ballInfo.width > prc->right)
	{
		g_ballInfo.x = prc->right - g_ballInfo.width;
		g_ballInfo.dx = -BALL_MOVE_DELTA;
	}

	if(g_ballInfo.y < 0)
	{
		g_ballInfo.y = 0;
		g_ballInfo.dy = BALL_MOVE_DELTA;
	}
	else if(g_ballInfo.y + g_ballInfo.height > prc->bottom)
	{
		g_ballInfo.y = prc->bottom - g_ballInfo.height;
		g_ballInfo.dy = -BALL_MOVE_DELTA;
	}
}

BOOL CALLBACK AboutDlgProc(HWND hwnd, UINT Message, WPARAM wParam, LPARAM lParam)
{
	switch(Message)
	{
		case WM_INITDIALOG:

		return TRUE;
		case WM_COMMAND:
			switch(LOWORD(wParam))
			{
				case IDOK:
					EndDialog(hwnd, IDOK);

				break;
			}
		break;
		default:
			return FALSE;
	}
	return TRUE;
}


BOOL CALLBACK ToolDlgProc(HWND hwnd, UINT Message, WPARAM wParam, LPARAM lParam)
{
    switch(Message)
	{
		case WM_INITDIALOG:

		return TRUE;
		case WM_COMMAND:
			switch(LOWORD(wParam))
			{
				case IDOK:
					EndDialog(hwnd, IDOK);

				break;
				case IDCANCEL:
					EndDialog(hwnd, IDCANCEL);
				break;
			}
		break;
		default:
			return FALSE;
	}
	return TRUE;
}



HINSTANCE hInst;
LRESULT CALLBACK WndProc(HWND hwnd, UINT Message, WPARAM wParam, LPARAM lParam)
{
    BITMAP bm;
	switch(Message)
	{
	    //static HWND hButton;
	    case WM_CREATE:
		{
			UINT ret;
			g_hbmBall = LoadBitmap(GetModuleHandle(NULL), MAKEINTRESOURCE(IDB_BALL));
			if(g_hbmBall == NULL)
				MessageBox(hwnd, "Could not load IDB_BALL!", "Error", MB_OK | MB_ICONEXCLAMATION);

			g_hbmMask = CreateBitmapMask(g_hbmBall, RGB(0, 0, 0));
			if(g_hbmMask == NULL)
				MessageBox(hwnd, "Could not create mask!", "Error", MB_OK | MB_ICONEXCLAMATION);

			GetObject(g_hbmBall, sizeof(bm), &bm);

			ZeroMemory(&g_ballInfo, sizeof(g_ballInfo));
			g_ballInfo.width = bm.bmWidth;
			g_ballInfo.height = bm.bmHeight;

			g_ballInfo.dx = BALL_MOVE_DELTA;
			g_ballInfo.dy = BALL_MOVE_DELTA;

			SetTimer(hwnd, ID_TIMER, 50,0);

		}
		break;

		case WM_KEYDOWN:
		     {
		         switch(wParam)
		         {
                case VK_LEFT:
                    {
                    int retour = MessageBox(hwnd,"Vous allez quitter le programme, êtes vous sûr?","ATTENTION!", MB_YESNO | MB_ICONWARNING);
                    if(retour==IDYES)
                        PostMessage(hwnd, WM_CLOSE, 0, 0);
                    break;
                    }
                case VK_RIGHT:
                    {
                    DeleteObject(g_hbmBall);
                    DeleteObject(g_hbmMask);
                    break;
                    }
                case VK_DOWN:
                    {

                    }

		         }
		     }

		case WM_COMMAND:
			switch(LOWORD(wParam))
			{
				case ID_FILE_EXIT:
					PostMessage(hwnd, WM_CLOSE, 0, 0);
				break;
				case ID_HELP:
				{
					int rett = DialogBox(GetModuleHandle(NULL),
						MAKEINTRESOURCE(IDD_ABOUT), hwnd, AboutDlgProc);
				}
				break;
				case ENP:
                ShellExecute( hwnd,"open","enp.pdf",NULL,NULL,SW_SHOWMAXIMIZED);break;
                case TP2:
                ShellExecute( hwnd,"open","tp2.pdf",NULL,NULL,SW_SHOWMAXIMIZED);break;
                case TP3:
                ShellExecute( hwnd,"open","tp3.pdf",NULL,NULL,SW_SHOWMAXIMIZED);break;
                case TP4:
                ShellExecute( hwnd,"open","tp4.pdf",NULL,NULL,SW_SHOWMAXIMIZED);break;
                case TP5:
                ShellExecute( hwnd,"open","tp5.pdf",NULL,NULL,SW_SHOWMAXIMIZED);break;
			}
		break;

/*****************************************************************************************************************************/
		case WM_PAINT:
		{
			RECT rcClient;
			PAINTSTRUCT ps;
			HDC hdc = BeginPaint(hwnd, &ps);
            GetClientRect(hwnd, &rcClient);
			DrawBall(hdc, &rcClient);
			EndPaint(hwnd, &ps);
		}
		break;

		case WM_TIMER:
		{
			RECT rcClient;
			HDC hdc = GetDC(hwnd);
			GetClientRect(hwnd, &rcClient);
			UpdateBall(&rcClient);
			SetTextColor(hdc,RGB(255,0,0));
			DrawBall(hdc, &rcClient);
			ReleaseDC(hwnd, hdc);

		}
		break;
		/*****************************DESTROYING THE MOVING OBJECT *****************************/
		case WM_CLOSE:
			DestroyWindow(hwnd);
		break;
		case WM_DESTROY:
			KillTimer(hwnd, ID_TIMER);
			DeleteObject(g_hbmBall);
			DeleteObject(g_hbmMask);
			PostQuitMessage(0);
		break;
		default:
			return DefWindowProc(hwnd, Message, wParam, lParam);
	}
	return 0;
}


int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
	LPSTR lpCmdLine, int nCmdShow)
{

	WNDCLASSEX wc;
	HWND hwnd;
	MSG Msg;

	wc.cbSize		 = sizeof(WNDCLASSEX);
	wc.style		 = 0;
	wc.lpfnWndProc	 = WndProc;
	wc.cbClsExtra	 = 0;
	wc.cbWndExtra	 = 0;
	wc.hInstance	 = hInstance;
	wc.hIcon		 = LoadIcon(GetModuleHandle(NULL), MAKEINTRESOURCE(""));
	wc.hCursor		 = LoadCursorFromFile("curseur.cur");
	wc.hbrBackground = (HBRUSH)(COLOR_WINDOW);
	wc.lpszMenuName  = MAKEINTRESOURCE(IDR_MYMENU);
	wc.lpszClassName = g_szClassName;
	wc.hIconSm		 = (HICON)LoadImage(NULL, "Icons/menu_one.ico", IMAGE_ICON, 32, 32, LR_LOADFROMFILE);
	SetCursor(wc.hCursor);
	if(!RegisterClassEx(&wc))
	{
		MessageBox(NULL, "Window Registration Failed!", "Error!",
			MB_ICONEXCLAMATION | MB_OK);
		return 0;
	}

	hwnd = CreateWindowEx(
		0,
		g_szClassName,
		"Mini-Projet-C-Programming",
		WS_POPUP|WS_SYSMENU | WS_MINIMIZEBOX,
		CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
		HWND_DESKTOP, NULL, hInstance, NULL);
        SetWindowPos(hwnd, HWND_NOTOPMOST, 0,  0, GetSystemMetrics(SM_CXSCREEN ), GetSystemMetrics( SM_CYSCREEN ), NULL );


	if(hwnd == NULL)
	{
		MessageBox(NULL, "Window Creation Failed!", "Error!",
			MB_ICONEXCLAMATION | MB_OK);
		return 0;
	}

	ShowWindow(hwnd, nCmdShow);
	UpdateWindow(hwnd);

	while(GetMessage(&Msg, NULL, 0, 0) > 0)
	{
		TranslateMessage(&Msg);
		DispatchMessage(&Msg);
	}
	return Msg.wParam;
}


void addcontrols(hdc)
{
    TextOut(hdc, 0, 0, "Première lignedqsdqs", strlen("Première lignedqsdqs"));
}

 

On 12/16/2018 at 2:59 PM, Hachem said:

i don't how to begin i've a lot of problem and i've been searching for more than a month (everyday) i'm really tired so i'm gonna ask maybe some of you would help me and it will be really appreciated. Anyways i'm just gonna say one problem. I wanna execute a program from Window i mean u know with stdio.h normal C program i get the .exe file when i do my prog but in windows.h i don't get it the .exe file and idk where to type my prog. I have feeling that it's something related to createProcess function but i don't know how to use it and where to put the program. For now i just want that .exe file appears and it shows a simple thing like printf.

 To be more specific, when i launch my program i get a window i want to read some variables, like Insert a variable 'N' or something. In a normal C program we get the .exe file where i can insert my variables etc but in WinAPI i don't get any execution panel that's what i'm searching for. Program used: CodeBlocks Language : C, not C++.

Thank you.

Is your goal to launch a console window from your win32 api graphical window? If so, this should give you a starting point:

https://docs.microsoft.com/en-us/windows/console/creation-of-a-console

This topic is closed to new replies.

Advertisement