愚蠢而基本,但仍然困扰着我 [英] Stupid and basic, but still bothering me

查看:86
本文介绍了愚蠢而基本,但仍然困扰着我的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从来都不是Windows编程的忠实拥护者.我在嵌入式世界中度过了自己的一生.

但是我在Borland Builder 6环境中玩Windows,真的不介意对野兽的无知无知.

但是我一直想知道位图以及将它们显示在屏幕上所需的看似无穷无尽的代码.此外,我对众多的技术和无数的示例感到惊讶.

所以我的问题是:最简单的方法是将代表alpha,red,grn,blue的32位int数组(我知道alpha被神秘地忽略了)并放在屏幕上?

Borland Builder很好地隐藏了所有必要的东西.我知道需要更新Windows消息等等,但是对于本示例,假设它只是采用二维像素数组并将它们写入屏幕的相同大小区域.

实现此目标所需的最低C代码是多少?在此先感谢您.

I have never been a big fan of Windows programming. I spend my life in the embedded world.

But I play with windows in a Borland Builder 6 environment and don''t really mind being ignorant to the underbelly of the beast.

But I''ve always wondered about Bitmaps and the seemingly endless code needed to get them to the screen. Further, I''m amazed at the plethora of techniques and myriad examples.

So my question is: what is the simplest way to take an array of 32-bit ints which represent alpha, red, grn, blue (I know alpha is myteriously ignored) and put them on the screen?

Borland Builder does a nice job of hiding all that is necessary. I know Windows messages are needed to update and so forth, but assume for this example it''s just taking a 2-dimensional array of pixels and writing them to a same-sized area of the screen.

What''s the minimum C code needed to make this happen? Thanks in advance.

推荐答案

您将使用C++(例如,通过MFC/ATL CImage或直接使用GDI+)获得最少的代码.相反,如果您想精打细算",则事情会更加复杂,例如,请参见下面的代码示例
You would have a minimal code using C++, via, for instance MFC/ATL CImage or directly using GDI+. If you instead want to go ''to the metal'' then things are a bit more complex, see, for instance, the following code sample Scaling an image[^].
However you try use the following code inside the WM_PAINT handler (assuming you have a 32 bit color depth on your system :rolleyes:)
#define H 100
#define W 80
    HDC hdc;  
    HDC hmemdc;
    PAINTSTRUCT ps;
    DWORD buffer[H][W];
    INT r,c;
    BITMAP hbmp;
    hdc = BeginPaint(hwnd, &ps);
    hmemdc = CreateCompatibleDC(hdc);
    for (r=0; r<h; r++)
      for (c=0; c<w; c++)
       buffer[r][c] = RGB((BYTE)(r*255/H), (BYTE)(c*255/W), 0); 
    hbmp = CreateCompatibleBitmap(hdc, W,H);
    SetBitmapBits(hbmp, sizeof(DWORD)*W*H, buffer);
    SelectObject(hmemdc, hbmp);
    BitBlt(hdc, 0,0, W, H, hmemdc, 0,0, SRCCOPY);
    ReleaseDC(hwnd, hmemdc);
    EndPaint(hwnd, &ps);



(请注意:上面的代码效率很低,在实际实现中,应将所有可移动对象"移至WM_PAINT处理程序之外),例如,数组初始化,位图创建,...-)



(Please note: the above code is inefficient, in actual implementation, you should move ''all the moveable'' -e.g. array initialization, bitmap creation, ...- outside the WM_PAINT handler)


这篇关于愚蠢而基本,但仍然困扰着我的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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