如何使用visual c + +在win32 API中添加图片框 [英] How to add picture box in win32 API using visual c++

查看:772
本文介绍了如何使用visual c + +在win32 API中添加图片框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在视觉c ++中有一个Window(win32 API)应用程序。我不使用MFC。我必须添加一个picutre框,我的应用程序,并定期更改此图片框的图像。任何人可以帮助我实现上述任务?提前感谢。

解决方案

这是一个相当复杂的任务,在这里发布完整的代码,但我会尽量给一些指导如何做:



第一种方法是加载图片并绘制它


  1. 载入您的图片(不幸的是,简单的Win32 API支持相当多的图片格式BMP,ICO ...)。

      HBITMAP hImage =(HBITMAP)LoadImage(NULL,(LPCSTR)file,IMAGE_BITMAP,0,0,LR_LOADFROMFILE | LR_LOADTRANSPARENT); 


  2. 将句柄存储在应用程序中某处,您可以从WindowProcedure


  3. 在WinProc的WM_PAINT消息中,您需要绘制图像。代码如下:

      HDC hdcMem = CreateCompatibleDC(hDC); // hDC是由Win32API提供的DC结构
    SelectObject(hdcMem,hImage);
    StretchBlt(
    hDC,// destination DC
    left,// x左上角
    top,// y左上角
    宽度,//目标宽度
    height,// destination height
    hdcMem,//你刚刚创建了上面的
    0,
    0,// x和y左上角
    w,//源位图宽度
    h,//源位图高度
    SRCCOPY); //光栅操作


p>

现在,第二种方法是创建一个静态控件,类型为SS_BITMAP,并将其设置为:

  hImage = LoadImage(NULL,file,IMAGE_BITMAP,w,h,LR_LOADFROMFILE); 
SendMessage(hwnd,STM_SETIMAGE,IMAGE_BITMAP,(LPARAM)hImage);

其中hwnd是静态控件的句柄。


I have a Window (win32 API) Application in visual c++. I am not using MFC. I have to add a picutre box to my application and Change the image of this picture box periodically. Can any one help me out in achieving the above task? Thanks in advance.

解决方案

This is quite a complex task to post full code here, but I will try to give a few guidelines on how to do it:

First method is to load the image and paint it

  1. Load your image (unfortunately the plain Win32 API has support for quite a few image formats BMP, ICO ...).

    HBITMAP hImage = (HBITMAP)LoadImage(NULL, (LPCSTR)file, IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE | LR_LOADTRANSPARENT);
    

  2. Store the handle above somewhere in your application where you can access it from your WindowProcedure

  3. In the WinProc on the WM_PAINT message you will need to paint the image. The code is something like:

    HDC hdcMem = CreateCompatibleDC(hDC); // hDC is a DC structure supplied by Win32API
    SelectObject(hdcMem, hImage);
    StretchBlt(
    	hDC,         // destination DC
    	left,        // x upper left
    	top,         // y upper left
    	width,       // destination width
    	height,      // destination height
    	hdcMem,	     // you just created this above
    	0,
    	0,			// x and y upper left
    	w,			// source bitmap width
    	h,			// source bitmap height
    	SRCCOPY);	// raster operation
    

Should work.

Now, the second way of doing it is to create a static control, with type being SS_BITMAP and set its image as:

hImage = LoadImage(NULL, file, IMAGE_BITMAP, w, h, LR_LOADFROMFILE);
SendMessage(hwnd, STM_SETIMAGE, IMAGE_BITMAP, (LPARAM)hImage);

where hwnd is the handle of your static control.

这篇关于如何使用visual c + +在win32 API中添加图片框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆