c++ - 如何使用winapi使窗口更新C中的整个背景颜色
问题描述
我正在使用 winapi 在 C 中创建一个文本编辑器,每当我使用 ChangeBKColor 和 ChangeTextColor 更新颜色时,它只会更改一行,我必须按 Enter 键来填写其余部分。
当用户按下按钮时,如何让它自动更新整个窗口?我已经尝试过 RedrawWindow() 和 InvalidateRect 函数,但它只更新第一行
相关函数是 WindowProcedure() 和 bkDialogProcedure()
代码总结,当用户点击菜单按钮改变背景颜色时,调用了changebackgroundcolor函数
#include <stdio.h>
#include <windows.h>
#include <stdlib.h>
#include <time.h>
//#Defines for menu events
#define FILEMENU_NEW 1
#define FILEMENU_OPEN 2
#define FILEMENU_SAVE 3
#define VIEWMENU_CHANGETEXTCOLOR 4
#define VIEWMENU_CHANGEBACKGROUNDCOLOR 5
#define exit_window 7
LRESULT CALLBACK WindowProcedure(HWND,UINT,WPARAM,LPARAM);
LRESULT CALLBACK bkDialogProcedure(HWND, UINT, WPARAM,LPARAM);
void AddMenus(HWND);
void AddControls(HWND);
void displayDialog(HWND);
void open_file_window(HWND);
void save_file(HWND);
void AddTextFont(HWND);
void changetextcolor(HWND);
void changebackgroundcolor(HWND);
void registerTextDialogClass(HINSTANCE);
void registerBkDialogClass(HINSTANCE);
HMENU hMenu;
HDC hdcEdit,hdcStatic;
HWND hPic,hMainWindow,hEdit,hBannerWindow;
HWND R,G,B, hPreview, hDlg; //Windows for the different color values corresponding to either R, G, or B
HWND sorry; //Window for the sorry message in the change colors dialog window hopefully can get rid of it soon
HCURSOR hCursor;
HFONT textfont;
char char_text_red[4],char_text_green[4],char_text_blue[4],char_background_red[4],char_background_green[4],char_background_blue[4];
short int_text_RGB[3] = {255,255,255}; //Yes yes I know its not a int anymore, just to much work to change it all
short int_background_RGB[3] = {85,85,85};
short dialog_text_RGB[3];
short dialog_background_RGB[3];
LRESULT CALLBACK WindowProcedure(HWND hWnd,UINT msg,WPARAM wp,LPARAM lp)
{
int value;
hdcEdit = (HDC)wp;
switch(msg)
{
case WM_COMMAND: //Cases for menu events in the main window
switch (wp)
{
case FILEMENU_NEW: //Event for new button being pressed
break;
case FILEMENU_OPEN: //Events for open button being pressed
open_file_window(hWnd);
break;
case FILEMENU_SAVE:
save_file(hWnd);
break;
case VIEWMENU_CHANGETEXTCOLOR:
changetextcolor(hWnd);
break;
case VIEWMENU_CHANGEBACKGROUNDCOLOR:
changebackgroundcolor(hWnd);
break;
case WM_CREATE: //Case for when window is created
AddMenus(hWnd); // all of these functions are needed to
AddControls(hWnd); // create everything on startup
break;
case WM_DESTROY: //Case for when window is closed
DeleteObject(textfont);
PostQuitMessage(0);
break;
case WM_CTLCOLOREDIT: //Case for edit windows color
SetTextColor(hdcEdit, RGB(int_text_RGB[0], int_text_RGB[1], int_text_RGB[2])); //Change text color to white
SetBkColor(hdcEdit, RGB(int_background_RGB[0], int_background_RGB[1], int_background_RGB[2]));
return (LRESULT)CreateSolidBrush(RGB(int_background_RGB[0], int_background_RGB[1], int_background_RGB[2])); //Make entire edit window gray
break;
case WM_CTLCOLORSTATIC: //Used to color the Banner window
hdcStatic = (HDC)hBannerWindow;
SetBkColor(hdcStatic,RGB(255,255,255));
return (LRESULT)CreateSolidBrush(RGB(254,254,255));
break; **
default:
return DefWindowProcW(hWnd,msg,wp,lp);
}
return 1;
}
HWND mes;
HDC hdcPreview;
LRESULT CALLBACK bkDialogProcedure(HWND hWnd, UINT msg, WPARAM wp,LPARAM lp)
{
#define confirm 1
#define preview 2
switch(msg)
{
case WM_CREATE:
dialog_background_RGB[0] = int_background_RGB[0];
dialog_background_RGB[1] = int_background_RGB[1];
dialog_background_RGB[2] = int_background_RGB[2];
dialog_text_RGB[0] = int_text_RGB[0];
dialog_text_RGB[1] = int_text_RGB[1];
dialog_text_RGB[2] = int_text_RGB[2];
SendMessageW(hDlg, WM_SETFONT, (WPARAM)textfont, TRUE);
break;
case WM_CLOSE: //Events for dialog window closing
EnableWindow(hMainWindow,1);
DestroyWindow(hWnd);
break;
case WM_COMMAND: //Events for button being pressed in the dialog window
switch(wp)
{
case confirm: //Events for the confirm button being pressed
GetWindowText(R, char_background_red,4);
GetWindowText(G, char_background_green,4);
GetWindowText(B, char_background_blue,4);
int_background_RGB[0] = atoi(char_background_red);
int_background_RGB[1] = atoi(char_background_green);
int_background_RGB[2] = atoi(char_background_blue);
RedrawWindow(hEdit, NULL, NULL, RDW_INVALIDATE | RDW_UPDATENOW);
EnableWindow(hMainWindow,1);
DestroyWindow(hWnd);
break;
case preview:
GetWindowText(R, char_background_red,4);
GetWindowText(G, char_background_green,4);
GetWindowText(B, char_background_blue,4);
dialog_background_RGB[0] = atoi(char_background_red);
dialog_background_RGB[1] = atoi(char_background_green);
dialog_background_RGB[2] = atoi(char_background_blue);
RedrawWindow(hPreview, NULL, NULL, RDW_INVALIDATE | RDW_UPDATENOW);
SendMessage(hPreview,WM_CTLCOLOREDIT,0,0);
break;
}
case WM_CTLCOLORSTATIC:
SetBkColor((HDC)wp,RGB(255,255,255));
return (LRESULT)CreateSolidBrush(RGB(255,255,255));
break;
case WM_CTLCOLOREDIT:
SetBkColor((HDC)wp,RGB(dialog_background_RGB[0],dialog_background_RGB[1],dialog_background_RGB[2]));
SetTextColor((HDC)wp,RGB(dialog_text_RGB[0],dialog_text_RGB[1],dialog_text_RGB[2]));
return (LRESULT)CreateSolidBrush(RGB(int_background_RGB[0],int_background_RGB[1],int_background_RGB[2]));
break;
default:
return DefWindowProcW(hWnd,msg,wp,lp);
}
return 1;
} **
void registerBkDialogClass(HINSTANCE hInst) //Registers the dialog window
{
WNDCLASSW bk_dialog = {0};
bk_dialog.hbrBackground = CreateSolidBrush(RGB(255,255,255));
bk_dialog.hCursor = LoadCursor(NULL,IDC_ARROW);
bk_dialog.hInstance = hInst;
bk_dialog.lpszClassName = L"myBkDialogClass";
bk_dialog.lpfnWndProc = bkDialogProcedure;
RegisterClassW(&bk_dialog);
}
void changebackgroundcolor(HWND hWnd) //Displays the dialog window
{
hDlg = CreateWindowW(L"myBkDialogClass",L"Change the background color",WS_OVERLAPPED|WS_VISIBLE|WS_MINIMIZEBOX|WS_SYSMENU,500,300,
600,500, hWnd,NULL,NULL,NULL);
R = CreateWindowW(L"Edit",L"",WS_VISIBLE | WS_CHILD | WS_BORDER,280,230,45,20,hDlg,NULL,NULL,NULL);
G = CreateWindowW(L"Edit",L"",WS_VISIBLE | WS_CHILD | WS_BORDER,330,230,45,20,hDlg,NULL,NULL,NULL);
B = CreateWindowW(L"Edit",L"",WS_VISIBLE | WS_CHILD | WS_BORDER,380,230,45,20,hDlg,NULL,NULL,NULL);
CreateWindowW(L"Button",L"Confirm",WS_VISIBLE|WS_CHILD,200,445,100,20,hDlg,(HMENU)confirm,NULL,NULL); //Creates close button in dialog window
sorry = CreateWindowW(L"Static",L"Sorry for the abomination please leave a suggestion for a better color",
WS_VISIBLE|WS_CHILD | ES_CENTER,170,300,400,37,hDlg,NULL,NULL,NULL);
mes = CreateWindowW(L"Static",L"Enter the RGB code for the color you want",WS_VISIBLE | WS_CHILD| SS_CENTER,200,205,350,20,hDlg,NULL,NULL,NULL);
CreateWindowW(L"Button",L"Preview Color",WS_VISIBLE|WS_CHILD,500,125,100,20,hDlg,(HMENU)preview,NULL,NULL);
hPreview = CreateWindowW(L"Edit",L"This is the color of the background",WS_VISIBLE | WS_CHILD | ES_MULTILINE | ES_WANTRETURN,0,0,300,200,hDlg,0,0,0);
AddTextFont(hPreview);
AddTextFont(R);
AddTextFont(G);
AddTextFont(B);
AddTextFont(mes);
AddTextFont(sorry);
EnableWindow(hWnd,0); //Disables all but the dialog window when the dialog window is open
}
解决方案
推荐阅读
- java - Elasticsearch 异常 [type=illegal_argument_exception, reason=Provided password hash uses [NOOP] but the configured hashing algorithm is [BCRYPT]]
- javascript - HTML/JavaScript - 为文本框输入激活键盘麦克风
- javascript - 我有这个“ throw new RangeError('BITFIELD_INVALID', bit);” 错误; 我只是找不到解决方案
- mysql - 从终端中的文件执行 MYSQL 命令
- c# - 如何在不知道底层记录类型的情况下在界面上使用关键字“with”?
- node.js - nest.js 构建抛出很多错误
- matlab - MATLAB中竞争性捕食者猎物模型的分岔图
- python - 从已排序的字典键中仅删除前导零
- php - PHP:如何检测函数中不正确的参数数量?
- c# - 为什么我不能跳?