Using Win32 API

Started by
16 comments, last by programering 17 years, 6 months ago
Quote:Original post by programering
Quote:Original post by DaBookshah
General Tip: If you need info about win32 api method xxx, Google: site:microsoft.com xxx. Works every time.


Isn't that porn?


lol,

... where xxx is the phrase you are searching for. This minimises Googles search to just Microsoft.com, and msdn.microsoft.com is better too.
Advertisement
Quote:Original post by programering
I need to load a bitmap from a FILE *pFile ptr, how todo that?
And how to blit it? The prototype for blitting takes a HDC.
BOOL BitBlt(
HDC hdcDest, // handle to destination DC
int nXDest, // x-coord of destination upper-left corner
int nYDest, // y-coord of destination upper-left corner
int nWidth, // width of destination rectangle
int nHeight, // height of destination rectangle
HDC hdcSrc, // handle to source DC
int nXSrc, // x-coordinate of source upper-left corner
int nYSrc, // y-coordinate of source upper-left corner
DWORD dwRop // raster operation code
);
To convert it seems to be a bit complicated.

BTW, what the difference between BITMAP/HBITMAP and HDC?

The only image format that Win32 API seems to support is just *.bmp, why?


Do you really need to load from a FILE*? Or do you just want to laod from a file on disk? Loading from a FILE* will involve parsing the file youself, since the Win32 methods don't have any concept of FILE*'s. So you'd need to allocate an empty HBITMAP, then parse the file into it.
If you want to load from a file on disk, then you can use LoadImage and specify the LR_LOADFROMFILE flag.

I'd advise having a look around google for some Win32 GDI tutorials - I intend to write one myself soon.
Basically, all drawing operations use HDCs, which is a Handle to a Device Context. A HDC is basically where all the drawing commands go. The HDC usually represents a monitor, but it can also represent a printer and things too.
To draw onto a bitmap, you select the HBITMAP into the HDC using SelectObject(), which sort of attaches the bitmap to the HDC for use. You do the same with HPENs, HBRUSHes, and anything else if you want to draw.
Once you have the bitmap selected into the HDC you can use the BitBlt() command to blit rectangular regions of the device context to another device context, which copies chunks of the bitmap.
Quote:Original post by Evil Steve
Quote:Original post by programering
I need to load a bitmap from a FILE *pFile ptr, how todo that?
And how to blit it? The prototype for blitting takes a HDC.
BOOL BitBlt(
HDC hdcDest, // handle to destination DC
int nXDest, // x-coord of destination upper-left corner
int nYDest, // y-coord of destination upper-left corner
int nWidth, // width of destination rectangle
int nHeight, // height of destination rectangle
HDC hdcSrc, // handle to source DC
int nXSrc, // x-coordinate of source upper-left corner
int nYSrc, // y-coordinate of source upper-left corner
DWORD dwRop // raster operation code
);
To convert it seems to be a bit complicated.

BTW, what the difference between BITMAP/HBITMAP and HDC?

The only image format that Win32 API seems to support is just *.bmp, why?


Do you really need to load from a FILE*? Or do you just want to laod from a file on disk? Loading from a FILE* will involve parsing the file youself, since the Win32 methods don't have any concept of FILE*'s. So you'd need to allocate an empty HBITMAP, then parse the file into it.
If you want to load from a file on disk, then you can use LoadImage and specify the LR_LOADFROMFILE flag.

I'd advise having a look around google for some Win32 GDI tutorials - I intend to write one myself soon.
Basically, all drawing operations use HDCs, which is a Handle to a Device Context. A HDC is basically where all the drawing commands go. The HDC usually represents a monitor, but it can also represent a printer and things too.
To draw onto a bitmap, you select the HBITMAP into the HDC using SelectObject(), which sort of attaches the bitmap to the HDC for use. You do the same with HPENs, HBRUSHes, and anything else if you want to draw.
Once you have the bitmap selected into the HDC you can use the BitBlt() command to blit rectangular regions of the device context to another device context, which copies chunks of the bitmap.


I want to use the FILE *pointer cos I've written a directory filepath parser.
That open the FILE *.

The prototype:
FILE *LoadFile(FILEPATH *lpFilePath, LPCSTR mode);

The FILEPATH struct definition:
struct tagFILEPATH {
FILEDIR fileDir;
LPSTR fileDirs[MAX_FILEPATH_DIRS];
LPSTR fileName;
LPSTR fullPath;
};
typedef struct tagFILEPATH FILEPATH;

The code: File.h
#ifndef _File_h#define _File_h#include <stdio.h>enum FILEDIR {	FILEDIR_APP,	FILEDIR_DATA,	FILEDIR_ENTITIES,	FILEDIR_HUMANS,        NUM_FILEDRIS,	FILEDIR_CUSTOM = NUM_FILEDRIS};#define MAX_FILEPATH_DIRS 15struct tagFILEPATH {	FILEDIR fileDir;	LPSTR fileDirs[MAX_FILEPATH_DIRS];	LPSTR fileName;	LPSTR fullPath;};typedef struct tagFILEPATH FILEPATH;FILE *LoadFile(FILEPATH *lpFilePath, LPCSTR mode);#endif


File.cpp
#include "StdAfx.h"#include "File.h"/////////////////////////////////////LPSTR fileDirs[NUM_FILEDIRS] ={	NULL,	NULL, // To be assigned by LoadDataDir();	"Entities",	"Humans"};#define MAX_FILEPATH_CHARS 256WORD fullPathPos;CHAR fullPath[MAX_FILEPATH_CHARS];BOOL AddToFullPath(LPSTR pasteStr){	WORD pasteStrLen = strlen(pasteStr);	for (WORD i = 0; i < pasteStrLen; i++)	{		if (fullPathPos < MAX_FILEPATH_CHARS)			fullPath[fullPathPos++] = pasteStr;		else break;	}}FILEPATH filePath;/*VOID InitFilePath(VOID){	fullPathPos = 0;	filePath.fullPath = fullPath;}*/FILEPATH *GetFilePath(VOID){	filePath.fullPath = fullPath;	return &filePath;}/////////////////////////////////////FILE *LoadFile(FILEPATH *lpFilePath, LPCSTR mode){	switch (lpFilePath->fileDir)	{	case FILEDIR_APP:		lpFilePath->nFileDirs = 0;		break;	case FILEDIR_DATA:		lpFilePath->nFileDirs = 1;		lpFilePath->fileDirs[0] = fileDirs[FILEDIR_DATA];		break;	case FILEDIR_ENTITIES:		lpFilePath->nFileDirs = 2;		lpFilePath->fileDirs[0] = fileDirs[FILEDIR_DATA];		lpFilePath->fileDirs[1] = fileDirs[FILEDIR_ENTITIES];		break;	case FILEDIR_HUMANS:		lpFilePath->nFileDirs = 3;		lpFilePath->fileDirs[0] = fileDirs[FILEDIR_DATA];		lpFilePath->fileDirs[1] = fileDirs[FILEDIR_ENTITIES];		lpFilePath->fileDirs[2] = fileDirs[FILEDIR_HUMANS];		break;	default:		break;	}	fullPathPos = 0;	UCHAR count = lpFilePath->nFileDirs;	if (count < MAX_FILEPATH_DIRS)	{		for (UCHAR i = 0; i < count; i++)		{			AddToFullPath(lpFilePath->fileDirs);			AddToFullPath("\");		}	}	else fprintf(stderr,"Too many directories in file path. "			"The limit is %d directories.\n\n",MAX_FILEPATH_DIRS);	AddToFullPath(lpFilePath->fileName);	fullPath[fullPathPos] = '\0';	FILE *lpFile = fopen(fullPath,mode);	if (!lpFile)	{		LPSTR modeType;		if (strcmp(mode,"r") == 0) modeType = "for reading";		else		if (strcmp(mode,"w") == 0) modeType = "for writing";		else modeType = "in an unknown mode";		fprintf(stderr,"Couldn't open file \"%s\"\n",fullPath,modeType);	}	return lpFile;}


[Edited by - programering on October 19, 2006 11:34:56 AM]
Now I found this with google.
What's the difference between the usual CreateWindow()/WNDCLASS and that Ex/EX version?
Quote:Original post by programering
What's the difference between the usual CreateWindow()/WNDCLASS and that Ex/EX version?


For right now, think of the Ex versions as extended versions. They allow you to do a little more customization than would normally be possible by the plain CreateWindow. Such as having a small icon and a large icon, etc. (Or more window styles, etc).
Quote:Original post by programering
What's the difference between the usual CreateWindow()/WNDCLASS and that Ex/EX version?

Read the documentation. Surprisingly, it actually describes the differences.

As for your loading a bitmap from a FILE *, that's just setting yourself up for a lot of pain. Yeah, you've written a directory file path parser... but Win32 already had one (FindFile, FindFileNext).

Before you start writing functions and bits of code that later force you to put things together in the least elegant manner possible, familiarize yourself with the facilities that come included in the box - what your language's standard library provides, what your platform's SDK provides.

Happy hacking.
Quote:Original post by Oluseyi
As for your loading a bitmap from a FILE *, that's just setting yourself up for a lot of pain. Yeah, you've written a directory file path parser... but Win32 already had one (FindFile, FindFileNext).


I think I solve that with:
LPSTR CalcFilePath(FILEPATH *lpFilePath){	switch (lpFilePath->fileDir)	{	case FILEDIR_APP:		lpFilePath->nFileDirs = 0;		break;	case FILEDIR_DATA:		lpFilePath->nFileDirs = 1;		lpFilePath->fileDirs[0] = fileDirs[FILEDIR_DATA];		break;	case FILEDIR_ENTITIES:		lpFilePath->nFileDirs = 2;		lpFilePath->fileDirs[0] = fileDirs[FILEDIR_DATA];		lpFilePath->fileDirs[1] = fileDirs[FILEDIR_ENTITIES];		break;	case FILEDIR_HUMANS:		lpFilePath->nFileDirs = 3;		lpFilePath->fileDirs[0] = fileDirs[FILEDIR_DATA];		lpFilePath->fileDirs[1] = fileDirs[FILEDIR_ENTITIES];		lpFilePath->fileDirs[2] = fileDirs[FILEDIR_HUMANS];		break;	default:		break;	}	fullPathPos = 0;	UCHAR count = lpFilePath->nFileDirs;	if (count < MAX_FILEPATH_DIRS)	{		for (UCHAR i = 0; i < count; i++)		{			AddToFullPath(lpFilePath->szFileDirs);			AddToFullPath("\");		}	}	else fprintf(stderr,"Too many directories in file path. "			"The limit is %d directories.\n\n",MAX_FILEPATH_DIRS);	AddToFullPath(lpFilePath->szFileName);	szFullPath[wFullPathPos] = '\0';        return szFullPath;}...// Usage:handle = LoadImage(NULL,CalcFilePath(lpFilePath),IMAGE_BITMAP,0,0,LR_LOADFROMFILE);

This topic is closed to new replies.

Advertisement