Click here to Skip to main content
15,910,603 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
AnswerRe: How to display the program code as output using C or C++ Pin
PravinSingh6-Oct-08 22:20
PravinSingh6-Oct-08 22:20 
QuestionVisual Studio Express Edition 2008 for Commercial Application Pin
Karmendra Suthar3-Oct-08 22:15
Karmendra Suthar3-Oct-08 22:15 
AnswerRe: Visual Studio Express Edition 2008 for Commercial Application Pin
CPallini4-Oct-08 0:47
mveCPallini4-Oct-08 0:47 
QuestionGradientFill in Moving Dialog.. Pin
gothic_coder3-Oct-08 21:27
gothic_coder3-Oct-08 21:27 
AnswerRe: GradientFill in Moving Dialog.. Pin
enhzflep3-Oct-08 21:58
enhzflep3-Oct-08 21:58 
GeneralRe: GradientFill in Moving Dialog.. Pin
gothic_coder3-Oct-08 22:53
gothic_coder3-Oct-08 22:53 
GeneralRe: GradientFill in Moving Dialog.. Pin
gothic_coder3-Oct-08 23:30
gothic_coder3-Oct-08 23:30 
GeneralRe: GradientFill in Moving Dialog.. Pin
enhzflep4-Oct-08 1:05
enhzflep4-Oct-08 1:05 
Cool magool, at least I'm on the right track.
Been eating dinner, sorry to leave you waiting.

It seems that the thing that strikes me is that I forgot Blush | :O the fact that the function
expects the rect to be logical units - not screen units.

Say if I have a 10x10 rect located at 50,50 on the screen, the function wants

RECT gradRect = {0,0, 10,10} not RECT gradRect = {50, 50, 60, 60} like you would expect.

Sorry for the confusion there.

As for the bool isVerticle param, you can run the gradient from leftToRight or topToBottom.
isVerticle = true ---> topToBottom gradient
isVerticle = false ---> leftToRight gradient


Here's a 45 second example. You need to link in gdi32, user32 and kernel32.
No points for guessing which IDE I've used Shucks | :-\
#include <windows.h>

/*  Declare Windows procedure  */
LRESULT CALLBACK WindowProcedure (HWND, UINT, WPARAM, LPARAM);

/*  Make the class name into a global variable  */
char szClassName[ ] = "CodeBlocksWindowsApp";

int WINAPI WinMain (HINSTANCE hThisInstance,
                     HINSTANCE hPrevInstance,
                     LPSTR lpszArgument,
                     int nCmdShow)
{
    HWND hwnd;               /* This is the handle for our window */
    MSG messages;            /* Here messages to the application are saved */
    WNDCLASSEX wincl;        /* Data structure for the windowclass */

    /* The Window structure */
    wincl.hInstance = hThisInstance;
    wincl.lpszClassName = szClassName;
    wincl.lpfnWndProc = WindowProcedure;      /* This function is called by windows */
    wincl.style = CS_DBLCLKS;                 /* Catch double-clicks */
    wincl.cbSize = sizeof (WNDCLASSEX);

    /* Use default icon and mouse-pointer */
    wincl.hIcon = LoadIcon (NULL, IDI_APPLICATION);
    wincl.hIconSm = LoadIcon (NULL, IDI_APPLICATION);
    wincl.hCursor = LoadCursor (NULL, IDC_ARROW);
    wincl.lpszMenuName = NULL;                 /* No menu */
    wincl.cbClsExtra = 0;                      /* No extra bytes after the window class */
    wincl.cbWndExtra = 0;                      /* structure or the window instance */
    /* Use Windows's default colour as the background of the window */
    wincl.hbrBackground = (HBRUSH) COLOR_BACKGROUND;

    /* Register the window class, and if it fails quit the program */
    if (!RegisterClassEx (&wincl))
        return 0;

    /* The class is registered, let's create the program*/
    hwnd = CreateWindowEx (
           0,                   /* Extended possibilites for variation */
           szClassName,         /* Classname */
           "Code::Blocks Template Windows App",       /* Title Text */
           WS_OVERLAPPEDWINDOW, /* default window */
           CW_USEDEFAULT,       /* Windows decides the position */
           CW_USEDEFAULT,       /* where the window ends up on the screen */
           544,                 /* The programs width */
           375,                 /* and height in pixels */
           HWND_DESKTOP,        /* The window is a child-window to desktop */
           NULL,                /* No menu */
           hThisInstance,       /* Program Instance handler */
           NULL                 /* No Window Creation data */
           );

    /* Make the window visible on the screen */
    ShowWindow (hwnd, nCmdShow);

    /* Run the message loop. It will run until GetMessage() returns 0 */
    while (GetMessage (&messages, NULL, 0, 0))
    {
        /* Translate virtual-key messages into character messages */
        TranslateMessage(&messages);
        /* Send message to WindowProcedure */
        DispatchMessage(&messages);
    }

    /* The program return-value is 0 - The value that PostQuitMessage() gave */
    return messages.wParam;
}



// frees you from using the GDI function, which requires a region
void GradientFillRect(HDC hdc, LPRECT rcGradient, COLORREF start, COLORREF end, BOOL isVertical)
{
	BYTE startRed = GetRValue(start);
	BYTE startGreen = GetGValue(start);
	BYTE startBlue = GetBValue(start);

	BYTE endRed = GetRValue(end);
	BYTE endGreen = GetGValue(end);
	BYTE endBlue = GetBValue(end);

	HBRUSH endColor = CreateSolidBrush(end);
	FillRect(hdc, rcGradient, endColor);
	DeleteObject(endColor);

	//Gradient line width/height
	int dy = 1;

	int length = (isVertical ? rcGradient->bottom - rcGradient->top : rcGradient->right - rcGradient->left) - dy;

	for (int dn = 0; dn >= length; dn += dy)
	{
		BYTE currentRed = (BYTE)MulDiv(endRed-startRed, dn, length) + startRed;
		BYTE currentGreen = (BYTE)MulDiv(endGreen-startGreen, dn, length) + startGreen;
		BYTE currentBlue = (BYTE)MulDiv(endBlue-startBlue, dn, length) + startBlue;

		RECT currentRect = {0};
		if (isVertical)
		{
			currentRect.left = rcGradient->left;
			currentRect.top = rcGradient->top + dn;
			currentRect.right = currentRect.left + rcGradient->right - rcGradient->left;
			currentRect.bottom = currentRect.top + dy;
		}
		else
		{
			currentRect.left = rcGradient->left + dn;
			currentRect.top = rcGradient->top;
			currentRect.right = currentRect.left + dy;
			currentRect.bottom = currentRect.top + rcGradient->bottom - rcGradient->top;
		}

		HBRUSH currentColor = CreateSolidBrush(RGB(currentRed, currentGreen, currentBlue));
		FillRect(hdc, ¤tRect, currentColor);
		DeleteObject(currentColor);
	}
}


/*  This function is called by the Windows function DispatchMessage()  */
LRESULT CALLBACK WindowProcedure (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    RECT myRect;
    COLORREF startCol = RGB(71,71,71), endCol = RGB(200,200,200);

    switch (message)                  /* handle the messages */
    {
        case WM_DESTROY:
            PostQuitMessage (0);       /* send a WM_QUIT to the message queue */
            break;
        case WM_ERASEBKGND:
            GetClientRect(hwnd, &myRect);
            GradientFillRect(HDC (wParam), &myRect, startCol, endCol, false);
            return 1;                           // indicate that WM_ERASEBKGND was handled

        default:                      /* for messages that we don't deal with */
            return DefWindowProc (hwnd, message, wParam, lParam);
    }
    return 0;
}

GeneralRe: GradientFill in Moving Dialog.. Pin
gothic_coder4-Oct-08 1:21
gothic_coder4-Oct-08 1:21 
GeneralRe: GradientFill in Moving Dialog.. Pin
enhzflep4-Oct-08 1:57
enhzflep4-Oct-08 1:57 
GeneralRe: GradientFill in Moving Dialog.. Pin
gothic_coder4-Oct-08 2:50
gothic_coder4-Oct-08 2:50 
GeneralRe: GradientFill in Moving Dialog.. Pin
gothic_coder4-Oct-08 3:01
gothic_coder4-Oct-08 3:01 
GeneralRe: GradientFill in Moving Dialog.. Pin
enhzflep4-Oct-08 3:48
enhzflep4-Oct-08 3:48 
GeneralRe: GradientFill in Moving Dialog.. Pin
gothic_coder4-Oct-08 4:39
gothic_coder4-Oct-08 4:39 
GeneralRe: GradientFill in Moving Dialog.. Pin
gothic_coder6-Oct-08 0:10
gothic_coder6-Oct-08 0:10 
QuestionNew Stand-alone application Pin
Karmendra Suthar3-Oct-08 21:05
Karmendra Suthar3-Oct-08 21:05 
AnswerRe: New Stand-alone application Pin
Saurabh.Garg3-Oct-08 21:11
Saurabh.Garg3-Oct-08 21:11 
GeneralRe: New Stand-alone application Pin
Karmendra Suthar3-Oct-08 21:59
Karmendra Suthar3-Oct-08 21:59 
GeneralRe: New Stand-alone application Pin
Saurabh.Garg3-Oct-08 22:02
Saurabh.Garg3-Oct-08 22:02 
GeneralRe: New Stand-alone application Pin
CPallini4-Oct-08 0:45
mveCPallini4-Oct-08 0:45 
QuestionHow to test ActiveX control in VC++ 2008? Pin
followait3-Oct-08 19:15
followait3-Oct-08 19:15 
AnswerRe: How to test ActiveX control in VC++ 2008? Pin
Jaime Olivares3-Oct-08 19:57
Jaime Olivares3-Oct-08 19:57 
QuestionCString Array To LPCSTR Pin
MsmVc3-Oct-08 18:54
MsmVc3-Oct-08 18:54 
AnswerRe: CString Array To LPCSTR Pin
Jaime Olivares3-Oct-08 19:17
Jaime Olivares3-Oct-08 19:17 
GeneralRe: CString Array To LPCSTR Pin
MsmVc3-Oct-08 19:24
MsmVc3-Oct-08 19:24 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.