Лабораторна робота 9
«main.cpp»(З виведенням картинки і тексту по натиску миші)
#include <windows.h>
void imageView(HDC hdc, HDC hmdc,HBITMAP hB,BITMAP bmt,DWORD dw,int ofLeft, int ofHeight){
hmdc=CreateCompatibleDC(hdc);
SelectObject(hmdc, hB);
GetObject(hB, sizeof(bmt), (LPSTR) &bmt);
BitBlt(hdc, ofLeft,ofHeight, bmt.bmWidth, bmt.bmHeight, hmdc, 5,5, dw);
DeleteDC(hmdc);
}
HWND b1, b2;
int width_frame=1000;
int height_frame = 600;
int out_x = 100;
int out_y = 100;
char szIconName[] = "ICON2";
char szCursorName[] = "CURSOR1";
char image_2[]={"IDB_im"};
HBITMAP hBitmap_i;
BITMAP bm;
const char g_szClassName[] = "myWindowClass";
int strLength(const char* str)
{
int size = 0;
while(*str++) ++size;
return size;
}
LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
HDC hdc, hmdc;
PAINTSTRUCT ps;
switch(msg)
{
int x,y;
case WM_CREATE:
b1 = CreateWindow("BUTTON", " Системний курсор",WS_VISIBLE|WS_CHILD|WS_BORDER,500,3,200,30,
hwnd, (HMENU) 1, NULL, NULL);
b2 = CreateWindow("BUTTON", " Завдання 2",WS_VISIBLE|WS_CHILD|WS_BORDER,500,39,200,30,
hwnd, (HMENU) 2, NULL, NULL);
break;
case WM_COMMAND:
switch(LOWORD(wParam)){
case 1: SetClassLong(hwnd,GCL_HCURSOR,(LONG) LoadCursor(NULL, MAKEINTRESOURCE(NULL)));
break;
case 2: system("start zavd_2_lab_9_API/lab_9_API_zavd_2_test_1.exe");
exit(1);
break;
}
break;
case WM_CLOSE:
DestroyWindow(hwnd);
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
case WM_RBUTTONDOWN:
HDC hDC;
hDC=GetDC(hwnd);
x=LOWORD(lParam);
y=HIWORD(lParam);
TextOut(hDC, x, y, "Іван Гребенюк", strlen("Іван Гребенюк"));
break;
case WM_LBUTTONDOWN:
SetWindowPos(hwnd, NULL, out_x,out_y, width_frame-1,height_frame, SWP_NOMOVE );
SetWindowPos(hwnd, NULL, out_x,out_y, width_frame,height_frame, SWP_NOMOVE );
hDC=GetDC(hwnd);
x=LOWORD(lParam);
y=HIWORD(lParam);
case WM_PAINT:
{
imageView(hDC, hmdc,hBitmap_i,bm,SRCCOPY,x,y);
}
break;
default:
return DefWindowProc(hwnd, msg, wParam, lParam);
}
return 0;
}
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
LPSTR lpCmdLine, int nCmdShow)
{
WNDCLASSEX wc;
HWND hwnd;
MSG Msg;
hBitmap_i=LoadBitmap(hInstance, image_2);
wc.cbSize = sizeof(WNDCLASSEX);
wc.style = 0;
wc.lpfnWndProc = WndProc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = hInstance;
wc.hIcon = LoadIcon(hInstance, szIconName);
wc.hCursor = LoadCursor(hInstance, szCursorName);
wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
wc.lpszMenuName = NULL;
wc.lpszClassName = g_szClassName;
wc.hIconSm = LoadIcon(NULL, szIconName);
if(!RegisterClassEx(&wc))
{
MessageBox(NULL, "Grebenuk Ivan 4 lab", "Error of create window!",
MB_ICONSTOP | MB_OK);
return 0;
}
hwnd = CreateWindowEx(
WS_EX_CLIENTEDGE,
g_szClassName,
"Гребенюк Іван 4 лаб ПЗС-1644",
WS_SYSMENU,//WS_OVERLAPPEDWINDOW
out_x,out_y, width_frame,height_frame,
NULL, NULL, hInstance, NULL);
HBRUSH brush = CreateSolidBrush(RGB(112, 121, 205));//162 181 205
SetClassLongPtr(hwnd, GCLP_HBRBACKGROUND, (LONG)brush);
ShowWindow(hwnd, nCmdShow);
UpdateWindow(hwnd);
while(GetMessage(&Msg, NULL, 0, 0) > 0)
{
TranslateMessage(&Msg);
DispatchMessage(&Msg);
}
return Msg.wParam;
}
«main.cpp»(З натисканням миші по компонентам вікна та пустого місця)
#include <windows.h>
HWND b1, b2, edit1, radio1, check1,memo1, image_button;
int width_frame=1000;
int height_frame = 600;
int out_x = 100;
int out_y = 100;
char szIconName[] = "ICON2";
char szCursorName[] = "CURSOR1";
char image_2[]={"IDB_im"};
HBITMAP hBitmap_i;
BITMAP bm;
const char g_szClassName[] = "myWindowClass";
int strLength(const char* str)
{
int size = 0;
while(*str++) ++size;
return size;
}
char text1[]={"Виберіть будь-яку карту"};
LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
POINT ptCursor;
HDC hdc, hmdc;
PAINTSTRUCT ps;
switch(msg)
{
int x,y;
case WM_CREATE:
b1 = CreateWindow("BUTTON", " Системний курсор",WS_VISIBLE|WS_CHILD|WS_BORDER,500,3,200,30,
hwnd, (HMENU) 1, NULL, NULL);
edit1 = CreateWindowEx(WS_EX_CLIENTEDGE,"EDIT", " Напиши сюди текст",WS_VISIBLE|WS_CHILD|WS_BORDER,500,113,400,30,
hwnd, (HMENU) 2, NULL, NULL);
radio1 = CreateWindowEx(WS_EX_CLIENTEDGE,"BUTTON", " Напиши сюди текст",WS_VISIBLE|WS_CHILD|BS_RADIOBUTTON,500,213,400,30,
hwnd, (HMENU) 3, NULL, NULL);
check1 = CreateWindowEx(WS_EX_CLIENTEDGE,"BUTTON", " Напиши сюди текст",WS_VISIBLE|WS_CHILD|BS_CHECKBOX,500,313,400,30,
hwnd, (HMENU) 4, NULL, NULL);
memo1 = CreateWindowEx(WS_EX_CLIENTEDGE,"EDIT", " Напиши сюди текст",ES_MULTILINE | WS_VISIBLE | WS_CHILD | WS_BORDER | WS_TABSTOP,500,403,400,130,
hwnd, (HMENU) 5, NULL, NULL);
BITMAP bmt;
GetObject(hBitmap_i, sizeof(bmt), (LPSTR) &bmt);
image_button = CreateWindow("BUTTON", " Системний курсор",WS_VISIBLE|WS_CHILD|BS_BITMAP,100,100,bmt.bmWidth,bmt.bmHeight,
hwnd, (HMENU) 6, NULL, NULL);
SendMessage (image_button, BM_SETIMAGE, IMAGE_BITMAP, (LPARAM)hBitmap_i);
break;
case WM_RBUTTONDOWN:MessageBox(hwnd, "Порожнє поле вікна", "Гребенюк",MB_OK);break;
case WM_COMMAND:
switch(LOWORD(wParam)){
case 1: SetClassLong(hwnd,GCL_HCURSOR,(LONG) LoadCursor(NULL, MAKEINTRESOURCE(NULL)));
MessageBox(hwnd, "Кнопка системний курсор", "Гребенюк",MB_OK);
break;
case 2:
MessageBox(hwnd, "Текстове поле з текстом 'напиши сюди текст'", "Гребенюк",MB_OK);
break;
case 3:
MessageBox(hwnd, "РадіоКнопка системний курсор", "Гребенюк",MB_OK);
break;
case 4:
MessageBox(hwnd, "CheckBox Кнопка системний курсор", "Гребенюк",MB_OK);
break;
case 5:
MessageBox(hwnd, "Memo поле з текстом 'системний курсор'", "Гребенюк",MB_OK);
break;
case 6: MessageBox(hwnd, "Кнопка з картинкою", "Гребенюк",MB_OK); break;
}
break;
case WM_CLOSE:
DestroyWindow(hwnd);
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hwnd, msg, wParam, lParam);
}
return 0;
}
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
LPSTR lpCmdLine, int nCmdShow)
{
hBitmap_i=LoadBitmap(hInstance, image_2); //Зв'язування
SendMessage (image_button, BM_SETIMAGE, IMAGE_BITMAP, (LPARAM)hBitmap_i);
WNDCLASSEX wc;
HWND hwnd;
MSG Msg;
wc.cbSize = sizeof(WNDCLASSEX);
wc.style = 0;
wc.lpfnWndProc = WndProc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = hInstance;
wc.hIcon = LoadIcon(hInstance, szIconName);
wc.hCursor = LoadCursor(hInstance, szCursorName);
wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
wc.lpszMenuName = NULL;
wc.lpszClassName = g_szClassName;
wc.hIconSm = LoadIcon(NULL, szIconName);
if(!RegisterClassEx(&wc))
{
MessageBox(NULL, "Grebenuk Ivan 4 lab", "Error of create window!",
MB_ICONSTOP | MB_OK);
return 0;
}
hwnd = CreateWindowEx(
WS_EX_CLIENTEDGE,
g_szClassName,
"Гребенюк Іван 9 лаб ПЗС-1644",
WS_SYSMENU,
out_x,out_y, width_frame,height_frame,
NULL, NULL, hInstance, NULL);
HBRUSH brush = CreateSolidBrush(RGB(112, 121, 205));//162 181 205
SetClassLongPtr(hwnd, GCLP_HBRBACKGROUND, (LONG)brush);
ShowWindow(hwnd, nCmdShow);
UpdateWindow(hwnd);
while(GetMessage(&Msg, NULL, 0, 0) > 0)
{
TranslateMessage(&Msg);
DispatchMessage(&Msg);
}
return Msg.wParam;
}
«res.rc»
ICON2 ICON "icon7.ico"
CURSOR1 CURSOR "main_cur.cur"
IDB_im BITMAP "image_9_lab_1.bmp"
