• Welcome to Valhalla Legends Archive.
 

BitBlt?

Started by brew, November 12, 2007, 11:43 AM

Previous topic - Next topic

brew


#define WINVER 0x0500
#define _WIN32_WINNT 0x0501
#include <windows.h>
#include <stdio.h>

const char szAppName[] = "blah";
HWND hWnd_main, hWnd_label;
HINSTANCE g_hInst;
ULARGE_INTEGER systemul_last, idleul_last;
HBITMAP hPicture1, hPicture2;
unsigned long lpfnGetSystemTimes;

LRESULT CALLBACK WndProc(HWND hwnd, UINT iMsg, WPARAM wParam, LPARAM lParam);
void __stdcall asdf(HWND hwnd, unsigned int asdf, unsigned int asdfg, unsigned long lparam);
int GetCPUUsage();

typedef BOOL (WINAPI *pfGetSystemTimes)(LPFILETIME, LPFILETIME, LPFILETIME);

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR szCmdLine, int iCmdShow) {
if (hPrevInstance)
return 0;
WNDCLASSEX wndclass;
MSG msg;
wndclass.cbSize        = sizeof(wndclass);
wndclass.style         = CS_HREDRAW | CS_VREDRAW;
wndclass.lpfnWndProc   = WndProc;
wndclass.cbClsExtra    = 0;
wndclass.cbWndExtra    = 0;
wndclass.hInstance     = hInstance;
wndclass.hIcon         = 0;
wndclass.hCursor       = LoadCursor(NULL, IDC_ARROW);
wndclass.hbrBackground = (HBRUSH) (COLOR_WINDOW);
wndclass.lpszMenuName  = NULL;
wndclass.lpszClassName = szAppName;
wndclass.hIconSm       = 0;
RegisterClassEx(&wndclass);
hWnd_main = CreateWindow(szAppName, szAppName, WS_POPUP, 305, 0,
450, 45, NULL, NULL, hInstance, NULL);
ShowWindow(hWnd_main, iCmdShow);
UpdateWindow(hWnd_main);
g_hInst = hInstance;
while (GetMessage(&msg, NULL, 0, 0)) {
TranslateMessage(&msg);
DispatchMessage(&msg);
}  
return msg.wParam;
}

LRESULT CALLBACK WndProc(HWND hwnd, UINT iMsg, WPARAM wParam, LPARAM lParam) {
switch (iMsg) {
case WM_CREATE:
hPicture1 = (HBITMAP)LoadImage(GetModuleHandle(NULL),
"untitled.bmp", IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE);
hPicture2 = (HBITMAP)LoadImage(GetModuleHandle(NULL),
"untitled2.bmp", IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE);
hWnd_label = CreateWindow("Static", NULL, WS_VISIBLE | WS_CHILD,
5, 5, 70, 45, hwnd, (HMENU)30, GetModuleHandle(NULL), NULL);
SendMessage(hWnd_label, WM_SETFONT,
(LPARAM)GetStockObject(OEM_FIXED_FONT), 0);
SetWindowLong(hwnd, GWL_EXSTYLE,
GetWindowLong(hwnd, GWL_EXSTYLE) | WS_EX_LAYERED);
SetLayeredWindowAttributes(hwnd, 0, 126, 2);
HMODULE hKernel;
hKernel = LoadLibrary("kernel32.dll");
lpfnGetSystemTimes = (unsigned long)GetProcAddress(hKernel, "GetSystemTimes");
SetTimer(hwnd, 1337, 4000, asdf);
break;
default:
return DefWindowProc(hwnd, iMsg, wParam, lParam);
}
return 0;
}

void __stdcall asdf(HWND hwnd, unsigned int dsfarg, unsigned int asdfg, unsigned long sdfsds) {
char asdf[32];
int numblox_ram, numblox_cpu, i, tmpcpu = GetCPUUsage();
MEMORYSTATUS memstat;
HDC hdc_main, hdc_picture;
GlobalMemoryStatus(&memstat);
numblox_ram = int(double((memstat.dwTotalPhys - memstat.dwAvailPhys)
>> 20) / double(memstat.dwTotalPhys >> 20) * 32.f);
numblox_cpu = int(double(((double)tmpcpu / 100.f) * 32.f));
sprintf(asdf, "ram %%%d\ncpu %%%d", int((numblox_ram / 32.0) * 100.0), tmpcpu);
SetWindowText(hWnd_label, asdf);
hdc_main = GetDC(hwnd);
hdc_picture = CreateCompatibleDC(hdc_main);
for (i = 0; i != 32; i++) {
SelectObject(hdc_picture, (i < numblox_ram) ? hPicture2 : hPicture1);
BitBlt(hdc_main, 435 - (i * 12), 5, 10, 15, hdc_picture, 0, 0, SRCCOPY);
}
for (i = 0; i != 32; i++) {
SelectObject(hdc_picture, (i < numblox_cpu) ? hPicture2 : hPicture1);
BitBlt(hdc_main, 435 - (i * 12), 25, 10, 15, hdc_picture, 0, 0, SRCCOPY);
}
}

int GetCPUUsage() {
int cpu;
FILETIME idletime, kerneltime, usertime;
ULARGE_INTEGER idleul, kernelul, userul, systemul, sys_diff, idle_diff;
__asm {
lea eax, [ebp - 1Ch]
push eax
lea eax, [ebp - 14h]
push eax
lea eax, [ebp - 0Ch]
push eax
call lpfnGetSystemTimes
}
//(pfGetSystemTimes)lpfnGetSystemTimes(&idletime, &kerneltime, &usertime);
idleul.LowPart = idletime.dwLowDateTime;
idleul.HighPart = idletime.dwHighDateTime;
kernelul.LowPart = kerneltime.dwLowDateTime;
kernelul.HighPart = kerneltime.dwHighDateTime;
userul.LowPart = usertime.dwLowDateTime;
userul.HighPart = usertime.dwHighDateTime;
systemul.QuadPart = kernelul.QuadPart + userul.QuadPart;
idle_diff.QuadPart = idleul.QuadPart - idleul_last.QuadPart;
sys_diff.QuadPart = systemul.QuadPart - systemul_last.QuadPart;
cpu = int((sys_diff.QuadPart - idle_diff.QuadPart) * 100 / sys_diff.QuadPart);
systemul_last.QuadPart = systemul.QuadPart;
idleul_last.QuadPart = idleul.QuadPart;
return cpu;
}

On the first call of the timer proc, it works perfect, then on the second and everything after, all the blocks turn bright green (hPicture2) where it's supposed to be a proportionate number.
Is this because I didn't clear the window before calling BitBlt again? the numblox values seem to be perfectly suitable, which leaves me to believe that BitBlt is the problem. (i'm not an expert on windows gdi, so it's really just a guess)

[Kp edit: broke up long lines.  This is your last chance.  The next time you break the table layout, I will delete the offending lines.]
<3 Zorm
Quote[01:08:05 AM] <@Zorm> haha, me get pussy? don't kid yourself quik
Scio te esse, sed quid sumne? :P

MyndFyre

Wow....  It seems to me that the ability to graph something onto a specific picture seems almost.... object-oriented.  But that's allright, I know you definitely wouldn't want to refactor your code to do so.  Especially when it might make the maintenance of your code easier.
QuoteEvery generation of humans believed it had all the answers it needed, except for a few mysteries they assumed would be solved at any moment. And they all believed their ancestors were simplistic and deluded. What are the odds that you are the first generation of humans who will understand reality?

After 3 years, it's on the horizon.  The new JinxBot, and BN#, the managed Battle.net Client library.

Quote from: chyea on January 16, 2009, 05:05 PM
You've just located global warming.

brew

Quote from: MyndFyre[vL] on November 12, 2007, 12:26 PM
Wow....  It seems to me that the ability to graph something onto a specific picture seems almost.... object-oriented.  But that's allright, I know you definitely wouldn't want to refactor your code to do so.  Especially when it might make the maintenance of your code easier.
That really didn't help.
But while I was working on a different project, I needed to use BitBlt. And guess what, I'm having the same exact problem. Well, not really. I think it's just because I'm misusing the Blt functions somehow. I'm no GDI expert.
I am trying to give my rich edit control a backround. The first thing I thought of is to do what I did in vb6, make the RE box transparent and put an image in the back window. Doesn't exactly work out though, it seems it only is displayed once then any redraws after that, it would replace where the picture should be with a null brush. this gives me undesired results, such as blurring of text, not to mention taking the image of the last drawn window in the foreground. I believe a similar (if not the same) thing is happening here. This is my code:

case WM_PAINT:
PAINTSTRUCT ps;
RECT rect;
HDC rtfChathdc, imagedc;
rtfChathdc = BeginPaint(hwnd, &ps);
GetWindowRect(hWnd_rtfChat, &rect);
imagedc = CreateCompatibleDC(rtfChathdc);
SelectObject(imagedc, hRTBPic);
StretchBlt(rtfChathdc, 0, 0, rect.right - rect.left, rect.bottom - rect.top, imagedc, 0, 0, 480, 400, SRCCOPY);
EndPaint(hwnd, &ps);
//return DefWindowProc(hwnd, iMsg, wParam, lParam);
break;
<3 Zorm
Quote[01:08:05 AM] <@Zorm> haha, me get pussy? don't kid yourself quik
Scio te esse, sed quid sumne? :P

Warrior

Quote from: MyndFyre[vL] on November 12, 2007, 12:26 PM
Wow....  It seems to me that the ability to graph something onto a specific picture seems almost.... object-oriented.  But that's allright, I know you definitely wouldn't want to refactor your code to do so.  Especially when it might make the maintenance of your code easier.

hahahahaha
Quote from: effect on March 09, 2006, 11:52 PM
Islam is a steaming pile of fucking dog shit. Everything about it is flawed, anybody who believes in it is a terrorist, if you disagree with me, then im sorry your wrong.

Quote from: Rule on May 07, 2006, 01:30 PM
Why don't you stop being American and start acting like a decent human?

brew

Hmmmm. I checked out exactly what SRCCOPY did on msdn, and it does what i thought.... only that it modifies the gdiobj to the current brush of the window. (see: http://msdn2.microsoft.com/en-us/library/ms532314.aspx)
Pretty gay if you ask me :(
I have to reload the image every single timer event. Is there a better way of doing this? *crosses his fingers*
<3 Zorm
Quote[01:08:05 AM] <@Zorm> haha, me get pussy? don't kid yourself quik
Scio te esse, sed quid sumne? :P

NicoQwertyu

I was able to use Window GDI to "blt" an image onto a rich text box, but there was a problem. By letting the RTB redraw itself, then posting an image over it, I would draw over all the text/scroll bars/borders. By drawing my image, and then letting the RTB redraw itself, it would draw over my image.

The only way to do this, that I've found, is to literally draw everything manually, the image, the text, the scroll bars, the borders, etc...



    Public Property Backimage() As Image
        Get
            Return _backimage
        End Get
        Set(ByVal value As Image)
            _backimage = value
        End Set
    End Property

    Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message)
        MyBase.WndProc(m)
        If m.Msg = WM_PAINT And _backimage IsNot Nothing Then
            Dim hdc As Integer = GetWindowDC(Me.Handle)
            Using g As Graphics = Graphics.FromHdc(hdc)
                Using myfont As New Font("Times New Roman", 10, FontStyle.Bold)
                    g.DrawImage(Me.Backimage, 0, 0, Me.Width, Me.Height)
                    g.DrawString(Me.Text, myfont, Brushes.Black, 0, 0)
                End Using
            End Using
            ReleaseDC(Me.Handle, hdc)
        End If
    End Sub


Oh, and to answer your question, I had to redraw the image every time the RTB received the WM_PAINT message.

MyndFyre

If you look up the RTF spec on Microsoft's site it explains how to encode a bitmap directly into RTF so that it can be inserted where you want to toss in your image.
QuoteEvery generation of humans believed it had all the answers it needed, except for a few mysteries they assumed would be solved at any moment. And they all believed their ancestors were simplistic and deluded. What are the odds that you are the first generation of humans who will understand reality?

After 3 years, it's on the horizon.  The new JinxBot, and BN#, the managed Battle.net Client library.

Quote from: chyea on January 16, 2009, 05:05 PM
You've just located global warming.