i am compiling a simple Win32 api program but the problem is that it compiles fine but no window appears. I checked the process in taskmanager and the program is there but doesn't show up a window. I am using CodeBlocks IDE 10.05 with GNU GCC compiler under windows 7 32-bit.
I know there is a similar question here on stackoverflow but the solution given there was a'==' instead of '='. here is the code:
#include <windows.h>
#include <tchar.h>
/* Global Vars */
HINSTANCE hInst;
HWND wndHandle;
int winWidth = 640;
int winHeight = 480;
//func prototypes
bool InitWindow(HINSTANCE hInst, int width, int height);
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
/********************************************************
* WINMAIN FUNCTION
*********************************************************/
int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPTSTR lpCmdLine, int nCmdShow)
{
hInst = hInstance;
//app window
if (!InitWindow(hInst, winWidth, winHeight))
{
return false;
}
// MESSAGE LOOP
MSG msg = {0};
while (WM_QUIT != msg.message)
{
//window messages
while(PeekMessage(&msg, NULL,0,0, PM_REMOVE) == TRUE)
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
//additional logic
}
return (int)msg.wParam;
}
/*******************************************************************
* InitWindow
* Inits and creates and main app window
* Inputs - application instance - HINSTANCE
Window width - int
Window height - int
* Outputs - true if successful, false if failed - bool
*******************************************************************/
bool InitWindow(HINSTANCE hInstance, int width, int height)
{
// Register class
WNDCLASSEX wcex;
wcex.cbSize = sizeof(WNDCLASSEX);
wcex.style = CS_HREDRAW | CS_VREDRAW;
wcex.lpfnWndProc = WndProc;
wcex.cbClsExtra = 0;
wcex.cbWndExtra = 0;
wcex.hInstance = hInstance;
wcex.hIcon = 0;
wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
wcex.hbrBackground = (HBRUSH) + (COLOR_WINDOW+1);
wcex.lpszMenuName = NULL;
wcex.lpszClassName = TEXT("DirectX 10 Example");
wcex.hIconSm = 0;
if(!RegisterClassEx(&wcex))
{
return false;
}
// resize window
RECT rect = { 0, 0, width, height };
AdjustWindowRect(&rect, WS_OVERLAPPEDWINDOW, false);
// create the window from the class above
wndHandle = CreateWindow( TEXT("DX 10 Example"),
TEXT("DX10 WINDOW"),
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT,
CW_USEDEFAULT,
rect.right - rect.left,
rect.bottom - rect.top,
0,
0,
hInstance,
0);
if(wndHandle == 0)
{
return false;
}
ShowWindow(wndHandle, SW_SHOW);
UpdateWindow(wndHandle);
return true;
}
/*******************************************************************
* WndProc
* The main window procedure for the application
* Inputs - application window handle - HWND
message sent to the window - UINT
wParam of the message being sent - WPARAM
lParam of the message being sent - LPARAM
* Outputs - LRESULT
*******************************************************************/
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
switch (message)
{
// Allow the user to press the escape key to end the application
case WM_KEYDOWN:
switch(wParam)
{
// Check if the user hit the escape key
case VK_ESCAPE:
PostQuitMessage(0);
break;
}
break;
// The user hit the close button, close the application
case WM_DESTROY:
PostQuitMessage(0);
break;
}
return DefWindowProc(hWnd, message, wParam, lParam);
}
you have
wcex.lpszClassName = TEXT("DirectX 10 Example");
and
wndHandle = CreateWindow( TEXT("DX 10 Example"),
TEXT("DX10 WINDOW"),
Your have to use the same class name for "CreateWindow" as you used for "RegisterClass".
In your source code those two differ:
wcex.lpszClassName = TEXT("DirectX 10 Example");
...
wndHandle = CreateWindow(TEXT("DX 10 Example"), ...
Related
In my program I am trying to create a child window (via CreateWindow("STATIC", ...)) to contain some other controls such as edit boxes and buttons. However, I want the background for this static control to be a gradient.
My goal is to have something that looks like this as a child window:
My efforts so far have yielded the window being created with controls visible but as soon as it is redrawn with WM_ERASEBKGND, the controls are hidden behind the drawn gradient.
I can find plenty of examples on drawing a gradient (or other such graphic backgrounds) and also, independently, creating a window with controls. But I have yet to find any resource of having both at the same time.
Here is my example code:
#include <windows.h>
#pragma comment( lib, "Msimg32" ) // load that dll for GradientFill
// Global macros
inline COLOR16 ToColor16(BYTE byte) { return byte << 8; }
inline COLOR16 RVal16(COLORREF color) { return ToColor16(GetRValue(color)); }
inline COLOR16 GVal16(COLORREF color) { return ToColor16(GetGValue(color)); }
inline COLOR16 BVal16(COLORREF color) { return ToColor16(GetBValue(color)); }
// Global variables
HINSTANCE hInst;
// Forward declarations
LRESULT CALLBACK WndProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
// Entry point
int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE, PWSTR pCmdLine, int nCmdShow)
{
// Register the window class.
const TCHAR CLASS_NAME[] = "mcvewinapi";
WNDCLASS wc = { };
wc.lpfnWndProc = WndProc;
wc.hInstance = hInstance;
wc.lpszClassName = CLASS_NAME;
wc.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
wc.hCursor = LoadCursor(nullptr, IDC_ARROW);
RegisterClass(&wc);
// Create the window.
HWND hwnd = CreateWindowEx(
0, // Optional window styles.
CLASS_NAME, // Window class
"MCVE WINAPI", // Window text
WS_OVERLAPPEDWINDOW, // Window style
// Size and position
300, //horizontal position
50, //vertical position
700, //width
500, //height
NULL, // Parent window
NULL, // Menu
hInstance, // Instance handle
NULL // Additional application data
);
hInst = hInstance;
if (hwnd == NULL) {
return 0;
}
ShowWindow(hwnd, nCmdShow);
// Run the message loop.
MSG msg = { };
while (GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return 0;
}
// vill geven area with vertical gradient
void VerticalGradient(HDC hDC, const RECT &RectToFill, COLORREF rgbTop, COLORREF rgbBottom) {
GRADIENT_RECT gradRect;
TRIVERTEX TriVert[2];
// references for the verticies
gradRect.UpperLeft = 0; // point to TriVert[0]
gradRect.LowerRight = 1; // point to TriVert[1]
//setup top of gradient attributes
TriVert[0].x = RectToFill.left - 1;
TriVert[0].y = RectToFill.top - 1;
TriVert[0].Red = RVal16(rgbTop);
TriVert[0].Green = GVal16(rgbTop);
TriVert[0].Blue = BVal16(rgbTop);
TriVert[0].Alpha = 0x0000;
//setup bottom of gradient attributes
TriVert[1].x = RectToFill.right;
TriVert[1].y = RectToFill.bottom;
TriVert[1].Red = RVal16(rgbBottom);
TriVert[1].Green = GVal16(rgbBottom);
TriVert[1].Blue = BVal16(rgbBottom);
TriVert[1].Alpha = 0x0000;
// draw the shaded rectangle
GradientFill(hDC, TriVert, 2, &gradRect, 1, GRADIENT_FILL_RECT_V);
}
// discover area to fill with gradient
void vFill(HWND ctrlhwnd) {
HDC gBoxDC;
HWND gBoxH;
RECT gBoxR;
POINT xWhere;
// get rectangle from control
gBoxH = ctrlhwnd;
GetWindowRect(gBoxH, &gBoxR);
// get DC for control
gBoxDC = GetDC(gBoxH);
// load up RECT from POINT
xWhere = { gBoxR.left, gBoxR.top };
ScreenToClient(gBoxH, &xWhere);
gBoxR.left = xWhere.x + 2;
gBoxR.top = xWhere.y + 2;
// load up RECT from POINT
xWhere = { gBoxR.right, gBoxR.bottom };
ScreenToClient(gBoxH, &xWhere);
gBoxR.right = xWhere.x - 1;
gBoxR.bottom = xWhere.y - 1;
//paint area
VerticalGradient(gBoxDC, gBoxR, RGB(250, 191, 145), RGB(191, 191, 191));
ReleaseDC(gBoxH, gBoxDC);
}
// Processes messages for the main window.
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) {
switch (message) {
case WM_CREATE:
struct wAnchor {
int udPos;
int lrPos;
int width;
int height;
};
wAnchor Section1[4], TextLine;
unsigned int LineHeight, k;
HWND FillBox;
LineHeight = 24;
Section1[1].lrPos = 5;
Section1[1].udPos = 20;
Section1[1].width = 325;
Section1[1].height = 360;
TextLine.lrPos = Section1[1].lrPos + 5;
TextLine.udPos = Section1[1].udPos + 5;
TextLine.width = 0;
TextLine.height = LineHeight;
k = 1;
FillBox = CreateWindow("STATIC",
"",
WS_VISIBLE | WS_CHILD | WS_BORDER | WS_EX_CLIENTEDGE,
Section1[1].lrPos,
Section1[1].udPos,
Section1[1].width,
Section1[1].height,
hWnd, // child of parent
(HMENU)8200,
hInst,
NULL);
CreateWindow("STATIC",
"Section title",
WS_VISIBLE | WS_CHILD | WS_BORDER | SS_CENTER | WS_EX_CLIENTEDGE,
TextLine.lrPos,
TextLine.udPos + (TextLine.height * k++),
TextLine.width + 315,
TextLine.height,
FillBox, // child of FillBox
(HMENU)8201,
hInst,
NULL);
CreateWindow("STATIC",
"Entry:",
WS_VISIBLE | WS_CHILD | WS_BORDER | WS_EX_CLIENTEDGE,
TextLine.lrPos,
TextLine.udPos + (TextLine.height * k),
TextLine.width + 75,
TextLine.height,
FillBox, // child of FillBox
(HMENU)8202,
hInst,
NULL);
CreateWindow("EDIT", // edit bos
"109",
WS_VISIBLE | WS_CHILD | WS_BORDER | WS_TABSTOP | ES_MULTILINE,
TextLine.lrPos + 75,
TextLine.udPos + (TextLine.height * k),
TextLine.width + 35,
TextLine.height,
FillBox, // child of FillBox
(HMENU)8203,
hInst,
NULL);
CreateWindow(
"BUTTON", // Predefined class; Unicode assumed
"test", // Button text
WS_TABSTOP | WS_VISIBLE | WS_CHILD | BS_DEFPUSHBUTTON, // Styles
50, // x position
150, // y position
50, // Button width
30, // Button height
FillBox, // child of FillBox
(HMENU)8204, // No menu
hInst,
NULL); // Pointer not needed
UpdateWindow(FillBox);
break;
case WM_PAINT: {
PAINTSTRUCT ps;
HDC hdc = BeginPaint(hWnd, &ps);
EndPaint(hWnd, &ps);
}
break;
case WM_ERASEBKGND:
vFill(GetDlgItem(hWnd, 8200)); //fill background with gradient
return 1L;
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
return 0;
}
Initially, no gradient is drawn until I resize the window, then the gradient covers the controls. Of course, I would like the gradient to be redrawn as needed, but the controls to remain visible.
How do I accomplish this?
You can add the WS_CLIPCHILDREN style to the parent window:
FillBox = CreateWindow(TEXT("STATIC"),
TEXT(""),
WS_VISIBLE | WS_CHILD | WS_BORDER | WS_EX_CLIENTEDGE | WS_CLIPCHILDREN,
Section1[1].lrPos,
Section1[1].udPos,
Section1[1].width,
Section1[1].height,
hWnd, // child of parent
(HMENU)8200,
hInst,
NULL);
This will not include its child windows when drawing the parent window, which works for me:
If you're going to create a window to act as a container for some controls, the obvious choice is usually to create a dialog. It already (automatically) handles most of what you're doing right now, and a few things you might want to (like using the tab to move between controls) but haven't yet.
So, for example, starting from a generic Windows app, I changed the default About box to host roughly the controls you wanted, then popped it up as a modeless dialog when the application initialized.
// frame.cpp : Defines the entry point for the application.
//
#include "framework.h"
#include "frame.h"
#pragma comment( lib, "Msimg32" ) // load that dll for GradientFill
#define MAX_LOADSTRING 100
// Global Variables:
HINSTANCE hInst; // current instance
WCHAR szTitle[MAX_LOADSTRING]; // The title bar text
WCHAR szWindowClass[MAX_LOADSTRING]; // the main window class name
// Forward declarations of functions included in this code module:
ATOM MyRegisterClass(HINSTANCE hInstance);
BOOL InitInstance(HINSTANCE, int);
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
INT_PTR CALLBACK About(HWND, UINT, WPARAM, LPARAM);
int APIENTRY wWinMain(_In_ HINSTANCE hInstance,
_In_opt_ HINSTANCE hPrevInstance,
_In_ LPWSTR lpCmdLine,
_In_ int nCmdShow)
{
UNREFERENCED_PARAMETER(hPrevInstance);
UNREFERENCED_PARAMETER(lpCmdLine);
// TODO: Place code here.
// Initialize global strings
LoadStringW(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
LoadStringW(hInstance, IDC_FRAME, szWindowClass, MAX_LOADSTRING);
MyRegisterClass(hInstance);
// Perform application initialization:
if (!InitInstance (hInstance, nCmdShow))
{
return FALSE;
}
HACCEL hAccelTable = LoadAccelerators(hInstance, MAKEINTRESOURCE(IDC_FRAME));
MSG msg;
// Main message loop:
while (GetMessage(&msg, nullptr, 0, 0))
{
if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
return (int) msg.wParam;
}
//
// FUNCTION: MyRegisterClass()
//
// PURPOSE: Registers the window class.
//
ATOM MyRegisterClass(HINSTANCE hInstance)
{
WNDCLASSEXW wcex;
wcex.cbSize = sizeof(WNDCLASSEX);
wcex.style = CS_HREDRAW | CS_VREDRAW;
wcex.lpfnWndProc = WndProc;
wcex.cbClsExtra = 0;
wcex.cbWndExtra = 0;
wcex.hInstance = hInstance;
wcex.hIcon = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_FRAME));
wcex.hCursor = LoadCursor(nullptr, IDC_ARROW);
wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
wcex.lpszMenuName = MAKEINTRESOURCEW(IDC_FRAME);
wcex.lpszClassName = szWindowClass;
wcex.hIconSm = LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_SMALL));
return RegisterClassExW(&wcex);
}
//
// FUNCTION: InitInstance(HINSTANCE, int)
//
// PURPOSE: Saves instance handle and creates main window
//
// COMMENTS:
//
// In this function, we save the instance handle in a global variable and
// create and display the main program window.
//
BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
hInst = hInstance; // Store instance handle in our global variable
HWND hWnd = CreateWindowW(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, nullptr, nullptr, hInstance, nullptr);
if (!hWnd)
{
return FALSE;
}
ShowWindow(hWnd, nCmdShow);
UpdateWindow(hWnd);
return TRUE;
}
//
// FUNCTION: WndProc(HWND, UINT, WPARAM, LPARAM)
//
// PURPOSE: Processes messages for the main window.
//
// WM_COMMAND - process the application menu
// WM_PAINT - Paint the main window
// WM_DESTROY - post a quit message and return
//
//
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
static HWND dialog;
switch (message)
{
case WM_CREATE:
dialog = CreateDialog(hInst, MAKEINTRESOURCE(IDD_ABOUTBOX), hWnd, About);
ShowWindow(dialog, SW_SHOW);
break;
case WM_COMMAND:
{
int wmId = LOWORD(wParam);
// Parse the menu selections:
switch (wmId)
{
case IDM_EXIT:
DestroyWindow(hWnd);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
}
break;
case WM_PAINT:
{
PAINTSTRUCT ps;
HDC hdc = BeginPaint(hWnd, &ps);
// TODO: Add any drawing code that uses hdc here...
EndPaint(hWnd, &ps);
}
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
return 0;
}
inline COLOR16 ToColor16(BYTE byte) { return byte << 8; }
inline COLOR16 RVal16(COLORREF color) { return ToColor16(GetRValue(color)); }
inline COLOR16 GVal16(COLORREF color) { return ToColor16(GetGValue(color)); }
inline COLOR16 BVal16(COLORREF color) { return ToColor16(GetBValue(color)); }
// Message handler for about box.
INT_PTR CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
UNREFERENCED_PARAMETER(lParam);
switch (message)
{
case WM_INITDIALOG:
return (INT_PTR)TRUE;
case WM_ERASEBKGND: {
COLORREF rgbTop(RGB(250, 191, 145));
COLORREF rgbBottom(RGB(191, 191, 191));
RECT RectToFill;
GetClientRect(hDlg, &RectToFill);
GRADIENT_RECT gradRect;
TRIVERTEX TriVert[2];
// references for the verticies
gradRect.UpperLeft = 0; // point to TriVert[0]
gradRect.LowerRight = 1; // point to TriVert[1]
//setup top of gradient attributes
TriVert[0].x = RectToFill.left - 1;
TriVert[0].y = RectToFill.top - 1;
TriVert[0].Red = RVal16(rgbTop);
TriVert[0].Green = GVal16(rgbTop);
TriVert[0].Blue = BVal16(rgbTop);
TriVert[0].Alpha = 0x0000;
//setup bottom of gradient attributes
TriVert[1].x = RectToFill.right;
TriVert[1].y = RectToFill.bottom;
TriVert[1].Red = RVal16(rgbBottom);
TriVert[1].Green = GVal16(rgbBottom);
TriVert[1].Blue = BVal16(rgbBottom);
TriVert[1].Alpha = 0x0000;
HDC dc = GetDC(hDlg);
GradientFill(dc, TriVert, 2, &gradRect, 1, GRADIENT_FILL_RECT_V);
ReleaseDC(hDlg, dc);
return TRUE;
}
case WM_COMMAND:
if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL)
{
EndDialog(hDlg, LOWORD(wParam));
return (INT_PTR)TRUE;
}
break;
}
return (INT_PTR)FALSE;
}
I didn't really complete the transformation from it being a modal dialog to modeless (e.g., a modeless dialog usually won't have an OK button, or code to handle its being pressed, but it's still enough to get the idea of how it can look:
I've made two simple classes to use for displaying UIs with Awesomium. However creating the Awesomium WebView and setting the parent window to my Win32 window causes the program to hang and the page is never displayed. The classes are simple and creating a window is simple so there isn't much I can think of that could be going wrong. Perhaps there is something else required than what I've done to display a WebView?
To clarify: Creating the Win32 window without creating the WebView works fine, the window functions including the drag code etc... The hang only happens when you call set_parent_window.
UI.h
#pragma once
#include <Windows.h>
#include <windowsx.h>
#include <Awesomium/WebCore.h>
#include <Awesomium/STLHelpers.h>
using namespace Awesomium;
LRESULT CALLBACK LoginUICallback(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam);
class UI
{
public:
//Window Variables
HINSTANCE instance;
HWND window;
BOOL drag_window = false;
SHORT mouse_x, mouse_y, mouse_x_prev, mouse_y_prev;
//Awesomium Variables
WebCore* webcore = 0;
WebView* webview;
static HWND InitWindow(INT width, INT height, WNDPROC callback)
{
HWND hwnd;
WNDCLASSEX wc;
wc.cbSize = sizeof(WNDCLASSEX);
wc.style = 0;
wc.lpfnWndProc = callback;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = GetModuleHandle(0);
wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
wc.lpszMenuName = NULL;
wc.lpszClassName = "MyUI";
wc.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
if (!RegisterClassEx(&wc))
{
char msg[100];
sprintf(msg, "System Error: %i", GetLastError());
MessageBox(NULL, msg, "ERROR", MB_OK);
return NULL;
}
hwnd = CreateWindow("MyUI",
"",
WS_POPUP,
CW_USEDEFAULT,
CW_USEDEFAULT,
width,
height,
NULL,
NULL,
GetModuleHandle(0),
NULL);
if (!hwnd)
{
char msg[100];
sprintf(msg, "System Error: %i", GetLastError());
MessageBox(NULL, msg, "ERROR", MB_OK);
return NULL;
}
ShowWindow(hwnd, SW_SHOWNORMAL);
UpdateWindow(hwnd);
SetTimer(hwnd, 0, 15, NULL);
return hwnd;
}
};
class LoginUI : public UI
{
public:
INT width = 600;
INT height = 600;
INT RunUI()
{
this->window = UI::InitWindow(this->width, this->height, ::LoginUICallback);
if (!this->window)
return 0;
WebConfig config;
this->webcore = WebCore::Initialize(config);
this->webview = this->webcore->instance()->CreateWebView(this->width, this->height, 0, kWebViewType_Window);
this->webview->set_parent_window(this->window);
this->webview->LoadURL(WebURL(WSLit("http://www.google.com")));
MSG msg;
while(GetMessage(&msg, this->window, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
WebCore::Shutdown();
return 1;
}
}login_ui;
LRESULT CALLBACK LoginUICallback(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
switch (message)
{
case WM_COMMAND:
return DefWindowProc(hWnd, message, wParam, lParam);
break;
case WM_TIMER:
break;
case WM_MOUSEMOVE:
{
if (login_ui.drag_window && (wParam & MK_LBUTTON))
{
// code executed when the dialog window is moved around on the screen
RECT win_rect;
GetWindowRect(hWnd, &win_rect);
int x_coord = GET_X_LPARAM(lParam);
int y_coord = GET_Y_LPARAM(lParam);
MoveWindow(hWnd,
win_rect.left + x_coord - login_ui.mouse_x_prev,
win_rect.top + y_coord - login_ui.mouse_y_prev,
win_rect.right - win_rect.left,
win_rect.bottom - win_rect.top,
false
);
}
break;
}
case WM_LBUTTONDOWN:
{
login_ui.mouse_x = GET_X_LPARAM(lParam);
login_ui.mouse_y = GET_Y_LPARAM(lParam);
if (login_ui.mouse_y < 41)
{
login_ui.mouse_x_prev = login_ui.mouse_x;
login_ui.mouse_y_prev = login_ui.mouse_y;
SetCapture(hWnd);
login_ui.drag_window = true;
}
break;
}
case WM_LBUTTONUP:
{
if (login_ui.drag_window)
{
login_ui.drag_window = false;
ReleaseCapture();
}
break;
}
case WM_SIZE:
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
case WM_QUIT:
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
return 0;
}
Main.Cpp
#include "UI.h"
int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE, char*, int nCmdShow)
{
login_ui.RunUI();
}
Have not used Awesomium but your GetMessage only pumps messages for the WebCore window. So instead you should pass NULL so your message pump dispatches messages for all windows created on that thread.
I want to switch my win32 console application to graphics mode to use SetPixel function to draw lines:
#include "stdafx.h"
int _tmain(int argc, _TCHAR* argv[])
{
//code to switch to graphics mode
return 0;
}
please advice :)
Here's a fine SetPixel() example.
Create a win32 application project , paste the code and compile it
//header files to include
#include<windows.h>
#include<stdlib.h>
#include<time.h>
//application title
#define APPTITLE "Hello World"
//function prototypes (forward declarations)
BOOL InitInstance(HINSTANCE, int);
ATOM MyRegisterClass(HINSTANCE);
LRESULT CALLBACK WinProc(HWND, UINT, WPARAM, LPARAM);
//the window event callback function
LRESULT CALLBACK WinProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
PAINTSTRUCT ps;
HDC hdc;
char *szHello = "SetPixel";
RECT rt;
int x=0, y=0, n=0;
COLORREF c;
int j;
switch (message)
{
case WM_PAINT:
//get the dimensions of the window
GetClientRect(hWnd, &rt);
//start drawing on devicce context
hdc = BeginPaint (hWnd, &ps);
//draw some text
DrawText(hdc, szHello, strlen(szHello), &rt, DT_CENTER);
j = (rand()*100);
c = RGB(0, 0, 0);
while( x<25000)
{
SetPixel(hdc, rand()%400, rand()%400, rand()%255);
x++;
}
//stop drawing
EndPaint(hWnd, &ps);
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
}
return DefWindowProc(hWnd, message, wParam, lParam);
}
//helper function to set up the window properties
ATOM MyRegisterClass(HINSTANCE hInstance)
{
//create the window class structure
WNDCLASSEX wc;
wc.cbSize = sizeof(WNDCLASSEX);
//fill the struct with info
wc.style = CS_HREDRAW | CS_VREDRAW;
wc.lpfnWndProc = (WNDPROC)WinProc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = hInstance;
wc.hIcon = NULL;
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.hbrBackground = (HBRUSH) GetStockObject(WHITE_BRUSH);
wc.lpszMenuName = NULL;
wc.lpszClassName = APPTITLE;
wc.hIconSm = NULL;
//set up the window with the class info
return RegisterClassEx(&wc);
}
//helper function to create the window and refresh it
BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
HWND hWnd;
//create a new window
hWnd = CreateWindow(
APPTITLE, //window class
APPTITLE, //title bar
WS_OVERLAPPEDWINDOW, //window style
CW_USEDEFAULT, //x position of window
CW_USEDEFAULT, //y position of window
400, //width of the window
400, //height of the window
NULL, //parent window
NULL, //menu
hInstance, //application instance
NULL); //window parameters
//was there an error creating the window?
if(!hWnd)
return FALSE;
//display the window
ShowWindow(hWnd, nCmdShow);
UpdateWindow(hWnd);
return TRUE;
}
//entry point for a Windows program
int WINAPI WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow)
{
//declare variables
MSG msg;
//register the class
MyRegisterClass(hInstance);
//initialize application
if(!InitInstance (hInstance, nCmdShow))
return FALSE;
//set random number seed
srand(time(NULL));
//main message loop
while(GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return msg.wParam;
}
you can switch to "graphich mode" means to windows forms application mode using project setting. but you'll have to change the main function to winMain
PROJECT SETTINGS >
LINKER > SYSTEM > SUBSYSTEM > Windows (/SUBSYSTEM:WINDOWS)
C/C++ > PREPROCESSOR > PREPROCESSOR DEFINITIONS >
WIN32;_DEBUG;_WINDOWS;%(PreprocessorDefinitions)
and this is how your function in this case should look:
int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE, LPWSTR lpCmdLine, int nCmdShow)
{
///....
}
I was working through a book which shows me how to create a windows application. I have written the code, however when I compile and run it, it says that it was successfuly built but it doesn't show a window where it should write "Hello World". I am using Visual Studio 2010 with C++, what might be the problem?
Thanks
Here is the code;
//Header Files
#include <windows.h>
#include <stdlib.h>
#include <time.h>
//Application Title
#define APPTITLE L"Hello World"
//function prototypes (forward declarations)
BOOL InitInstance( HINSTANCE, int);
ATOM MyRegisterClass( HINSTANCE);
LRESULT CALLBACK WinProc( HWND, UINT, WPARAM, LPARAM);
//The window event callback function
LRESULT CALLBACK WinProc( HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
PAINTSTRUCT ps;
HDC hdc;
//char *szHello = "Hello World!";
RECT rt;
int x, y, n;
COLORREF c;
switch(message)
{
case WM_PAINT:
//get the dimensions of the window
GetClientRect( hWnd, &rt);
//Start drawing on device context
hdc = BeginPaint( hWnd, &ps);
//Draw some text
DrawText( hdc, L"Hello World!", strlen( "Hello World!"), &rt, DT_CENTER);
//Draw 1000 random pixels
for( n=0; n < 3000; n++)
{
x = rand() % (rt.right - rt.left);
y = rand() % (rt.bottom - rt.top);
c = RGB( rand()%256, rand()%256, rand()%256);
SetPixel( hdc, x, y, c);
}
//Stop drawing
EndPaint( hWnd, &ps);
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
}
return DefWindowProc(hWnd, message, wParam, lParam);
}
//helper function to set up the window properties
ATOM MyRegisterClass(HINSTANCE hInstance)
{
//create the window class structure
WNDCLASSEX wc;
wc.cbSize = sizeof( WNDCLASSEX);
//FILL THE STRUCT WITH INGO
wc.cbSize = CS_HREDRAW | CS_VREDRAW;
wc.lpfnWndProc = (WNDPROC)WinProc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = hInstance;
wc.hIcon = NULL;
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
wc.lpszMenuName = NULL;
wc.lpszClassName = APPTITLE;
wc.hIconSm = NULL;
//set up the window with the class info
return RegisterClassEx(&wc);
}
BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
HWND hWnd;
//create a new window
hWnd = CreateWindow(
APPTITLE, //Window class
APPTITLE, //title bar
WS_OVERLAPPEDWINDOW, //window Style
CW_USEDEFAULT, //x position of window
CW_USEDEFAULT, //y postion of window
500, //width of the window
400, //height of the window
NULL, //parent window
NULL, //menu
hInstance, //application instance
NULL); //window parameters
//was there an error creating the window?
if( !hWnd)
return FALSE;
//Display the window
ShowWindow( hWnd, nCmdShow);
UpdateWindow( hWnd);
return TRUE;
}
//Entry point for a Windows program
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
//declare varuables
MSG msg;
//register the class
MyRegisterClass( hInstance);
//Initialize application
if( !InitInstance(hInstance, nCmdShow))
return FALSE;
//set random number seed
srand(time(NULL));
//Main message loop
while( GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage( &msg);
DispatchMessage( &msg);
}
return msg.wParam;
}
This line is wrong:
wc.cbSize = CS_HREDRAW | CS_VREDRAW;
You mean
wc.style = CS_HREDRAW | CS_VREDRAW;
In fact I would change the window class initialisation code so that you make it very clear in the code that the entire struct is initialised.
WNDCLASSEX wc = { 0 };//initialise struct to 0
wc.cbSize = sizeof( WNDCLASSEX);
//FILL THE STRUCT WITH INGO
wc.style = CS_HREDRAW | CS_VREDRAW;
wc.lpfnWndProc = (WNDPROC)WinProc;
wc.hInstance = hInstance;
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
wc.lpszClassName = APPTITLE;
And you were missing some error checking:
if (!MyRegisterClass( hInstance))
return FALSE;
Stepping through under the debugger will allow you to see where in the process things are going wrong.
I am trying to work out how to fade out or dim the Windows Desktop and then display a rectangular portion of the desktop normally. This is for a screen area capture program. You can see the precise effect I am after in Jing Fading the background in a Web page is also commonly done. Any tips/pointers/C++ source much appreciated. Google has not helped so far.
Thanks,
Neville
Use a layered window that covers the entire screen, but paint it with color key values such that the rectangular region of interest (the area that should be undarkened) is filled entirely with the color key. This region will then be completely transparent, and not darkened like the rest of the desktop. The rest of your layered window should be set to have a constant alpha value that is mostly transparent and filled with a dark color.
Here's a complete, working example:
#include "stdafx.h"
#include "ScreenCapper.h"
#define MAX_LOADSTRING 100
// Global Variables:
HINSTANCE hInst; // current instance
TCHAR szTitle[MAX_LOADSTRING]; // The title bar text
TCHAR szWindowClass[MAX_LOADSTRING]; // the main window class name
// Forward declarations of functions included in this code module:
ATOM MyRegisterClass(HINSTANCE hInstance);
BOOL InitInstance(HINSTANCE, int);
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
INT_PTR CALLBACK About(HWND, UINT, WPARAM, LPARAM);
const COLORREF transparentColor = RGB(255,0,0); // Pure red is the color key, or totally transparent color
const BYTE overallTranparencyAmount = 90; // Out of 255
int DesktopWidth,DesktopHeight;
int APIENTRY _tWinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPTSTR lpCmdLine,int nCmdShow)
{
UNREFERENCED_PARAMETER(hPrevInstance);
UNREFERENCED_PARAMETER(lpCmdLine);
DesktopWidth = GetSystemMetrics(SM_CXSCREEN);
DesktopHeight = GetSystemMetrics(SM_CYSCREEN);
MSG msg;
HACCEL hAccelTable;
LoadString(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
LoadString(hInstance, IDC_SCREENCAPPER, szWindowClass, MAX_LOADSTRING);
MyRegisterClass(hInstance);
if (!InitInstance (hInstance, nCmdShow))
{
return FALSE;
}
hAccelTable = LoadAccelerators(hInstance, MAKEINTRESOURCE(IDC_SCREENCAPPER));
while (GetMessage(&msg, NULL, 0, 0))
{
if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
return (int) msg.wParam;
}
ATOM MyRegisterClass(HINSTANCE hInstance)
{
WNDCLASSEX wcex;
memset(&wcex,0,sizeof(WNDCLASSEX));
wcex.cbSize = sizeof(WNDCLASSEX);
wcex.style = CS_HREDRAW | CS_VREDRAW;
wcex.lpfnWndProc = WndProc;
wcex.cbClsExtra = 0;
wcex.cbWndExtra = 0;
wcex.hInstance = hInstance;
wcex.hIcon = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_SCREENCAPPER));
wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
//wcex.lpszMenuName = MAKEINTRESOURCE(IDC_SCREENCAPPER);
wcex.lpszClassName = szWindowClass;
wcex.hIconSm = LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_SMALL));
return RegisterClassEx(&wcex);
}
BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
hInst = hInstance;
HWND hWnd = CreateWindowEx(WS_EX_LAYERED, szWindowClass, szTitle,WS_POPUP, 0, 0, DesktopWidth, DesktopHeight, NULL, NULL, hInstance, NULL);
if (!hWnd)
return FALSE;
SetLayeredWindowAttributes(hWnd,transparentColor,32,LWA_COLORKEY | LWA_ALPHA);
ShowWindow(hWnd, nCmdShow);
UpdateWindow(hWnd);
return TRUE;
}
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
int wmId, wmEvent;
PAINTSTRUCT ps;
HDC hdc;
if (message == WM_COMMAND)
{
wmId = LOWORD(wParam);
wmEvent = HIWORD(wParam);
// Parse the menu selections:
switch (wmId)
{
case IDM_EXIT:
DestroyWindow(hWnd);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
}
else if (message == WM_PAINT)
{
hdc = BeginPaint(hWnd, &ps);
HBRUSH hDarkBackgroundBrush = CreateSolidBrush(RGB(0,0,0));
HBRUSH hRegionOfInterestBrush = CreateSolidBrush(transparentColor);
RECT screenRect;
screenRect.left = screenRect.top = 0;
screenRect.right = DesktopWidth;
screenRect.bottom = DesktopHeight;
RECT interestRect;
interestRect.left = interestRect.top = 300;
interestRect.right = interestRect.bottom = 600;
FillRect(hdc,&screenRect,hDarkBackgroundBrush);
FillRect(hdc,&interestRect,hRegionOfInterestBrush);
DeleteObject(hDarkBackgroundBrush);
DeleteObject(hRegionOfInterestBrush);
EndPaint(hWnd, &ps);
}
else if (message == WM_DESTROY)
{
PostQuitMessage(0);
}
else
return DefWindowProc(hWnd, message, wParam, lParam);
return 0;
}
The official way is with FadeWindow() api :
Windows does that on Display Control Panel
See the standard code in C