Hi all,
after hours of searching in the SDK in vain I give up.

The GDI function Rectangle() draws a rectangle but doesn't let me specify the color.

How can I set the RGB color that will be used? SetROP2() doesn't really seem the case.

Thanks. :P
Mav
Posted on 2003-04-02 07:38:01 by Maverick
FillRect will probably be of some help : http://msdn.microsoft.com/library/default.asp?url=/library/en-us/gdi/fillshap_6i0k.asp

I think you can code a FilledRectangle() function to draw a rectangle and fill it automatically with a function parameter...
There is maybe a function that does already that, but it does not seem so, but I am not a really GDI expert...

If you do not need different colors borders, FillRect may even be enough : you will just have to fill a RECT structure...

Uhm, I now realize that I posted too quickly and maybe misunderstood the question : you want to draw a rectangle with an edge, right ? : it think you have to use FrameRect() but I never used it :

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/gdi/fillshap_6i0k.asp
Posted on 2003-04-02 07:57:51 by JCP
Hi Maverick

Select desired pen and brush into the DC first.

KetilO
Posted on 2003-04-02 08:47:55 by KetilO

Hi Maverick

Select desired pen and brush into the DC first.

KetilO
How do I do it? :P
Posted on 2003-04-02 10:36:11 by Maverick

FillRect will probably be of some help : http://msdn.microsoft.com/library/default.asp?url=/library/en-us/gdi/fillshap_6i0k.asp

I think you can code a FilledRectangle() function to draw a rectangle and fill it automatically with a function parameter...
There is maybe a function that does already that, but it does not seem so, but I am not a really GDI expert...

If you do not need different colors borders, FillRect may even be enough : you will just have to fill a RECT structure...

Uhm, I now realize that I posted too quickly and maybe misunderstood the question : you want to draw a rectangle with an edge, right ? : it think you have to use FrameRect() but I never used it :

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/gdi/fillshap_6i0k.asp

Thanks, I'll look at the links. No, I just want to draw a simple rectangle, but as you can see from Rectangle() proto, it doesn't let you specify the color. That's the problem. SetPixel instead lets you do it, for example.
Posted on 2003-04-02 10:37:36 by Maverick
Stupid MSDN website that does not change the active url when clicking on functions :

here is the prototype for FrameRect :
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/gdi/fillshap_4jec.asp



int FrameRect(
HDC hDC, // handle to DC
CONST RECT *lprc, // rectangle
HBRUSH hbr // handle to brush
);


and to select the color (HBRUSH)GetStockObject (BLACK_BRUSH); or whatever...

you can also use CreateSolidBrush and the RGB macro to specify the color, but do not forget to free the object :

example:


hBrush = CreateSolidBrush(RGB(0,0,0));
FrameRect(hDC, &rect, hBrush);
DeleteObject(hBrush);


Hope this helps...

EDIT : Another interesting link : http://msdn.microsoft.com/library/default.asp?url=/library/en-us/gdi/fillshap_7m7n.asp
Posted on 2003-04-02 10:53:50 by JCP
Here you go Maverick.. this should (hopefully) clear things up for you...

I used Hutch's prostart tool.. I added a few menu options... look at the WM_INIT/DESTROY/PAINT

Also, its worth checking out the GetStockObject as well as the GetSysColor API's (i use the latter for the "see through" rectangle).

Note the how the WM_PAINT is set up to work. This is the safest way to ensure no memory leaks on a DC (i find). Its resembles a message loop (kinda) in structure, some background setup stuff, and then enters the GDI paint proc for painting. The important thing is i get and save the old handles first so they can be restored on exiting. This means i can freely do anything i want in the proc...

Hope it helps..
:alright:
NaN
Posted on 2003-04-02 21:41:02 by NaN

Thx NaN :)

One last question.. if you all don't mind.

How do I get a pointer to the bitmap, so I can manipulate the pixels in a quick way (sort of DirectDraw's Lock() )?
Posted on 2003-04-03 00:22:47 by Maverick
see MSDN for GetDIBits
use it if you already have a bitmap. It just copies the bitmap data onto a buffer that you specify. After that use CreateDIBSection or CreateDIBitmap (?? )

if you want to create a bitmap, where you can draw programatically, then use
CreateDIBSection

I'll post code later
Posted on 2003-04-03 03:05:19 by Ultrano


;=====[[ CREATE new CDA >>===\

cda8bitTemporary struct
bih BITMAPINFOHEADER <>
Colors dd 256 dup (?)
cda8bitTemporary ends

Create8bitCDA proc uses edx esi edi Width2,Height2
local baka2,hDC,hBmp,Pointa
local baka:cda8bitTemporary
; cda1 is a pointer to a cda structure
;----[ zero structures ]------\
mov hDC,0
mov hBmp,0
mov Pointa,0
xor al,al
lea edi,baka
mov ecx,sizeof cda8bitTemporary
rep stosb
;-----------------------------/
invoke CreateCompatibleDC,0
or eax,eax
jz _ret
mov hDC,eax

lea edx,baka
mov [edx].BITMAPINFOHEADER.biSize,sizeof BITMAPINFOHEADER
mov eax,Width2
mov [edx].BITMAPINFOHEADER.biWidth, ea
mov eax,Height2
mov [edx].BITMAPINFOHEADER.biHeight,eax
mov [edx].BITMAPINFOHEADER.biPlanes,1
mov [edx].BITMAPINFOHEADER.biBitCount,8
mov [edx].BITMAPINFOHEADER.biCompression,BI_RGB ; Pixel storage format
mov [edx].BITMAPINFOHEADER.biClrUsed,256
mov [edx].BITMAPINFOHEADER.biClrImportant,256
invoke CreateCDA_ColorTable,addr baka.Colors ; this function of mine fills in the colors
invoke CreateDIBSection,hDC,addr baka,DIB_RGB_COLORS,addr baka2,0,0
.if !eax
invoke DeleteDC,hDC
mov hDC,0
jmp _ret
.endif

mov hBmp,eax
mov eax,baka2
mov Pointa,eax
invoke SelectObject,hDC,hBmp

_ret:
mov eax,hDC
mov ebx,hBmp
mov ecx,Pointa
ret
Create8bitCDA endp
;=======/




"Pointa" gives you the start of the bitmap. Note that in DIBs the first pixel is the bottom left, not the top left! And you must have the width DWORD-aligned:


mov eax,my_wid
.if eax & 3
shr eax,2
inc eax
shl eax,2
.endif
mov my_wid,eax


you can, of course, use 24-bit buffer. I noticed something bad with 8-bit DIBs. When you try to bitblt a 24-bit into the 8-bit, you have to wait 250 to 1000 ms!! :
CyclesTaken= Width * Height * (256 * 8 + 6.5)
6.5 cycles are usually taken to get a new byte, the 256 *8 are taken for the algorithm that searches the 8-bit color that best describes the 24-bit color.
So, only extreme need will make you do: BitBlt(My_8_bit_DC,0,0,wid,hei,My_24_bit_DC,0,0,SRCCOPY)
BitBlt -ing from 8-bit to 24-bit takes a lot cpu, too.


To modify an already existing bitmap hBit1 :


Approach 1:
1) Pointa = GetDIBits(hBit1)
2) hBit2 = CreateDIBSection(24-bit, Pointa, wid,hei ...)
3) MaverickModifyBits(Pointa)
4) hDC1 = CreateCompatibleDC,0
5) hDC2 = CreateCompatibleDC,0
6) SelectObject(hDC1,hBit1)
7) SelectObject(hDC2,hBit2)
8) invoke BitBlt,hDC1,0,0,wid,hei,hDC2,0,0,SRCCOPY
9) invoke DeleteDC,hDC1
10) invoke DeleteDC,hDC2
11) invoke DeleteObject,hBit2




There is a SetDIBits function, but I do not think it will be as fast as this. Remember, "more code means faster" ;)
btw, I have never used GetDIBits, but everything should work fine
Posted on 2003-04-03 04:11:01 by Ultrano

Thanks for all the help Ultrano, but I decided to go the direct way on the screen. :P
Posted on 2003-04-04 03:42:35 by Maverick