SetWindowLong(hWnd, GWL_EX_STYLE,
GetWindowLong(hWnd, GWL_EX_STYLE) or WS_EX_LAYERED);
SetLayeredWindowAttributes(hWnd, 0, <прозрачность_1..255>, LWA_ALPHA);
Прозрачное окно в FAQ - ???
Сам посоветовал Максу глянуть в FAQ, сам сунулся и фиг!
SetLayeredWindowAttributes - неизвестная функция!
Код:
GWL_EX_STYLE - Это вообще непонятная константа!
WS_EX_LAYERED - тоже!
SetLayeredWindowAttributes - это не функция API, а непонятно какая функция!
Кстати, ещё вопрос:
Как сменить фон окна?
А это моё круглок окошко с кнопками:
Код:
#include "stdafx.h"
#include "resource.h"
#define MAX_LOADSTRING 100
// Global Variables:
HINSTANCE hInst; // current instance
TCHAR szTitle[MAX_LOADSTRING]; // The title bar text
TCHAR szWindowClass[MAX_LOADSTRING]; // The title bar text
//Controls:
HANDLE button1,button2;
// Foward declarations of functions included in this code module:
ATOM MyRegisterClass(HINSTANCE hInstance);
BOOL InitInstance(HINSTANCE, int);
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
LRESULT CALLBACK About(HWND, UINT, WPARAM, LPARAM);
int APIENTRY WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow)
{
// TODO: Place code here.
MSG msg;
HACCEL hAccelTable;
// Initialize global strings
LoadString(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
LoadString(hInstance, IDC_WIN, szWindowClass, MAX_LOADSTRING);
MyRegisterClass(hInstance);
// Perform application initialization:
if (!InitInstance (hInstance, nCmdShow))
{
return FALSE;
}
hAccelTable = LoadAccelerators(hInstance, (LPCTSTR)IDC_WIN);
// Main message loop:
while (GetMessage(&msg, NULL, 0, 0))
{
if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
return msg.wParam;
}
//
// FUNCTION: MyRegisterClass()
//
// PURPOSE: Registers the window class.
//
// COMMENTS:
//
// This function and its usage is only necessary if you want this code
// to be compatible with Win32 systems prior to the 'RegisterClassEx'
// function that was added to Windows 95. It is important to call this function
// so that the application will get 'well formed' small icons associated
// with it.
//
ATOM MyRegisterClass(HINSTANCE hInstance)
{
WNDCLASSEX wcex;
wcex.cbSize = sizeof(WNDCLASSEX);
wcex.style = CS_HREDRAW | CS_VREDRAW;
wcex.lpfnWndProc = (WNDPROC)WndProc;
wcex.cbClsExtra = 0;
wcex.cbWndExtra = 0;
wcex.hInstance = hInstance;
wcex.hIcon = LoadIcon(hInstance, (LPCTSTR)IDI_WIN);
wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
wcex.lpszMenuName = (LPCSTR)IDC_WIN;
wcex.lpszClassName = szWindowClass;
wcex.hIconSm = LoadIcon(wcex.hInstance, (LPCTSTR)IDI_SMALL);
return RegisterClassEx(&wcex);
}
void WinProperties(HWND hWnd,HINSTANCE hInstance)
{
RECT myRect;
HRGN hRgn;
GetClientRect(hWnd, &myRect);
//hRgn = CreateEllipticRgn(myRect.left, myRect.top, myRect.right, myRect.bottom);
hRgn = CreateEllipticRgn((myRect.right-myRect.left)/2-150,myRect.top+50,(myRect.right-myRect.left)/2+150,myRect.top+200);
SetWindowRgn(hWnd,hRgn, TRUE);
//SetWindowLong(hWnd, GWL_STYLE,GetWindowLong(hWnd, GWL_STYLE) WS_VISIBLE );
//SetLayeredWindowAttributes(hWnd, 0, 0, LWA_COLORKEY);//Óäàëèòü áåëûé öâåò!
button1 = CreateWindowEx(WS_EX_TRANSPARENT,"BUTTON","My Button",WS_VISIBLE | WS_CHILD | BS_DEFPUSHBUTTON,((myRect.right-myRect.left)/2)-50, myRect.top+30,100,30,hWnd,0,hInstance,0);
button2 = CreateWindowEx(WS_EX_TRANSPARENT,"BUTTON","Exit",WS_VISIBLE | WS_CHILD | BS_DEFPUSHBUTTON,((myRect.right-myRect.left)/2)-50, myRect.top+60,100,30,hWnd,0,hInstance,0);
}
//
// FUNCTION: InitInstance(HANDLE, 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)
{
HWND hWnd;
hInst = hInstance; // Store instance handle in our global variable
hWnd = CreateWindow(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInstance, NULL);
if (!hWnd)
{
return FALSE;
}
WinProperties(hWnd,hInstance);
ShowWindow(hWnd, nCmdShow);
UpdateWindow(hWnd);
return TRUE;
}
//
// FUNCTION: WndProc(HWND, unsigned, WORD, LONG)
//
// 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)
{
// RECT myRect;
// HRGN hRgn;
int wmId, wmEvent;
PAINTSTRUCT ps;
HDC hdc;
TCHAR szHello[MAX_LOADSTRING];
LoadString(hInst, IDS_HELLO, szHello, MAX_LOADSTRING);
switch (message)
{
case WM_COMMAND:
wmId = LOWORD(wParam);
wmEvent = HIWORD(wParam);
// Parse the menu selections:
if((HWND) lParam==button1)
{
MessageBox(hWnd,"Hello World!","Hello World!",MB_OK);
}
if((HWND) lParam==button2)
{
PostQuitMessage(0);
}
switch (wmId)
{
case IDM_about :
DialogBox(hInst, (LPCTSTR)IDD_ABOUTBOX, hWnd, (DLGPROC)About);
break;
case IDM_EXIT:
DestroyWindow(hWnd);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
break;
case WM_PAINT:
hdc = BeginPaint(hWnd, &ps);
// TODO: Add any drawing code here...
RECT rt;
GetClientRect(hWnd, &rt);
DrawText(hdc, szHello, strlen(szHello), &rt, DT_CENTER);
EndPaint(hWnd, &ps);
break;
case WM_NCHITTEST:
return HTCAPTION;
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
return 0;
}
// Mesage handler for about box.
LRESULT CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
switch (message)
{
case WM_INITDIALOG:
return TRUE;
case WM_COMMAND:
if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL)
{
EndDialog(hDlg, LOWORD(wParam));
return TRUE;
}
break;
}
return FALSE;
}
#include "resource.h"
#define MAX_LOADSTRING 100
// Global Variables:
HINSTANCE hInst; // current instance
TCHAR szTitle[MAX_LOADSTRING]; // The title bar text
TCHAR szWindowClass[MAX_LOADSTRING]; // The title bar text
//Controls:
HANDLE button1,button2;
// Foward declarations of functions included in this code module:
ATOM MyRegisterClass(HINSTANCE hInstance);
BOOL InitInstance(HINSTANCE, int);
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
LRESULT CALLBACK About(HWND, UINT, WPARAM, LPARAM);
int APIENTRY WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow)
{
// TODO: Place code here.
MSG msg;
HACCEL hAccelTable;
// Initialize global strings
LoadString(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
LoadString(hInstance, IDC_WIN, szWindowClass, MAX_LOADSTRING);
MyRegisterClass(hInstance);
// Perform application initialization:
if (!InitInstance (hInstance, nCmdShow))
{
return FALSE;
}
hAccelTable = LoadAccelerators(hInstance, (LPCTSTR)IDC_WIN);
// Main message loop:
while (GetMessage(&msg, NULL, 0, 0))
{
if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
return msg.wParam;
}
//
// FUNCTION: MyRegisterClass()
//
// PURPOSE: Registers the window class.
//
// COMMENTS:
//
// This function and its usage is only necessary if you want this code
// to be compatible with Win32 systems prior to the 'RegisterClassEx'
// function that was added to Windows 95. It is important to call this function
// so that the application will get 'well formed' small icons associated
// with it.
//
ATOM MyRegisterClass(HINSTANCE hInstance)
{
WNDCLASSEX wcex;
wcex.cbSize = sizeof(WNDCLASSEX);
wcex.style = CS_HREDRAW | CS_VREDRAW;
wcex.lpfnWndProc = (WNDPROC)WndProc;
wcex.cbClsExtra = 0;
wcex.cbWndExtra = 0;
wcex.hInstance = hInstance;
wcex.hIcon = LoadIcon(hInstance, (LPCTSTR)IDI_WIN);
wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
wcex.lpszMenuName = (LPCSTR)IDC_WIN;
wcex.lpszClassName = szWindowClass;
wcex.hIconSm = LoadIcon(wcex.hInstance, (LPCTSTR)IDI_SMALL);
return RegisterClassEx(&wcex);
}
void WinProperties(HWND hWnd,HINSTANCE hInstance)
{
RECT myRect;
HRGN hRgn;
GetClientRect(hWnd, &myRect);
//hRgn = CreateEllipticRgn(myRect.left, myRect.top, myRect.right, myRect.bottom);
hRgn = CreateEllipticRgn((myRect.right-myRect.left)/2-150,myRect.top+50,(myRect.right-myRect.left)/2+150,myRect.top+200);
SetWindowRgn(hWnd,hRgn, TRUE);
//SetWindowLong(hWnd, GWL_STYLE,GetWindowLong(hWnd, GWL_STYLE) WS_VISIBLE );
//SetLayeredWindowAttributes(hWnd, 0, 0, LWA_COLORKEY);//Óäàëèòü áåëûé öâåò!
button1 = CreateWindowEx(WS_EX_TRANSPARENT,"BUTTON","My Button",WS_VISIBLE | WS_CHILD | BS_DEFPUSHBUTTON,((myRect.right-myRect.left)/2)-50, myRect.top+30,100,30,hWnd,0,hInstance,0);
button2 = CreateWindowEx(WS_EX_TRANSPARENT,"BUTTON","Exit",WS_VISIBLE | WS_CHILD | BS_DEFPUSHBUTTON,((myRect.right-myRect.left)/2)-50, myRect.top+60,100,30,hWnd,0,hInstance,0);
}
//
// FUNCTION: InitInstance(HANDLE, 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)
{
HWND hWnd;
hInst = hInstance; // Store instance handle in our global variable
hWnd = CreateWindow(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInstance, NULL);
if (!hWnd)
{
return FALSE;
}
WinProperties(hWnd,hInstance);
ShowWindow(hWnd, nCmdShow);
UpdateWindow(hWnd);
return TRUE;
}
//
// FUNCTION: WndProc(HWND, unsigned, WORD, LONG)
//
// 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)
{
// RECT myRect;
// HRGN hRgn;
int wmId, wmEvent;
PAINTSTRUCT ps;
HDC hdc;
TCHAR szHello[MAX_LOADSTRING];
LoadString(hInst, IDS_HELLO, szHello, MAX_LOADSTRING);
switch (message)
{
case WM_COMMAND:
wmId = LOWORD(wParam);
wmEvent = HIWORD(wParam);
// Parse the menu selections:
if((HWND) lParam==button1)
{
MessageBox(hWnd,"Hello World!","Hello World!",MB_OK);
}
if((HWND) lParam==button2)
{
PostQuitMessage(0);
}
switch (wmId)
{
case IDM_about :
DialogBox(hInst, (LPCTSTR)IDD_ABOUTBOX, hWnd, (DLGPROC)About);
break;
case IDM_EXIT:
DestroyWindow(hWnd);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
break;
case WM_PAINT:
hdc = BeginPaint(hWnd, &ps);
// TODO: Add any drawing code here...
RECT rt;
GetClientRect(hWnd, &rt);
DrawText(hdc, szHello, strlen(szHello), &rt, DT_CENTER);
EndPaint(hWnd, &ps);
break;
case WM_NCHITTEST:
return HTCAPTION;
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
return 0;
}
// Mesage handler for about box.
LRESULT CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
switch (message)
{
case WM_INITDIALOG:
return TRUE;
case WM_COMMAND:
if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL)
{
EndDialog(hDlg, LOWORD(wParam));
return TRUE;
}
break;
}
return FALSE;
}
Код:
COLORREF cl;
cl = RGB(0,180,26);
HBRUSH hBrColor = CreateSolidBrush(cl);
SetClassLong(hWndFir, GCL_HBRBACKGROUND, (LONG)hBrColor); // изменяем цвет фона
InvalidateRect(hWndFir, NULL, TRUE); // заставляем окно перерисоваться
cl = RGB(0,180,26);
HBRUSH hBrColor = CreateSolidBrush(cl);
SetClassLong(hWndFir, GCL_HBRBACKGROUND, (LONG)hBrColor); // изменяем цвет фона
InvalidateRect(hWndFir, NULL, TRUE); // заставляем окно перерисоваться
Вот так можно динамически изменить фон окна.
Цитата:
GWL_EX_STYLE - Это вообще непонятная константа!
WS_EX_LAYERED - тоже!
SetLayeredWindowAttributes - это не функция API, а непонятно какая функция!
Это зависит от версии заголовочных файлов.
Можно получить эту функцию динамически:
подгрузить user32.dll и найти ее по имени
константы:
#define WS_EX_LAYERED 0x00080000
#define GWL_EX_STYLE -20
прототип функции:
__stdcall BOOL SetLayeredWindowAttributes(HWND hWnd, COLORREF crKey, BYTE bAlpha, DWORD dwFlags);
Примерно это можно сделать так:
Код:
#define WS_EX_LAYERED 0x00080000
#define GWL_EX_STYLE -20
#define LWA_COLORKEY 0x00000001
#define LWA_ALPHA 0x00000002
typedef BOOL (__stdcall*layeredproc)(HWND hWnd, COLORREF crKey, BYTE bAlpha, DWORD dwFlags);
layeredproc lpSetLayeredWindowAttributes=NULL;
HMODULE user32=(HMODULE)LoadLibrary("user32.dll");
BOOL SetLayeredWindowAttributes(HWND hWnd, COLORREF crKey, BYTE bAlpha, DWORD dwFlags) {
BOOL result=FALSE;
if(lpSetLayeredWindowAttributes==NULL) {
if(user32!=NULL) {
lpSetLayeredWindowAttributes=(layeredproc)GetProcAddress(user32,"SetLayeredWindowAttributes");
}
}
if(lpSetLayeredWindowAttributes!=NULL) {
result=lpSetLayeredWindowAttributes(hWnd,crKey,bAlpha,dwFlags);
}
return result;
}
#define GWL_EX_STYLE -20
#define LWA_COLORKEY 0x00000001
#define LWA_ALPHA 0x00000002
typedef BOOL (__stdcall*layeredproc)(HWND hWnd, COLORREF crKey, BYTE bAlpha, DWORD dwFlags);
layeredproc lpSetLayeredWindowAttributes=NULL;
HMODULE user32=(HMODULE)LoadLibrary("user32.dll");
BOOL SetLayeredWindowAttributes(HWND hWnd, COLORREF crKey, BYTE bAlpha, DWORD dwFlags) {
BOOL result=FALSE;
if(lpSetLayeredWindowAttributes==NULL) {
if(user32!=NULL) {
lpSetLayeredWindowAttributes=(layeredproc)GetProcAddress(user32,"SetLayeredWindowAttributes");
}
}
if(lpSetLayeredWindowAttributes!=NULL) {
result=lpSetLayeredWindowAttributes(hWnd,crKey,bAlpha,dwFlags);
}
return result;
}
Код:
#include "stdafx.h"
#include "resource.h"
#define MAX_LOADSTRING 100
#define WS_EX_LAYERED 0x00080000
#define GWL_EX_STYLE -20
#define LWA_COLORKEY 0x00000001
#define LWA_ALPHA 0x00000002
// Global Variables:
HINSTANCE hInst; // current instance
TCHAR szTitle[MAX_LOADSTRING]; // The title bar text
TCHAR szWindowClass[MAX_LOADSTRING]; // The title bar text
//Controls:
HANDLE button1,button2;
// Foward declarations of functions included in this code module:
ATOM MyRegisterClass(HINSTANCE hInstance);
BOOL InitInstance(HINSTANCE, int);
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
LRESULT CALLBACK About(HWND, UINT, WPARAM, LPARAM);
int APIENTRY WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow)
{
// TODO: Place code here.
MSG msg;
HACCEL hAccelTable;
// Initialize global strings
LoadString(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
LoadString(hInstance, IDC_WIN, szWindowClass, MAX_LOADSTRING);
MyRegisterClass(hInstance);
// Perform application initialization:
if (!InitInstance (hInstance, nCmdShow))
{
return FALSE;
}
hAccelTable = LoadAccelerators(hInstance, (LPCTSTR)IDC_WIN);
// Main message loop:
while (GetMessage(&msg, NULL, 0, 0))
{
if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
return msg.wParam;
}
//
// FUNCTION: MyRegisterClass()
//
// PURPOSE: Registers the window class.
//
// COMMENTS:
//
// This function and its usage is only necessary if you want this code
// to be compatible with Win32 systems prior to the 'RegisterClassEx'
// function that was added to Windows 95. It is important to call this function
// so that the application will get 'well formed' small icons associated
// with it.
//
ATOM MyRegisterClass(HINSTANCE hInstance)
{
WNDCLASSEX wcex;
wcex.cbSize = sizeof(WNDCLASSEX);
wcex.style = CS_HREDRAW | CS_VREDRAW;
wcex.lpfnWndProc = (WNDPROC)WndProc;
wcex.cbClsExtra = 0;
wcex.cbWndExtra = 0;
wcex.hInstance = hInstance;
wcex.hIcon = LoadIcon(hInstance, (LPCTSTR)IDI_WIN);
wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
wcex.lpszMenuName = (LPCSTR)IDC_WIN;
wcex.lpszClassName = szWindowClass;
wcex.hIconSm = LoadIcon(wcex.hInstance, (LPCTSTR)IDI_SMALL);
return RegisterClassEx(&wcex);
}
typedef BOOL (__stdcall*layeredproc)(HWND hWnd, COLORREF crKey, BYTE bAlpha, DWORD dwFlags);
layeredproc lpSetLayeredWindowAttributes=NULL;
HMODULE user32=(HMODULE)LoadLibrary("user32.dll");
BOOL SetLayeredWindowAttributes(HWND hWnd, COLORREF crKey, BYTE bAlpha, DWORD dwFlags) {
BOOL result=FALSE;
if(lpSetLayeredWindowAttributes==NULL) {
if(user32!=NULL) {
lpSetLayeredWindowAttributes=(layeredproc)GetProcAddress(user32,"SetLayeredWindowAttributes");
}
}
if(lpSetLayeredWindowAttributes!=NULL) {
result=lpSetLayeredWindowAttributes(hWnd,crKey,bAlpha,dwFlags);
}
return result;
}
void WinProperties(HWND hWnd,HINSTANCE hInstance)
{
RECT myRect;
HRGN hRgn;
GetClientRect(hWnd, &myRect);
//hRgn = CreateEllipticRgn(myRect.left, myRect.top, myRect.right, myRect.bottom);
hRgn = CreateEllipticRgn((myRect.right-myRect.left)/2-150,myRect.top+50,(myRect.right-myRect.left)/2+150,myRect.top+200);
SetWindowRgn(hWnd,hRgn, TRUE);
COLORREF cl;
//cl = RGB(0,180,26);
cl = RGB(255,255,255);
HBRUSH hBrColor = CreateSolidBrush(cl);
SetClassLong(hWnd, GCL_HBRBACKGROUND, (LONG)hBrColor); // èçìåíÿåì öâåò ôîíà
InvalidateRect(hWnd, NULL, TRUE); // çàñòàâëÿåì îêíî ïåðåðèñîâàòüñÿ
SetWindowLong(hWnd, GWL_EX_STYLE, GetWindowLong(hWnd, GWL_EX_STYLE) | WS_EX_LAYERED);
//SetLayeredWindowAttributes(hWnd, RGB(0,180,26), 0, LWA_COLORKEY);//Óäàëèòü çåë¸íûé öâåò!
SetLayeredWindowAttributes(hWnd, RGB(255,255,255), 120, LWA_COLORKEY | LWA_ALPHA);//Óäàëèòü áåëûé öâåò!
//SetLayeredWindowAttributes(hWnd, RGB(0,180,26), 120, LWA_COLORKEY | LWA_ALPHA);//Óäàëèòü áåëûé öâåò!
//SetWindowLong(hWnd, GWL_EX_STYLE, GetWindowLong(hWnd, GWL_EX_STYLE) | WS_EX_LAYERED);
//SetLayeredWindowAttributes(hWnd, 0, 120, LWA_ALPHA);
button1 = CreateWindowEx(WS_EX_TRANSPARENT,"BUTTON","My Button",WS_VISIBLE | WS_CHILD | BS_DEFPUSHBUTTON,((myRect.right-myRect.left)/2)-50, myRect.top+30,100,30,hWnd,0,hInstance,0);
button2 = CreateWindowEx(WS_EX_TRANSPARENT,"BUTTON","Exit",WS_VISIBLE | WS_CHILD | BS_DEFPUSHBUTTON,((myRect.right-myRect.left)/2)-50, myRect.top+60,100,30,hWnd,0,hInstance,0);
}
//
// FUNCTION: InitInstance(HANDLE, 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)
{
HWND hWnd;
hInst = hInstance; // Store instance handle in our global variable
hWnd = CreateWindow(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInstance, NULL);
if (!hWnd)
{
return FALSE;
}
WinProperties(hWnd,hInstance);
ShowWindow(hWnd, nCmdShow);
UpdateWindow(hWnd);
return TRUE;
}
//
// FUNCTION: WndProc(HWND, unsigned, WORD, LONG)
//
// 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)
{
// RECT myRect;
// HRGN hRgn;
int wmId, wmEvent;
PAINTSTRUCT ps;
HDC hdc;
TCHAR szHello[MAX_LOADSTRING];
LoadString(hInst, IDS_HELLO, szHello, MAX_LOADSTRING);
switch (message)
{
case WM_COMMAND:
wmId = LOWORD(wParam);
wmEvent = HIWORD(wParam);
// Parse the menu selections:
if((HWND) lParam==button1)
{
MessageBox(hWnd,"Hello World!","Hello World!",MB_OK);
}
if((HWND) lParam==button2)
{
PostQuitMessage(0);
}
switch (wmId)
{
case IDM_about :
DialogBox(hInst, (LPCTSTR)IDD_ABOUTBOX, hWnd, (DLGPROC)About);
break;
case IDM_EXIT:
DestroyWindow(hWnd);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
break;
case WM_PAINT:
hdc = BeginPaint(hWnd, &ps);
// TODO: Add any drawing code here...
RECT rt;
GetClientRect(hWnd, &rt);
DrawText(hdc, szHello, strlen(szHello), &rt, DT_CENTER);
EndPaint(hWnd, &ps);
break;
case WM_NCHITTEST:
//if(ìûøêà â òîé îáëàñòè îêíà çà êîòîðóþ ìîæíî òÿíóòü)
return HTCAPTION;
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
return 0;
}
// Mesage handler for about box.
LRESULT CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
switch (message)
{
case WM_INITDIALOG:
return TRUE;
case WM_COMMAND:
if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL)
{
EndDialog(hDlg, LOWORD(wParam));
return TRUE;
}
break;
}
return FALSE;
}
#include "resource.h"
#define MAX_LOADSTRING 100
#define WS_EX_LAYERED 0x00080000
#define GWL_EX_STYLE -20
#define LWA_COLORKEY 0x00000001
#define LWA_ALPHA 0x00000002
// Global Variables:
HINSTANCE hInst; // current instance
TCHAR szTitle[MAX_LOADSTRING]; // The title bar text
TCHAR szWindowClass[MAX_LOADSTRING]; // The title bar text
//Controls:
HANDLE button1,button2;
// Foward declarations of functions included in this code module:
ATOM MyRegisterClass(HINSTANCE hInstance);
BOOL InitInstance(HINSTANCE, int);
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
LRESULT CALLBACK About(HWND, UINT, WPARAM, LPARAM);
int APIENTRY WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow)
{
// TODO: Place code here.
MSG msg;
HACCEL hAccelTable;
// Initialize global strings
LoadString(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
LoadString(hInstance, IDC_WIN, szWindowClass, MAX_LOADSTRING);
MyRegisterClass(hInstance);
// Perform application initialization:
if (!InitInstance (hInstance, nCmdShow))
{
return FALSE;
}
hAccelTable = LoadAccelerators(hInstance, (LPCTSTR)IDC_WIN);
// Main message loop:
while (GetMessage(&msg, NULL, 0, 0))
{
if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
return msg.wParam;
}
//
// FUNCTION: MyRegisterClass()
//
// PURPOSE: Registers the window class.
//
// COMMENTS:
//
// This function and its usage is only necessary if you want this code
// to be compatible with Win32 systems prior to the 'RegisterClassEx'
// function that was added to Windows 95. It is important to call this function
// so that the application will get 'well formed' small icons associated
// with it.
//
ATOM MyRegisterClass(HINSTANCE hInstance)
{
WNDCLASSEX wcex;
wcex.cbSize = sizeof(WNDCLASSEX);
wcex.style = CS_HREDRAW | CS_VREDRAW;
wcex.lpfnWndProc = (WNDPROC)WndProc;
wcex.cbClsExtra = 0;
wcex.cbWndExtra = 0;
wcex.hInstance = hInstance;
wcex.hIcon = LoadIcon(hInstance, (LPCTSTR)IDI_WIN);
wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
wcex.lpszMenuName = (LPCSTR)IDC_WIN;
wcex.lpszClassName = szWindowClass;
wcex.hIconSm = LoadIcon(wcex.hInstance, (LPCTSTR)IDI_SMALL);
return RegisterClassEx(&wcex);
}
typedef BOOL (__stdcall*layeredproc)(HWND hWnd, COLORREF crKey, BYTE bAlpha, DWORD dwFlags);
layeredproc lpSetLayeredWindowAttributes=NULL;
HMODULE user32=(HMODULE)LoadLibrary("user32.dll");
BOOL SetLayeredWindowAttributes(HWND hWnd, COLORREF crKey, BYTE bAlpha, DWORD dwFlags) {
BOOL result=FALSE;
if(lpSetLayeredWindowAttributes==NULL) {
if(user32!=NULL) {
lpSetLayeredWindowAttributes=(layeredproc)GetProcAddress(user32,"SetLayeredWindowAttributes");
}
}
if(lpSetLayeredWindowAttributes!=NULL) {
result=lpSetLayeredWindowAttributes(hWnd,crKey,bAlpha,dwFlags);
}
return result;
}
void WinProperties(HWND hWnd,HINSTANCE hInstance)
{
RECT myRect;
HRGN hRgn;
GetClientRect(hWnd, &myRect);
//hRgn = CreateEllipticRgn(myRect.left, myRect.top, myRect.right, myRect.bottom);
hRgn = CreateEllipticRgn((myRect.right-myRect.left)/2-150,myRect.top+50,(myRect.right-myRect.left)/2+150,myRect.top+200);
SetWindowRgn(hWnd,hRgn, TRUE);
COLORREF cl;
//cl = RGB(0,180,26);
cl = RGB(255,255,255);
HBRUSH hBrColor = CreateSolidBrush(cl);
SetClassLong(hWnd, GCL_HBRBACKGROUND, (LONG)hBrColor); // èçìåíÿåì öâåò ôîíà
InvalidateRect(hWnd, NULL, TRUE); // çàñòàâëÿåì îêíî ïåðåðèñîâàòüñÿ
SetWindowLong(hWnd, GWL_EX_STYLE, GetWindowLong(hWnd, GWL_EX_STYLE) | WS_EX_LAYERED);
//SetLayeredWindowAttributes(hWnd, RGB(0,180,26), 0, LWA_COLORKEY);//Óäàëèòü çåë¸íûé öâåò!
SetLayeredWindowAttributes(hWnd, RGB(255,255,255), 120, LWA_COLORKEY | LWA_ALPHA);//Óäàëèòü áåëûé öâåò!
//SetLayeredWindowAttributes(hWnd, RGB(0,180,26), 120, LWA_COLORKEY | LWA_ALPHA);//Óäàëèòü áåëûé öâåò!
//SetWindowLong(hWnd, GWL_EX_STYLE, GetWindowLong(hWnd, GWL_EX_STYLE) | WS_EX_LAYERED);
//SetLayeredWindowAttributes(hWnd, 0, 120, LWA_ALPHA);
button1 = CreateWindowEx(WS_EX_TRANSPARENT,"BUTTON","My Button",WS_VISIBLE | WS_CHILD | BS_DEFPUSHBUTTON,((myRect.right-myRect.left)/2)-50, myRect.top+30,100,30,hWnd,0,hInstance,0);
button2 = CreateWindowEx(WS_EX_TRANSPARENT,"BUTTON","Exit",WS_VISIBLE | WS_CHILD | BS_DEFPUSHBUTTON,((myRect.right-myRect.left)/2)-50, myRect.top+60,100,30,hWnd,0,hInstance,0);
}
//
// FUNCTION: InitInstance(HANDLE, 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)
{
HWND hWnd;
hInst = hInstance; // Store instance handle in our global variable
hWnd = CreateWindow(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInstance, NULL);
if (!hWnd)
{
return FALSE;
}
WinProperties(hWnd,hInstance);
ShowWindow(hWnd, nCmdShow);
UpdateWindow(hWnd);
return TRUE;
}
//
// FUNCTION: WndProc(HWND, unsigned, WORD, LONG)
//
// 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)
{
// RECT myRect;
// HRGN hRgn;
int wmId, wmEvent;
PAINTSTRUCT ps;
HDC hdc;
TCHAR szHello[MAX_LOADSTRING];
LoadString(hInst, IDS_HELLO, szHello, MAX_LOADSTRING);
switch (message)
{
case WM_COMMAND:
wmId = LOWORD(wParam);
wmEvent = HIWORD(wParam);
// Parse the menu selections:
if((HWND) lParam==button1)
{
MessageBox(hWnd,"Hello World!","Hello World!",MB_OK);
}
if((HWND) lParam==button2)
{
PostQuitMessage(0);
}
switch (wmId)
{
case IDM_about :
DialogBox(hInst, (LPCTSTR)IDD_ABOUTBOX, hWnd, (DLGPROC)About);
break;
case IDM_EXIT:
DestroyWindow(hWnd);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
break;
case WM_PAINT:
hdc = BeginPaint(hWnd, &ps);
// TODO: Add any drawing code here...
RECT rt;
GetClientRect(hWnd, &rt);
DrawText(hdc, szHello, strlen(szHello), &rt, DT_CENTER);
EndPaint(hWnd, &ps);
break;
case WM_NCHITTEST:
//if(ìûøêà â òîé îáëàñòè îêíà çà êîòîðóþ ìîæíî òÿíóòü)
return HTCAPTION;
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
return 0;
}
// Mesage handler for about box.
LRESULT CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
switch (message)
{
case WM_INITDIALOG:
return TRUE;
case WM_COMMAND:
if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL)
{
EndDialog(hDlg, LOWORD(wParam));
return TRUE;
}
break;
}
return FALSE;
}
В окнах просто не силён...
Один раз мне попалась в руки книжка, которая называлась типа
"C++ для чайников". Точное название не запомнил, но примерно так. Автора тоже не помню. Но в той книжке было примерно 3\4 про создание интерфейсов окон, диалоги и т.д. В то время я её прочитал бегло (ещё не надо было). И от неё теперь остались лишь смутные воспоминания.
Может быть кто знает где скачать книжку по C++, в которой написано про создание окон, кнопок, диалогов, закладок, radio и т.д. и т.п.?