간단한 win api 프로그래밍 질문 있습니다.
글쓴이: kyuho6942 / 작성시간: 목, 2018/10/18 - 11:36오후
프로그램 실행 중 동적으로 윈도우의 제목을 바꾸고 싶습니다. CreateWindow에서 두번째 인자를 변수로 주고
다른 부분에서 wsprintf(변수, TEXT("바꿀제목이름"), 바꿀제목이름길이);로 바꾸면 될 줄알았는데 실행이 안되고 가만히 있는데 어떻게 가능하게 하는지 궁금합니다. 혹시 몰라서 코드도 같이 올립니다..
//======================================================================
// Win32 API Example - Timer Event 2
//
//======================================================================
#include <windows.h> // 윈도우 관련 정의
#include "TimerEvent_2.h" // 프로그램 관련 정의
#include <math.h>
//----------------------------------------------------------------------
// Timer 식별자
//
#define ID_TIMER 1
//----------------------------------------------------------------------
// 전역 데이터
//
static const LPCWSTR szAppName = TEXT("TimerEvent2"); // 프로그램 이름
LPWSTR AppName = TEXT("시험");
HINSTANCE hInst; // 프로그램 인스턴스 핸들
HWND hwndMain; // 메인 윈도우 핸들
//======================================================================
// 프로그램 시작점
//
int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
LPSTR lpCmdLine, int nCmdShow)
{
MSG msg;
int rc = 0;
// 어플리케이션 초기화
if( !InitApplication(hInstance) )
return 0;
// 인스턴스 초기화
if( !InitInstance(hInstance, lpCmdLine, nCmdShow) )
return 0;
// 메시지 루프
while (GetMessage (&msg, NULL, 0, 0)) {
TranslateMessage (&msg);
DispatchMessage (&msg);
}
// 인스턴스 소거
return TermInstance (hInstance, msg.wParam);
}
//----------------------------------------------------------------------
// InitApp - 어플리케이션 초기화
//
BOOL InitApplication (HINSTANCE hInstance)
{
WNDCLASS wc;
// 전역 변수에 인스턴스 핸들 보관
hInst = hInstance;
// 주 윈도우 클래스 등록
wc.style = CS_HREDRAW | CS_VREDRAW | CS_DBLCLKS; // 윈도우 스타일
wc.lpfnWndProc = MainWndProc; // 윈도우 프로시저
wc.cbClsExtra = 0; // 추가 클래스 데이터
wc.cbWndExtra = 0; // 추가 윈도우 데이터
wc.hInstance = hInstance; // 소유자 핸들
wc.hIcon = LoadIcon(NULL, IDI_APPLICATION); // 프로그램 아이콘
wc.hCursor = LoadCursor (NULL, IDC_ARROW);// 기본 커서
wc.hbrBackground = (HBRUSH) GetStockObject (WHITE_BRUSH);
wc.lpszMenuName = NULL; // 메뉴 이름
wc.lpszClassName = (LPWSTR)szAppName; // 윈도우 클래스 이름
if (!RegisterClass(&wc) )
return FALSE;
return TRUE;
}
//----------------------------------------------------------------------
// InitInstance - 인스턴스 초기화
//
BOOL InitInstance (HINSTANCE hInstance, LPSTR lpCmdLine, int nCmdShow)
{
HWND hWnd;
// 주 윈도우 생성
hwndMain = CreateWindowEx (WS_EX_APPWINDOW,
szAppName, // 윈도우 클래스
AppName, // 윈도우 타이틀
// 스타일 플래그
WS_OVERLAPPEDWINDOW, //WS_VISIBLE | WS_SYSMENU | WS_CAPTION,
CW_USEDEFAULT, // x 좌표
CW_USEDEFAULT, // y 좌표
CW_USEDEFAULT, // 초기 너비
CW_USEDEFAULT, // 초기 높이
NULL, // 부모 윈도우 핸들
NULL, // 메뉴 (NULL로 설정)
hInstance, // 응용프로그램 인스턴스
NULL); // 생성 매개변수 포인터
if (!IsWindow (hwndMain))
return FALSE; // 윈도우 생성 실패시 작동 실패
// 윈도우 표시 및 갱신
ShowWindow (hwndMain, nCmdShow);
UpdateWindow (hwndMain);
return TRUE;
}
//----------------------------------------------------------------------
// TermInstance - 프로그램 소거
//
int TermInstance (HINSTANCE hInstance, int nDefRC)
{
return nDefRC;
}
//======================================================================
// 주 윈도우를 위한 메시지 처리 핸들러
//
//----------------------------------------------------------------------
// MainWndProc - 주 윈도우의 콜백 함수
//
LRESULT CALLBACK MainWndProc (HWND hWnd, UINT wMsg, WPARAM wParam, LPARAM lParam)
{
int i;
//
// 메시지 분배 테이블을 검사하여 해당 메시지를 처리할지 확인한 후
// 해당 메시지 핸들러를 호출
//
for (i = 0; i < dim(MainMessages); i++) {
if (wMsg == MainMessages[i].Code)
return (*MainMessages[i].Fxn)(hWnd, wMsg, wParam, lParam);
}
return DefWindowProc (hWnd, wMsg, wParam, lParam);
}
//----------------------------------------------------------------------
// DoCreateMain - WM_CREATE 메시지 처리
//
LRESULT DoCreateMain (HWND hWnd, UINT wMsg, WPARAM wParam, LPARAM lParam)
{
SetTimer( hWnd, ID_TIMER, 100, NULL );
wsprintf(AppName,TEXT("제목변경"));
return 0;
}
//----------------------------------------------------------------------
// DoPaintMain - WM_PAINT 메시지 처리
//
//----------------------------------------------------------------------
// DoPaintMain - WM_PAINT 메시지 처리
//
LRESULT DoPaintMain(HWND hWnd, UINT wMsg, WPARAM wParam, LPARAM lParam)
{
PAINTSTRUCT ps;
RECT rect;
HDC hdc;
int static movePoint = 20;
int static point = 40;
GetClientRect(hWnd, &rect);
hdc = BeginPaint(hWnd, &ps);
const int SIZE = 20;
const int SPEED = 50;
HBRUSH brush = CreateSolidBrush(RGB(0, 0, 255));
HBRUSH ub = (HBRUSH)SelectObject(hdc, brush);
POINT s;
static POINT curr;
static int xDistance = 0;
static int yDistance = 0;
if (xDistance == 0 && yDistance == 0) //처음 시작부분
{
s.x = (rect.left + rect.right) / 2;
s.y = (rect.top + rect.bottom) / 2;
int random = 0;
curr = s;
srand(time(NULL));
xDistance = rand() % (SPEED + 1); //랜덤 함수
yDistance = sqrt(pow(SPEED, 2) - pow(xDistance, 2)); //랜덤함수 사용해서 위치 계산
random = rand() % 2; //랜덤 값 재 계산
if (random == 1)
xDistance *= -1;
random = rand() % 2;
if (random == 1)
yDistance *= -1;
}
Ellipse(hdc, curr.x - SIZE, curr.y - SIZE, curr.x + SIZE, curr.y + SIZE);
if (curr.x < rect.left || curr.x > rect.right || curr.y < rect.top || curr.y > rect.bottom) {
xDistance = 0;
yDistance = 0;
}
const int gap_TopBottom = 20;
if (curr.y + SIZE + yDistance < rect.top + gap_TopBottom || curr.y - SIZE + yDistance > rect.bottom - gap_TopBottom) {
yDistance *= -1;
}
else if (curr.x - SIZE + xDistance < rect.left || curr.x + SIZE + xDistance > rect.right) {
xDistance *= -1;
}
curr.x += xDistance;
curr.y += yDistance;
EndPaint(hWnd, &ps);
return 0;
}
//----------------------------------------------------------------------
// DoTimerMain - WM_TIMER 메시지 처리
//
LRESULT DoTimerMain (HWND hWnd, UINT wMsg, WPARAM wParam, LPARAM lParam)
{
InvalidateRect( hWnd, NULL, TRUE );
return 0;
}
//----------------------------------------------------------------------
// DoDestroyMain - WM_DESTROY 메시지 처리
//
LRESULT DoDestroyMain (HWND hWnd, UINT wMsg, WPARAM wParam, LPARAM lParam)
{
KillTimer( hWnd, ID_TIMER );
PostQuitMessage (0);
return 0;
} 바꿀 변수의 이름은 LPWSTR 타입의 AppName으로 했고, 제목 변경 코드는 DoCreateMain 함수에 wsprintf(AppName,TEXT("제목변경")); 로 집어넣었습니다
Forums:


댓글 달기