Creating an Image Control Class with hWnd with Windows API

Started by
5 comments, last by wintertime 10 years, 7 months ago

Hi there,

I'm writing a home made Business Intelligence application. It will be a Windows-based program written with C++. Something I can log into, view data in graphs, maps, etc.

I only started C++ a few months ago and I started writing a basic wrapper around the Windows API so I can reuse it more easily. I've got to the point where I'm starting to create Control objects to be added to the Forms. One Control I'm now trying to create is an Image Control.

I want the Image to control to also receive events through it's own WndProc(). I just finished making a TextBox control, where I had to subclass it so I could raise the OnKeyDown(...) events, etc. It was using the Windows CreateWindowEx(. "EDIT" ..).

My question is what type of window should I be using to make an image control. Could I create a "STATIC" control and subclass it. I want to be able to use SetWindowPos().. on it and have the object move like any other regular control (i.e: TextBox).

Thanks!

Advertisement

Register&create a normal child window (WS_CHILD) and define its paint handler (handle WM_PAINT in its window procedure) to draw the image associated with the control.

If you want SetWindowPos to redraw the image with the new window size, you should also handle WM_RESIZE in the window procedure, and invalidate the window area therein. Also invalidate the control area when you change the source image property (which you define as part of your control class).

Niko Suni

Ah, so just a regular Window with WS_CHILD, but without using a system class. Thanks! smile.png

Hi there

You can simply create image control follow the demo code:

using RasterEdge.Imaging.Basic.Core;
using RasterEdge.Imaging.Basic.Codec;
using RasterEdge.Imaging.TIFF;
using RasterEdge.Imaging.Basic;

namespace RE__Test
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void button1_Click(object sender, EventArgs e)
{
int width = 100;
int height = 60;

REImage reImage = new REImage(width, height, ImageMode.RGB888);// Create a image with specified width, height and color mode; all pixels are set to white

REFile.SaveImageFile(reImage, "c:/reimage.bmp", new BMPEncoder());
REFile.SaveImageFile(reImage, "c:/reimage.png", new PNGEncoder());
REFile.SaveImageFile(reImage, "c:/reimage.jpg", new JPGEncoder());
REFile.SaveImageFile(reImage, "c:/reimage.tif", new TIFEncoder());
}
}
}

Hi there

You can simply create image control follow the demo code:

He asked for C++, not C#.


I only started C++ a few months ago and I started writing a basic wrapper around the Windows API so I can reuse it more easily.

Creating a reasonable and reusable wrapper for the API, especially for windows (HWND), is a surprisingly complex task. Are you sure this is what you want? I mean, if you do it for educational purposes or just for fun... go ahead. But if you just want to finish your application, consider using a framework like QT or wxWidgets.

Yeah for handling windows it could get difficult to wrap the calls in a good way into classes, but at least hiding the handles so not everything needs to include windows.h would be helpful.

Also there are some smaller things where you need platformspecific calls and its easy and helpful to wrap them, for example memory mapped files, maybe VirtualAlloc and related functions, critical sections and other thread-related things.

Oh and maybe this library helps, I didnt actually use it, but it seems good for making a simple image manipulation program and even opening windows with images and graphs generated from them: http://cimg.sourceforge.net/

This topic is closed to new replies.

Advertisement